MediaWiki  REL1_22
populateRevisionLength.php
Go to the documentation of this file.
00001 <?php
00024 require_once __DIR__ . '/Maintenance.php';
00025 
00032 class PopulateRevisionLength extends LoggedUpdateMaintenance {
00033     public function __construct() {
00034         parent::__construct();
00035         $this->mDescription = "Populates the rev_len field";
00036         $this->setBatchSize( 200 );
00037     }
00038 
00039     protected function getUpdateKey() {
00040         return 'populate rev_len';
00041     }
00042 
00043     protected function updateSkippedMessage() {
00044         return 'rev_len column of revision table already populated.';
00045     }
00046 
00047     public function doDBUpdates() {
00048         $db = $this->getDB( DB_MASTER );
00049         if ( !$db->tableExists( 'revision' ) ) {
00050             $this->error( "revision table does not exist", true );
00051         } elseif ( !$db->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
00052             $this->output( "rev_len column does not exist\n\n", true );
00053             return false;
00054         }
00055 
00056         $this->output( "Populating rev_len column\n" );
00057 
00058         $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __METHOD__ );
00059         $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ );
00060         if ( !$start || !$end ) {
00061             $this->output( "...revision table seems to be empty.\n" );
00062             return true;
00063         }
00064 
00065         # Do remaining chunks
00066         $blockStart = intval( $start );
00067         $blockEnd = intval( $start ) + $this->mBatchSize - 1;
00068         $count = 0;
00069         $missing = 0;
00070         $fields = Revision::selectFields();
00071         while ( $blockStart <= $end ) {
00072             $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
00073             $res = $db->select(
00074                 'revision',
00075                 $fields,
00076                 array(
00077                     "rev_id >= $blockStart",
00078                     "rev_id <= $blockEnd",
00079                     "rev_len IS NULL"
00080                 ),
00081                 __METHOD__
00082             );
00083             # Go through and update rev_len from these rows.
00084             foreach ( $res as $row ) {
00085                 $rev = new Revision( $row );
00086                 $content = $rev->getContent();
00087                 if ( !$content ) {
00088                     # This should not happen, but sometimes does (bug 20757)
00089                     $this->output( "Content of revision {$row->rev_id} unavailable!\n" );
00090                     $missing++;
00091                 }
00092                 else {
00093                     # Update the row...
00094                     $db->update( 'revision',
00095                              array( 'rev_len' => $content->getSize() ),
00096                              array( 'rev_id' => $row->rev_id ),
00097                              __METHOD__ );
00098                     $count++;
00099                 }
00100             }
00101             $blockStart += $this->mBatchSize;
00102             $blockEnd += $this->mBatchSize;
00103             wfWaitForSlaves();
00104         }
00105 
00106         $this->output( "rev_len population complete ... {$count} rows changed ({$missing} missing)\n" );
00107         return true;
00108     }
00109 }
00110 
00111 $maintClass = "PopulateRevisionLength";
00112 require_once RUN_MAINTENANCE_IF_MAIN;