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