3 * Find all rows in the categorylinks table whose collation is out-of-date
4 * (cl_collation != $wgCategoryCollation) and repopulate cl_sortkey
5 * using the page title and cl_sortkey_prefix.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup Maintenance
24 * @author Aryeh Gregor (Simetrical)
27 require_once __DIR__
. '/Maintenance.php';
30 * Maintenance script that will find all rows in the categorylinks table
31 * whose collation is out-of-date.
33 * @ingroup Maintenance
35 class UpdateCollation
extends Maintenance
{
36 const BATCH_SIZE
= 10000; // Number of rows to process in one batch
37 const SYNC_INTERVAL
= 20; // Wait for slaves after this many batches
39 public $sizeHistogram = array();
41 public function __construct() {
42 parent
::__construct();
44 global $wgCategoryCollation;
45 $this->addDescription( <<<TEXT
46 This script will find all rows in the categorylinks table whose collation is
47 out-of-date (cl_collation != '$wgCategoryCollation') and repopulate cl_sortkey
48 using the page title and cl_sortkey_prefix. If all collations are
49 up-to-date, it will do nothing.
53 $this->addOption( 'force', 'Run on all rows, even if the collation is ' .
54 'supposed to be up-to-date.' );
55 $this->addOption( 'previous-collation', 'Set the previous value of ' .
56 '$wgCategoryCollation here to speed up this script, especially if your ' .
57 'categorylinks table is large. This will only update rows with that ' .
58 'collation, though, so it may miss out-of-date rows with a different, ' .
59 'even older collation.', false, true );
60 $this->addOption( 'target-collation', 'Set this to the new collation type to ' .
61 'use instead of $wgCategoryCollation. Usually you should not use this, ' .
62 'you should just update $wgCategoryCollation in LocalSettings.php.',
64 $this->addOption( 'dry-run', 'Don\'t actually change the collations, just ' .
65 'compile statistics.' );
66 $this->addOption( 'verbose-stats', 'Show more statistics.' );
69 public function execute() {
70 global $wgCategoryCollation;
72 $dbw = $this->getDB( DB_MASTER
);
73 $force = $this->getOption( 'force' );
74 $dryRun = $this->getOption( 'dry-run' );
75 $verboseStats = $this->getOption( 'verbose-stats' );
76 if ( $this->hasOption( 'target-collation' ) ) {
77 $collationName = $this->getOption( 'target-collation' );
78 $collation = Collation
::factory( $collationName );
80 $collationName = $wgCategoryCollation;
81 $collation = Collation
::singleton();
84 // Collation sanity check: in some cases the constructor will work,
85 // but this will raise an exception, breaking all category pages
86 $collation->getFirstLetter( 'MediaWiki' );
89 'LIMIT' => self
::BATCH_SIZE
,
90 'ORDER BY' => 'cl_from, cl_to',
94 if ( $force ||
$dryRun ) {
95 $collationConds = array();
97 if ( $this->hasOption( 'previous-collation' ) ) {
98 $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
100 $collationConds = array( 0 =>
101 'cl_collation != ' . $dbw->addQuotes( $collationName )
105 $count = $dbw->estimateRowCount(
111 // Improve estimate if feasible
112 if ( $count < 1000000 ) {
113 $count = $dbw->selectField(
121 $this->output( "Collations up-to-date.\n" );
125 $this->output( "Fixing collation for $count rows.\n" );
130 $batchConds = array();
132 $this->output( "Selecting next " . self
::BATCH_SIZE
. " rows..." );
134 array( 'categorylinks', 'page' ),
135 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
136 'cl_sortkey', 'page_namespace', 'page_title'
138 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
142 $this->output( " processing..." );
145 $this->beginTransaction( $dbw, __METHOD__
);
147 foreach ( $res as $row ) {
148 $title = Title
::newFromRow( $row );
149 if ( !$row->cl_collation
) {
150 # This is an old-style row, so the sortkey needs to be
152 if ( $row->cl_sortkey
== $title->getText()
153 ||
$row->cl_sortkey
== $title->getPrefixedText()
157 # Custom sortkey, use it as a prefix
158 $prefix = $row->cl_sortkey
;
161 $prefix = $row->cl_sortkey_prefix
;
163 # cl_type will be wrong for lots of pages if cl_collation is 0,
164 # so let's update it while we're here.
165 if ( $title->getNamespace() == NS_CATEGORY
) {
167 } elseif ( $title->getNamespace() == NS_FILE
) {
172 $newSortKey = $collation->getSortKey(
173 $title->getCategorySortkey( $prefix ) );
174 if ( $verboseStats ) {
175 $this->updateSortKeySizeHistogram( $newSortKey );
182 'cl_sortkey' => $newSortKey,
183 'cl_sortkey_prefix' => $prefix,
184 'cl_collation' => $collationName,
186 'cl_timestamp = cl_timestamp',
188 array( 'cl_from' => $row->cl_from
, 'cl_to' => $row->cl_to
),
193 $batchConds = array( $this->getBatchCondition( $row, $dbw ) );
197 $this->commitTransaction( $dbw, __METHOD__
);
200 $count +
= $res->numRows();
201 $this->output( "$count done.\n" );
203 if ( !$dryRun && ++
$batchCount % self
::SYNC_INTERVAL
== 0 ) {
204 $this->output( "Waiting for slaves ... " );
206 $this->output( "done\n" );
208 } while ( $res->numRows() == self
::BATCH_SIZE
);
210 $this->output( "$count rows processed\n" );
212 if ( $verboseStats ) {
213 $this->output( "\n" );
214 $this->showSortKeySizeHistogram();
219 * Return an SQL expression selecting rows which sort above the given row,
220 * assuming an ordering of cl_from, cl_to
221 * @param stdClass $row
222 * @param DatabaseBase $dbw
225 function getBatchCondition( $row, $dbw ) {
226 $fields = array( 'cl_from', 'cl_to' );
230 foreach ( $fields as $field ) {
231 $encValue = $dbw->addQuotes( $row->$field );
232 $inequality = "$field > $encValue";
233 $equality = "$field = $encValue";
239 $cond .= " OR ($prefix AND $inequality)";
240 $prefix .= " AND $equality";
247 function updateSortKeySizeHistogram( $key ) {
248 $length = strlen( $key );
249 if ( !isset( $this->sizeHistogram
[$length] ) ) {
250 $this->sizeHistogram
[$length] = 0;
252 $this->sizeHistogram
[$length]++
;
255 function showSortKeySizeHistogram() {
256 $maxLength = max( array_keys( $this->sizeHistogram
) );
257 if ( $maxLength == 0 ) {
261 $coarseHistogram = array_fill( 0, $numBins, 0 );
262 $coarseBoundaries = array();
264 for ( $i = 0; $i < $numBins - 1; $i++
) {
265 $boundary +
= $maxLength / $numBins;
266 $coarseBoundaries[$i] = round( $boundary );
268 $coarseBoundaries[$numBins - 1] = $maxLength +
1;
270 for ( $i = 0; $i <= $maxLength; $i++
) {
274 if ( !isset( $this->sizeHistogram
[$i] ) ) {
277 $val = $this->sizeHistogram
[$i];
279 for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++
) {
280 if ( $coarseBoundaries[$coarseIndex] > $i ) {
281 $coarseHistogram[$coarseIndex] +
= $val;
285 if ( $coarseIndex == $numBins - 1 ) {
286 $coarseHistogram[$coarseIndex] +
= $val;
291 $this->output( "Sort key size histogram\nRaw data: $raw\n\n" );
293 $maxBinVal = max( $coarseHistogram );
294 $scale = 60 / $maxBinVal;
296 for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++
) {
297 if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
300 $val = $coarseHistogram[$coarseIndex];
302 $boundary = $coarseBoundaries[$coarseIndex];
303 $this->output( sprintf( "%-10s %-10d |%s\n",
304 $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
306 str_repeat( '*', $scale * $val ) ) );
307 $prevBoundary = $boundary;
312 $maintClass = "UpdateCollation";
313 require_once RUN_MAINTENANCE_IF_MAIN
;