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