Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / filerepo / backend / filejournal / FileJournal.php
blob234788b4eed12dacc3be43dfcb1f36ae6c718f98
1 <?php
2 /**
3 * @defgroup FileJournal File journal
4 * @ingroup FileBackend
5 */
7 /**
8 * File operation journaling.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
26 * @ingroup FileJournal
27 * @author Aaron Schulz
30 /**
31 * @brief Class for handling file operation journaling.
33 * Subclasses should avoid throwing exceptions at all costs.
35 * @ingroup FileJournal
36 * @since 1.20
38 abstract class FileJournal {
39 protected $backend; // string
40 protected $ttlDays; // integer
42 /**
43 * Construct a new instance from configuration.
44 * $config includes:
45 * 'ttlDays' : days to keep log entries around (false means "forever")
47 * @param $config Array
49 protected function __construct( array $config ) {
50 $this->ttlDays = isset( $config['ttlDays'] ) ? $config['ttlDays'] : false;
53 /**
54 * Create an appropriate FileJournal object from config
56 * @param $config Array
57 * @param $backend string A registered file backend name
58 * @throws MWException
59 * @return FileJournal
61 final public static function factory( array $config, $backend ) {
62 $class = $config['class'];
63 $jrn = new $class( $config );
64 if ( !$jrn instanceof self ) {
65 throw new MWException( "Class given is not an instance of FileJournal." );
67 $jrn->backend = $backend;
68 return $jrn;
71 /**
72 * Get a statistically unique ID string
74 * @return string <9 char TS_MW timestamp in base 36><22 random base 36 chars>
76 final public function getTimestampedUUID() {
77 $s = '';
78 for ( $i = 0; $i < 5; $i++ ) {
79 $s .= mt_rand( 0, 2147483647 );
81 $s = wfBaseConvert( sha1( $s ), 16, 36, 31 );
82 return substr( wfBaseConvert( wfTimestamp( TS_MW ), 10, 36, 9 ) . $s, 0, 31 );
85 /**
86 * Log changes made by a batch file operation.
87 * $entries is an array of log entries, each of which contains:
88 * op : Basic operation name (create, store, copy, delete)
89 * path : The storage path of the file
90 * newSha1 : The final base 36 SHA-1 of the file
91 * Note that 'false' should be used as the SHA-1 for non-existing files.
93 * @param $entries Array List of file operations (each an array of parameters)
94 * @param $batchId string UUID string that identifies the operation batch
95 * @return Status
97 final public function logChangeBatch( array $entries, $batchId ) {
98 if ( !count( $entries ) ) {
99 return Status::newGood();
101 return $this->doLogChangeBatch( $entries, $batchId );
105 * @see FileJournal::logChangeBatch()
107 * @param $entries Array List of file operations (each an array of parameters)
108 * @param $batchId string UUID string that identifies the operation batch
109 * @return Status
111 abstract protected function doLogChangeBatch( array $entries, $batchId );
114 * Get an array of file change log entries.
115 * A starting change ID and/or limit can be specified.
117 * The result as a list of associative arrays, each having:
118 * id : unique, monotonic, ID for this change
119 * batch_uuid : UUID for an operation batch
120 * backend : the backend name
121 * op : primitive operation (create,update,delete)
122 * path : affected storage path
123 * path_sha1 : base 36 sha1 of the affected storage path
124 * timestamp : TS_MW timestamp of the batch change
126 * Also, $next is updated to the ID of the next entry.
128 * @param $start integer Starting change ID or null
129 * @param $limit integer Maximum number of items to return
130 * @param &$next string
131 * @return Array
133 final public function getChangeEntries( $start = null, $limit = 0, &$next = null ) {
134 $entries = $this->doGetChangeEntries( $start, $limit ? $limit + 1 : 0 );
135 if ( $limit && count( $entries ) > $limit ) {
136 $last = array_pop( $entries ); // remove the extra entry
137 $next = $last['id']; // update for next call
138 } else {
139 $next = null; // end of list
141 return $entries;
145 * @see FileJournal::getChangeEntries()
146 * @return Array
148 abstract protected function doGetChangeEntries( $start, $limit );
151 * Purge any old log entries
153 * @return Status
155 final public function purgeOldLogs() {
156 return $this->doPurgeOldLogs();
160 * @see FileJournal::purgeOldLogs()
161 * @return Status
163 abstract protected function doPurgeOldLogs();
167 * Simple version of FileJournal that does nothing
168 * @since 1.20
170 class NullFileJournal extends FileJournal {
172 * @see FileJournal::logChangeBatch()
173 * @param $entries array
174 * @param $batchId string
175 * @return Status
177 protected function doLogChangeBatch( array $entries, $batchId ) {
178 return Status::newGood();
182 * @see FileJournal::doGetChangeEntries()
183 * @return Array
185 protected function doGetChangeEntries( $start, $limit ) {
186 return array();
190 * @see FileJournal::purgeOldLogs()
191 * @return Status
193 protected function doPurgeOldLogs() {
194 return Status::newGood();