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  global $wgUser;
53 
54  $this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval );
55 
56  $this->dryrun = $this->hasOption( 'dry-run' );
57 
58  if ( $this->dryrun ) {
59  $user = User::newFromName( 'Conversion script' );
60  $this->output( "Checking for bad titles...\n" );
61  } else {
62  $user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
63  $this->output( "Checking and fixing bad titles...\n" );
64  }
65  // Support deprecated use of the global
66  $wgUser = $user;
67 
68  $this->runTable( $this->defaultParams );
69  }
70 
71  protected function init( $count, $table ) {
72  $this->processed = 0;
73  $this->updated = 0;
74  $this->count = $count;
75  $this->startTime = microtime( true );
76  $this->table = $table;
77  }
78 
82  protected function progress( $updated ) {
83  $this->updated += $updated;
84  $this->processed++;
85  if ( $this->processed % $this->reportInterval != 0 ) {
86  return;
87  }
88  $portion = $this->processed / $this->count;
89  $updateRate = $this->updated / $this->processed;
90 
91  $now = microtime( true );
92  $delta = $now - $this->startTime;
93  $estimatedTotalTime = $delta / $portion;
94  $eta = $this->startTime + $estimatedTotalTime;
95 
96  $this->output(
97  sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
99  wfTimestamp( TS_DB, intval( $now ) ),
100  $portion * 100.0,
101  $this->table,
102  wfTimestamp( TS_DB, intval( $eta ) ),
103  $this->processed,
104  $this->count,
105  $this->processed / $delta,
106  $updateRate * 100.0
107  )
108  );
109  flush();
110  }
111 
116  public function runTable( $params ) {
117  $dbr = $this->getDB( DB_REPLICA );
118 
119  if ( array_diff( array_keys( $params ),
120  [ 'table', 'conds', 'index', 'callback' ] )
121  ) {
122  throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
123  }
124 
125  $table = $params['table'];
126  // count(*) would melt the DB for huge tables, we can estimate here
127  $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
128  $this->init( $count, $table );
129  $this->output( "Processing $table...\n" );
130 
131  $index = (array)$params['index'];
132  $indexConds = [];
133  $options = [
134  'ORDER BY' => implode( ',', $index ),
135  'LIMIT' => $this->getBatchSize()
136  ];
137  $callback = [ $this, $params['callback'] ];
138 
139  while ( true ) {
140  $conds = array_merge( $params['conds'], $indexConds );
141  $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
142  if ( !$res->numRows() ) {
143  // Done
144  break;
145  }
146 
147  foreach ( $res as $row ) {
148  call_user_func( $callback, $row );
149  }
150 
151  if ( $res->numRows() < $this->getBatchSize() ) {
152  // Done
153  break;
154  }
155 
156  // Update the conditions to select the next batch.
157  // Construct a condition string by starting with the least significant part
158  // of the index, and adding more significant parts progressively to the left
159  // of the string.
160  $nextCond = '';
161  foreach ( array_reverse( $index ) as $field ) {
162  $encValue = $dbr->addQuotes( $row->$field );
163  if ( $nextCond === '' ) {
164  $nextCond = "$field > $encValue";
165  } else {
166  $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
167  }
168  }
169  $indexConds = [ $nextCond ];
170  }
171 
172  $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
173  }
174 
179  protected function hexChar( $matches ) {
180  return sprintf( "\\x%02x", ord( $matches[1] ) );
181  }
182 }
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:1832
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:586
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:789
MWException
MediaWiki exception.
Definition: MWException.php:29
TableCleanup\init
init( $count, $table)
Definition: TableCleanup.php:71
$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: 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:82
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:1386
TableCleanup\runTable
runTable( $params)
Definition: TableCleanup.php:116
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:282
TableCleanup\$count
$count
Definition: TableCleanup.php:42
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:364
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:431
TableCleanup\$startTime
$startTime
Definition: TableCleanup.php:42
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:267
TableCleanup\hexChar
hexChar( $matches)
Definition: TableCleanup.php:179
TableCleanup\$updated
$updated
Definition: TableCleanup.php:42
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:371