MediaWiki master
TableCleanup.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
34 protected $defaultParams = [
35 'table' => 'page',
36 'conds' => [],
37 'index' => 'page_id',
38 'callback' => 'processRow',
39 ];
40
41 protected $dryrun = false;
42 protected $reportInterval = 100;
43
44 protected int $processed;
45 protected int $updated;
46 protected int $count;
47 protected float $startTime;
48 protected string $table;
49
50 public function __construct() {
51 parent::__construct();
52 $this->addOption( 'dry-run', 'Perform a dry run' );
53 $this->addOption( 'reporting-interval', 'How often to print status line' );
54 $this->setBatchSize( 100 );
55 }
56
57 public function execute() {
58 $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval );
59
60 $this->dryrun = $this->hasOption( 'dry-run' );
61
62 if ( $this->dryrun ) {
63 $this->output( "Checking for bad titles...\n" );
64 } else {
65 $this->output( "Checking and fixing bad titles...\n" );
66 }
67
68 $this->runTable( $this->defaultParams );
69 }
70
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;
77 }
78
82 protected function progress( $updated ) {
83 $this->updated += $updated;
84 $this->processed++;
85 if ( $this->processed % $this->reportInterval != 0 ) {
86 return;
87 }
88 $portion = $this->processed / $this->count;
89 $updateRate = $this->updated / $this->processed;
90
91 $now = microtime( true );
92 $delta = $now - $this->startTime;
93 $estimatedTotalTime = $delta / $portion;
94 $eta = $this->startTime + $estimatedTotalTime;
95
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
107 )
108 );
109 flush();
110 }
111
115 public function runTable( $params ) {
116 $dbr = $this->getReplicaDB();
117
118 if ( array_diff( array_keys( $params ),
119 [ 'table', 'conds', 'index', 'callback' ] )
120 ) {
121 $this->fatalError( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
122 }
123
124 $table = $params['table'];
125 // count(*) would melt the DB for huge tables, we can estimate here
126 $count = $dbr->newSelectQueryBuilder()
127 ->table( $table )
128 ->caller( __METHOD__ )
129 ->estimateRowCount();
130 $this->init( $count, $table );
131 $this->output( "Processing $table...\n" );
132
133 $index = (array)$params['index'];
134 $indexConds = [];
135 $callback = [ $this, $params['callback'] ];
136
137 while ( true ) {
138 $conds = array_merge( $params['conds'], $indexConds );
139 $res = $dbr->newSelectQueryBuilder()
140 ->select( '*' )
141 ->from( $table )
142 ->where( $conds )
143 ->orderBy( implode( ',', $index ) )
144 ->limit( $this->getBatchSize() )
145 ->caller( __METHOD__ )->fetchResultSet();
146 if ( !$res->numRows() ) {
147 // Done
148 break;
149 }
150
151 foreach ( $res as $row ) {
152 call_user_func( $callback, $row );
153 }
154
155 if ( $res->numRows() < $this->getBatchSize() ) {
156 // Done
157 break;
158 }
159
160 // Update the conditions to select the next batch.
161 $conds = [];
162 foreach ( $index as $field ) {
163 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item
164 $conds[ $field ] = $row->$field;
165 }
166 $indexConds = [ $dbr->buildComparison( '>', $conds ) ];
167 }
168
169 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
170 }
171
176 protected function hexChar( $matches ) {
177 return sprintf( "\\x%02x", ord( $matches[1] ) );
178 }
179}
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)
runTable( $params)
__construct()
Default constructor.