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