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 protected $reportInterval = 100;
42 protected $processed, $updated, $count, $startTime, $table;
44 public function __construct() {
45 parent
::__construct();
46 $this->addOption( 'dry-run', 'Perform a dry run' );
47 $this->addOption( 'reporting-interval', 'How often to print status line' );
48 $this->setBatchSize( 100 );
51 public function execute() {
54 $this->reportInterval
= $this->getOption( 'reporting-interval', $this->reportInterval
);
56 $this->dryrun
= $this->hasOption( 'dry-run' );
58 if ( $this->dryrun
) {
59 $user = User
::newFromName( 'Conversion script' );
60 $this->output( "Checking for bad titles...\n" );
62 $user = User
::newSystemUser( 'Conversion script', [ 'steal' => true ] );
63 $this->output( "Checking and fixing bad titles...\n" );
65 // Support deprecated use of the global
68 $this->runTable( $this->defaultParams
);
71 protected function init( $count, $table ) {
74 $this->count
= $count;
75 $this->startTime
= microtime( true );
76 $this->table
= $table;
82 protected function progress( $updated ) {
83 $this->updated +
= $updated;
85 if ( $this->processed %
$this->reportInterval
!= 0 ) {
88 $portion = $this->processed
/ $this->count
;
89 $updateRate = $this->updated
/ $this->processed
;
91 $now = microtime( true );
92 $delta = $now - $this->startTime
;
93 $estimatedTotalTime = $delta / $portion;
94 $eta = $this->startTime +
$estimatedTotalTime;
97 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
98 WikiMap
::getCurrentWikiDbDomain()->getId(),
99 wfTimestamp( TS_DB
, intval( $now ) ),
102 wfTimestamp( TS_DB
, intval( $eta ) ),
105 $this->processed
/ $delta,
113 * @param array $params
114 * @throws MWException
116 public function runTable( $params ) {
117 $dbr = $this->getDB( DB_REPLICA
);
119 if ( array_diff( array_keys( $params ),
120 [ 'table', 'conds', 'index', 'callback' ] )
122 throw new MWException( __METHOD__
. ': Missing parameter ' . implode( ', ', $params ) );
125 $table = $params['table'];
126 // count(*) would melt the DB for huge tables, we can estimate here
127 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__
);
128 $this->init( $count, $table );
129 $this->output( "Processing $table...\n" );
131 $index = (array)$params['index'];
134 'ORDER BY' => implode( ',', $index ),
135 'LIMIT' => $this->getBatchSize()
137 $callback = [ $this, $params['callback'] ];
140 $conds = array_merge( $params['conds'], $indexConds );
141 $res = $dbr->select( $table, '*', $conds, __METHOD__
, $options );
142 if ( !$res->numRows() ) {
147 foreach ( $res as $row ) {
148 call_user_func( $callback, $row );
151 if ( $res->numRows() < $this->getBatchSize() ) {
156 // Update the conditions to select the next batch.
157 // Construct a condition string by starting with the least significant part
158 // of the index, and adding more significant parts progressively to the left
161 foreach ( array_reverse( $index ) as $field ) {
162 $encValue = $dbr->addQuotes( $row->$field );
163 if ( $nextCond === '' ) {
164 $nextCond = "$field > $encValue";
166 $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
169 $indexConds = [ $nextCond ];
172 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
176 * @param string[] $matches
179 protected function hexChar( $matches ) {
180 return sprintf( "\\x%02x", ord( $matches[1] ) );