Merge "Fix regression in API log events (bug 35635)"
[lhc/web/wiklou.git] / maintenance / updateCollation.php
1 <?php
2 /**
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.
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( dirname( __FILE__ ) . '/Maintenance.php' );
30
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
34
35 var $sizeHistogram = array();
36
37 public function __construct() {
38 parent::__construct();
39
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.
46 TEXT;
47
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.',
58 false, true );
59 $this->addOption( 'dry-run', 'Don\'t actually change the collations, just ' .
60 'compile statistics.' );
61 $this->addOption( 'verbose-stats', 'Show more statistics.' );
62 }
63
64 public function execute() {
65 global $wgCategoryCollation, $wgMiserMode;
66
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 );
74 } else {
75 $collationName = $wgCategoryCollation;
76 $collation = Collation::singleton();
77 }
78
79 $options = array( 'LIMIT' => self::BATCH_SIZE, 'STRAIGHT_JOIN' );
80
81 if ( $force || $dryRun ) {
82 $options['ORDER BY'] = 'cl_from, cl_to';
83 $collationConds = array();
84 } else {
85 if ( $this->hasOption( 'previous-collation' ) ) {
86 $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
87 } else {
88 $collationConds = array( 0 =>
89 'cl_collation != ' . $dbw->addQuotes( $collationName )
90 );
91 }
92
93 if ( !$wgMiserMode ) {
94 $count = $dbw->selectField(
95 'categorylinks',
96 'COUNT(*)',
97 $collationConds,
98 __METHOD__
99 );
100 } else {
101 $count = $dbw->estimateRowCount(
102 'categorylinks',
103 '*',
104 $collationConds,
105 __METHOD__
106 );
107 }
108 if ( $count == 0 ) {
109 $this->output( "Collations up-to-date.\n" );
110 return;
111 }
112 $this->output( "Fixing collation for $count rows.\n" );
113 }
114
115 $count = 0;
116 $batchCount = 0;
117 $batchConds = array();
118 do {
119 $this->output( "Selecting next " . self::BATCH_SIZE . " rows..." );
120 $res = $dbw->select(
121 array( 'categorylinks', 'page' ),
122 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
123 'cl_sortkey', 'page_namespace', 'page_title'
124 ),
125 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
126 __METHOD__,
127 $options
128 );
129 $this->output( " processing..." );
130
131 if ( !$dryRun ) {
132 $dbw->begin( __METHOD__ );
133 }
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
138 # converted.
139 if ( $row->cl_sortkey == $title->getText()
140 || $row->cl_sortkey == $title->getPrefixedText() ) {
141 $prefix = '';
142 } else {
143 # Custom sortkey, use it as a prefix
144 $prefix = $row->cl_sortkey;
145 }
146 } else {
147 $prefix = $row->cl_sortkey_prefix;
148 }
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 ) {
152 $type = 'subcat';
153 } elseif ( $title->getNamespace() == NS_FILE ) {
154 $type = 'file';
155 } else {
156 $type = 'page';
157 }
158 $newSortKey = $collation->getSortKey(
159 $title->getCategorySortkey( $prefix ) );
160 if ( $verboseStats ) {
161 $this->updateSortKeySizeHistogram( $newSortKey );
162 }
163
164 if ( !$dryRun ) {
165 $dbw->update(
166 'categorylinks',
167 array(
168 'cl_sortkey' => $newSortKey,
169 'cl_sortkey_prefix' => $prefix,
170 'cl_collation' => $collationName,
171 'cl_type' => $type,
172 'cl_timestamp = cl_timestamp',
173 ),
174 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
175 __METHOD__
176 );
177 }
178 }
179 if ( !$dryRun ) {
180 $dbw->commit( __METHOD__ );
181 }
182
183 if ( ( $force || $dryRun ) && $row ) {
184 $encFrom = $dbw->addQuotes( $row->cl_from );
185 $encTo = $dbw->addQuotes( $row->cl_to );
186 $batchConds = array(
187 "(cl_from = $encFrom AND cl_to > $encTo) " .
188 " OR cl_from > $encFrom" );
189 }
190
191 $count += $res->numRows();
192 $this->output( "$count done.\n" );
193
194 if ( !$dryRun && ++$batchCount % self::SYNC_INTERVAL == 0 ) {
195 $this->output( "Waiting for slaves ... " );
196 wfWaitForSlaves();
197 $this->output( "done\n" );
198 }
199 } while ( $res->numRows() == self::BATCH_SIZE );
200
201 $this->output( "$count rows processed\n" );
202
203 if ( $verboseStats ) {
204 $this->output( "\n" );
205 $this->showSortKeySizeHistogram();
206 }
207 }
208
209 function updateSortKeySizeHistogram( $key ) {
210 $length = strlen( $key );
211 if ( !isset( $this->sizeHistogram[$length] ) ) {
212 $this->sizeHistogram[$length] = 0;
213 }
214 $this->sizeHistogram[$length]++;
215 }
216
217 function showSortKeySizeHistogram() {
218 $maxLength = max( array_keys( $this->sizeHistogram ) );
219 if ( $maxLength == 0 ) {
220 return;
221 }
222 $numBins = 20;
223 $coarseHistogram = array_fill( 0, $numBins, 0 );
224 $coarseBoundaries = array();
225 $boundary = 0;
226 for ( $i = 0; $i < $numBins - 1; $i++ ) {
227 $boundary += $maxLength / $numBins;
228 $coarseBoundaries[$i] = round( $boundary );
229 }
230 $coarseBoundaries[$numBins - 1] = $maxLength + 1;
231 $raw = '';
232 for ( $i = 0; $i <= $maxLength; $i++ ) {
233 if ( $raw !== '' ) {
234 $raw .= ', ';
235 }
236 if ( !isset( $this->sizeHistogram[$i] ) ) {
237 $val = 0;
238 } else {
239 $val = $this->sizeHistogram[$i];
240 }
241 for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++ ) {
242 if ( $coarseBoundaries[$coarseIndex] > $i ) {
243 $coarseHistogram[$coarseIndex] += $val;
244 break;
245 }
246 }
247 if ( $coarseIndex == $numBins - 1 ) {
248 $coarseHistogram[$coarseIndex] += $val;
249 }
250 $raw .= $val;
251 }
252
253 $this->output( "Sort key size histogram\nRaw data: $raw\n\n" );
254
255 $maxBinVal = max( $coarseHistogram );
256 $scale = 60 / $maxBinVal;
257 $prevBoundary = 0;
258 for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++ ) {
259 if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
260 $val = 0;
261 } else {
262 $val = $coarseHistogram[$coarseIndex];
263 }
264 $boundary = $coarseBoundaries[$coarseIndex];
265 $this->output( sprintf( "%-10s %-10d |%s\n",
266 $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
267 $val,
268 str_repeat( '*', $scale * $val ) ) );
269 $prevBoundary = $boundary;
270 }
271 }
272 }
273
274 $maintClass = "UpdateCollation";
275 require_once( RUN_MAINTENANCE_IF_MAIN );