Updating composer/semver (1.5.1 => 1.5.2)
[mediawiki.git] / maintenance / TableCleanup.php
blobe1b513e459b9fd00507e1917ca1f06b340fd8ebc
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Generic class to cleanup a database table. Already subclasses Maintenance.
29 * @ingroup Maintenance
31 class TableCleanup extends Maintenance {
32 protected $defaultParams = [
33 'table' => 'page',
34 'conds' => [],
35 'index' => 'page_id',
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() {
52 global $wgUser;
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" );
61 } else {
62 $user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
63 $this->output( "Checking and fixing bad titles...\n" );
65 // Support deprecated use of the global
66 $wgUser = $user;
68 $this->runTable( $this->defaultParams );
71 protected function init( $count, $table ) {
72 $this->processed = 0;
73 $this->updated = 0;
74 $this->count = $count;
75 $this->startTime = microtime( true );
76 $this->table = $table;
79 /**
80 * @param int $updated
82 protected function progress( $updated ) {
83 $this->updated += $updated;
84 $this->processed++;
85 if ( $this->processed % $this->reportInterval != 0 ) {
86 return;
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;
96 $this->output(
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 ) ),
100 $portion * 100.0,
101 $this->table,
102 wfTimestamp( TS_DB, intval( $eta ) ),
103 $this->processed,
104 $this->count,
105 $this->processed / $delta,
106 $updateRate * 100.0
109 flush();
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'];
132 $indexConds = [];
133 $options = [
134 'ORDER BY' => implode( ',', $index ),
135 'LIMIT' => $this->getBatchSize()
137 $callback = [ $this, $params['callback'] ];
139 while ( true ) {
140 $conds = array_merge( $params['conds'], $indexConds );
141 $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
142 if ( !$res->numRows() ) {
143 // Done
144 break;
147 foreach ( $res as $row ) {
148 call_user_func( $callback, $row );
151 if ( $res->numRows() < $this->getBatchSize() ) {
152 // Done
153 break;
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
159 // of the string.
160 $nextCond = '';
161 foreach ( array_reverse( $index ) as $field ) {
162 $encValue = $dbr->addQuotes( $row->$field );
163 if ( $nextCond === '' ) {
164 $nextCond = "$field > $encValue";
165 } else {
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
177 * @return string
179 protected function hexChar( $matches ) {
180 return sprintf( "\\x%02x", ord( $matches[1] ) );