00001 <?php
00002
00003 require_once( 'FiveUpgrade.inc' );
00004
00008 abstract class TableCleanup extends FiveUpgrade {
00009 function __construct( $table, $dryrun = false ) {
00010 parent::__construct();
00011
00012 $this->targetTable = $table;
00013 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
00014 $this->dryrun = $dryrun;
00015 }
00016
00017 function cleanup() {
00018 if( $this->dryrun ) {
00019 echo "Checking for bad titles...\n";
00020 } else {
00021 echo "Checking and fixing bad titles...\n";
00022 }
00023 $this->runTable( $this->targetTable,
00024 '',
00025 array( $this, 'processPage' ) );
00026 }
00027
00028 function init( $count, $table ) {
00029 $this->processed = 0;
00030 $this->updated = 0;
00031 $this->count = $count;
00032 $this->startTime = wfTime();
00033 $this->table = $table;
00034 }
00035
00036 function progress( $updated ) {
00037 $this->updated += $updated;
00038 $this->processed++;
00039 if( $this->processed % 100 != 0 ) {
00040 return;
00041 }
00042 $portion = $this->processed / $this->count;
00043 $updateRate = $this->updated / $this->processed;
00044
00045 $now = wfTime();
00046 $delta = $now - $this->startTime;
00047 $estimatedTotalTime = $delta / $portion;
00048 $eta = $this->startTime + $estimatedTotalTime;
00049
00050 printf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
00051 wfWikiID(),
00052 wfTimestamp( TS_DB, intval( $now ) ),
00053 $portion * 100.0,
00054 $this->table,
00055 wfTimestamp( TS_DB, intval( $eta ) ),
00056 $this->processed,
00057 $this->count,
00058 $this->processed / $delta,
00059 $updateRate * 100.0 );
00060 flush();
00061 }
00062
00063 function runTable( $table, $where, $callback ) {
00064 $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
00065 $this->init( $count, $table );
00066 $this->log( "Processing $table..." );
00067
00068 $tableName = $this->dbr->tableName( $table );
00069 $sql = "SELECT * FROM $tableName $where";
00070 $result = $this->dbr->query( $sql, __METHOD__ );
00071
00072 foreach( $result as $row ) {
00073 call_user_func( $callback, $row );
00074 }
00075 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
00076 $result->free();
00077 }
00078
00079 function hexChar( $matches ) {
00080 return sprintf( "\\x%02x", ord( $matches[1] ) );
00081 }
00082
00083 abstract function processPage( $row );
00084
00085 }