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