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
21 * @ingroup Maintenance
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26 class TableCleanup extends Maintenance {
27 protected $defaultParams = array(
31 'callback' => 'processRow',
34 protected $dryrun = false;
35 protected $maxLag = 10; # if slaves are lagged more than 10 secs, wait
36 public $batchSize = 100;
37 public $reportInterval = 100;
39 public function __construct() {
40 parent::__construct();
41 $this->addOption( 'dry-run', 'Perform a dry run' );
44 public function execute() {
46 $wgUser = User::newFromName( 'Conversion script' );
47 $this->dryrun = $this->hasOption( 'dry-run' );
48 if ( $this->dryrun ) {
49 $this->output( "Checking for bad titles...\n" );
51 $this->output( "Checking and fixing bad titles...\n" );
53 $this->runTable( $this->defaultParams );
56 protected function init( $count, $table ) {
59 $this->count = $count;
60 $this->startTime = wfTime();
61 $this->table = $table;
64 protected function progress( $updated ) {
65 $this->updated += $updated;
67 if ( $this->processed % $this->reportInterval != 0 ) {
70 $portion = $this->processed / $this->count;
71 $updateRate = $this->updated / $this->processed;
74 $delta = $now - $this->startTime;
75 $estimatedTotalTime = $delta / $portion;
76 $eta = $this->startTime + $estimatedTotalTime;
79 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
81 wfTimestamp( TS_DB, intval( $now ) ),
84 wfTimestamp( TS_DB, intval( $eta ) ),
87 $this->processed / $delta,
94 public function runTable( $params ) {
95 $dbr = wfGetDB( DB_SLAVE );
97 if ( array_diff( array_keys( $params ),
98 array( 'table', 'conds', 'index', 'callback' ) ) )
100 throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
103 $table = $params['table'];
104 // count(*) would melt the DB for huge tables, we can estimate here
105 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
106 $this->init( $count, $table );
107 $this->output( "Processing $table...\n" );
110 $index = (array)$params['index'];
111 $indexConds = array();
113 'ORDER BY' => implode( ',', $index ),
114 'LIMIT' => $this->batchSize
116 $callback = array( $this, $params['callback'] );
119 $conds = array_merge( $params['conds'], $indexConds );
120 $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
121 if ( !$res->numRows() ) {
126 foreach ( $res as $row ) {
127 call_user_func( $callback, $row );
130 if ( $res->numRows() < $this->batchSize ) {
135 // Update the conditions to select the next batch.
136 // Construct a condition string by starting with the least significant part
137 // of the index, and adding more significant parts progressively to the left
140 foreach ( array_reverse( $index ) as $field ) {
141 $encValue = $dbr->addQuotes( $row->$field );
142 if ( $nextCond === '' ) {
143 $nextCond = "$field > $encValue";
145 $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
148 $indexConds = array( $nextCond );
151 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
154 protected function hexChar( $matches ) {
155 return sprintf( "\\x%02x", ord( $matches[1] ) );
159 class TableCleanupTest extends TableCleanup {
160 function processRow( $row ) {
161 $this->progress( mt_rand( 0, 1 ) );