MediaWiki  master
cleanupTable.inc
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  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",
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 }
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
TableCleanup\$defaultParams
$defaultParams
Definition: cleanupTable.inc:32
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1808
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:538
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$res
$res
Definition: testCompression.php:57
$dbr
$dbr
Definition: testCompression.php:54
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:755
MWException
MediaWiki exception.
Definition: MWException.php:29
TableCleanup\init
init( $count, $table)
Definition: cleanupTable.inc:67
$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:245
TableCleanup\execute
execute()
Do the actual work.
Definition: cleanupTable.inc:51
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
TableCleanup\$processed
$processed
Definition: cleanupTable.inc:42
TableCleanup\$reportInterval
$reportInterval
Definition: cleanupTable.inc:40
TableCleanup\progress
progress( $updated)
Definition: cleanupTable.inc:78
TableCleanup\$dryrun
$dryrun
Definition: cleanupTable.inc:39
TableCleanup
Generic class to cleanup a database table.
Definition: cleanupTable.inc:31
TableCleanup\$table
$table
Definition: cleanupTable.inc:42
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
TableCleanup\runTable
runTable( $params)
Definition: cleanupTable.inc:112
TableCleanup\__construct
__construct()
Default constructor.
Definition: cleanupTable.inc:44
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
TableCleanup\$count
$count
Definition: cleanupTable.inc:42
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:366
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
TableCleanup\$startTime
$startTime
Definition: cleanupTable.inc:42
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
TableCleanup\hexChar
hexChar( $matches)
Definition: cleanupTable.inc:175
TableCleanup\$updated
$updated
Definition: cleanupTable.inc:42
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:374