MediaWiki  REL1_21
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 
00042         protected function __construct( array $config ) {
00043                 parent::__construct( $config );
00044 
00045                 $this->wiki = $config['wiki'];
00046         }
00047 
00052         protected function doLogChangeBatch( array $entries, $batchId ) {
00053                 $status = Status::newGood();
00054 
00055                 try {
00056                         $dbw = $this->getMasterDB();
00057                 } catch ( DBError $e ) {
00058                         $status->fatal( 'filejournal-fail-dbconnect', $this->backend );
00059                         return $status;
00060                 }
00061 
00062                 $now = wfTimestamp( TS_UNIX );
00063 
00064                 $data = array();
00065                 foreach ( $entries as $entry ) {
00066                         $data[] = array(
00067                                 'fj_batch_uuid' => $batchId,
00068                                 'fj_backend'    => $this->backend,
00069                                 'fj_op'         => $entry['op'],
00070                                 'fj_path'       => $entry['path'],
00071                                 'fj_new_sha1'   => $entry['newSha1'],
00072                                 'fj_timestamp'  => $dbw->timestamp( $now )
00073                         );
00074                 }
00075 
00076                 try {
00077                         $dbw->insert( 'filejournal', $data, __METHOD__ );
00078                         if ( mt_rand( 0, 99 ) == 0 ) {
00079                                 $this->purgeOldLogs(); // occasionally delete old logs
00080                         }
00081                 } catch ( DBError $e ) {
00082                         $status->fatal( 'filejournal-fail-dbquery', $this->backend );
00083                         return $status;
00084                 }
00085 
00086                 return $status;
00087         }
00088 
00093         protected function doGetCurrentPosition() {
00094                 $dbw = $this->getMasterDB();
00095 
00096                 return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
00097                         array( 'fj_backend' => $this->backend ),
00098                         __METHOD__
00099                 );
00100         }
00101 
00107         protected function doGetPositionAtTime( $time ) {
00108                 $dbw = $this->getMasterDB();
00109 
00110                 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
00111                 return $dbw->selectField( 'filejournal', 'fj_id',
00112                         array( 'fj_backend' => $this->backend, "fj_timestamp <= $encTimestamp" ),
00113                         __METHOD__,
00114                         array( 'ORDER BY' => 'fj_timestamp DESC' )
00115                 );
00116         }
00117 
00123         protected function doGetChangeEntries( $start, $limit ) {
00124                 $dbw = $this->getMasterDB();
00125 
00126                 $res = $dbw->select( 'filejournal', '*',
00127                         array(
00128                                 'fj_backend' => $this->backend,
00129                                 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
00130                         __METHOD__,
00131                         array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
00132                                 $limit ? array( 'LIMIT' => $limit ) : array() )
00133                 );
00134 
00135                 $entries = array();
00136                 foreach ( $res as $row ) {
00137                         $item = array();
00138                         foreach ( (array)$row as $key => $value ) {
00139                                 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
00140                         }
00141                         $entries[] = $item;
00142                 }
00143 
00144                 return $entries;
00145         }
00146 
00152         protected function doPurgeOldLogs() {
00153                 $status = Status::newGood();
00154                 if ( $this->ttlDays <= 0 ) {
00155                         return $status; // nothing to do
00156                 }
00157 
00158                 $dbw = $this->getMasterDB();
00159                 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
00160 
00161                 $dbw->delete( 'filejournal',
00162                         array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
00163                         __METHOD__
00164                 );
00165 
00166                 return $status;
00167         }
00168 
00175         protected function getMasterDB() {
00176                 if ( !$this->dbw ) {
00177                         // Get a separate connection in autocommit mode
00178                         $lb = wfGetLBFactory()->newMainLB();
00179                         $this->dbw = $lb->getConnection( DB_MASTER, array(), $this->wiki );
00180                         $this->dbw->clearFlag( DBO_TRX );
00181                 }
00182                 return $this->dbw;
00183         }
00184 }