]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/cleanupTable.inc
MediaWiki 1.16.4-scripts
[autoinstalls/mediawiki.git] / maintenance / cleanupTable.inc
1 <?php
2 /**
3  * Generic table cleanup class. Already subclasses maintenance
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @ingroup Maintenance
21  */
22
23 require_once( dirname(__FILE__) . '/Maintenance.php' );
24
25 class TableCleanup extends Maintenance {
26         protected $defaultParams = array(
27                 'table' => 'page',
28                 'conds' => array(),
29                 'index' => 'page_id',
30                 'callback' => 'processRow',
31         );
32
33         protected $dryrun = false;
34         protected $maxLag = 10; # if slaves are lagged more than 10 secs, wait
35         public $batchSize = 100;
36         public $reportInterval = 100;
37
38         public function __construct() {
39                 parent::__construct();
40                 $this->addOption( 'dry-run', 'Perform a dry run' );
41         }
42
43         public function execute() {
44                 global $wgUser;
45                 $wgUser->setName( 'Conversion script' );
46                 $this->dryrun = $this->hasOption( 'dry-run' );
47                 if( $this->dryrun ) {
48                         $this->output( "Checking for bad titles...\n" );
49                 } else {
50                         $this->output( "Checking and fixing bad titles...\n" );
51                 }
52                 $this->runTable( $this->defaultParams );
53         }
54
55         protected function init( $count, $table ) {
56                 $this->processed = 0;
57                 $this->updated = 0;
58                 $this->count = $count;
59                 $this->startTime = wfTime();
60                 $this->table = $table;
61         }
62
63         protected function progress( $updated ) {
64                 $this->updated += $updated;
65                 $this->processed++;
66                 if( $this->processed % $this->reportInterval != 0 ) {
67                         return;
68                 }
69                 $portion = $this->processed / $this->count;
70                 $updateRate = $this->updated / $this->processed;
71
72                 $now = wfTime();
73                 $delta = $now - $this->startTime;
74                 $estimatedTotalTime = $delta / $portion;
75                 $eta = $this->startTime + $estimatedTotalTime;
76
77                 $this->output( 
78                         sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
79                                 wfWikiID(),
80                                 wfTimestamp( TS_DB, intval( $now ) ),
81                                 $portion * 100.0,
82                                 $this->table,
83                                 wfTimestamp( TS_DB, intval( $eta ) ),
84                                 $this->processed,
85                                 $this->count,
86                                 $this->processed / $delta,
87                                 $updateRate * 100.0 
88                         )
89                 );
90                 flush();
91         }
92
93         public function runTable( $params ) {
94                 $dbr = wfGetDB( DB_SLAVE );
95
96                 if ( array_diff( array_keys( $params ),
97                         array( 'table', 'conds', 'index', 'callback' ) ) ) 
98                 {
99                         throw new MWException( __METHOD__.': Missing parameter ' . implode( ', ', $params ) );
100                 }
101
102                 $table = $params['table'];
103                 $count = $dbr->selectField( $table, 'count(*)', $params['conds'], __METHOD__ );
104                 $this->init( $count, $table );
105                 $this->output( "Processing $table...\n" );
106
107
108                 $index = (array)$params['index'];
109                 $indexConds = array();
110                 $options = array(
111                         'ORDER BY' => implode( ',', $index ),
112                         'LIMIT' => $this->batchSize
113                 );
114                 $callback = array( $this, $params['callback'] );
115
116                 while ( true ) {
117                         $conds = array_merge( $params['conds'], $indexConds );
118                         $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
119                         if ( !$res->numRows() ) {
120                                 // Done
121                                 break;
122                         }
123
124                         foreach ( $res as $row ) {
125                                 call_user_func( $callback, $row );
126                         }
127
128                         if ( $res->numRows() < $this->batchSize ) {
129                                 // Done
130                                 break;
131                         }
132
133                         // Update the conditions to select the next batch.
134                         // Construct a condition string by starting with the least significant part
135                         // of the index, and adding more significant parts progressively to the left
136                         // of the string.
137                         $nextCond = '';
138                         foreach ( array_reverse( $index ) as $field ) {
139                                 $encValue = $dbr->addQuotes( $row->$field );
140                                 if ( $nextCond === '' ) {
141                                         $nextCond = "$field > $encValue";
142                                 } else {
143                                         $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
144                                 }
145                         }
146                         $indexConds = array( $nextCond );
147                 }
148
149                 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
150         }
151
152         protected function hexChar( $matches ) {
153                 return sprintf( "\\x%02x", ord( $matches[1] ) );
154         }
155 }
156
157 class TableCleanupTest extends TableCleanup {
158         function processRow( $row ) {
159                 $this->progress( mt_rand( 0, 1 ) );
160         }
161 }
162