Make list=logevents display log entries by anonymous users
[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 $options = array(
86 'LIMIT' => self::BATCH_SIZE,
87 'ORDER BY' => 'cl_to, cl_type, cl_from',
88 'STRAIGHT_JOIN',
89 );
90
91 if ( $force || $dryRun ) {
92 $collationConds = array();
93 } else {
94 if ( $this->hasOption( 'previous-collation' ) ) {
95 $collationConds['cl_collation'] = $this->getOption( 'previous-collation' );
96 } else {
97 $collationConds = array( 0 =>
98 'cl_collation != ' . $dbw->addQuotes( $collationName )
99 );
100 }
101
102 $count = $dbw->estimateRowCount(
103 'categorylinks',
104 '*',
105 $collationConds,
106 __METHOD__
107 );
108 // Improve estimate if feasible
109 if ( $count < 1000000 ) {
110 $count = $dbw->selectField(
111 'categorylinks',
112 'COUNT(*)',
113 $collationConds,
114 __METHOD__
115 );
116 }
117 if ( $count == 0 ) {
118 $this->output( "Collations up-to-date.\n" );
119 return;
120 }
121 $this->output( "Fixing collation for $count rows.\n" );
122 }
123
124 $count = 0;
125 $batchCount = 0;
126 $batchConds = array();
127 do {
128 $this->output( "Selecting next " . self::BATCH_SIZE . " rows..." );
129 $res = $dbw->select(
130 array( 'categorylinks', 'page' ),
131 array( 'cl_from', 'cl_to', 'cl_sortkey_prefix', 'cl_collation',
132 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title'
133 ),
134 array_merge( $collationConds, $batchConds, array( 'cl_from = page_id' ) ),
135 __METHOD__,
136 $options
137 );
138 $this->output( " processing..." );
139
140 if ( !$dryRun ) {
141 $dbw->begin( __METHOD__ );
142 }
143 foreach ( $res as $row ) {
144 $title = Title::newFromRow( $row );
145 if ( !$row->cl_collation ) {
146 # This is an old-style row, so the sortkey needs to be
147 # converted.
148 if ( $row->cl_sortkey == $title->getText()
149 || $row->cl_sortkey == $title->getPrefixedText() ) {
150 $prefix = '';
151 } else {
152 # Custom sortkey, use it as a prefix
153 $prefix = $row->cl_sortkey;
154 }
155 } else {
156 $prefix = $row->cl_sortkey_prefix;
157 }
158 # cl_type will be wrong for lots of pages if cl_collation is 0,
159 # so let's update it while we're here.
160 if ( $title->getNamespace() == NS_CATEGORY ) {
161 $type = 'subcat';
162 } elseif ( $title->getNamespace() == NS_FILE ) {
163 $type = 'file';
164 } else {
165 $type = 'page';
166 }
167 $newSortKey = $collation->getSortKey(
168 $title->getCategorySortkey( $prefix ) );
169 if ( $verboseStats ) {
170 $this->updateSortKeySizeHistogram( $newSortKey );
171 }
172
173 if ( !$dryRun ) {
174 $dbw->update(
175 'categorylinks',
176 array(
177 'cl_sortkey' => $newSortKey,
178 'cl_sortkey_prefix' => $prefix,
179 'cl_collation' => $collationName,
180 'cl_type' => $type,
181 'cl_timestamp = cl_timestamp',
182 ),
183 array( 'cl_from' => $row->cl_from, 'cl_to' => $row->cl_to ),
184 __METHOD__
185 );
186 }
187 }
188 if ( !$dryRun ) {
189 $dbw->commit( __METHOD__ );
190 }
191
192 if ( $row ) {
193 $batchConds = array( $this->getBatchCondition( $row ) );
194 }
195
196 $count += $res->numRows();
197 $this->output( "$count done.\n" );
198
199 if ( !$dryRun && ++$batchCount % self::SYNC_INTERVAL == 0 ) {
200 $this->output( "Waiting for slaves ... " );
201 wfWaitForSlaves();
202 $this->output( "done\n" );
203 }
204 } while ( $res->numRows() == self::BATCH_SIZE );
205
206 $this->output( "$count rows processed\n" );
207
208 if ( $verboseStats ) {
209 $this->output( "\n" );
210 $this->showSortKeySizeHistogram();
211 }
212 }
213
214 /**
215 * Return an SQL expression selecting rows which sort above the given row,
216 * assuming an ordering of cl_to, cl_type, cl_from
217 */
218 function getBatchCondition( $row ) {
219 $dbw = $this->getDB( DB_MASTER );
220 $fields = array( 'cl_to', 'cl_type', 'cl_from' );
221 $first = true;
222 $cond = false;
223 $prefix = false;
224 foreach ( $fields as $field ) {
225 $encValue = $dbw->addQuotes( $row->$field );
226 $inequality = "$field > $encValue";
227 $equality = "$field = $encValue";
228 if ( $first ) {
229 $cond = $inequality;
230 $prefix = $equality;
231 $first = false;
232 } else {
233 $cond .= " OR ($prefix AND $inequality)";
234 $prefix .= " AND $equality";
235 }
236 }
237 return $cond;
238 }
239
240 function updateSortKeySizeHistogram( $key ) {
241 $length = strlen( $key );
242 if ( !isset( $this->sizeHistogram[$length] ) ) {
243 $this->sizeHistogram[$length] = 0;
244 }
245 $this->sizeHistogram[$length]++;
246 }
247
248 function showSortKeySizeHistogram() {
249 $maxLength = max( array_keys( $this->sizeHistogram ) );
250 if ( $maxLength == 0 ) {
251 return;
252 }
253 $numBins = 20;
254 $coarseHistogram = array_fill( 0, $numBins, 0 );
255 $coarseBoundaries = array();
256 $boundary = 0;
257 for ( $i = 0; $i < $numBins - 1; $i++ ) {
258 $boundary += $maxLength / $numBins;
259 $coarseBoundaries[$i] = round( $boundary );
260 }
261 $coarseBoundaries[$numBins - 1] = $maxLength + 1;
262 $raw = '';
263 for ( $i = 0; $i <= $maxLength; $i++ ) {
264 if ( $raw !== '' ) {
265 $raw .= ', ';
266 }
267 if ( !isset( $this->sizeHistogram[$i] ) ) {
268 $val = 0;
269 } else {
270 $val = $this->sizeHistogram[$i];
271 }
272 for ( $coarseIndex = 0; $coarseIndex < $numBins - 1; $coarseIndex++ ) {
273 if ( $coarseBoundaries[$coarseIndex] > $i ) {
274 $coarseHistogram[$coarseIndex] += $val;
275 break;
276 }
277 }
278 if ( $coarseIndex == $numBins - 1 ) {
279 $coarseHistogram[$coarseIndex] += $val;
280 }
281 $raw .= $val;
282 }
283
284 $this->output( "Sort key size histogram\nRaw data: $raw\n\n" );
285
286 $maxBinVal = max( $coarseHistogram );
287 $scale = 60 / $maxBinVal;
288 $prevBoundary = 0;
289 for ( $coarseIndex = 0; $coarseIndex < $numBins; $coarseIndex++ ) {
290 if ( !isset( $coarseHistogram[$coarseIndex] ) ) {
291 $val = 0;
292 } else {
293 $val = $coarseHistogram[$coarseIndex];
294 }
295 $boundary = $coarseBoundaries[$coarseIndex];
296 $this->output( sprintf( "%-10s %-10d |%s\n",
297 $prevBoundary . '-' . ( $boundary - 1 ) . ': ',
298 $val,
299 str_repeat( '*', $scale * $val ) ) );
300 $prevBoundary = $boundary;
301 }
302 }
303 }
304
305 $maintClass = "UpdateCollation";
306 require_once RUN_MAINTENANCE_IF_MAIN;