Merge "jquery.checkboxShiftClick: Don't toggle disabled checkboxes"
[lhc/web/wiklou.git] / maintenance / updateCollation.php
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.
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup Maintenance
24 * @author Aryeh Gregor (Simetrical)
25 */
26
27 #$optionsWithArgs = array( 'begin', 'max-slave-lag' );
28
29 require_once __DIR__ . '/Maintenance.php';
30
31 /**
32 * Maintenance script that will find all rows in the categorylinks table
33 * whose collation is out-of-date.
34 *
35 * @ingroup Maintenance
36 */
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
40
41 public $sizeHistogram = array();
42
43 public function __construct() {
44 parent::__construct();
45
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 everything's collation is
51 up-to-date, it will do nothing.
52 TEXT;
53
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.' );
68 }
69
70 public function execute() {
71 global $wgCategoryCollation;
72
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();
83 }
84
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' );
88
89 $options = array(
90 'LIMIT' => self::BATCH_SIZE,
91 'ORDER BY' => 'cl_to, cl_type, cl_from',
92 'STRAIGHT_JOIN',
93 );
94
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 )
103 );
104 }
105
106 $count = $dbw->estimateRowCount(
107 'categorylinks',
108 '*',
109 $collationConds,
110 __METHOD__
111 );
112 // Improve estimate if feasible
113 if ( $count < 1000000 ) {
114 $count = $dbw->selectField(
115 'categorylinks',
116 'COUNT(*)',
117 $collationConds,
118 __METHOD__
119 );
120 }
121 if ( $count == 0 ) {
122 $this->output( "Collations up-to-date.\n" );
123 return;
124 }
125 $this->output( "Fixing collation for $count rows.\n" );
126 }
127
128 $count = 0;
129 $batchCount = 0;
130 $batchConds = array();
131 do {
132 $this->output( "Selecting next " . self::BATCH_SIZE . " rows..." );
133 $res = $dbw->select(
134 array( 'categorylinks', 'page' ),
135 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
136 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title'
137 ),
138 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
139 __METHOD__,
140 $options
141 );
142 $this->output( " processing..." );
143
144 if ( !$dryRun ) {
145 $dbw->begin( __METHOD__ );
146 }
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
151 # converted.
152 if ( $row->cl_sortkey == $title->getText()
153 || $row->cl_sortkey == $title->getPrefixedText() ) {
154 $prefix = '';
155 } else {
156 # Custom sortkey, use it as a prefix
157 $prefix = $row->cl_sortkey;
158 }
159 } else {
160 $prefix = $row->cl_sortkey_prefix;
161 }
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 ) {
165 $type = 'subcat';
166 } elseif ( $title->getNamespace() == NS_FILE ) {
167 $type = 'file';
168 } else {
169 $type = 'page';
170 }
171 $newSortKey = $collation->getSortKey(
172 $title->getCategorySortkey( $prefix ) );
173 if ( $verboseStats ) {
174 $this->updateSortKeySizeHistogram( $newSortKey );
175 }
176
177 if ( !$dryRun ) {
178 $dbw->update(
179 'categorylinks',
180 array(
181 'cl_sortkey' => $newSortKey,
182 'cl_sortkey_prefix' => $prefix,
183 'cl_collation' => $collationName,
184 'cl_type' => $type,
185 'cl_timestamp = cl_timestamp',
186 ),
187 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
188 __METHOD__
189 );
190 }
191 }
192 if ( !$dryRun ) {
193 $dbw->commit( __METHOD__ );
194 }
195
196 if ( $row ) {
197 $batchConds = array( $this->getBatchCondition( $row ) );
198 }
199
200 $count += $res->numRows();
201 $this->output( "$count done.\n" );
202
203 if ( !$dryRun && ++$batchCount % self::SYNC_INTERVAL == 0 ) {
204 $this->output( "Waiting for slaves ... " );
205 wfWaitForSlaves();
206 $this->output( "done\n" );
207 }
208 } while ( $res->numRows() == self::BATCH_SIZE );
209
210 $this->output( "$count rows processed\n" );
211
212 if ( $verboseStats ) {
213 $this->output( "\n" );
214 $this->showSortKeySizeHistogram();
215 }
216 }
217
218 /**
219 * Return an SQL expression selecting rows which sort above the given row,
220 * assuming an ordering of cl_to, cl_type, cl_from
221 */
222 function getBatchCondition( $row ) {
223 $dbw = $this->getDB( DB_MASTER );
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";
239 }
240 }
241 return $cond;
242 }
243
244 function updateSortKeySizeHistogram( $key ) {
245 $length = strlen( $key );
246 if ( !isset( $this->sizeHistogram[$length] ) ) {
247 $this->sizeHistogram[$length] = 0;
248 }
249 $this->sizeHistogram[$length]++;
250 }
251
252 function showSortKeySizeHistogram() {
253 $maxLength = max( array_keys( $this->sizeHistogram ) );
254 if ( $maxLength == 0 ) {
255 return;
256 }
257 $numBins = 20;
258 $coarseHistogram = array_fill( 0, $numBins, 0 );
259 $coarseBoundaries = array();
260 $boundary = 0;
261 for ( $i = 0; $i < $numBins - 1; $i++ ) {
262 $boundary += $maxLength / $numBins;
263 $coarseBoundaries[$i] = round( $boundary );
264 }
265 $coarseBoundaries[$numBins - 1] = $maxLength + 1;
266 $raw = '';
267 for ( $i = 0; $i <= $maxLength; $i++ ) {
268 if ( $raw !== '' ) {
269 $raw .= ', ';
270 }
271 if ( !isset( $this->sizeHistogram[$i] ) ) {
272 $val = 0;
273 } else {
274 $val = $this->sizeHistogram[$i];
275 }
276 for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++ ) {
277 if ( $coarseBoundaries[$coarseIndex] > $i ) {
278 $coarseHistogram[$coarseIndex] += $val;
279 break;
280 }
281 }
282 if ( $coarseIndex == $numBins - 1 ) {
283 $coarseHistogram[$coarseIndex] += $val;
284 }
285 $raw .= $val;
286 }
287
288 $this->output( "Sort key size histogram\nRaw data: $raw\n\n" );
289
290 $maxBinVal = max( $coarseHistogram );
291 $scale = 60 / $maxBinVal;
292 $prevBoundary = 0;
293 for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++ ) {
294 if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
295 $val = 0;
296 } else {
297 $val = $coarseHistogram[$coarseIndex];
298 }
299 $boundary = $coarseBoundaries[$coarseIndex];
300 $this->output( sprintf( "%-10s %-10d |%s\n",
301 $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
302 $val,
303 str_repeat( '*', $scale * $val ) ) );
304 $prevBoundary = $boundary;
305 }
306 }
307 }
308
309 $maintClass = "UpdateCollation";
310 require_once RUN_MAINTENANCE_IF_MAIN;