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