MediaWiki REL1_35
cleanupTable.inc
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 global $wgUser;
53
54 $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval );
55
56 $this->dryrun = $this->hasOption( 'dry-run' );
57 if ( $this->dryrun ) {
58 $wgUser = User::newFromName( 'Conversion script' );
59 $this->output( "Checking for bad titles...\n" );
60 } else {
61 $wgUser = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
62 $this->output( "Checking and fixing bad titles...\n" );
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
112 public function runTable( $params ) {
113 $dbr = $this->getDB( DB_REPLICA );
114
115 if ( array_diff( array_keys( $params ),
116 [ 'table', 'conds', 'index', 'callback' ] )
117 ) {
118 throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
119 }
120
121 $table = $params['table'];
122 // count(*) would melt the DB for huge tables, we can estimate here
123 $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
124 $this->init( $count, $table );
125 $this->output( "Processing $table...\n" );
126
127 $index = (array)$params['index'];
128 $indexConds = [];
129 $options = [
130 'ORDER BY' => implode( ',', $index ),
131 'LIMIT' => $this->getBatchSize()
132 ];
133 $callback = [ $this, $params['callback'] ];
134
135 while ( true ) {
136 $conds = array_merge( $params['conds'], $indexConds );
137 $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
138 if ( !$res->numRows() ) {
139 // Done
140 break;
141 }
142
143 foreach ( $res as $row ) {
144 call_user_func( $callback, $row );
145 }
146
147 if ( $res->numRows() < $this->getBatchSize() ) {
148 // Done
149 break;
150 }
151
152 // Update the conditions to select the next batch.
153 // Construct a condition string by starting with the least significant part
154 // of the index, and adding more significant parts progressively to the left
155 // of the string.
156 $nextCond = '';
157 foreach ( array_reverse( $index ) as $field ) {
158 $encValue = $dbr->addQuotes( $row->$field );
159 if ( $nextCond === '' ) {
160 $nextCond = "$field > $encValue";
161 } else {
162 $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
163 }
164 }
165 $indexConds = [ $nextCond ];
166 }
167
168 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
169 }
170
175 protected function hexChar( $matches ) {
176 return sprintf( "\\x%02x", ord( $matches[1] ) );
177 }
178}
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)
Set the batch size.
Generic class to cleanup a database table.
progress( $updated)
init( $count, $table)
execute()
Do the actual work.
hexChar( $matches)
runTable( $params)
__construct()
Default constructor.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:758
const DB_REPLICA
Definition defines.php:25