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 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 $wgUser = User::newFromName( 'Conversion script' );
53 $this->dryrun = $this->hasOption( 'dry-run' );
54 if ( $this->dryrun ) {
55 $this->output( "Checking for bad titles...\n" );
57 $this->output( "Checking and fixing bad titles...\n" );
59 $this->runTable( $this->defaultParams );
62 protected function init( $count, $table ) {
65 $this->count = $count;
66 $this->startTime = microtime( true );
67 $this->table = $table;
73 protected function progress( $updated ) {
74 $this->updated += $updated;
76 if ( $this->processed % $this->reportInterval != 0 ) {
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;
88 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
90 wfTimestamp( TS_DB, intval( $now ) ),
93 wfTimestamp( TS_DB, intval( $eta ) ),
96 $this->processed / $delta,
104 * @param array $params
105 * @throws MWException
107 public function runTable( $params ) {
108 $dbr = wfGetDB( DB_SLAVE );
110 if ( array_diff( array_keys( $params ),
111 array( 'table', 'conds', 'index', 'callback' ) )
113 throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
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();
125 'ORDER BY' => implode( ',', $index ),
126 'LIMIT' => $this->batchSize
128 $callback = array( $this, $params['callback'] );
131 $conds = array_merge( $params['conds'], $indexConds );
132 $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
133 if ( !$res->numRows() ) {
138 foreach ( $res as $row ) {
139 call_user_func( $callback, $row );
142 if ( $res->numRows() < $this->batchSize ) {
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
152 foreach ( array_reverse( $index ) as $field ) {
153 $encValue = $dbr->addQuotes( $row->$field );
154 if ( $nextCond === '' ) {
155 $nextCond = "$field > $encValue";
157 $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
160 $indexConds = array( $nextCond );
163 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
167 * @param array $matches
170 protected function hexChar( $matches ) {
171 return sprintf( "\\x%02x", ord( $matches[1] ) );
175 class TableCleanupTest extends TableCleanup {
176 function processRow( $row ) {
177 $this->progress( mt_rand( 0, 1 ) );