StatusTest.php: Make lines shorter to make phpcs happier
[mediawiki.git] / maintenance / cleanupTable.inc
blobf6259e95c8fb6b8f481ccea9490c54e9ca970d4f
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         public $batchSize = 100;
41         public $reportInterval = 100;
43         protected $processed, $updated, $count, $startTime, $table;
45         public function __construct() {
46                 parent::__construct();
47                 $this->addOption( 'dry-run', 'Perform a dry run' );
48         }
50         public function execute() {
51                 global $wgUser;
52                 $wgUser = User::newFromName( 'Conversion script' );
53                 $this->dryrun = $this->hasOption( 'dry-run' );
54                 if ( $this->dryrun ) {
55                         $this->output( "Checking for bad titles...\n" );
56                 } else {
57                         $this->output( "Checking and fixing bad titles...\n" );
58                 }
59                 $this->runTable( $this->defaultParams );
60         }
62         protected function init( $count, $table ) {
63                 $this->processed = 0;
64                 $this->updated = 0;
65                 $this->count = $count;
66                 $this->startTime = microtime( true );
67                 $this->table = $table;
68         }
70         /**
71          * @param int $updated
72          */
73         protected function progress( $updated ) {
74                 $this->updated += $updated;
75                 $this->processed++;
76                 if ( $this->processed % $this->reportInterval != 0 ) {
77                         return;
78                 }
79                 $portion = $this->processed / $this->count;
80                 $updateRate = $this->updated / $this->processed;
82                 $now = microtime( true );
83                 $delta = $now - $this->startTime;
84                 $estimatedTotalTime = $delta / $portion;
85                 $eta = $this->startTime + $estimatedTotalTime;
87                 $this->output(
88                         sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
89                                 wfWikiID(),
90                                 wfTimestamp( TS_DB, intval( $now ) ),
91                                 $portion * 100.0,
92                                 $this->table,
93                                 wfTimestamp( TS_DB, intval( $eta ) ),
94                                 $this->processed,
95                                 $this->count,
96                                 $this->processed / $delta,
97                                 $updateRate * 100.0
98                         )
99                 );
100                 flush();
101         }
103         /**
104          * @param array $params
105          * @throws MWException
106          */
107         public function runTable( $params ) {
108                 $dbr = wfGetDB( DB_SLAVE );
110                 if ( array_diff( array_keys( $params ),
111                         array( 'table', 'conds', 'index', 'callback' ) )
112                 ) {
113                         throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
114                 }
116                 $table = $params['table'];
117                 // count(*) would melt the DB for huge tables, we can estimate here
118                 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
119                 $this->init( $count, $table );
120                 $this->output( "Processing $table...\n" );
122                 $index = (array)$params['index'];
123                 $indexConds = array();
124                 $options = array(
125                         'ORDER BY' => implode( ',', $index ),
126                         'LIMIT' => $this->batchSize
127                 );
128                 $callback = array( $this, $params['callback'] );
130                 while ( true ) {
131                         $conds = array_merge( $params['conds'], $indexConds );
132                         $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
133                         if ( !$res->numRows() ) {
134                                 // Done
135                                 break;
136                         }
138                         foreach ( $res as $row ) {
139                                 call_user_func( $callback, $row );
140                         }
142                         if ( $res->numRows() < $this->batchSize ) {
143                                 // Done
144                                 break;
145                         }
147                         // Update the conditions to select the next batch.
148                         // Construct a condition string by starting with the least significant part
149                         // of the index, and adding more significant parts progressively to the left
150                         // of the string.
151                         $nextCond = '';
152                         foreach ( array_reverse( $index ) as $field ) {
153                                 $encValue = $dbr->addQuotes( $row->$field );
154                                 if ( $nextCond === '' ) {
155                                         $nextCond = "$field > $encValue";
156                                 } else {
157                                         $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
158                                 }
159                         }
160                         $indexConds = array( $nextCond );
161                 }
163                 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
164         }
166         /**
167          * @param array $matches
168          * @return string
169          */
170         protected function hexChar( $matches ) {
171                 return sprintf( "\\x%02x", ord( $matches[1] ) );
172         }
175 class TableCleanupTest extends TableCleanup {
176         function processRow( $row ) {
177                 $this->progress( mt_rand( 0, 1 ) );
178         }