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->mDescription
= <<<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.
52 $this->addOption( 'force', 'Run on all rows, even if the collation is ' .
53 'supposed to be up-to-date.' );
54 $this->addOption( 'previous-collation', 'Set the previous value of ' .
55 '$wgCategoryCollation here to speed up this script, especially if your ' .
56 'categorylinks table is large. This will only update rows with that ' .
57 'collation, though, so it may miss out-of-date rows with a different, ' .
58 'even older collation.', false, true );
59 $this->addOption( 'target-collation', 'Set this to the new collation type to ' .
60 'use instead of $wgCategoryCollation. Usually you should not use this, ' .
61 'you should just update $wgCategoryCollation in LocalSettings.php.',
63 $this->addOption( 'dry-run', 'Don\'t actually change the collations, just ' .
64 'compile statistics.' );
65 $this->addOption( 'verbose-stats', 'Show more statistics.' );
68 public function execute() {
69 global $wgCategoryCollation;
71 $dbw = $this->getDB( DB_MASTER
);
72 $force = $this->getOption( 'force' );
73 $dryRun = $this->getOption( 'dry-run' );
74 $verboseStats = $this->getOption( 'verbose-stats' );
75 if ( $this->hasOption( 'target-collation' ) ) {
76 $collationName = $this->getOption( 'target-collation' );
77 $collation = Collation
::factory( $collationName );
79 $collationName = $wgCategoryCollation;
80 $collation = Collation
::singleton();
83 // Collation sanity check: in some cases the constructor will work,
84 // but this will raise an exception, breaking all category pages
85 $collation->getFirstLetter( 'MediaWiki' );
88 'LIMIT' => self
::BATCH_SIZE
,
89 'ORDER BY' => 'cl_to, cl_type, cl_from',
93 if ( $force ||
$dryRun ) {
94 $collationConds = array();
96 if ( $this->hasOption( 'previous-collation' ) ) {
97 $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
99 $collationConds = array( 0 =>
100 'cl_collation != ' . $dbw->addQuotes( $collationName )
104 $count = $dbw->estimateRowCount(
110 // Improve estimate if feasible
111 if ( $count < 1000000 ) {
112 $count = $dbw->selectField(
120 $this->output( "Collations up-to-date.\n" );
124 $this->output( "Fixing collation for $count rows.\n" );
129 $batchConds = array();
131 $this->output( "Selecting next " . self
::BATCH_SIZE
. " rows..." );
133 array( 'categorylinks', 'page' ),
134 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
135 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title'
137 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
141 $this->output( " processing..." );
144 $dbw->begin( __METHOD__
);
146 foreach ( $res as $row ) {
147 $title = Title
::newFromRow( $row );
148 if ( !$row->cl_collation
) {
149 # This is an old-style row, so the sortkey needs to be
151 if ( $row->cl_sortkey
== $title->getText()
152 ||
$row->cl_sortkey
== $title->getPrefixedText()
156 # Custom sortkey, use it as a prefix
157 $prefix = $row->cl_sortkey
;
160 $prefix = $row->cl_sortkey_prefix
;
162 # cl_type will be wrong for lots of pages if cl_collation is 0,
163 # so let's update it while we're here.
164 if ( $title->getNamespace() == NS_CATEGORY
) {
166 } elseif ( $title->getNamespace() == NS_FILE
) {
171 $newSortKey = $collation->getSortKey(
172 $title->getCategorySortkey( $prefix ) );
173 if ( $verboseStats ) {
174 $this->updateSortKeySizeHistogram( $newSortKey );
181 'cl_sortkey' => $newSortKey,
182 'cl_sortkey_prefix' => $prefix,
183 'cl_collation' => $collationName,
185 'cl_timestamp = cl_timestamp',
187 array( 'cl_from' => $row->cl_from
, 'cl_to' => $row->cl_to
),
192 $batchConds = array( $this->getBatchCondition( $row, $dbw ) );
196 $dbw->commit( __METHOD__
);
199 $count +
= $res->numRows();
200 $this->output( "$count done.\n" );
202 if ( !$dryRun && ++
$batchCount % self
::SYNC_INTERVAL
== 0 ) {
203 $this->output( "Waiting for slaves ... " );
205 $this->output( "done\n" );
207 } while ( $res->numRows() == self
::BATCH_SIZE
);
209 $this->output( "$count rows processed\n" );
211 if ( $verboseStats ) {
212 $this->output( "\n" );
213 $this->showSortKeySizeHistogram();
218 * Return an SQL expression selecting rows which sort above the given row,
219 * assuming an ordering of cl_to, cl_type, cl_from
220 * @param stdClass $row
221 * @param DatabaseBase $dbw
224 function getBatchCondition( $row, $dbw ) {
225 $fields = array( 'cl_to', 'cl_type', 'cl_from' );
229 foreach ( $fields as $field ) {
230 $encValue = $dbw->addQuotes( $row->$field );
231 $inequality = "$field > $encValue";
232 $equality = "$field = $encValue";
238 $cond .= " OR ($prefix AND $inequality)";
239 $prefix .= " AND $equality";
246 function updateSortKeySizeHistogram( $key ) {
247 $length = strlen( $key );
248 if ( !isset( $this->sizeHistogram
[$length] ) ) {
249 $this->sizeHistogram
[$length] = 0;
251 $this->sizeHistogram
[$length]++
;
254 function showSortKeySizeHistogram() {
255 $maxLength = max( array_keys( $this->sizeHistogram
) );
256 if ( $maxLength == 0 ) {
260 $coarseHistogram = array_fill( 0, $numBins, 0 );
261 $coarseBoundaries = array();
263 for ( $i = 0; $i < $numBins - 1; $i++
) {
264 $boundary +
= $maxLength / $numBins;
265 $coarseBoundaries[$i] = round( $boundary );
267 $coarseBoundaries[$numBins - 1] = $maxLength +
1;
269 for ( $i = 0; $i <= $maxLength; $i++
) {
273 if ( !isset( $this->sizeHistogram
[$i] ) ) {
276 $val = $this->sizeHistogram
[$i];
278 for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++
) {
279 if ( $coarseBoundaries[$coarseIndex] > $i ) {
280 $coarseHistogram[$coarseIndex] +
= $val;
284 if ( $coarseIndex == $numBins - 1 ) {
285 $coarseHistogram[$coarseIndex] +
= $val;
290 $this->output( "Sort key size histogram\nRaw data: $raw\n\n" );
292 $maxBinVal = max( $coarseHistogram );
293 $scale = 60 / $maxBinVal;
295 for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++
) {
296 if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
299 $val = $coarseHistogram[$coarseIndex];
301 $boundary = $coarseBoundaries[$coarseIndex];
302 $this->output( sprintf( "%-10s %-10d |%s\n",
303 $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
305 str_repeat( '*', $scale * $val ) ) );
306 $prevBoundary = $boundary;
311 $maintClass = "UpdateCollation";
312 require_once RUN_MAINTENANCE_IF_MAIN
;