MediaWiki  REL1_20
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                 } catch ( DBError $e ) {
00079                         $status->fatal( 'filejournal-fail-dbquery', $this->backend );
00080                         return $status;
00081                 }
00082 
00083                 return $status;
00084         }
00085 
00091         protected function doGetChangeEntries( $start, $limit ) {
00092                 $dbw = $this->getMasterDB();
00093 
00094                 $res = $dbw->select( 'filejournal', '*',
00095                         array(
00096                                 'fj_backend' => $this->backend,
00097                                 'fj_id >= ' . $dbw->addQuotes( (int)$start ) ), // $start may be 0
00098                         __METHOD__,
00099                         array_merge( array( 'ORDER BY' => 'fj_id ASC' ),
00100                                 $limit ? array( 'LIMIT' => $limit ) : array() )
00101                 );
00102 
00103                 $entries = array();
00104                 foreach ( $res as $row ) {
00105                         $item = array();
00106                         foreach ( (array)$row as $key => $value ) {
00107                                 $item[substr( $key, 3 )] = $value; // "fj_op" => "op"
00108                         }
00109                         $entries[] = $item;
00110                 }
00111 
00112                 return $entries;
00113         }
00114 
00120         protected function doPurgeOldLogs() {
00121                 $status = Status::newGood();
00122                 if ( $this->ttlDays <= 0 ) {
00123                         return $status; // nothing to do
00124                 }
00125 
00126                 $dbw = $this->getMasterDB();
00127                 $dbCutoff = $dbw->timestamp( time() - 86400 * $this->ttlDays );
00128 
00129                 $dbw->delete( 'filejournal',
00130                         array( 'fj_timestamp < ' . $dbw->addQuotes( $dbCutoff ) ),
00131                         __METHOD__
00132                 );
00133 
00134                 return $status;
00135         }
00136 
00143         protected function getMasterDB() {
00144                 if ( !$this->dbw ) {
00145                         // Get a separate connection in autocommit mode
00146                         $lb = wfGetLBFactory()->newMainLB();
00147                         $this->dbw = $lb->getConnection( DB_MASTER, array(), $this->wiki );
00148                         $this->dbw->clearFlag( DBO_TRX );
00149                 }
00150                 return $this->dbw;
00151         }
00152 }