3 * Generic table cleanup class. Already subclasses maintenance
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
20 * @ingroup Maintenance
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25 class TableCleanup extends Maintenance {
26 protected $defaultParams = array(
30 'callback' => 'processRow',
33 protected $dryrun = false;
34 protected $maxLag = 10; # if slaves are lagged more than 10 secs, wait
35 public $batchSize = 100;
36 public $reportInterval = 100;
38 public function __construct() {
39 parent::__construct();
40 $this->addOption( 'dry-run', 'Perform a dry run' );
43 public function execute() {
45 $wgUser->setName( 'Conversion script' );
46 $this->dryrun = $this->hasOption( 'dry-run' );
47 if ( $this->dryrun ) {
48 $this->output( "Checking for bad titles...\n" );
50 $this->output( "Checking and fixing bad titles...\n" );
52 $this->runTable( $this->defaultParams );
55 protected function init( $count, $table ) {
58 $this->count = $count;
59 $this->startTime = wfTime();
60 $this->table = $table;
63 protected function progress( $updated ) {
64 $this->updated += $updated;
66 if ( $this->processed % $this->reportInterval != 0 ) {
69 $portion = $this->processed / $this->count;
70 $updateRate = $this->updated / $this->processed;
73 $delta = $now - $this->startTime;
74 $estimatedTotalTime = $delta / $portion;
75 $eta = $this->startTime + $estimatedTotalTime;
78 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
80 wfTimestamp( TS_DB, intval( $now ) ),
83 wfTimestamp( TS_DB, intval( $eta ) ),
86 $this->processed / $delta,
93 public function runTable( $params ) {
94 $dbr = wfGetDB( DB_SLAVE );
96 if ( array_diff( array_keys( $params ),
97 array( 'table', 'conds', 'index', 'callback' ) ) )
99 throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
102 $table = $params['table'];
103 $count = $dbr->selectField( $table, 'count(*)', $params['conds'], __METHOD__ );
104 $this->init( $count, $table );
105 $this->output( "Processing $table...\n" );
108 $index = (array)$params['index'];
109 $indexConds = array();
111 'ORDER BY' => implode( ',', $index ),
112 'LIMIT' => $this->batchSize
114 $callback = array( $this, $params['callback'] );
117 $conds = array_merge( $params['conds'], $indexConds );
118 $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
119 if ( !$res->numRows() ) {
124 foreach ( $res as $row ) {
125 call_user_func( $callback, $row );
128 if ( $res->numRows() < $this->batchSize ) {
133 // Update the conditions to select the next batch.
134 // Construct a condition string by starting with the least significant part
135 // of the index, and adding more significant parts progressively to the left
138 foreach ( array_reverse( $index ) as $field ) {
139 $encValue = $dbr->addQuotes( $row->$field );
140 if ( $nextCond === '' ) {
141 $nextCond = "$field > $encValue";
143 $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
146 $indexConds = array( $nextCond );
149 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
152 protected function hexChar( $matches ) {
153 return sprintf( "\\x%02x", ord( $matches[1] ) );
157 class TableCleanupTest extends TableCleanup {
158 function processRow( $row ) {
159 $this->progress( mt_rand( 0, 1 ) );