MediaWiki  1.23.12
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 = array(
33  'table' => 'page',
34  'conds' => array(),
35  'index' => 'page_id',
36  'callback' => 'processRow',
37  );
38 
39  protected $dryrun = false;
40  protected $maxLag = 10; # if slaves are lagged more than 10 secs, wait
41  public $batchSize = 100;
42  public $reportInterval = 100;
43 
45 
46  public function __construct() {
47  parent::__construct();
48  $this->addOption( 'dry-run', 'Perform a dry run' );
49  }
50 
51  public function execute() {
53  $wgUser = User::newFromName( 'Conversion script' );
54  $this->dryrun = $this->hasOption( 'dry-run' );
55  if ( $this->dryrun ) {
56  $this->output( "Checking for bad titles...\n" );
57  } else {
58  $this->output( "Checking and fixing bad titles...\n" );
59  }
60  $this->runTable( $this->defaultParams );
61  }
62 
63  protected function init( $count, $table ) {
64  $this->processed = 0;
65  $this->updated = 0;
66  $this->count = $count;
67  $this->startTime = microtime( true );
68  $this->table = $table;
69  }
70 
74  protected function progress( $updated ) {
75  $this->updated += $updated;
76  $this->processed++;
77  if ( $this->processed % $this->reportInterval != 0 ) {
78  return;
79  }
80  $portion = $this->processed / $this->count;
81  $updateRate = $this->updated / $this->processed;
82 
83  $now = microtime( true );
84  $delta = $now - $this->startTime;
85  $estimatedTotalTime = $delta / $portion;
86  $eta = $this->startTime + $estimatedTotalTime;
87 
88  $this->output(
89  sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
90  wfWikiID(),
91  wfTimestamp( TS_DB, intval( $now ) ),
92  $portion * 100.0,
93  $this->table,
94  wfTimestamp( TS_DB, intval( $eta ) ),
95  $this->processed,
96  $this->count,
97  $this->processed / $delta,
98  $updateRate * 100.0
99  )
100  );
101  flush();
102  }
103 
108  public function runTable( $params ) {
109  $dbr = wfGetDB( DB_SLAVE );
110 
111  if ( array_diff( array_keys( $params ),
112  array( 'table', 'conds', 'index', 'callback' ) )
113  ) {
114  throw new MWException( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
115  }
116 
117  $table = $params['table'];
118  // count(*) would melt the DB for huge tables, we can estimate here
119  $count = $dbr->estimateRowCount( $table, '*', '', __METHOD__ );
120  $this->init( $count, $table );
121  $this->output( "Processing $table...\n" );
122 
123  $index = (array)$params['index'];
124  $indexConds = array();
125  $options = array(
126  'ORDER BY' => implode( ',', $index ),
127  'LIMIT' => $this->batchSize
128  );
129  $callback = array( $this, $params['callback'] );
130 
131  while ( true ) {
132  $conds = array_merge( $params['conds'], $indexConds );
133  $res = $dbr->select( $table, '*', $conds, __METHOD__, $options );
134  if ( !$res->numRows() ) {
135  // Done
136  break;
137  }
138 
139  foreach ( $res as $row ) {
140  call_user_func( $callback, $row );
141  }
142 
143  if ( $res->numRows() < $this->batchSize ) {
144  // Done
145  break;
146  }
147 
148  // Update the conditions to select the next batch.
149  // Construct a condition string by starting with the least significant part
150  // of the index, and adding more significant parts progressively to the left
151  // of the string.
152  $nextCond = '';
153  foreach ( array_reverse( $index ) as $field ) {
154  $encValue = $dbr->addQuotes( $row->$field );
155  if ( $nextCond === '' ) {
156  $nextCond = "$field > $encValue";
157  } else {
158  $nextCond = "$field > $encValue OR ($field = $encValue AND ($nextCond))";
159  }
160  }
161  $indexConds = array( $nextCond );
162  }
163 
164  $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
165  }
166 
171  protected function hexChar( $matches ) {
172  return sprintf( "\\x%02x", ord( $matches[1] ) );
173  }
174 }
175 
177  function processRow( $row ) {
178  $this->progress( mt_rand( 0, 1 ) );
179  }
180 }
181 
$wgUser
$wgUser
Definition: Setup.php:572
TableCleanupTest
Definition: cleanupTable.inc:176
TableCleanup\$defaultParams
$defaultParams
Definition: cleanupTable.inc:32
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
TableCleanupTest\processRow
processRow( $row)
Definition: cleanupTable.inc:177
TableCleanup\$maxLag
$maxLag
Definition: cleanupTable.inc:40
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3706
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2530
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
$params
$params
Definition: styleTest.css.php:40
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:389
TS_DB
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
Definition: GlobalFunctions.php:2483
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$dbr
$dbr
Definition: testCompression.php:48
MWException
MediaWiki exception.
Definition: MWException.php:26
TableCleanup\init
init( $count, $table)
Definition: cleanupTable.inc:63
table
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs table
Definition: deferred.txt:11
TableCleanup\execute
execute()
Do the actual work.
Definition: cleanupTable.inc:51
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
TableCleanup\$batchSize
$batchSize
Definition: cleanupTable.inc:41
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
TableCleanup\$processed
$processed
Definition: cleanupTable.inc:44
TableCleanup\$reportInterval
$reportInterval
Definition: cleanupTable.inc:42
TableCleanup\progress
progress( $updated)
Definition: cleanupTable.inc:74
TableCleanup\$dryrun
$dryrun
Definition: cleanupTable.inc:39
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3660
TableCleanup
Generic class to cleanup a database table.
Definition: cleanupTable.inc:31
$matches
if(!defined( 'MEDIAWIKI')) if(!isset( $wgVersion)) $matches
Definition: NoLocalSettings.php:33
TableCleanup\$table
$table
Definition: cleanupTable.inc:44
TableCleanup\runTable
runTable( $params)
Definition: cleanupTable.inc:108
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
TableCleanup\__construct
__construct()
Default constructor.
Definition: cleanupTable.inc:46
are
The ContentHandler facility adds support for arbitrary content types on wiki instead of relying on wikitext for everything It was introduced in MediaWiki Each kind of and so on Built in content types are
Definition: contenthandler.txt:5
TableCleanup\$count
$count
Definition: cleanupTable.inc:44
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
TableCleanup\$startTime
$startTime
Definition: cleanupTable.inc:44
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
TableCleanup\hexChar
hexChar( $matches)
Definition: cleanupTable.inc:171
$res
$res
Definition: database.txt:21
TableCleanup\$updated
$updated
Definition: cleanupTable.inc:44