MediaWiki  REL1_22
populateFilearchiveSha1.php
Go to the documentation of this file.
00001 <?php
00024 require_once dirname( __FILE__ ) . '/Maintenance.php';
00025 
00032 class PopulateFilearchiveSha1 extends LoggedUpdateMaintenance {
00033     public function __construct() {
00034         parent::__construct();
00035         $this->mDescription = "Populate the fa_sha1 field from fa_storage_key";
00036     }
00037 
00038     protected function getUpdateKey() {
00039         return 'populate fa_sha1';
00040     }
00041 
00042     protected function updateSkippedMessage() {
00043         return 'fa_sha1 column of filearchive table already populated.';
00044     }
00045 
00046     public function doDBUpdates() {
00047         $startTime = microtime( true );
00048         $dbw = wfGetDB( DB_MASTER );
00049         $table = 'filearchive';
00050         $conds = array( 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' );
00051 
00052         if ( !$dbw->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) {
00053             $this->output( "fa_sha1 column does not exist\n\n", true );
00054             return false;
00055         }
00056 
00057         $this->output( "Populating fa_sha1 field from fa_storage_key\n" );
00058         $endId = $dbw->selectField( $table, 'MAX(fa_id)', false, __METHOD__ );
00059 
00060         $batchSize = $this->mBatchSize;
00061         $done = 0;
00062 
00063         do {
00064             $res = $dbw->select(
00065                 $table,
00066                 array( 'fa_id', 'fa_storage_key' ),
00067                 $conds,
00068                 __METHOD__,
00069                 array( 'LIMIT' => $batchSize )
00070             );
00071 
00072             $i = 0;
00073             foreach ( $res as $row ) {
00074                 if ( $row->fa_storage_key == '' ) {
00075                     // Revision was missing pre-deletion
00076                     continue;
00077                 }
00078                 $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
00079                 $dbw->update( $table,
00080                     array( 'fa_sha1' => $sha1 ),
00081                     array( 'fa_id' => $row->fa_id ),
00082                     __METHOD__
00083                 );
00084                 $lastId = $row->fa_id;
00085                 $i++;
00086             }
00087 
00088             $done += $i;
00089             if ( $i !== $batchSize ) {
00090                 break;
00091             }
00092 
00093             // print status and let slaves catch up
00094             $this->output( sprintf(
00095                 "id %d done (up to %d), %5.3f%%  \r", $lastId, $endId, $lastId / $endId * 100 ) );
00096             wfWaitForSlaves();
00097         } while ( true );
00098 
00099         $processingTime = microtime( true ) - $startTime;
00100         $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
00101 
00102         return true; // we only updated *some* files, don't log
00103     }
00104 }
00105 
00106 $maintClass = "PopulateFilearchiveSha1";
00107 require_once RUN_MAINTENANCE_IF_MAIN;