fd9b5af83471dfdd97d50a17bcdc2f728fd8dd74
[lhc/web/wiklou.git] / maintenance / cleanupTable.inc
1 <?php
2
3 require_once( 'FiveUpgrade.inc' );
4
5 abstract class TableCleanup extends FiveUpgrade {
6 function __construct( $table, $dryrun = false ) {
7 parent::__construct();
8
9 $this->targetTable = $table;
10 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
11 $this->dryrun = $dryrun;
12 }
13
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 }
24
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 }
32
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;
41
42 $now = wfTime();
43 $delta = $now - $this->startTime;
44 $estimatedTotalTime = $delta / $portion;
45 $eta = $this->startTime + $estimatedTotalTime;
46
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 }
60
61 function runTable( $table, $where, $callback ) {
62 $fname = 'CapsCleanup::buildTable';
63
64 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
65 $this->init( $count, $table );
66 $this->log( "Processing $table..." );
67
68 $tableName = $this->dbr->tableName( $table );
69 $sql = "SELECT * FROM $tableName $where";
70 $result = $this->dbr->query( $sql, $fname );
71
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 }
78
79 abstract function processPage( $row );
80
81 }
82
83 ?>