MediaWiki  master
TableCleanup.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class TableCleanup extends Maintenance {
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",
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  $encValue = $dbr->addQuotes( $row->$field );
157  if ( $nextCond === '' ) {
158  $nextCond = "$field > $encValue";
159  } else {
160  $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
161  }
162  }
163  $indexConds = [ $nextCond ];
164  }
165 
166  $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
167  }
168 
173  protected function hexChar( $matches ) {
174  return sprintf( "\\x%02x", ord( $matches[1] ) );
175  }
176 }
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
TableCleanup\$defaultParams
$defaultParams
Definition: TableCleanup.php:32
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1664
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$res
$res
Definition: testCompression.php:57
$dbr
$dbr
Definition: testCompression.php:54
MWException
MediaWiki exception.
Definition: MWException.php:29
TableCleanup\init
init( $count, $table)
Definition: TableCleanup.php:65
$matches
$matches
Definition: NoLocalSettings.php:24
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
TableCleanup\execute
execute()
Do the actual work.
Definition: TableCleanup.php:51
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
TableCleanup\$processed
$processed
Definition: TableCleanup.php:42
TableCleanup\$reportInterval
$reportInterval
Definition: TableCleanup.php:40
TableCleanup\progress
progress( $updated)
Definition: TableCleanup.php:76
TableCleanup\$dryrun
$dryrun
Definition: TableCleanup.php:39
TableCleanup
Generic class to cleanup a database table.
Definition: TableCleanup.php:31
TableCleanup\$table
$table
Definition: TableCleanup.php:42
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
TableCleanup\runTable
runTable( $params)
Definition: TableCleanup.php:110
TableCleanup\__construct
__construct()
Default constructor.
Definition: TableCleanup.php:44
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
TableCleanup\$count
$count
Definition: TableCleanup.php:42
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:368
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
TableCleanup\$startTime
$startTime
Definition: TableCleanup.php:42
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
TableCleanup\hexChar
hexChar( $matches)
Definition: TableCleanup.php:173
TableCleanup\$updated
$updated
Definition: TableCleanup.php:42
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:375