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 = array(
36 'callback' => 'processRow',
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' );
49 public function execute() {
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" );
56 $this->output( "Checking and fixing bad titles...\n" );
58 $this->runTable( $this->defaultParams );
61 protected function init( $count, $table ) {
64 $this->count = $count;
65 $this->startTime = microtime( true );
66 $this->table = $table;
69 protected function progress( $updated ) {
70 $this->updated += $updated;
72 if ( $this->processed % $this->reportInterval != 0 ) {
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;
84 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
86 wfTimestamp( TS_DB, intval( $now ) ),
89 wfTimestamp( TS_DB, intval( $eta ) ),
92 $this->processed / $delta,
99 public function runTable( $params ) {
100 $dbr = wfGetDB( DB_SLAVE );
102 if ( array_diff( array_keys( $params ),
103 array( 'table', 'conds', 'index', 'callback' ) ) )
105 throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
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();
118 'ORDER BY' => implode( ',', $index ),
119 'LIMIT' => $this->batchSize
121 $callback = array( $this, $params['callback'] );
124 $conds = array_merge( $params['conds'], $indexConds );
125 $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
126 if ( !$res->numRows() ) {
131 foreach ( $res as $row ) {
132 call_user_func( $callback, $row );
135 if ( $res->numRows() < $this->batchSize ) {
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
145 foreach ( array_reverse( $index ) as $field ) {
146 $encValue = $dbr->addQuotes( $row->$field );
147 if ( $nextCond === '' ) {
148 $nextCond = "$field > $encValue";
150 $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
153 $indexConds = array( $nextCond );
156 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
159 protected function hexChar( $matches ) {
160 return sprintf( "\\x%02x", ord( $matches[1] ) );
164 class TableCleanupTest extends TableCleanup {
165 function processRow( $row ) {
166 $this->progress( mt_rand( 0, 1 ) );