MediaWiki  REL1_23
populateRevisionLength.php
Go to the documentation of this file.
00001 <?php
00025 require_once __DIR__ . '/Maintenance.php';
00026 
00033 class PopulateRevisionLength extends LoggedUpdateMaintenance {
00034     public function __construct() {
00035         parent::__construct();
00036         $this->mDescription = "Populates the rev_len and ar_len fields";
00037         $this->setBatchSize( 200 );
00038     }
00039 
00040     protected function getUpdateKey() {
00041         return 'populate rev_len and ar_len';
00042     }
00043 
00044     public function doDBUpdates() {
00045         $db = $this->getDB( DB_MASTER );
00046         if ( !$db->tableExists( 'revision' ) ) {
00047             $this->error( "revision table does not exist", true );
00048         } elseif ( !$db->tableExists( 'archive' ) ) {
00049             $this->error( "archive table does not exist", true );
00050         } elseif ( !$db->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
00051             $this->output( "rev_len column does not exist\n\n", true );
00052             return false;
00053         }
00054 
00055         $this->output( "Populating rev_len column\n" );
00056         $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::selectFields() );
00057 
00058         $this->output( "Populating ar_len column\n" );
00059         $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::selectArchiveFields() );
00060 
00061         $this->output( "rev_len and ar_len population complete [$rev revision rows, $ar archive rows].\n" );
00062         return true;
00063     }
00064 
00072     protected function doLenUpdates( $table, $idCol, $prefix, $fields ) {
00073         $db = $this->getDB( DB_MASTER );
00074         $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
00075         $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
00076         if ( !$start || !$end ) {
00077             $this->output( "...$table table seems to be empty.\n" );
00078             return 0;
00079         }
00080 
00081         # Do remaining chunks
00082         $blockStart = intval( $start );
00083         $blockEnd = intval( $start ) + $this->mBatchSize - 1;
00084         $count = 0;
00085 
00086         while ( $blockStart <= $end ) {
00087             $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
00088             $res = $db->select(
00089                 $table,
00090                 $fields,
00091                 array(
00092                     "$idCol >= $blockStart",
00093                     "$idCol <= $blockEnd",
00094                     "{$prefix}_len IS NULL"
00095                 ),
00096                 __METHOD__
00097             );
00098 
00099             $db->begin( __METHOD__ );
00100             # Go through and update rev_len from these rows.
00101             foreach ( $res as $row ) {
00102                 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
00103                     $count++;
00104                 }
00105             }
00106             $db->commit( __METHOD__ );
00107 
00108             $blockStart += $this->mBatchSize;
00109             $blockEnd += $this->mBatchSize;
00110             wfWaitForSlaves();
00111         }
00112 
00113         return $count;
00114     }
00115 
00123     protected function upgradeRow( $row, $table, $idCol, $prefix ) {
00124         $db = $this->getDB( DB_MASTER );
00125 
00126         $rev = ( $table === 'archive' )
00127             ? Revision::newFromArchiveRow( $row )
00128             : new Revision( $row );
00129 
00130         $content = $rev->getContent();
00131         if ( !$content ) {
00132             # This should not happen, but sometimes does (bug 20757)
00133             $id = $row->$idCol;
00134             $this->output( "Content of $table $id unavailable!\n" );
00135             return false;
00136         }
00137 
00138         # Update the row...
00139         $db->update( $table,
00140             array( "{$prefix}_len" => $content->getSize() ),
00141             array( $idCol => $row->$idCol ),
00142             __METHOD__
00143         );
00144 
00145         return true;
00146     }
00147 }
00148 
00149 $maintClass = "PopulateRevisionLength";
00150 require_once RUN_MAINTENANCE_IF_MAIN;