3 * Script will find all rows in the categorylinks table whose collation is
4 * out-of-date (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 #$optionsWithArgs = array( 'begin', 'max-slave-lag' );
29 require_once( __DIR__
. '/Maintenance.php' );
31 class UpdateCollation
extends Maintenance
{
32 const BATCH_SIZE
= 50; // Number of rows to process in one batch
33 const SYNC_INTERVAL
= 20; // Wait for slaves after this many batches
35 var $sizeHistogram = array();
37 public function __construct() {
38 parent
::__construct();
40 global $wgCategoryCollation;
41 $this->mDescription
= <<<TEXT
42 This script will find all rows in the categorylinks table whose collation is
43 out-of-date (cl_collation != '$wgCategoryCollation') and repopulate cl_sortkey
44 using the page title and cl_sortkey_prefix. If everything's collation is
45 up-to-date, it will do nothing.
48 $this->addOption( 'force', 'Run on all rows, even if the collation is ' .
49 'supposed to be up-to-date.' );
50 $this->addOption( 'previous-collation', 'Set the previous value of ' .
51 '$wgCategoryCollation here to speed up this script, especially if your ' .
52 'categorylinks table is large. This will only update rows with that ' .
53 'collation, though, so it may miss out-of-date rows with a different, ' .
54 'even older collation.', false, true );
55 $this->addOption( 'target-collation', 'Set this to the new collation type to ' .
56 'use instead of $wgCategoryCollation. Usually you should not use this, ' .
57 'you should just update $wgCategoryCollation in LocalSettings.php.',
59 $this->addOption( 'dry-run', 'Don\'t actually change the collations, just ' .
60 'compile statistics.' );
61 $this->addOption( 'verbose-stats', 'Show more statistics.' );
64 public function execute() {
65 global $wgCategoryCollation, $wgMiserMode;
67 $dbw = $this->getDB( DB_MASTER
);
68 $force = $this->getOption( 'force' );
69 $dryRun = $this->getOption( 'dry-run' );
70 $verboseStats = $this->getOption( 'verbose-stats' );
71 if ( $this->hasOption( 'target-collation' ) ) {
72 $collationName = $this->getOption( 'target-collation' );
73 $collation = Collation
::factory( $collationName );
75 $collationName = $wgCategoryCollation;
76 $collation = Collation
::singleton();
79 $options = array( 'LIMIT' => self
::BATCH_SIZE
, 'STRAIGHT_JOIN' );
81 if ( $force ||
$dryRun ) {
82 $options['ORDER BY'] = 'cl_from, cl_to';
83 $collationConds = array();
85 if ( $this->hasOption( 'previous-collation' ) ) {
86 $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
88 $collationConds = array( 0 =>
89 'cl_collation != ' . $dbw->addQuotes( $collationName )
93 if ( !$wgMiserMode ) {
94 $count = $dbw->selectField(
101 $count = $dbw->estimateRowCount(
109 $this->output( "Collations up-to-date.\n" );
112 $this->output( "Fixing collation for $count rows.\n" );
117 $batchConds = array();
119 $this->output( "Selecting next " . self
::BATCH_SIZE
. " rows..." );
121 array( 'categorylinks', 'page' ),
122 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
123 'cl_sortkey', 'page_namespace', 'page_title'
125 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
129 $this->output( " processing..." );
132 $dbw->begin( __METHOD__
);
134 foreach ( $res as $row ) {
135 $title = Title
::newFromRow( $row );
136 if ( !$row->cl_collation
) {
137 # This is an old-style row, so the sortkey needs to be
139 if ( $row->cl_sortkey
== $title->getText()
140 ||
$row->cl_sortkey
== $title->getPrefixedText() ) {
143 # Custom sortkey, use it as a prefix
144 $prefix = $row->cl_sortkey
;
147 $prefix = $row->cl_sortkey_prefix
;
149 # cl_type will be wrong for lots of pages if cl_collation is 0,
150 # so let's update it while we're here.
151 if ( $title->getNamespace() == NS_CATEGORY
) {
153 } elseif ( $title->getNamespace() == NS_FILE
) {
158 $newSortKey = $collation->getSortKey(
159 $title->getCategorySortkey( $prefix ) );
160 if ( $verboseStats ) {
161 $this->updateSortKeySizeHistogram( $newSortKey );
168 'cl_sortkey' => $newSortKey,
169 'cl_sortkey_prefix' => $prefix,
170 'cl_collation' => $collationName,
172 'cl_timestamp = cl_timestamp',
174 array( 'cl_from' => $row->cl_from
, 'cl_to' => $row->cl_to
),
180 $dbw->commit( __METHOD__
);
183 if ( ( $force ||
$dryRun ) && $row ) {
184 $encFrom = $dbw->addQuotes( $row->cl_from
);
185 $encTo = $dbw->addQuotes( $row->cl_to
);
187 "(cl_from = $encFrom AND cl_to > $encTo) " .
188 " OR cl_from > $encFrom" );
191 $count +
= $res->numRows();
192 $this->output( "$count done.\n" );
194 if ( !$dryRun && ++
$batchCount % self
::SYNC_INTERVAL
== 0 ) {
195 $this->output( "Waiting for slaves ... " );
197 $this->output( "done\n" );
199 } while ( $res->numRows() == self
::BATCH_SIZE
);
201 $this->output( "$count rows processed\n" );
203 if ( $verboseStats ) {
204 $this->output( "\n" );
205 $this->showSortKeySizeHistogram();
209 function updateSortKeySizeHistogram( $key ) {
210 $length = strlen( $key );
211 if ( !isset( $this->sizeHistogram
[$length] ) ) {
212 $this->sizeHistogram
[$length] = 0;
214 $this->sizeHistogram
[$length]++
;
217 function showSortKeySizeHistogram() {
218 $maxLength = max( array_keys( $this->sizeHistogram
) );
219 if ( $maxLength == 0 ) {
223 $coarseHistogram = array_fill( 0, $numBins, 0 );
224 $coarseBoundaries = array();
226 for ( $i = 0; $i < $numBins - 1; $i++
) {
227 $boundary +
= $maxLength / $numBins;
228 $coarseBoundaries[$i] = round( $boundary );
230 $coarseBoundaries[$numBins - 1] = $maxLength +
1;
232 for ( $i = 0; $i <= $maxLength; $i++
) {
236 if ( !isset( $this->sizeHistogram
[$i] ) ) {
239 $val = $this->sizeHistogram
[$i];
241 for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++
) {
242 if ( $coarseBoundaries[$coarseIndex] > $i ) {
243 $coarseHistogram[$coarseIndex] +
= $val;
247 if ( $coarseIndex == $numBins - 1 ) {
248 $coarseHistogram[$coarseIndex] +
= $val;
253 $this->output( "Sort key size histogram\nRaw data: $raw\n\n" );
255 $maxBinVal = max( $coarseHistogram );
256 $scale = 60 / $maxBinVal;
258 for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++
) {
259 if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
262 $val = $coarseHistogram[$coarseIndex];
264 $boundary = $coarseBoundaries[$coarseIndex];
265 $this->output( sprintf( "%-10s %-10d |%s\n",
266 $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
268 str_repeat( '*', $scale * $val ) ) );
269 $prevBoundary = $boundary;
274 $maintClass = "UpdateCollation";
275 require_once( RUN_MAINTENANCE_IF_MAIN
);