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