* API: All pages list
[mediawiki.git] / maintenance / cleanupTable.inc
blob265eca9768c30c13b516d0eb3ac41724b81d15e2
1 <?php
3 require_once( 'FiveUpgrade.inc' );
5 abstract class TableCleanup extends FiveUpgrade {
6         function __construct( $table, $dryrun = false ) {
7                 parent::__construct();
9                 $this->targetTable = $table;
10                 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
11                 $this->dryrun = $dryrun;
12         }
14         function cleanup() {
15                 if( $this->dryrun ) {
16                         echo "Checking for bad titles...\n";
17                 } else {
18                         echo "Checking and fixing bad titles...\n";
19                 }
20                 $this->runTable( $this->targetTable,
21                         '', //'WHERE page_namespace=0',
22                         array( $this, 'processPage' ) );
23         }
25         function init( $count, $table ) {
26                 $this->processed = 0;
27                 $this->updated = 0;
28                 $this->count = $count;
29                 $this->startTime = wfTime();
30                 $this->table = $table;
31         }
33         function progress( $updated ) {
34                 $this->updated += $updated;
35                 $this->processed++;
36                 if( $this->processed % 100 != 0 ) {
37                         return;
38                 }
39                 $portion = $this->processed / $this->count;
40                 $updateRate = $this->updated / $this->processed;
42                 $now = wfTime();
43                 $delta = $now - $this->startTime;
44                 $estimatedTotalTime = $delta / $portion;
45                 $eta = $this->startTime + $estimatedTotalTime;
47                 global $wgDBname;
48                 printf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
49                         $wgDBname,
50                         wfTimestamp( TS_DB, intval( $now ) ),
51                         $portion * 100.0,
52                         $this->table,
53                         wfTimestamp( TS_DB, intval( $eta ) ),
54                         $this->processed,
55                         $this->count,
56                         $this->processed / $delta,
57                         $updateRate * 100.0 );
58                 flush();
59         }
61         function runTable( $table, $where, $callback ) {
62                 $fname = 'CapsCleanup::buildTable';
64                 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
65                 $this->init( $count, $table );
66                 $this->log( "Processing $table..." );
68                 $tableName = $this->dbr->tableName( $table );
69                 $sql = "SELECT * FROM $tableName $where";
70                 $result = $this->dbr->query( $sql, $fname );
72                 while( $row = $this->dbr->fetchObject( $result ) ) {
73                         $updated = call_user_func( $callback, $row );
74                 }
75                 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
76                 $this->dbr->freeResult( $result );
77         }
79         function hexChar( $matches ) {
80                 return sprintf( "\\x%02x", ord( $matches[1] ) );
81         }
82         
83         abstract function processPage( $row );
84