Merge "Make update.php file executable"
[mediawiki.git] / maintenance / updateCollation.php
blob19c1d24c7948a9858310a2bcc3dd5182ff90c150
1 <?php
2 /**
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
22 * @file
23 * @ingroup Maintenance
24 * @author Aryeh Gregor (Simetrical)
27 #$optionsWithArgs = array( 'begin', 'max-slave-lag' );
29 require_once __DIR__ . '/Maintenance.php';
31 /**
32 * Maintenance script that will find all rows in the categorylinks table
33 * whose collation is out-of-date.
35 * @ingroup Maintenance
37 class UpdateCollation extends Maintenance {
38 const BATCH_SIZE = 10000; // Number of rows to process in one batch
39 const SYNC_INTERVAL = 20; // Wait for slaves after this many batches
41 public $sizeHistogram = array();
43 public function __construct() {
44 parent::__construct();
46 global $wgCategoryCollation;
47 $this->mDescription = <<<TEXT
48 This script will find all rows in the categorylinks table whose collation is
49 out-of-date (cl_collation != '$wgCategoryCollation') and repopulate cl_sortkey
50 using the page title and cl_sortkey_prefix. If all collations are
51 up-to-date, it will do nothing.
52 TEXT;
54 $this->addOption( 'force', 'Run on all rows, even if the collation is ' .
55 'supposed to be up-to-date.' );
56 $this->addOption( 'previous-collation', 'Set the previous value of ' .
57 '$wgCategoryCollation here to speed up this script, especially if your ' .
58 'categorylinks table is large. This will only update rows with that ' .
59 'collation, though, so it may miss out-of-date rows with a different, ' .
60 'even older collation.', false, true );
61 $this->addOption( 'target-collation', 'Set this to the new collation type to ' .
62 'use instead of $wgCategoryCollation. Usually you should not use this, ' .
63 'you should just update $wgCategoryCollation in LocalSettings.php.',
64 false, true );
65 $this->addOption( 'dry-run', 'Don\'t actually change the collations, just ' .
66 'compile statistics.' );
67 $this->addOption( 'verbose-stats', 'Show more statistics.' );
70 public function execute() {
71 global $wgCategoryCollation;
73 $dbw = $this->getDB( DB_MASTER );
74 $force = $this->getOption( 'force' );
75 $dryRun = $this->getOption( 'dry-run' );
76 $verboseStats = $this->getOption( 'verbose-stats' );
77 if ( $this->hasOption( 'target-collation' ) ) {
78 $collationName = $this->getOption( 'target-collation' );
79 $collation = Collation::factory( $collationName );
80 } else {
81 $collationName = $wgCategoryCollation;
82 $collation = Collation::singleton();
85 // Collation sanity check: in some cases the constructor will work,
86 // but this will raise an exception, breaking all category pages
87 $collation->getFirstLetter( 'MediaWiki' );
89 $options = array(
90 'LIMIT' => self::BATCH_SIZE,
91 'ORDER BY' => 'cl_to, cl_type, cl_from',
92 'STRAIGHT_JOIN',
95 if ( $force || $dryRun ) {
96 $collationConds = array();
97 } else {
98 if ( $this->hasOption( 'previous-collation' ) ) {
99 $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
100 } else {
101 $collationConds = array( 0 =>
102 'cl_collation != ' . $dbw->addQuotes( $collationName )
106 $count = $dbw->estimateRowCount(
107 'categorylinks',
108 '*',
109 $collationConds,
110 __METHOD__
112 // Improve estimate if feasible
113 if ( $count < 1000000 ) {
114 $count = $dbw->selectField(
115 'categorylinks',
116 'COUNT(*)',
117 $collationConds,
118 __METHOD__
121 if ( $count == 0 ) {
122 $this->output( "Collations up-to-date.\n" );
124 return;
126 $this->output( "Fixing collation for $count rows.\n" );
129 $count = 0;
130 $batchCount = 0;
131 $batchConds = array();
132 do {
133 $this->output( "Selecting next " . self::BATCH_SIZE . " rows..." );
134 $res = $dbw->select(
135 array( 'categorylinks', 'page' ),
136 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
137 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title'
139 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
140 __METHOD__,
141 $options
143 $this->output( " processing..." );
145 if ( !$dryRun ) {
146 $dbw->begin( __METHOD__ );
148 foreach ( $res as $row ) {
149 $title = Title::newFromRow( $row );
150 if ( !$row->cl_collation ) {
151 # This is an old-style row, so the sortkey needs to be
152 # converted.
153 if ( $row->cl_sortkey == $title->getText()
154 || $row->cl_sortkey == $title->getPrefixedText()
156 $prefix = '';
157 } else {
158 # Custom sortkey, use it as a prefix
159 $prefix = $row->cl_sortkey;
161 } else {
162 $prefix = $row->cl_sortkey_prefix;
164 # cl_type will be wrong for lots of pages if cl_collation is 0,
165 # so let's update it while we're here.
166 if ( $title->getNamespace() == NS_CATEGORY ) {
167 $type = 'subcat';
168 } elseif ( $title->getNamespace() == NS_FILE ) {
169 $type = 'file';
170 } else {
171 $type = 'page';
173 $newSortKey = $collation->getSortKey(
174 $title->getCategorySortkey( $prefix ) );
175 if ( $verboseStats ) {
176 $this->updateSortKeySizeHistogram( $newSortKey );
179 if ( !$dryRun ) {
180 $dbw->update(
181 'categorylinks',
182 array(
183 'cl_sortkey' => $newSortKey,
184 'cl_sortkey_prefix' => $prefix,
185 'cl_collation' => $collationName,
186 'cl_type' => $type,
187 'cl_timestamp = cl_timestamp',
189 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
190 __METHOD__
193 if ( $row ) {
194 $batchConds = array( $this->getBatchCondition( $row, $dbw ) );
197 if ( !$dryRun ) {
198 $dbw->commit( __METHOD__ );
201 $count += $res->numRows();
202 $this->output( "$count done.\n" );
204 if ( !$dryRun && ++$batchCount % self::SYNC_INTERVAL == 0 ) {
205 $this->output( "Waiting for slaves ... " );
206 wfWaitForSlaves();
207 $this->output( "done\n" );
209 } while ( $res->numRows() == self::BATCH_SIZE );
211 $this->output( "$count rows processed\n" );
213 if ( $verboseStats ) {
214 $this->output( "\n" );
215 $this->showSortKeySizeHistogram();
220 * Return an SQL expression selecting rows which sort above the given row,
221 * assuming an ordering of cl_to, cl_type, cl_from
223 function getBatchCondition( $row, $dbw ) {
224 $fields = array( 'cl_to', 'cl_type', 'cl_from' );
225 $first = true;
226 $cond = false;
227 $prefix = false;
228 foreach ( $fields as $field ) {
229 $encValue = $dbw->addQuotes( $row->$field );
230 $inequality = "$field > $encValue";
231 $equality = "$field = $encValue";
232 if ( $first ) {
233 $cond = $inequality;
234 $prefix = $equality;
235 $first = false;
236 } else {
237 $cond .= " OR ($prefix AND $inequality)";
238 $prefix .= " AND $equality";
242 return $cond;
245 function updateSortKeySizeHistogram( $key ) {
246 $length = strlen( $key );
247 if ( !isset( $this->sizeHistogram[$length] ) ) {
248 $this->sizeHistogram[$length] = 0;
250 $this->sizeHistogram[$length]++;
253 function showSortKeySizeHistogram() {
254 $maxLength = max( array_keys( $this->sizeHistogram ) );
255 if ( $maxLength == 0 ) {
256 return;
258 $numBins = 20;
259 $coarseHistogram = array_fill( 0, $numBins, 0 );
260 $coarseBoundaries = array();
261 $boundary = 0;
262 for ( $i = 0; $i < $numBins - 1; $i++ ) {
263 $boundary += $maxLength / $numBins;
264 $coarseBoundaries[$i] = round( $boundary );
266 $coarseBoundaries[$numBins - 1] = $maxLength + 1;
267 $raw = '';
268 for ( $i = 0; $i <= $maxLength; $i++ ) {
269 if ( $raw !== '' ) {
270 $raw .= ', ';
272 if ( !isset( $this->sizeHistogram[$i] ) ) {
273 $val = 0;
274 } else {
275 $val = $this->sizeHistogram[$i];
277 for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++ ) {
278 if ( $coarseBoundaries[$coarseIndex] > $i ) {
279 $coarseHistogram[$coarseIndex] += $val;
280 break;
283 if ( $coarseIndex == $numBins - 1 ) {
284 $coarseHistogram[$coarseIndex] += $val;
286 $raw .= $val;
289 $this->output( "Sort key size histogram\nRaw data: $raw\n\n" );
291 $maxBinVal = max( $coarseHistogram );
292 $scale = 60 / $maxBinVal;
293 $prevBoundary = 0;
294 for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++ ) {
295 if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
296 $val = 0;
297 } else {
298 $val = $coarseHistogram[$coarseIndex];
300 $boundary = $coarseBoundaries[$coarseIndex];
301 $this->output( sprintf( "%-10s %-10d |%s\n",
302 $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
303 $val,
304 str_repeat( '*', $scale * $val ) ) );
305 $prevBoundary = $boundary;
310 $maintClass = "UpdateCollation";
311 require_once RUN_MAINTENANCE_IF_MAIN;