3 * @defgroup FileJournal File journal
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
26 * @ingroup FileJournal
27 * @author Aaron Schulz
31 * @brief Class for handling file operation journaling.
33 * Subclasses should avoid throwing exceptions at all costs.
35 * @ingroup FileJournal
38 abstract class FileJournal
{
39 protected $backend; // string
40 protected $ttlDays; // integer
43 * Construct a new instance from configuration.
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;
54 * Create an appropriate FileJournal object from config
56 * @param $config Array
57 * @param string $backend A registered file backend name
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;
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() {
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 );
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, update, 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 array $entries List of file operations (each an array of parameters)
94 * @param string $batchId UUID string that identifies the operation batch
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 array $entries List of file operations (each an array of parameters)
108 * @param string $batchId UUID string that identifies the operation batch
111 abstract protected function doLogChangeBatch( array $entries, $batchId );
114 * Get the position ID of the latest journal entry
116 * @return integer|false
118 final public function getCurrentPosition() {
119 return $this->doGetCurrentPosition();
123 * @see FileJournal::getCurrentPosition()
124 * @return integer|false
126 abstract protected function doGetCurrentPosition();
129 * Get the position ID of the latest journal entry at some point in time
131 * @param $time integer|string timestamp
132 * @return integer|false
134 final public function getPositionAtTime( $time ) {
135 return $this->doGetPositionAtTime( $time );
139 * @see FileJournal::getPositionAtTime()
140 * @param $time integer|string timestamp
141 * @return integer|false
143 abstract protected function doGetPositionAtTime( $time );
146 * Get an array of file change log entries.
147 * A starting change ID and/or limit can be specified.
149 * The result as a list of associative arrays, each having:
150 * id : unique, monotonic, ID for this change
151 * batch_uuid : UUID for an operation batch
152 * backend : the backend name
153 * op : primitive operation (create,update,delete,null)
154 * path : affected storage path
155 * new_sha1 : base 36 sha1 of the new file had the operation succeeded
156 * timestamp : TS_MW timestamp of the batch change
158 * Also, $next is updated to the ID of the next entry.
160 * @param $start integer Starting change ID or null
161 * @param $limit integer Maximum number of items to return
162 * @param &$next string
165 final public function getChangeEntries( $start = null, $limit = 0, &$next = null ) {
166 $entries = $this->doGetChangeEntries( $start, $limit ?
$limit +
1 : 0 );
167 if ( $limit && count( $entries ) > $limit ) {
168 $last = array_pop( $entries ); // remove the extra entry
169 $next = $last['id']; // update for next call
171 $next = null; // end of list
177 * @see FileJournal::getChangeEntries()
180 abstract protected function doGetChangeEntries( $start, $limit );
183 * Purge any old log entries
187 final public function purgeOldLogs() {
188 return $this->doPurgeOldLogs();
192 * @see FileJournal::purgeOldLogs()
195 abstract protected function doPurgeOldLogs();
199 * Simple version of FileJournal that does nothing
202 class NullFileJournal
extends FileJournal
{
204 * @see FileJournal::doLogChangeBatch()
205 * @param $entries array
206 * @param $batchId string
209 protected function doLogChangeBatch( array $entries, $batchId ) {
210 return Status
::newGood();
214 * @see FileJournal::doGetCurrentPosition()
215 * @return integer|false
217 protected function doGetCurrentPosition() {
222 * @see FileJournal::doGetPositionAtTime()
223 * @param $time integer|string timestamp
224 * @return integer|false
226 protected function doGetPositionAtTime( $time ) {
231 * @see FileJournal::doGetChangeEntries()
234 protected function doGetChangeEntries( $start, $limit ) {
239 * @see FileJournal::doPurgeOldLogs()
242 protected function doPurgeOldLogs() {
243 return Status
::newGood();