MediaWiki master
TableCleanup.php
Go to the documentation of this file.
1<?php
12use Wikimedia\Timestamp\TimestampFormat as TS;
13
14// @codeCoverageIgnoreStart
15require_once __DIR__ . '/Maintenance.php';
16// @codeCoverageIgnoreEnd
17
25 protected $defaultParams = [
26 'table' => 'page',
27 'conds' => [],
28 'index' => 'page_id',
29 'callback' => 'processRow',
30 ];
31
33 protected $dryrun = false;
35 protected $reportInterval = 100;
36
37 protected int $processed;
38 protected int $updated;
39 protected int $count;
40 protected float $startTime;
41 protected string $table;
42
43 public function __construct() {
44 parent::__construct();
45 $this->addOption( 'dry-run', 'Perform a dry run' );
46 $this->addOption( 'reporting-interval', 'How often to print status line' );
47 $this->setBatchSize( 100 );
48 }
49
50 public function execute() {
51 $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval );
52
53 $this->dryrun = $this->hasOption( 'dry-run' );
54
55 if ( $this->dryrun ) {
56 $this->output( "Checking for bad titles...\n" );
57 } else {
58 $this->output( "Checking and fixing bad titles...\n" );
59 }
60
61 $this->runTable( $this->defaultParams );
62 }
63
64 protected function init( int $count, string $table ) {
65 $this->processed = 0;
66 $this->updated = 0;
67 $this->count = $count;
68 $this->startTime = microtime( true );
69 $this->table = $table;
70 }
71
75 protected function progress( $updated ) {
76 $this->updated += $updated;
77 $this->processed++;
78 if ( $this->processed % $this->reportInterval != 0 ) {
79 return;
80 }
81 $portion = $this->processed / $this->count;
82 $updateRate = $this->updated / $this->processed;
83
84 $now = microtime( true );
85 $delta = $now - $this->startTime;
86 $estimatedTotalTime = $delta / $portion;
87 $eta = $this->startTime + $estimatedTotalTime;
88
89 $this->output(
90 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
91 WikiMap::getCurrentWikiDbDomain()->getId(),
92 wfTimestamp( TS::DB, intval( $now ) ),
93 $portion * 100.0,
94 $this->table,
95 wfTimestamp( TS::DB, intval( $eta ) ),
96 $this->processed,
97 $this->count,
98 $this->processed / $delta,
99 $updateRate * 100.0
100 )
101 );
102 flush();
103 }
104
108 public function runTable( $params ) {
109 $dbr = $this->getReplicaDB();
110
111 if ( array_diff( array_keys( $params ),
112 [ 'table', 'conds', 'index', 'callback' ] )
113 ) {
114 $this->fatalError( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
115 }
116
117 $table = $params['table'];
118 // count(*) would melt the DB for huge tables, we can estimate here
119 $count = $dbr->newSelectQueryBuilder()
120 ->table( $table )
121 ->caller( __METHOD__ )
122 ->estimateRowCount();
123 $this->init( $count, $table );
124 $this->output( "Processing $table...\n" );
125
126 $index = (array)$params['index'];
127 $indexConds = [];
128 $callback = [ $this, $params['callback'] ];
129
130 while ( true ) {
131 $conds = array_merge( $params['conds'], $indexConds );
132 $res = $dbr->newSelectQueryBuilder()
133 ->select( '*' )
134 ->from( $table )
135 ->where( $conds )
136 ->orderBy( implode( ',', $index ) )
137 ->limit( $this->getBatchSize() )
138 ->caller( __METHOD__ )->fetchResultSet();
139 if ( !$res->numRows() ) {
140 // Done
141 break;
142 }
143
144 foreach ( $res as $row ) {
145 $callback( $row );
146 }
147
148 if ( $res->numRows() < $this->getBatchSize() ) {
149 // Done
150 break;
151 }
152
153 // Update the conditions to select the next batch.
154 $conds = [];
155 foreach ( $index as $field ) {
156 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item
157 $conds[ $field ] = $row->$field;
158 }
159 $indexConds = [ $dbr->buildComparison( '>', $conds ) ];
160 }
161
162 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
163 }
164
169 protected function hexChar( $matches ) {
170 return sprintf( "\\x%02x", ord( $matches[1] ) );
171 }
172}
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:19
Generic class to cleanup a database table.
progress( $updated)
init(int $count, string $table)
execute()
Do the actual work.
hexChar( $matches)
array $defaultParams
runTable( $params)
__construct()
Default constructor.