MediaWiki  master
TableCleanup.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
33 class TableCleanup extends Maintenance {
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->getDB( DB_REPLICA );
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  $options = [
129  'ORDER BY' => implode( ',', $index ),
130  'LIMIT' => $this->getBatchSize()
131  ];
132  $callback = [ $this, $params['callback'] ];
133 
134  while ( true ) {
135  $conds = array_merge( $params['conds'], $indexConds );
136  $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
137  if ( !$res->numRows() ) {
138  // Done
139  break;
140  }
141 
142  foreach ( $res as $row ) {
143  call_user_func( $callback, $row );
144  }
145 
146  if ( $res->numRows() < $this->getBatchSize() ) {
147  // Done
148  break;
149  }
150 
151  // Update the conditions to select the next batch.
152  $conds = [];
153  foreach ( $index as $field ) {
154  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item
155  $conds[ $field ] = $row->$field;
156  }
157  $indexConds = [ $dbr->buildComparison( '>', $conds ) ];
158  }
159 
160  $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
161  }
162 
167  protected function hexChar( $matches ) {
168  return sprintf( "\\x%02x", ord( $matches[1] ) );
169  }
170 }
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
$matches
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.
Helper tools for dealing with other locally-hosted wikis.
Definition: WikiMap.php:33
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