3 * Generic class to cleanup a database table.
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.
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.
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
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
27 * Generic class to cleanup a database table. Already subclasses Maintenance.
29 * @ingroup Maintenance
31 class TableCleanup extends Maintenance {
32 protected $defaultParams = [
36 'callback' => 'processRow',
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' );
50 public function execute() {
52 $this->dryrun = $this->hasOption( 'dry-run' );
53 if ( $this->dryrun ) {
54 $wgUser = User::newFromName( 'Conversion script' );
55 $this->output( "Checking for bad titles...\n" );
57 $wgUser = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
58 $this->output( "Checking and fixing bad titles...\n" );
60 $this->runTable( $this->defaultParams );
63 protected function init( $count, $table ) {
66 $this->count = $count;
67 $this->startTime = microtime( true );
68 $this->table = $table;
74 protected function progress( $updated ) {
75 $this->updated += $updated;
77 if ( $this->processed % $this->reportInterval != 0 ) {
80 $portion = $this->processed / $this->count;
81 $updateRate = $this->updated / $this->processed;
83 $now = microtime( true );
84 $delta = $now - $this->startTime;
85 $estimatedTotalTime = $delta / $portion;
86 $eta = $this->startTime + $estimatedTotalTime;
89 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
91 wfTimestamp( TS_DB, intval( $now ) ),
94 wfTimestamp( TS_DB, intval( $eta ) ),
97 $this->processed / $delta,
105 * @param array $params
106 * @throws MWException
108 public function runTable( $params ) {
109 $dbr = $this->getDB( DB_REPLICA );
111 if ( array_diff( array_keys( $params ),
112 [ 'table', 'conds', 'index', 'callback' ] )
114 throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
117 $table = $params['table'];
118 // count(*) would melt the DB for huge tables, we can estimate here
119 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
120 $this->init( $count, $table );
121 $this->output( "Processing $table...\n" );
123 $index = (array)$params['index'];
126 'ORDER BY' => implode( ',', $index ),
127 'LIMIT' => $this->batchSize
129 $callback = [ $this, $params['callback'] ];
132 $conds = array_merge( $params['conds'], $indexConds );
133 $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
134 if ( !$res->numRows() ) {
139 foreach ( $res as $row ) {
140 call_user_func( $callback, $row );
143 if ( $res->numRows() < $this->batchSize ) {
148 // Update the conditions to select the next batch.
149 // Construct a condition string by starting with the least significant part
150 // of the index, and adding more significant parts progressively to the left
153 foreach ( array_reverse( $index ) as $field ) {
154 $encValue = $dbr->addQuotes( $row->$field );
155 if ( $nextCond === '' ) {
156 $nextCond = "$field > $encValue";
158 $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
161 $indexConds = [ $nextCond ];
164 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
168 * @param array $matches
171 protected function hexChar( $matches ) {
172 return sprintf( "\\x%02x", ord( $matches[1] ) );