MediaWiki REL1_39
TableCleanup.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
32 protected $defaultParams = [
33 'table' => 'page',
34 'conds' => [],
35 'index' => 'page_id',
36 'callback' => 'processRow',
37 ];
38
39 protected $dryrun = false;
40 protected $reportInterval = 100;
41
43
44 public function __construct() {
45 parent::__construct();
46 $this->addOption( 'dry-run', 'Perform a dry run' );
47 $this->addOption( 'reporting-interval', 'How often to print status line' );
48 $this->setBatchSize( 100 );
49 }
50
51 public function execute() {
52 $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval );
53
54 $this->dryrun = $this->hasOption( 'dry-run' );
55
56 if ( $this->dryrun ) {
57 $this->output( "Checking for bad titles...\n" );
58 } else {
59 $this->output( "Checking and fixing bad titles...\n" );
60 }
61
62 $this->runTable( $this->defaultParams );
63 }
64
65 protected function init( $count, $table ) {
66 $this->processed = 0;
67 $this->updated = 0;
68 $this->count = $count;
69 $this->startTime = microtime( true );
70 $this->table = $table;
71 }
72
76 protected function progress( $updated ) {
77 $this->updated += $updated;
78 $this->processed++;
79 if ( $this->processed % $this->reportInterval != 0 ) {
80 return;
81 }
82 $portion = $this->processed / $this->count;
83 $updateRate = $this->updated / $this->processed;
84
85 $now = microtime( true );
86 $delta = $now - $this->startTime;
87 $estimatedTotalTime = $delta / $portion;
88 $eta = $this->startTime + $estimatedTotalTime;
89
90 $this->output(
91 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
92 WikiMap::getCurrentWikiDbDomain()->getId(),
93 wfTimestamp( TS_DB, intval( $now ) ),
94 $portion * 100.0,
95 $this->table,
96 wfTimestamp( TS_DB, intval( $eta ) ),
97 $this->processed,
98 $this->count,
99 $this->processed / $delta,
100 $updateRate * 100.0
101 )
102 );
103 flush();
104 }
105
110 public function runTable( $params ) {
111 $dbr = $this->getDB( DB_REPLICA );
112
113 if ( array_diff( array_keys( $params ),
114 [ 'table', 'conds', 'index', 'callback' ] )
115 ) {
116 throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
117 }
118
119 $table = $params['table'];
120 // count(*) would melt the DB for huge tables, we can estimate here
121 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
122 $this->init( $count, $table );
123 $this->output( "Processing $table...\n" );
124
125 $index = (array)$params['index'];
126 $indexConds = [];
127 $options = [
128 'ORDER BY' => implode( ',', $index ),
129 'LIMIT' => $this->getBatchSize()
130 ];
131 $callback = [ $this, $params['callback'] ];
132
133 while ( true ) {
134 $conds = array_merge( $params['conds'], $indexConds );
135 $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
136 if ( !$res->numRows() ) {
137 // Done
138 break;
139 }
140
141 foreach ( $res as $row ) {
142 call_user_func( $callback, $row );
143 }
144
145 if ( $res->numRows() < $this->getBatchSize() ) {
146 // Done
147 break;
148 }
149
150 // Update the conditions to select the next batch.
151 // Construct a condition string by starting with the least significant part
152 // of the index, and adding more significant parts progressively to the left
153 // of the string.
154 $nextCond = '';
155 foreach ( array_reverse( $index ) as $field ) {
156 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item
157 $encValue = $dbr->addQuotes( $row->$field );
158 if ( $nextCond === '' ) {
159 $nextCond = "$field > $encValue";
160 } else {
161 $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
162 }
163 }
164 $indexConds = [ $nextCond ];
165 }
166
167 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
168 }
169
174 protected function hexChar( $matches ) {
175 return sprintf( "\\x%02x", ord( $matches[1] ) );
176 }
177}
getDB()
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
MediaWiki exception.
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)
Generic class to cleanup a database table.
progress( $updated)
init( $count, $table)
execute()
Do the actual work.
hexChar( $matches)
runTable( $params)
__construct()
Default constructor.
const DB_REPLICA
Definition defines.php:26