3 * @defgroup FileJournal File journal
10 * @author Aaron Schulz
14 * @brief Class for handling file operation journaling.
16 * Subclasses should avoid throwing exceptions at all costs.
18 * @ingroup FileJournal
21 abstract class FileJournal
{
22 protected $backend; // string
23 protected $ttlDays; // integer
26 * Construct a new instance from configuration.
28 * 'ttlDays' : days to keep log entries around (false means "forever")
30 * @param $config Array
32 protected function __construct( array $config ) {
33 $this->ttlDays
= isset( $config['ttlDays'] ) ?
$config['ttlDays'] : false;
37 * Create an appropriate FileJournal object from config
39 * @param $config Array
40 * @param $backend string A registered file backend name
43 final public static function factory( array $config, $backend ) {
44 $class = $config['class'];
45 $jrn = new $class( $config );
46 if ( !$jrn instanceof self
) {
47 throw new MWException( "Class given is not an instance of FileJournal." );
49 $jrn->backend
= $backend;
54 * Get a statistically unique ID string
56 * @return string <9 char TS_MW timestamp in base 36><22 random base 36 chars>
58 final public function getTimestampedUUID() {
60 for ( $i = 0; $i < 5; $i++
) {
61 $s .= mt_rand( 0, 2147483647 );
63 $s = wfBaseConvert( sha1( $s ), 16, 36, 31 );
64 return substr( wfBaseConvert( wfTimestamp( TS_MW
), 10, 36, 9 ) . $s, 0, 31 );
68 * Log changes made by a batch file operation.
69 * $entries is an array of log entries, each of which contains:
70 * op : Basic operation name (create, store, copy, delete)
71 * path : The storage path of the file
72 * newSha1 : The final base 36 SHA-1 of the file
73 * Note that 'false' should be used as the SHA-1 for non-existing files.
75 * @param $entries Array List of file operations (each an array of parameters)
76 * @param $batchId string UUID string that identifies the operation batch
79 final public function logChangeBatch( array $entries, $batchId ) {
80 if ( !count( $entries ) ) {
81 return Status
::newGood();
83 return $this->doLogChangeBatch( $entries, $batchId );
87 * @see FileJournal::logChangeBatch()
89 * @param $entries Array List of file operations (each an array of parameters)
90 * @param $batchId string UUID string that identifies the operation batch
93 abstract protected function doLogChangeBatch( array $entries, $batchId );
96 * Purge any old log entries
100 final public function purgeOldLogs() {
101 return $this->doPurgeOldLogs();
105 * @see FileJournal::purgeOldLogs()
108 abstract protected function doPurgeOldLogs();
112 * Simple version of FileJournal that does nothing
115 class NullFileJournal
extends FileJournal
{
117 * @see FileJournal::logChangeBatch()
120 protected function doLogChangeBatch( array $entries, $batchId ) {
121 return Status
::newGood();
125 * @see FileJournal::purgeOldLogs()
128 protected function doPurgeOldLogs() {
129 return Status
::newGood();