MediaWiki  REL1_19
cleanupTable.inc
Go to the documentation of this file.
00001 <?php
00024 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00025 
00026 class TableCleanup extends Maintenance {
00027         protected $defaultParams = array(
00028                 'table' => 'page',
00029                 'conds' => array(),
00030                 'index' => 'page_id',
00031                 'callback' => 'processRow',
00032         );
00033 
00034         protected $dryrun = false;
00035         protected $maxLag = 10; # if slaves are lagged more than 10 secs, wait
00036         public $batchSize = 100;
00037         public $reportInterval = 100;
00038 
00039         public function __construct() {
00040                 parent::__construct();
00041                 $this->addOption( 'dry-run', 'Perform a dry run' );
00042         }
00043 
00044         public function execute() {
00045                 global $wgUser;
00046                 $wgUser = User::newFromName( 'Conversion script' );
00047                 $this->dryrun = $this->hasOption( 'dry-run' );
00048                 if ( $this->dryrun ) {
00049                         $this->output( "Checking for bad titles...\n" );
00050                 } else {
00051                         $this->output( "Checking and fixing bad titles...\n" );
00052                 }
00053                 $this->runTable( $this->defaultParams );
00054         }
00055 
00056         protected function init( $count, $table ) {
00057                 $this->processed = 0;
00058                 $this->updated = 0;
00059                 $this->count = $count;
00060                 $this->startTime = wfTime();
00061                 $this->table = $table;
00062         }
00063 
00064         protected function progress( $updated ) {
00065                 $this->updated += $updated;
00066                 $this->processed++;
00067                 if ( $this->processed % $this->reportInterval != 0 ) {
00068                         return;
00069                 }
00070                 $portion = $this->processed / $this->count;
00071                 $updateRate = $this->updated / $this->processed;
00072 
00073                 $now = wfTime();
00074                 $delta = $now - $this->startTime;
00075                 $estimatedTotalTime = $delta / $portion;
00076                 $eta = $this->startTime + $estimatedTotalTime;
00077 
00078                 $this->output(
00079                         sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
00080                                 wfWikiID(),
00081                                 wfTimestamp( TS_DB, intval( $now ) ),
00082                                 $portion * 100.0,
00083                                 $this->table,
00084                                 wfTimestamp( TS_DB, intval( $eta ) ),
00085                                 $this->processed,
00086                                 $this->count,
00087                                 $this->processed / $delta,
00088                                 $updateRate * 100.0
00089                         )
00090                 );
00091                 flush();
00092         }
00093 
00094         public function runTable( $params ) {
00095                 $dbr = wfGetDB( DB_SLAVE );
00096 
00097                 if ( array_diff( array_keys( $params ),
00098                         array( 'table', 'conds', 'index', 'callback' ) ) )
00099                 {
00100                         throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
00101                 }
00102 
00103                 $table = $params['table'];
00104                 // count(*) would melt the DB for huge tables, we can estimate here
00105                 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
00106                 $this->init( $count, $table );
00107                 $this->output( "Processing $table...\n" );
00108 
00109 
00110                 $index = (array)$params['index'];
00111                 $indexConds = array();
00112                 $options = array(
00113                         'ORDER BY' => implode( ',', $index ),
00114                         'LIMIT' => $this->batchSize
00115                 );
00116                 $callback = array( $this, $params['callback'] );
00117 
00118                 while ( true ) {
00119                         $conds = array_merge( $params['conds'], $indexConds );
00120                         $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
00121                         if ( !$res->numRows() ) {
00122                                 // Done
00123                                 break;
00124                         }
00125 
00126                         foreach ( $res as $row ) {
00127                                 call_user_func( $callback, $row );
00128                         }
00129 
00130                         if ( $res->numRows() < $this->batchSize ) {
00131                                 // Done
00132                                 break;
00133                         }
00134 
00135                         // Update the conditions to select the next batch.
00136                         // Construct a condition string by starting with the least significant part
00137                         // of the index, and adding more significant parts progressively to the left
00138                         // of the string.
00139                         $nextCond = '';
00140                         foreach ( array_reverse( $index ) as $field ) {
00141                                 $encValue = $dbr->addQuotes( $row->$field );
00142                                 if ( $nextCond === '' ) {
00143                                         $nextCond = "$field > $encValue";
00144                                 } else {
00145                                         $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
00146                                 }
00147                         }
00148                         $indexConds = array( $nextCond );
00149                 }
00150 
00151                 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
00152         }
00153 
00154         protected function hexChar( $matches ) {
00155                 return sprintf( "\\x%02x", ord( $matches[1] ) );
00156         }
00157 }
00158 
00159 class TableCleanupTest extends TableCleanup {
00160         function processRow( $row ) {
00161                 $this->progress( mt_rand( 0, 1 ) );
00162         }
00163 }
00164