Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / cleanupTable.inc
blob57acfd8211223c80cce1869ebcc4f4f229b67dcb
1 <?php
2 /**
3  * Generic class to cleanup a database table.
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  * @file
21  * @ingroup Maintenance
22  */
24 require_once( __DIR__ . '/Maintenance.php' );
26 /**
27  * Generic class to cleanup a database table. Already subclasses Maintenance.
28  *
29  * @ingroup Maintenance
30  */
31 class TableCleanup extends Maintenance {
32         protected $defaultParams = array(
33                 'table' => 'page',
34                 'conds' => array(),
35                 'index' => 'page_id',
36                 'callback' => 'processRow',
37         );
39         protected $dryrun = false;
40         protected $maxLag = 10; # if slaves are lagged more than 10 secs, wait
41         public $batchSize = 100;
42         public $reportInterval = 100;
44         public function __construct() {
45                 parent::__construct();
46                 $this->addOption( 'dry-run', 'Perform a dry run' );
47         }
49         public function execute() {
50                 global $wgUser;
51                 $wgUser = User::newFromName( 'Conversion script' );
52                 $this->dryrun = $this->hasOption( 'dry-run' );
53                 if ( $this->dryrun ) {
54                         $this->output( "Checking for bad titles...\n" );
55                 } else {
56                         $this->output( "Checking and fixing bad titles...\n" );
57                 }
58                 $this->runTable( $this->defaultParams );
59         }
61         protected function init( $count, $table ) {
62                 $this->processed = 0;
63                 $this->updated = 0;
64                 $this->count = $count;
65                 $this->startTime = microtime( true );
66                 $this->table = $table;
67         }
69         protected function progress( $updated ) {
70                 $this->updated += $updated;
71                 $this->processed++;
72                 if ( $this->processed % $this->reportInterval != 0 ) {
73                         return;
74                 }
75                 $portion = $this->processed / $this->count;
76                 $updateRate = $this->updated / $this->processed;
78                 $now = microtime( true );
79                 $delta = $now - $this->startTime;
80                 $estimatedTotalTime = $delta / $portion;
81                 $eta = $this->startTime + $estimatedTotalTime;
83                 $this->output(
84                         sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
85                                 wfWikiID(),
86                                 wfTimestamp( TS_DB, intval( $now ) ),
87                                 $portion * 100.0,
88                                 $this->table,
89                                 wfTimestamp( TS_DB, intval( $eta ) ),
90                                 $this->processed,
91                                 $this->count,
92                                 $this->processed / $delta,
93                                 $updateRate * 100.0
94                         )
95                 );
96                 flush();
97         }
99         public function runTable( $params ) {
100                 $dbr = wfGetDB( DB_SLAVE );
102                 if ( array_diff( array_keys( $params ),
103                         array( 'table', 'conds', 'index', 'callback' ) ) )
104                 {
105                         throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
106                 }
108                 $table = $params['table'];
109                 // count(*) would melt the DB for huge tables, we can estimate here
110                 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
111                 $this->init( $count, $table );
112                 $this->output( "Processing $table...\n" );
115                 $index = (array)$params['index'];
116                 $indexConds = array();
117                 $options = array(
118                         'ORDER BY' => implode( ',', $index ),
119                         'LIMIT' => $this->batchSize
120                 );
121                 $callback = array( $this, $params['callback'] );
123                 while ( true ) {
124                         $conds = array_merge( $params['conds'], $indexConds );
125                         $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
126                         if ( !$res->numRows() ) {
127                                 // Done
128                                 break;
129                         }
131                         foreach ( $res as $row ) {
132                                 call_user_func( $callback, $row );
133                         }
135                         if ( $res->numRows() < $this->batchSize ) {
136                                 // Done
137                                 break;
138                         }
140                         // Update the conditions to select the next batch.
141                         // Construct a condition string by starting with the least significant part
142                         // of the index, and adding more significant parts progressively to the left
143                         // of the string.
144                         $nextCond = '';
145                         foreach ( array_reverse( $index ) as $field ) {
146                                 $encValue = $dbr->addQuotes( $row->$field );
147                                 if ( $nextCond === '' ) {
148                                         $nextCond = "$field > $encValue";
149                                 } else {
150                                         $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
151                                 }
152                         }
153                         $indexConds = array( $nextCond );
154                 }
156                 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
157         }
159         protected function hexChar( $matches ) {
160                 return sprintf( "\\x%02x", ord( $matches[1] ) );
161         }
164 class TableCleanupTest extends TableCleanup {
165         function processRow( $row ) {
166                 $this->progress( mt_rand( 0, 1 ) );
167         }