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