MediaWiki  REL1_24
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 
00053             return false;
00054         }
00055 
00056         $this->output( "Populating rev_len column\n" );
00057         $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::selectFields() );
00058 
00059         $this->output( "Populating ar_len column\n" );
00060         $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::selectArchiveFields() );
00061 
00062         $this->output( "rev_len and ar_len population complete "
00063             . "[$rev revision rows, $ar archive rows].\n" );
00064 
00065         return true;
00066     }
00067 
00075     protected function doLenUpdates( $table, $idCol, $prefix, $fields ) {
00076         $db = $this->getDB( DB_MASTER );
00077         $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
00078         $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
00079         if ( !$start || !$end ) {
00080             $this->output( "...$table table seems to be empty.\n" );
00081 
00082             return 0;
00083         }
00084 
00085         # Do remaining chunks
00086         $blockStart = intval( $start );
00087         $blockEnd = intval( $start ) + $this->mBatchSize - 1;
00088         $count = 0;
00089 
00090         while ( $blockStart <= $end ) {
00091             $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
00092             $res = $db->select(
00093                 $table,
00094                 $fields,
00095                 array(
00096                     "$idCol >= $blockStart",
00097                     "$idCol <= $blockEnd",
00098                     "{$prefix}_len IS NULL"
00099                 ),
00100                 __METHOD__
00101             );
00102 
00103             $db->begin( __METHOD__ );
00104             # Go through and update rev_len from these rows.
00105             foreach ( $res as $row ) {
00106                 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
00107                     $count++;
00108                 }
00109             }
00110             $db->commit( __METHOD__ );
00111 
00112             $blockStart += $this->mBatchSize;
00113             $blockEnd += $this->mBatchSize;
00114             wfWaitForSlaves();
00115         }
00116 
00117         return $count;
00118     }
00119 
00127     protected function upgradeRow( $row, $table, $idCol, $prefix ) {
00128         $db = $this->getDB( DB_MASTER );
00129 
00130         $rev = ( $table === 'archive' )
00131             ? Revision::newFromArchiveRow( $row )
00132             : new Revision( $row );
00133 
00134         $content = $rev->getContent();
00135         if ( !$content ) {
00136             # This should not happen, but sometimes does (bug 20757)
00137             $id = $row->$idCol;
00138             $this->output( "Content of $table $id unavailable!\n" );
00139 
00140             return false;
00141         }
00142 
00143         # Update the row...
00144         $db->update( $table,
00145             array( "{$prefix}_len" => $content->getSize() ),
00146             array( $idCol => $row->$idCol ),
00147             __METHOD__
00148         );
00149 
00150         return true;
00151     }
00152 }
00153 
00154 $maintClass = "PopulateRevisionLength";
00155 require_once RUN_MAINTENANCE_IF_MAIN;