48 private $numRowsProcessed = 0;
57 private $verboseStats;
63 private $collationName;
78 private $namespaceInfo;
81 parent::__construct();
84This script will find all rows in the categorylinks table whose collation is
86repopulate cl_sortkey
using the page title and cl_sortkey_prefix. If all
87collations are up-to-date, it will
do nothing.
92 $this->
addOption(
'force',
'Run on all rows, even if the collation is ' .
93 'supposed to be up-to-date.',
false,
false,
'f' );
94 $this->
addOption(
'previous-collation',
'Set the previous value of ' .
95 '$wgCategoryCollation here to speed up this script, especially if your ' .
96 'categorylinks table is large. This will only update rows with that ' .
97 'collation, though, so it may miss out-of-date rows with a different, ' .
98 'even older collation.',
false,
true );
99 $this->
addOption(
'target-collation',
'Set this to the new collation type to ' .
100 'use instead of $wgCategoryCollation. Usually you should not use this, ' .
101 'you should just update $wgCategoryCollation in LocalSettings.php.',
103 $this->
addOption(
'target-table',
'Copy rows from categorylinks into the ' .
104 'specified table instead of updating them in place.',
false,
true );
105 $this->
addOption(
'remote',
'Use Shellbox to calculate the new sort keys ' .
107 $this->
addOption(
'dry-run',
'Don\'t actually change the collations, just ' .
108 'compile statistics.' );
109 $this->
addOption(
'verbose-stats',
'Show more statistics.' );
115 private function init() {
116 $services = MediaWikiServices::getInstance();
117 $this->namespaceInfo = $services->getNamespaceInfo();
118 $this->lbFactory = $services->getDBLoadBalancerFactory();
120 if ( $this->
hasOption(
'target-collation' ) ) {
121 $this->collationName = $this->
getOption(
'target-collation' );
123 $this->collationName = $this->
getConfig()->get( MainConfigNames::CategoryCollation );
126 $realCollationName =
'remote-' . $this->collationName;
128 $realCollationName = $this->collationName;
130 $this->collation = $services->getCollationFactory()->makeCollation( $realCollationName );
134 $this->collation->getSortKey(
'MediaWiki' );
136 $this->force = $this->
getOption(
'force' );
137 $this->dryRun = $this->
getOption(
'dry-run' );
138 $this->verboseStats = $this->
getOption(
'verbose-stats' );
141 $this->targetTable = $this->
getOption(
'target-table' );
148 if ( $this->targetTable ) {
149 if ( !$this->dbw->tableExists( $this->targetTable, __METHOD__ ) ) {
150 $this->
output(
"Creating table {$this->targetTable}\n" );
152 'CREATE TABLE ' . $this->dbw->tableName( $this->targetTable ) .
153 ' LIKE ' . $this->dbw->tableName(
'categorylinks' ),
163 if ( $this->
hasOption(
'previous-collation' ) ) {
164 $orderBy =
'cl_to, cl_type, cl_from';
166 $orderBy =
'cl_collation, cl_to, cl_type, cl_from';
169 'LIMIT' => $batchSize,
170 'ORDER BY' => $orderBy,
174 $collationConds = [];
175 if ( !$this->force && !$this->targetTable ) {
176 if ( $this->
hasOption(
'previous-collation' ) ) {
177 $collationConds[
'cl_collation'] = $this->
getOption(
'previous-collation' );
180 0 =>
'cl_collation != ' . $this->dbr->addQuotes( $this->collationName )
184 $count = $this->dbr->estimateRowCount(
191 if ( $count < 1000000 ) {
192 $count = $this->dbr->selectField(
200 $this->
output(
"Collations up-to-date.\n" );
204 if ( $this->dryRun ) {
205 $this->
output(
"$count rows would be updated.\n" );
207 $this->
output(
"Fixing collation for $count rows.\n" );
212 $this->
output(
"Selecting next $batchSize rows..." );
216 if ( $this->dbw->getType() ===
'mysql' ) {
217 $clType =
'cl_type+0 AS "cl_type_numeric"';
221 $res = $this->dbw->select(
222 [
'categorylinks',
'page' ],
224 'cl_from',
'cl_to',
'cl_sortkey_prefix',
'cl_collation',
225 'cl_sortkey', $clType,
'cl_timestamp',
226 'page_namespace',
'page_title'
228 array_merge( $collationConds, $batchConds, [
'cl_from = page_id' ] ),
232 $this->
output(
" processing..." );
234 if (
$res->numRows() ) {
235 if ( $this->targetTable ) {
236 $this->copyBatch(
$res );
238 $this->updateBatch(
$res );
241 $lastRow =
$res->fetchObject();
242 $batchConds = [ $this->getBatchCondition( $lastRow, $this->dbw ) ];
245 if ( $this->dryRun ) {
246 $this->
output(
"{$this->numRowsProcessed} rows would be updated so far.\n" );
248 $this->
output(
"{$this->numRowsProcessed} done.\n" );
250 }
while (
$res->numRows() == $batchSize );
252 if ( !$this->dryRun ) {
253 $this->
output(
"{$this->numRowsProcessed} rows processed\n" );
256 if ( $this->verboseStats ) {
258 $this->showSortKeySizeHistogram();
269 private function getBatchCondition( $row, $dbw ) {
270 if ( $this->
hasOption(
'previous-collation' ) ) {
271 $fields = [
'cl_to',
'cl_type',
'cl_from' ];
273 $fields = [
'cl_collation',
'cl_to',
'cl_type',
'cl_from' ];
276 foreach ( $fields as $field ) {
277 if ( $dbw->
getType() ===
'mysql' && $field ===
'cl_type' ) {
280 $value = intval( $row->cl_type_numeric );
282 $value = $row->$field;
284 $conds[ $field ] = $value;
295 private function updateBatch(
$res ) {
296 if ( !$this->dryRun ) {
299 foreach (
$res as $row ) {
300 $title = Title::newFromRow( $row );
301 if ( !$row->cl_collation ) {
302 # This is an old-style row, so the sortkey needs to be
304 if ( $row->cl_sortkey ==
$title->getText()
305 || $row->cl_sortkey ==
$title->getPrefixedText()
309 # Custom sortkey, use it as a prefix
310 $prefix = $row->cl_sortkey;
313 $prefix = $row->cl_sortkey_prefix;
315 # cl_type will be wrong for lots of pages if cl_collation is 0,
316 # so let's update it while we're here.
317 $type = $this->namespaceInfo->getCategoryLinkType( $row->page_namespace );
318 $newSortKey = $this->collation->getSortKey(
319 $title->getCategorySortkey( $prefix ) );
320 $this->updateSortKeySizeHistogram( $newSortKey );
322 $newSortKey = substr( $newSortKey, 0, 230 );
324 if ( $this->dryRun ) {
327 $this->numRowsProcessed += ( $row->cl_sortkey !== $newSortKey );
332 'cl_sortkey' => $newSortKey,
333 'cl_sortkey_prefix' => $prefix,
334 'cl_collation' => $this->collationName,
336 'cl_timestamp = cl_timestamp',
338 [
'cl_from' => $row->cl_from,
'cl_to' => $row->cl_to ],
341 $this->numRowsProcessed++;
344 if ( !$this->dryRun ) {
354 private function copyBatch(
$res ) {
356 foreach (
$res as $row ) {
357 $title = Title::newFromRow( $row );
358 $sortKeyInputs[] =
$title->getCategorySortkey( $row->cl_sortkey_prefix );
360 $sortKeys = $this->collation->getSortKeys( $sortKeyInputs );
362 foreach (
$res as $i => $row ) {
363 if ( !isset( $sortKeys[$i] ) ) {
364 throw new RuntimeException(
'Unable to get sort key' );
366 $newSortKey = $sortKeys[$i];
367 $this->updateSortKeySizeHistogram( $newSortKey );
369 $newSortKey = substr( $newSortKey, 0, 230 );
370 $type = $this->namespaceInfo->getCategoryLinkType( $row->page_namespace );
372 'cl_from' => $row->cl_from,
373 'cl_to' => $row->cl_to,
374 'cl_sortkey' => $newSortKey,
375 'cl_sortkey_prefix' => $row->cl_sortkey_prefix,
376 'cl_collation' => $this->collationName,
378 'cl_timestamp' => $row->cl_timestamp
381 if ( $this->dryRun ) {
382 $this->numRowsProcessed += count( $rowsToInsert );
385 $this->dbw->insert( $this->targetTable, $rowsToInsert, __METHOD__, [
'IGNORE' ] );
386 $this->numRowsProcessed += $this->dbw->affectedRows();
396 private function updateSortKeySizeHistogram( $key ) {
397 if ( !$this->verboseStats ) {
400 $length = strlen( $key );
401 if ( !isset( $this->sizeHistogram[$length] ) ) {
402 $this->sizeHistogram[$length] = 0;
404 $this->sizeHistogram[$length]++;
410 private function showSortKeySizeHistogram() {
411 if ( !$this->sizeHistogram ) {
414 $maxLength = max( array_keys( $this->sizeHistogram ) );
415 if ( $maxLength == 0 ) {
419 $coarseHistogram = array_fill( 0, $numBins, 0 );
420 $coarseBoundaries = [];
422 for ( $i = 0; $i < $numBins - 1; $i++ ) {
423 $boundary += $maxLength / $numBins;
424 $coarseBoundaries[$i] = round( $boundary );
426 $coarseBoundaries[$numBins - 1] = $maxLength + 1;
428 for ( $i = 0; $i <= $maxLength; $i++ ) {
432 $val = $this->sizeHistogram[$i] ?? 0;
433 for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++ ) {
435 if ( $coarseBoundaries[$coarseIndex] > $i ) {
436 $coarseHistogram[$coarseIndex] += $val;
440 if ( $coarseIndex == $numBins - 1 ) {
441 $coarseHistogram[$coarseIndex] += $val;
446 $this->
output(
"Sort key size histogram\nRaw data: $raw\n\n" );
448 $maxBinVal = max( $coarseHistogram );
449 $scale = (int)( 60 / $maxBinVal );
451 for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++ ) {
452 $val = $coarseHistogram[$coarseIndex] ?? 0;
454 $boundary = $coarseBoundaries[$coarseIndex];
455 $this->
output( sprintf(
"%-10s %-10d |%s\n",
456 $prevBoundary .
'-' . ( $boundary - 1 ) .
': ',
458 str_repeat(
'*', $scale * $val ) ) );
459 $prevBoundary = $boundary;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
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.