MediaWiki 1.42.0
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
45
46 public function __construct() {
47 parent::__construct();
48 $this->addOption( 'dry-run', 'Perform a dry run' );
49 $this->addOption( 'reporting-interval', 'How often to print status line' );
50 $this->setBatchSize( 100 );
51 }
52
53 public function execute() {
54 $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval );
55
56 $this->dryrun = $this->hasOption( 'dry-run' );
57
58 if ( $this->dryrun ) {
59 $this->output( "Checking for bad titles...\n" );
60 } else {
61 $this->output( "Checking and fixing bad titles...\n" );
62 }
63
64 $this->runTable( $this->defaultParams );
65 }
66
67 protected function init( $count, $table ) {
68 $this->processed = 0;
69 $this->updated = 0;
70 $this->count = $count;
71 $this->startTime = microtime( true );
72 $this->table = $table;
73 }
74
78 protected function progress( $updated ) {
79 $this->updated += $updated;
80 $this->processed++;
81 if ( $this->processed % $this->reportInterval != 0 ) {
82 return;
83 }
84 $portion = $this->processed / $this->count;
85 $updateRate = $this->updated / $this->processed;
86
87 $now = microtime( true );
88 $delta = $now - $this->startTime;
89 $estimatedTotalTime = $delta / $portion;
90 $eta = $this->startTime + $estimatedTotalTime;
91
92 $this->output(
93 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
94 WikiMap::getCurrentWikiDbDomain()->getId(),
95 wfTimestamp( TS_DB, intval( $now ) ),
96 $portion * 100.0,
97 $this->table,
98 wfTimestamp( TS_DB, intval( $eta ) ),
99 $this->processed,
100 $this->count,
101 $this->processed / $delta,
102 $updateRate * 100.0
103 )
104 );
105 flush();
106 }
107
111 public function runTable( $params ) {
112 $dbr = $this->getReplicaDB();
113
114 if ( array_diff( array_keys( $params ),
115 [ 'table', 'conds', 'index', 'callback' ] )
116 ) {
117 $this->fatalError( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
118 }
119
120 $table = $params['table'];
121 // count(*) would melt the DB for huge tables, we can estimate here
122 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
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 call_user_func( $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.
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.