MediaWiki master
TableCleanup.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
38 protected $defaultParams = [
39 'table' => 'page',
40 'conds' => [],
41 'index' => 'page_id',
42 'callback' => 'processRow',
43 ];
44
46 protected $dryrun = false;
48 protected $reportInterval = 100;
49
50 protected int $processed;
51 protected int $updated;
52 protected int $count;
53 protected float $startTime;
54 protected string $table;
55
56 public function __construct() {
57 parent::__construct();
58 $this->addOption( 'dry-run', 'Perform a dry run' );
59 $this->addOption( 'reporting-interval', 'How often to print status line' );
60 $this->setBatchSize( 100 );
61 }
62
63 public function execute() {
64 $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval );
65
66 $this->dryrun = $this->hasOption( 'dry-run' );
67
68 if ( $this->dryrun ) {
69 $this->output( "Checking for bad titles...\n" );
70 } else {
71 $this->output( "Checking and fixing bad titles...\n" );
72 }
73
74 $this->runTable( $this->defaultParams );
75 }
76
77 protected function init( $count, $table ) {
78 $this->processed = 0;
79 $this->updated = 0;
80 $this->count = $count;
81 $this->startTime = microtime( true );
82 $this->table = $table;
83 }
84
88 protected function progress( $updated ) {
89 $this->updated += $updated;
90 $this->processed++;
91 if ( $this->processed % $this->reportInterval != 0 ) {
92 return;
93 }
94 $portion = $this->processed / $this->count;
95 $updateRate = $this->updated / $this->processed;
96
97 $now = microtime( true );
98 $delta = $now - $this->startTime;
99 $estimatedTotalTime = $delta / $portion;
100 $eta = $this->startTime + $estimatedTotalTime;
101
102 $this->output(
103 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
104 WikiMap::getCurrentWikiDbDomain()->getId(),
105 wfTimestamp( TS_DB, intval( $now ) ),
106 $portion * 100.0,
107 $this->table,
108 wfTimestamp( TS_DB, intval( $eta ) ),
109 $this->processed,
110 $this->count,
111 $this->processed / $delta,
112 $updateRate * 100.0
113 )
114 );
115 flush();
116 }
117
121 public function runTable( $params ) {
122 $dbr = $this->getReplicaDB();
123
124 if ( array_diff( array_keys( $params ),
125 [ 'table', 'conds', 'index', 'callback' ] )
126 ) {
127 $this->fatalError( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
128 }
129
130 $table = $params['table'];
131 // count(*) would melt the DB for huge tables, we can estimate here
132 $count = $dbr->newSelectQueryBuilder()
133 ->table( $table )
134 ->caller( __METHOD__ )
135 ->estimateRowCount();
136 $this->init( $count, $table );
137 $this->output( "Processing $table...\n" );
138
139 $index = (array)$params['index'];
140 $indexConds = [];
141 $callback = [ $this, $params['callback'] ];
142
143 while ( true ) {
144 $conds = array_merge( $params['conds'], $indexConds );
145 $res = $dbr->newSelectQueryBuilder()
146 ->select( '*' )
147 ->from( $table )
148 ->where( $conds )
149 ->orderBy( implode( ',', $index ) )
150 ->limit( $this->getBatchSize() )
151 ->caller( __METHOD__ )->fetchResultSet();
152 if ( !$res->numRows() ) {
153 // Done
154 break;
155 }
156
157 foreach ( $res as $row ) {
158 $callback( $row );
159 }
160
161 if ( $res->numRows() < $this->getBatchSize() ) {
162 // Done
163 break;
164 }
165
166 // Update the conditions to select the next batch.
167 $conds = [];
168 foreach ( $index as $field ) {
169 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item
170 $conds[ $field ] = $row->$field;
171 }
172 $indexConds = [ $dbr->buildComparison( '>', $conds ) ];
173 }
174
175 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
176 }
177
182 protected function hexChar( $matches ) {
183 return sprintf( "\\x%02x", ord( $matches[1] ) );
184 }
185}
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.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Generic class to cleanup a database table.
progress( $updated)
init( $count, $table)
execute()
Do the actual work.
hexChar( $matches)
array $defaultParams
runTable( $params)
__construct()
Default constructor.