MediaWiki  REL1_24
DBFileJournal.php
Go to the documentation of this file.
00001 <?php
00029 class DBFileJournal extends FileJournal {
00031     protected $dbw;
00032 
00033     protected $wiki = false; // string; wiki DB name
00034 
00041     protected function __construct( array $config ) {
00042         parent::__construct( $config );
00043 
00044         $this->wiki = $config['wiki'];
00045     }
00046 
00053     protected function doLogChangeBatch( array $entries, $batchId ) {
00054         $status = Status::newGood();
00055 
00056         try {
00057             $dbw = $this->getMasterDB();
00058         } catch ( DBError $e ) {
00059             $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
00060 
00061             return $status;
00062         }
00063 
00064         $now = wfTimestamp( TS_UNIX );
00065 
00066         $data = array();
00067         foreach ( $entries as $entry ) {
00068             $data[] = array(
00069                 'fj_batch_uuid' => $batchId,
00070                 'fj_backend' => $this->backend,
00071                 'fj_op' => $entry['op'],
00072                 'fj_path' => $entry['path'],
00073                 'fj_new_sha1' => $entry['newSha1'],
00074                 'fj_timestamp' => $dbw->timestamp( $now )
00075             );
00076         }
00077 
00078         try {
00079             $dbw->insert( 'filejournal', $data, __METHOD__ );
00080             if ( mt_rand( 0, 99 ) == 0 ) {
00081                 $this->purgeOldLogs(); // occasionally delete old logs
00082             }
00083         } catch ( DBError $e ) {
00084             $status->fatal( 'filejournal-fail-dbquery', $this->backend );
00085 
00086             return $status;
00087         }
00088 
00089         return $status;
00090     }
00091 
00096     protected function doGetCurrentPosition() {
00097         $dbw = $this->getMasterDB();
00098 
00099         return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
00100             array( 'fj_backend' => $this->backend ),
00101             __METHOD__
00102         );
00103     }
00104 
00110     protected function doGetPositionAtTime( $time ) {
00111         $dbw = $this->getMasterDB();
00112 
00113         $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
00114 
00115         return $dbw->selectField( 'filejournal', 'fj_id',
00116             array( 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ),
00117             __METHOD__,
00118             array( 'ORDER BY' => 'fj_timestamp DESC' )
00119         );
00120     }
00121 
00128     protected function doGetChangeEntries( $start, $limit ) {
00129         $dbw = $this->getMasterDB();
00130 
00131         $res = $dbw->select( 'filejournal', '*',
00132             array(
00133                 'fj_backend' => $this->backend,
00134                 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
00135             __METHOD__,
00136             array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
00137                 $limit ? array( 'LIMIT' => $limit ) : array() )
00138         );
00139 
00140         $entries = array();
00141         foreach ( $res as $row ) {
00142             $item = array();
00143             foreach ( (array)$row as $key => $value ) {
00144                 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
00145             }
00146             $entries[] = $item;
00147         }
00148 
00149         return $entries;
00150     }
00151 
00157     protected function doPurgeOldLogs() {
00158         $status = Status::newGood();
00159         if ( $this->ttlDays <= 0 ) {
00160             return $status; // nothing to do
00161         }
00162 
00163         $dbw = $this->getMasterDB();
00164         $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
00165 
00166         $dbw->delete( 'filejournal',
00167             array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
00168             __METHOD__
00169         );
00170 
00171         return $status;
00172     }
00173 
00180     protected function getMasterDB() {
00181         if ( !$this->dbw ) {
00182             // Get a separate connection in autocommit mode
00183             $lb = wfGetLBFactory()->newMainLB();
00184             $this->dbw = $lb->getConnection( DB_MASTER, array(), $this->wiki );
00185             $this->dbw->clearFlag( DBO_TRX );
00186         }
00187 
00188         return $this->dbw;
00189     }
00190 }