MediaWiki  REL1_20
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                 }
00052                 $this->output( "Populating rev_len column\n" );
00053 
00054                 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __METHOD__ );
00055                 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ );
00056                 if ( !$start || !$end ) {
00057                         $this->output( "...revision table seems to be empty.\n" );
00058                         return true;
00059                 }
00060 
00061                 # Do remaining chunks
00062                 $blockStart = intval( $start );
00063                 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
00064                 $count = 0;
00065                 $missing = 0;
00066                 while ( $blockStart <= $end ) {
00067                         $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
00068                         $res = $db->select( 'revision',
00069                                                 Revision::selectFields(),
00070                                                 array( "rev_id >= $blockStart",
00071                                                    "rev_id <= $blockEnd",
00072                                                    "rev_len IS NULL" ),
00073                                                 __METHOD__ );
00074                         # Go through and update rev_len from these rows.
00075                         foreach ( $res as $row ) {
00076                                 $rev = new Revision( $row );
00077                                 $text = $rev->getRawText();
00078                                 if ( !is_string( $text ) ) {
00079                                         # This should not happen, but sometimes does (bug 20757)
00080                                         $this->output( "Text of revision {$row->rev_id} unavailable!\n" );
00081                                         $missing++;
00082                                 }
00083                                 else {
00084                                         # Update the row...
00085                                         $db->update( 'revision',
00086                                                          array( 'rev_len' => strlen( $text ) ),
00087                                                          array( 'rev_id' => $row->rev_id ),
00088                                                          __METHOD__ );
00089                                         $count++;
00090                                 }
00091                         }
00092                         $blockStart += $this->mBatchSize;
00093                         $blockEnd += $this->mBatchSize;
00094                         wfWaitForSlaves();
00095                 }
00096 
00097                 $this->output( "rev_len population complete ... {$count} rows changed ({$missing} missing)\n" );
00098                 return true;
00099         }
00100 }
00101 
00102 $maintClass = "PopulateRevisionLength";
00103 require_once( RUN_MAINTENANCE_IF_MAIN );