Complete the trinity of blocking frontend interfaces by rewriting SpecialIpblocklist:
[lhc/web/wiklou.git] / maintenance / upgrade1_5.php
1 <?php
2 /**
3 * Alternate 1.4 -> 1.5 schema upgrade.
4 * This does only the main tables + UTF-8 and is designed to allow upgrades to
5 * interleave with other updates on the replication stream so that large wikis
6 * can be upgraded without disrupting other services.
7 *
8 * Note: this script DOES NOT apply every update, nor will it probably handle
9 * much older versions, etc.
10 * Run this, FOLLOWED BY update.php, for upgrading from 1.4.5 release to 1.5.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup Maintenance
29 */
30
31 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
32
33 define( 'MW_UPGRADE_COPY', false );
34 define( 'MW_UPGRADE_ENCODE', true );
35 define( 'MW_UPGRADE_NULL', null );
36 define( 'MW_UPGRADE_CALLBACK', null ); // for self-documentation only
37
38 /**
39 * @ingroup Maintenance
40 */
41 class FiveUpgrade extends Maintenance {
42 function __construct() {
43 parent::__construct();
44
45 $this->mDescription = 'Script for upgrades from 1.4 to 1.5 (NOT 1.15) in very special cases.';
46
47 $this->addOption( 'upgrade', 'Really run the script' );
48 $this->addOption( 'noimage', '' );
49 $this->addOption( 'step', 'Only do a specific step', false, true );
50 }
51
52 public function getDbType() {
53 return Maintenance::DB_ADMIN;
54 }
55
56 public function execute() {
57 $this->output( "ATTENTION: This script is for upgrades from 1.4 to 1.5 (NOT 1.15) in very special cases.\n" );
58 $this->output( "Use update.php for usual updates.\n" );
59
60 if ( !$this->hasOption( 'upgrade' ) ) {
61 $this->output( "Please run this script with --upgrade key to actually run the updater.\n" );
62 return;
63 }
64
65 $this->setMembers();
66
67 $tables = array(
68 'page',
69 'links',
70 'user',
71 'image',
72 'oldimage',
73 'watchlist',
74 'logging',
75 'archive',
76 'imagelinks',
77 'categorylinks',
78 'ipblocks',
79 'recentchanges',
80 'querycache'
81 );
82
83 foreach ( $tables as $table ) {
84 if ( $this->doing( $table ) ) {
85 $method = 'upgrade' . ucfirst( $table );
86 $this->$method();
87 }
88 }
89
90 if ( $this->doing( 'cleanup' ) ) {
91 $this->upgradeCleanup();
92 }
93 }
94
95 protected function setMembers() {
96 $this->conversionTables = $this->prepareWindows1252();
97
98 $this->loadBalancers = array();
99 $this->dbw = wfGetDB( DB_MASTER );
100 $this->dbr = $this->streamConnection();
101
102 $this->cleanupSwaps = array();
103 $this->emailAuth = false; # don't preauthenticate emails
104 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
105 $this->step = $this->getOption( 'step', null );
106 }
107
108 function doing( $step ) {
109 return is_null( $this->step ) || $step == $this->step;
110 }
111
112 /**
113 * Open a connection to the master server with the admin rights.
114 * @return Database
115 * @access private
116 */
117 function newConnection() {
118 $lb = wfGetLBFactory()->newMainLB();
119 $db = $lb->getConnection( DB_MASTER );
120
121 $this->loadBalancers[] = $lb;
122 return $db;
123 }
124
125 /**
126 * Commit transactions and close the connections when we're done...
127 */
128 function close() {
129 foreach ( $this->loadBalancers as $lb ) {
130 $lb->commitMasterChanges();
131 $lb->closeAll();
132 }
133 }
134
135 /**
136 * Open a second connection to the master server, with buffering off.
137 * This will let us stream large datasets in and write in chunks on the
138 * other end.
139 * @return Database
140 * @access private
141 */
142 function streamConnection() {
143 $timeout = 3600 * 24;
144 $db = $this->newConnection();
145 $db->bufferResults( false );
146 if ( $db->getType() == 'mysql' ) {
147 $db->query( "SET net_read_timeout=$timeout" );
148 $db->query( "SET net_write_timeout=$timeout" );
149 }
150 return $db;
151 }
152
153 /**
154 * Prepare a conversion array for converting Windows Code Page 1252 to
155 * UTF-8. This should provide proper conversion of text that was miscoded
156 * as Windows-1252 by naughty user-agents, and doesn't rely on an outside
157 * iconv library.
158 *
159 * @return array
160 * @access private
161 */
162 function prepareWindows1252() {
163 # Mappings from:
164 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
165 static $cp1252 = array(
166 0x80 => 0x20AC, # EURO SIGN
167 0x81 => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
168 0x82 => 0x201A, # SINGLE LOW-9 QUOTATION MARK
169 0x83 => 0x0192, # LATIN SMALL LETTER F WITH HOOK
170 0x84 => 0x201E, # DOUBLE LOW-9 QUOTATION MARK
171 0x85 => 0x2026, # HORIZONTAL ELLIPSIS
172 0x86 => 0x2020, # DAGGER
173 0x87 => 0x2021, # DOUBLE DAGGER
174 0x88 => 0x02C6, # MODIFIER LETTER CIRCUMFLEX ACCENT
175 0x89 => 0x2030, # PER MILLE SIGN
176 0x8A => 0x0160, # LATIN CAPITAL LETTER S WITH CARON
177 0x8B => 0x2039, # SINGLE LEFT-POINTING ANGLE QUOTATION MARK
178 0x8C => 0x0152, # LATIN CAPITAL LIGATURE OE
179 0x8D => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
180 0x8E => 0x017D, # LATIN CAPITAL LETTER Z WITH CARON
181 0x8F => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
182 0x90 => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
183 0x91 => 0x2018, # LEFT SINGLE QUOTATION MARK
184 0x92 => 0x2019, # RIGHT SINGLE QUOTATION MARK
185 0x93 => 0x201C, # LEFT DOUBLE QUOTATION MARK
186 0x94 => 0x201D, # RIGHT DOUBLE QUOTATION MARK
187 0x95 => 0x2022, # BULLET
188 0x96 => 0x2013, # EN DASH
189 0x97 => 0x2014, # EM DASH
190 0x98 => 0x02DC, # SMALL TILDE
191 0x99 => 0x2122, # TRADE MARK SIGN
192 0x9A => 0x0161, # LATIN SMALL LETTER S WITH CARON
193 0x9B => 0x203A, # SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
194 0x9C => 0x0153, # LATIN SMALL LIGATURE OE
195 0x9D => 0xFFFD, # REPLACEMENT CHARACTER (no mapping)
196 0x9E => 0x017E, # LATIN SMALL LETTER Z WITH CARON
197 0x9F => 0x0178, # LATIN CAPITAL LETTER Y WITH DIAERESIS
198 );
199 $pairs = array();
200 for ( $i = 0; $i < 0x100; $i++ ) {
201 $unicode = isset( $cp1252[$i] ) ? $cp1252[$i] : $i;
202 $pairs[chr( $i )] = codepointToUtf8( $unicode );
203 }
204 return $pairs;
205 }
206
207 /**
208 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
209 * @param string $text
210 * @return string
211 * @access private
212 */
213 function conv( $text ) {
214 global $wgUseLatin1;
215 return is_null( $text )
216 ? null
217 : ( $wgUseLatin1
218 ? strtr( $text, $this->conversionTables )
219 : $text );
220 }
221
222 /**
223 * Dump timestamp and message to output
224 * @param $message String
225 * @access private
226 */
227 function log( $message ) {
228 $this->output( wfWikiID() . ' ' . wfTimestamp( TS_DB ) . ': ' . $message . "\n" );
229 }
230
231 /**
232 * Initialize the chunked-insert system.
233 * Rows will be inserted in chunks of the given number, rather
234 * than in a giant INSERT...SELECT query, to keep the serialized
235 * MySQL database replication from getting hung up. This way other
236 * things can be going on during conversion without waiting for
237 * slaves to catch up as badly.
238 *
239 * @param int $chunksize Number of rows to insert at once
240 * @param int $final Total expected number of rows / id of last row,
241 * used for progress reports.
242 * @param string $table to insert on
243 * @param string $fname function name to report in SQL
244 * @access private
245 */
246 function setChunkScale( $chunksize, $final, $table, $fname ) {
247 $this->chunkSize = $chunksize;
248 $this->chunkFinal = $final;
249 $this->chunkCount = 0;
250 $this->chunkStartTime = wfTime();
251 $this->chunkOptions = array( 'IGNORE' );
252 $this->chunkTable = $table;
253 $this->chunkFunction = $fname;
254 }
255
256 /**
257 * Chunked inserts: perform an insert if we've reached the chunk limit.
258 * Prints a progress report with estimated completion time.
259 * @param array &$chunk -- This will be emptied if an insert is done.
260 * @param int $key A key identifier to use in progress estimation in
261 * place of the number of rows inserted. Use this if
262 * you provided a max key number instead of a count
263 * as the final chunk number in setChunkScale()
264 * @access private
265 */
266 function addChunk( &$chunk, $key = null ) {
267 if ( count( $chunk ) >= $this->chunkSize ) {
268 $this->insertChunk( $chunk );
269
270 $this->chunkCount += count( $chunk );
271 $now = wfTime();
272 $delta = $now - $this->chunkStartTime;
273 $rate = $this->chunkCount / $delta;
274
275 if ( is_null( $key ) ) {
276 $completed = $this->chunkCount;
277 } else {
278 $completed = $key;
279 }
280 $portion = $completed / $this->chunkFinal;
281
282 $estimatedTotalTime = $delta / $portion;
283 $eta = $this->chunkStartTime + $estimatedTotalTime;
284
285 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
286 wfTimestamp( TS_DB, intval( $now ) ),
287 $portion * 100.0,
288 $this->chunkTable,
289 wfTimestamp( TS_DB, intval( $eta ) ),
290 $completed,
291 $this->chunkFinal,
292 $rate );
293 flush();
294
295 $chunk = array();
296 }
297 }
298
299 /**
300 * Chunked inserts: perform an insert unconditionally, at the end, and log.
301 * @param array &$chunk -- This will be emptied if an insert is done.
302 * @access private
303 */
304 function lastChunk( &$chunk ) {
305 $n = count( $chunk );
306 if ( $n > 0 ) {
307 $this->insertChunk( $chunk );
308 }
309 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
310 }
311
312 /**
313 * Chunked inserts: perform an insert.
314 * @param array &$chunk -- This will be emptied if an insert is done.
315 * @access private
316 */
317 function insertChunk( &$chunk ) {
318 // Give slaves a chance to catch up
319 wfWaitForSlaves( $this->maxLag );
320 $this->dbw->insert( $this->chunkTable, $chunk, $this->chunkFunction, $this->chunkOptions );
321 }
322
323 /**
324 * Helper function for copyTable array_filter
325 */
326 static private function notUpgradeNull( $x ) {
327 return $x !== MW_UPGRADE_NULL;
328 }
329
330 /**
331 * Copy and transcode a table to table_temp.
332 * @param string $name Base name of the source table
333 * @param string $tabledef CREATE TABLE definition, w/ $1 for the name
334 * @param array $fields set of destination fields to these constants:
335 * MW_UPGRADE_COPY - straight copy
336 * MW_UPGRADE_ENCODE - for old Latin1 wikis, conv to UTF-8
337 * MW_UPGRADE_NULL - just put NULL
338 * @param callable $callback An optional callback to modify the data
339 * or perform other processing. Func should be
340 * ( object $row, array $copy ) and return $copy
341 * @access private
342 */
343 function copyTable( $name, $tabledef, $fields, $callback = null ) {
344 $name_temp = $name . '_temp';
345 $this->log( "Migrating $name table to $name_temp..." );
346
347 $table_temp = $this->dbw->tableName( $name_temp );
348
349 // Create temporary table; we're going to copy everything in there,
350 // then at the end rename the final tables into place.
351 $def = str_replace( '$1', $table_temp, $tabledef );
352 $this->dbw->query( $def, __METHOD__ );
353
354 $numRecords = $this->dbw->selectField( $name, 'COUNT(*)', '', __METHOD__ );
355 $this->setChunkScale( 100, $numRecords, $name_temp, __METHOD__ );
356
357 // Pull all records from the second, streaming database connection.
358 $sourceFields = array_keys( array_filter( $fields, 'FiveUpgrade::notUpgradeNull' ) );
359 $result = $this->dbr->select( $name,
360 $sourceFields,
361 '',
362 __METHOD__ );
363
364 $add = array();
365 foreach ( $result as $row ) {
366 $copy = array();
367 foreach ( $fields as $field => $source ) {
368 if ( $source === MW_UPGRADE_COPY ) {
369 $copy[$field] = $row->$field;
370 } elseif ( $source === MW_UPGRADE_ENCODE ) {
371 $copy[$field] = $this->conv( $row->$field );
372 } elseif ( $source === MW_UPGRADE_NULL ) {
373 $copy[$field] = null;
374 } else {
375 $this->log( "Unknown field copy type: $field => $source" );
376 }
377 }
378 if ( is_callable( $callback ) ) {
379 $copy = call_user_func( $callback, $row, $copy );
380 }
381 $add[] = $copy;
382 $this->addChunk( $add );
383 }
384 $this->lastChunk( $add );
385
386 $this->log( "Done converting $name." );
387 $this->cleanupSwaps[] = $name;
388 }
389
390 function upgradePage() {
391 $chunksize = 100;
392
393 if ( $this->dbw->tableExists( 'page' ) ) {
394 $this->error( 'Page table already exists.', true );
395 }
396
397 $this->log( "Checking cur table for unique title index and applying if necessary" );
398 $this->checkDupes();
399
400 $this->log( "...converting from cur/old to page/revision/text DB structure." );
401
402 list ( $cur, $old, $page, $revision, $text ) = $this->dbw->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
403
404 $this->log( "Creating page and revision tables..." );
405 $this->dbw->query( "CREATE TABLE $page (
406 page_id int(8) unsigned NOT NULL auto_increment,
407 page_namespace int NOT NULL,
408 page_title varchar(255) binary NOT NULL,
409 page_restrictions tinyblob NOT NULL default '',
410 page_counter bigint(20) unsigned NOT NULL default '0',
411 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
412 page_is_new tinyint(1) unsigned NOT NULL default '0',
413 page_random real unsigned NOT NULL,
414 page_touched char(14) binary NOT NULL default '',
415 page_latest int(8) unsigned NOT NULL,
416 page_len int(8) unsigned NOT NULL,
417
418 PRIMARY KEY page_id (page_id),
419 UNIQUE INDEX name_title (page_namespace,page_title),
420 INDEX (page_random),
421 INDEX (page_len)
422 ) TYPE=InnoDB", __METHOD__ );
423 $this->dbw->query( "CREATE TABLE $revision (
424 rev_id int(8) unsigned NOT NULL auto_increment,
425 rev_page int(8) unsigned NOT NULL,
426 rev_text_id int(8) unsigned NOT NULL,
427 rev_comment tinyblob NOT NULL default '',
428 rev_user int(5) unsigned NOT NULL default '0',
429 rev_user_text varchar(255) binary NOT NULL default '',
430 rev_timestamp char(14) binary NOT NULL default '',
431 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
432 rev_deleted tinyint(1) unsigned NOT NULL default '0',
433
434 PRIMARY KEY rev_page_id (rev_page, rev_id),
435 UNIQUE INDEX rev_id (rev_id),
436 INDEX rev_timestamp (rev_timestamp),
437 INDEX page_timestamp (rev_page,rev_timestamp),
438 INDEX user_timestamp (rev_user,rev_timestamp),
439 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
440 ) TYPE=InnoDB", __METHOD__ );
441
442 $maxold = intval( $this->dbw->selectField( 'old', 'max(old_id)', '', __METHOD__ ) );
443 $this->log( "Last old record is {$maxold}" );
444
445 global $wgLegacySchemaConversion;
446 if ( $wgLegacySchemaConversion ) {
447 // Create HistoryBlobCurStub entries.
448 // Text will be pulled from the leftover 'cur' table at runtime.
449 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
450 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
451 $cur_flags = "'object'";
452 } else {
453 // Copy all cur text in immediately: this may take longer but avoids
454 // having to keep an extra table around.
455 echo "......Moving text from cur.\n";
456 $cur_text = 'cur_text';
457 $cur_flags = "''";
458 }
459
460 $maxcur = $this->dbw->selectField( 'cur', 'max(cur_id)', '', __METHOD__ );
461 $this->log( "Last cur entry is $maxcur" );
462
463 /**
464 * Copy placeholder records for each page's current version into old
465 * Don't do any conversion here; text records are converted at runtime
466 * based on the flags (and may be originally binary!) while the meta
467 * fields will be converted in the old -> rev and cur -> page steps.
468 */
469 $this->setChunkScale( $chunksize, $maxcur, 'old', __METHOD__ );
470 $result = $this->dbr->query(
471 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
472 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
473 FROM $cur
474 ORDER BY cur_id", __METHOD__ );
475 $add = array();
476 foreach ( $result as $row ) {
477 $add[] = array(
478 'old_namespace' => $row->cur_namespace,
479 'old_title' => $row->cur_title,
480 'old_text' => $row->text,
481 'old_comment' => $row->cur_comment,
482 'old_user' => $row->cur_user,
483 'old_user_text' => $row->cur_user_text,
484 'old_timestamp' => $row->cur_timestamp,
485 'old_minor_edit' => $row->cur_minor_edit,
486 'old_flags' => $row->flags );
487 $this->addChunk( $add, $row->cur_id );
488 }
489 $this->lastChunk( $add );
490
491 /**
492 * Copy revision metadata from old into revision.
493 * We'll also do UTF-8 conversion of usernames and comments.
494 */
495 # $newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', __METHOD__ );
496 # $this->setChunkScale( $chunksize, $newmaxold, 'revision', __METHOD__ );
497 # $countold = $this->dbw->selectField( 'old', 'count(old_id)', '', __METHOD__ );
498 $countold = $this->dbw->selectField( 'old', 'max(old_id)', '', __METHOD__ );
499 $this->setChunkScale( $chunksize, $countold, 'revision', __METHOD__ );
500
501 $this->log( "......Setting up revision table." );
502 $result = $this->dbr->query(
503 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
504 old_timestamp, old_minor_edit
505 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
506 __METHOD__ );
507
508 $add = array();
509 foreach ( $result as $row ) {
510 $add[] = array(
511 'rev_id' => $row->old_id,
512 'rev_page' => $row->cur_id,
513 'rev_text_id' => $row->old_id,
514 'rev_comment' => $this->conv( $row->old_comment ),
515 'rev_user' => $row->old_user,
516 'rev_user_text' => $this->conv( $row->old_user_text ),
517 'rev_timestamp' => $row->old_timestamp,
518 'rev_minor_edit' => $row->old_minor_edit );
519 $this->addChunk( $add );
520 }
521 $this->lastChunk( $add );
522
523
524 /**
525 * Copy page metadata from cur into page.
526 * We'll also do UTF-8 conversion of titles.
527 */
528 $this->log( "......Setting up page table." );
529 $this->setChunkScale( $chunksize, $maxcur, 'page', __METHOD__ );
530 $result = $this->dbr->query( "
531 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
532 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
533 FROM $cur,$revision
534 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
535 ORDER BY cur_id", __METHOD__ );
536 $add = array();
537 foreach ( $result as $row ) {
538 $add[] = array(
539 'page_id' => $row->cur_id,
540 'page_namespace' => $row->cur_namespace,
541 'page_title' => $this->conv( $row->cur_title ),
542 'page_restrictions' => $row->cur_restrictions,
543 'page_counter' => $row->cur_counter,
544 'page_is_redirect' => $row->cur_is_redirect,
545 'page_is_new' => $row->cur_is_new,
546 'page_random' => $row->cur_random,
547 'page_touched' => $this->dbw->timestamp(),
548 'page_latest' => $row->rev_id,
549 'page_len' => $row->len );
550 # $this->addChunk( $add, $row->cur_id );
551 $this->addChunk( $add );
552 }
553 $this->lastChunk( $add );
554
555 $this->log( "...done with cur/old -> page/revision." );
556 }
557
558 function upgradeLinks() {
559 $chunksize = 200;
560 list ( $links, $brokenlinks, $pagelinks, $cur ) = $this->dbw->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
561
562 $this->log( 'Checking for interwiki table change in case of bogus items...' );
563 if ( $this->dbw->fieldExists( 'interwiki', 'iw_trans' ) ) {
564 $this->log( 'interwiki has iw_trans.' );
565 } else {
566 global $IP;
567 $this->log( 'adding iw_trans...' );
568 $this->dbw->sourceFile( $IP . '/maintenance/archives/patch-interwiki-trans.sql' );
569 $this->log( 'added iw_trans.' );
570 }
571
572 $this->log( 'Creating pagelinks table...' );
573 $this->dbw->query( "
574 CREATE TABLE $pagelinks (
575 -- Key to the page_id of the page containing the link.
576 pl_from int(8) unsigned NOT NULL default '0',
577
578 -- Key to page_namespace/page_title of the target page.
579 -- The target page may or may not exist, and due to renames
580 -- and deletions may refer to different page records as time
581 -- goes by.
582 pl_namespace int NOT NULL default '0',
583 pl_title varchar(255) binary NOT NULL default '',
584
585 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
586 KEY (pl_namespace,pl_title)
587
588 ) TYPE=InnoDB" );
589
590 $this->log( 'Importing live links -> pagelinks' );
591 $nlinks = $this->dbw->selectField( 'links', 'count(*)', '', __METHOD__ );
592 if ( $nlinks ) {
593 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', __METHOD__ );
594 $result = $this->dbr->query( "
595 SELECT l_from,cur_namespace,cur_title
596 FROM $links, $cur
597 WHERE l_to=cur_id", __METHOD__ );
598 $add = array();
599 foreach ( $result as $row ) {
600 $add[] = array(
601 'pl_from' => $row->l_from,
602 'pl_namespace' => $row->cur_namespace,
603 'pl_title' => $this->conv( $row->cur_title ) );
604 $this->addChunk( $add );
605 }
606 $this->lastChunk( $add );
607 } else {
608 $this->log( 'no links!' );
609 }
610
611 $this->log( 'Importing brokenlinks -> pagelinks' );
612 $nbrokenlinks = $this->dbw->selectField( 'brokenlinks', 'count(*)', '', __METHOD__ );
613 if ( $nbrokenlinks ) {
614 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', __METHOD__ );
615 $result = $this->dbr->query(
616 "SELECT bl_from, bl_to FROM $brokenlinks",
617 __METHOD__ );
618 $add = array();
619 foreach ( $result as $row ) {
620 $pagename = $this->conv( $row->bl_to );
621 $title = Title::newFromText( $pagename );
622 if ( is_null( $title ) ) {
623 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
624 } else {
625 $add[] = array(
626 'pl_from' => $row->bl_from,
627 'pl_namespace' => $title->getNamespace(),
628 'pl_title' => $title->getDBkey() );
629 $this->addChunk( $add );
630 }
631 }
632 $this->lastChunk( $add );
633 } else {
634 $this->log( 'no brokenlinks!' );
635 }
636
637 $this->log( 'Done with links.' );
638 }
639
640 function userDupeCallback( $str ) {
641 echo $str;
642 }
643
644 function upgradeUser() {
645 // Apply unique index, if necessary:
646 $duper = new UserDupes( $this->dbw, array( $this, 'userDupeCallback' ) );
647 if ( $duper->hasUniqueIndex() ) {
648 $this->log( "Already have unique user_name index." );
649 } else {
650 $this->log( "Clearing user duplicates..." );
651 if ( !$duper->clearDupes() ) {
652 $this->log( "WARNING: Duplicate user accounts, may explode!" );
653 }
654 }
655
656 $tabledef = <<<END
657 CREATE TABLE $1 (
658 user_id int(5) unsigned NOT NULL auto_increment,
659 user_name varchar(255) binary NOT NULL default '',
660 user_real_name varchar(255) binary NOT NULL default '',
661 user_password tinyblob NOT NULL default '',
662 user_newpassword tinyblob NOT NULL default '',
663 user_email tinytext NOT NULL default '',
664 user_options blob NOT NULL default '',
665 user_touched char(14) binary NOT NULL default '',
666 user_token char(32) binary NOT NULL default '',
667 user_email_authenticated CHAR(14) BINARY,
668 user_email_token CHAR(32) BINARY,
669 user_email_token_expires CHAR(14) BINARY,
670
671 PRIMARY KEY user_id (user_id),
672 UNIQUE INDEX user_name (user_name),
673 INDEX (user_email_token)
674
675 ) TYPE=InnoDB
676 END;
677 $fields = array(
678 'user_id' => MW_UPGRADE_COPY,
679 'user_name' => MW_UPGRADE_ENCODE,
680 'user_real_name' => MW_UPGRADE_ENCODE,
681 'user_password' => MW_UPGRADE_COPY,
682 'user_newpassword' => MW_UPGRADE_COPY,
683 'user_email' => MW_UPGRADE_ENCODE,
684 'user_options' => MW_UPGRADE_ENCODE,
685 'user_touched' => MW_UPGRADE_CALLBACK,
686 'user_token' => MW_UPGRADE_COPY,
687 'user_email_authenticated' => MW_UPGRADE_CALLBACK,
688 'user_email_token' => MW_UPGRADE_NULL,
689 'user_email_token_expires' => MW_UPGRADE_NULL );
690 $this->copyTable( 'user', $tabledef, $fields,
691 array( &$this, 'userCallback' ) );
692 }
693
694 function userCallback( $row, $copy ) {
695 $now = $this->dbw->timestamp();
696 $copy['user_touched'] = $now;
697 $copy['user_email_authenticated'] = $this->emailAuth ? $now : null;
698 return $copy;
699 }
700
701 function upgradeImage() {
702 $tabledef = <<<END
703 CREATE TABLE $1 (
704 img_name varchar(255) binary NOT NULL default '',
705 img_size int(8) unsigned NOT NULL default '0',
706 img_width int(5) NOT NULL default '0',
707 img_height int(5) NOT NULL default '0',
708 img_metadata mediumblob NOT NULL,
709 img_bits int(3) NOT NULL default '0',
710 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
711 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
712 img_minor_mime varchar(32) NOT NULL default "unknown",
713 img_description tinyblob NOT NULL default '',
714 img_user int(5) unsigned NOT NULL default '0',
715 img_user_text varchar(255) binary NOT NULL default '',
716 img_timestamp char(14) binary NOT NULL default '',
717
718 PRIMARY KEY img_name (img_name),
719 INDEX img_size (img_size),
720 INDEX img_timestamp (img_timestamp)
721 ) TYPE=InnoDB
722 END;
723 $fields = array(
724 'img_name' => MW_UPGRADE_ENCODE,
725 'img_size' => MW_UPGRADE_COPY,
726 'img_width' => MW_UPGRADE_CALLBACK,
727 'img_height' => MW_UPGRADE_CALLBACK,
728 'img_metadata' => MW_UPGRADE_CALLBACK,
729 'img_bits' => MW_UPGRADE_CALLBACK,
730 'img_media_type' => MW_UPGRADE_CALLBACK,
731 'img_major_mime' => MW_UPGRADE_CALLBACK,
732 'img_minor_mime' => MW_UPGRADE_CALLBACK,
733 'img_description' => MW_UPGRADE_ENCODE,
734 'img_user' => MW_UPGRADE_COPY,
735 'img_user_text' => MW_UPGRADE_ENCODE,
736 'img_timestamp' => MW_UPGRADE_COPY );
737 $this->copyTable( 'image', $tabledef, $fields,
738 array( &$this, 'imageCallback' ) );
739 }
740
741 function imageCallback( $row, $copy ) {
742 if ( !$this->hasOption( 'noimage' ) ) {
743 // Fill in the new image info fields
744 $info = $this->imageInfo( $row->img_name );
745
746 $copy['img_width' ] = $info['width'];
747 $copy['img_height' ] = $info['height'];
748 $copy['img_metadata' ] = ""; // loaded on-demand
749 $copy['img_bits' ] = $info['bits'];
750 $copy['img_media_type'] = $info['media'];
751 $copy['img_major_mime'] = $info['major'];
752 $copy['img_minor_mime'] = $info['minor'];
753 }
754
755 // If doing UTF8 conversion the file must be renamed
756 $this->renameFile( $row->img_name, 'wfImageDir' );
757
758 return $copy;
759 }
760
761 function imageInfo( $filename ) {
762 $info = array(
763 'width' => 0,
764 'height' => 0,
765 'bits' => 0,
766 'media' => '',
767 'major' => '',
768 'minor' => '' );
769
770 $magic = MimeMagic::singleton();
771 $mime = $magic->guessMimeType( $filename, true );
772 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
773
774 $info['media'] = $magic->getMediaType( $filename, $mime );
775
776 $image = UnregisteredLocalFile::newFromPath( $filename, $mime );
777
778 $info['width'] = $image->getWidth();
779 $info['height'] = $image->getHeight();
780
781 $gis = $image->getImageSize( $filename );
782 if ( isset( $gis['bits'] ) ) {
783 $info['bits'] = $gis['bits'];
784 }
785
786 return $info;
787 }
788
789
790 /**
791 * Truncate a table.
792 * @param string $table The table name to be truncated
793 */
794 function clearTable( $table ) {
795 print "Clearing $table...\n";
796 $tableName = $this->db->tableName( $table );
797 $this->db->query( "TRUNCATE $tableName" );
798 }
799
800 /**
801 * Rename a given image or archived image file to the converted filename,
802 * leaving a symlink for URL compatibility.
803 *
804 * @param string $oldname pre-conversion filename
805 * @param string $basename pre-conversion base filename for dir hashing, if an archive
806 * @access private
807 */
808 function renameFile( $oldname, $subdirCallback = 'wfImageDir', $basename = null ) {
809 $newname = $this->conv( $oldname );
810 if ( $newname == $oldname ) {
811 // No need to rename; another field triggered this row.
812 return false;
813 }
814
815 if ( is_null( $basename ) ) $basename = $oldname;
816 $ubasename = $this->conv( $basename );
817 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
818 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
819
820 $this->log( "$oldpath -> $newpath" );
821 if ( rename( $oldpath, $newpath ) ) {
822 $relpath = wfRelativePath( $newpath, dirname( $oldpath ) );
823 if ( !symlink( $relpath, $oldpath ) ) {
824 $this->log( "... symlink failed!" );
825 }
826 return $newname;
827 } else {
828 $this->log( "... rename failed!" );
829 return false;
830 }
831 }
832
833 function upgradeOldImage() {
834 $tabledef = <<<END
835 CREATE TABLE $1 (
836 -- Base filename: key to image.img_name
837 oi_name varchar(255) binary NOT NULL default '',
838
839 -- Filename of the archived file.
840 -- This is generally a timestamp and '!' prepended to the base name.
841 oi_archive_name varchar(255) binary NOT NULL default '',
842
843 -- Other fields as in image...
844 oi_size int(8) unsigned NOT NULL default 0,
845 oi_width int(5) NOT NULL default 0,
846 oi_height int(5) NOT NULL default 0,
847 oi_bits int(3) NOT NULL default 0,
848 oi_description tinyblob NOT NULL default '',
849 oi_user int(5) unsigned NOT NULL default '0',
850 oi_user_text varchar(255) binary NOT NULL default '',
851 oi_timestamp char(14) binary NOT NULL default '',
852
853 INDEX oi_name (oi_name(10))
854
855 ) TYPE=InnoDB;
856 END;
857 $fields = array(
858 'oi_name' => MW_UPGRADE_ENCODE,
859 'oi_archive_name' => MW_UPGRADE_ENCODE,
860 'oi_size' => MW_UPGRADE_COPY,
861 'oi_width' => MW_UPGRADE_CALLBACK,
862 'oi_height' => MW_UPGRADE_CALLBACK,
863 'oi_bits' => MW_UPGRADE_CALLBACK,
864 'oi_description' => MW_UPGRADE_ENCODE,
865 'oi_user' => MW_UPGRADE_COPY,
866 'oi_user_text' => MW_UPGRADE_ENCODE,
867 'oi_timestamp' => MW_UPGRADE_COPY );
868 $this->copyTable( 'oldimage', $tabledef, $fields,
869 array( &$this, 'oldimageCallback' ) );
870 }
871
872 function oldimageCallback( $row, $copy ) {
873 global $options;
874 if ( !isset( $options['noimage'] ) ) {
875 // Fill in the new image info fields
876 $info = $this->imageInfo( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
877 $copy['oi_width' ] = $info['width' ];
878 $copy['oi_height'] = $info['height'];
879 $copy['oi_bits' ] = $info['bits' ];
880 }
881
882 // If doing UTF8 conversion the file must be renamed
883 $this->renameFile( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
884
885 return $copy;
886 }
887
888
889 function upgradeWatchlist() {
890 $chunksize = 100;
891
892 list ( $watchlist, $watchlist_temp ) = $this->dbw->tableNamesN( 'watchlist', 'watchlist_temp' );
893
894 $this->log( 'Migrating watchlist table to watchlist_temp...' );
895 $this->dbw->query(
896 "CREATE TABLE $watchlist_temp (
897 -- Key to user_id
898 wl_user int(5) unsigned NOT NULL,
899
900 -- Key to page_namespace/page_title
901 -- Note that users may watch patches which do not exist yet,
902 -- or existed in the past but have been deleted.
903 wl_namespace int NOT NULL default '0',
904 wl_title varchar(255) binary NOT NULL default '',
905
906 -- Timestamp when user was last sent a notification e-mail;
907 -- cleared when the user visits the page.
908 -- FIXME: add proper null support etc
909 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
910
911 UNIQUE KEY (wl_user, wl_namespace, wl_title),
912 KEY namespace_title (wl_namespace,wl_title)
913
914 ) TYPE=InnoDB;", __METHOD__ );
915
916 // Fix encoding for Latin-1 upgrades, add some fields,
917 // and double article to article+talk pairs
918 $numwatched = $this->dbw->selectField( 'watchlist', 'count(*)', '', __METHOD__ );
919
920 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', __METHOD__ );
921 $result = $this->dbr->select( 'watchlist',
922 array(
923 'wl_user',
924 'wl_namespace',
925 'wl_title' ),
926 '',
927 __METHOD__ );
928
929 $add = array();
930 foreach ( $result as $row ) {
931 $add[] = array(
932 'wl_user' => $row->wl_user,
933 'wl_namespace' => MWNamespace::getSubject( $row->wl_namespace ),
934 'wl_title' => $this->conv( $row->wl_title ),
935 'wl_notificationtimestamp' => '0' );
936 $this->addChunk( $add );
937
938 $add[] = array(
939 'wl_user' => $row->wl_user,
940 'wl_namespace' => MWNamespace::getTalk( $row->wl_namespace ),
941 'wl_title' => $this->conv( $row->wl_title ),
942 'wl_notificationtimestamp' => '0' );
943 $this->addChunk( $add );
944 }
945 $this->lastChunk( $add );
946
947 $this->log( 'Done converting watchlist.' );
948 $this->cleanupSwaps[] = 'watchlist';
949 }
950
951 function upgradeLogging() {
952 $tabledef = <<<ENDS
953 CREATE TABLE $1 (
954 -- Symbolic keys for the general log type and the action type
955 -- within the log. The output format will be controlled by the
956 -- action field, but only the type controls categorization.
957 log_type char(10) NOT NULL default '',
958 log_action char(10) NOT NULL default '',
959
960 -- Timestamp. Duh.
961 log_timestamp char(14) NOT NULL default '19700101000000',
962
963 -- The user who performed this action; key to user_id
964 log_user int unsigned NOT NULL default 0,
965
966 -- Key to the page affected. Where a user is the target,
967 -- this will point to the user page.
968 log_namespace int NOT NULL default 0,
969 log_title varchar(255) binary NOT NULL default '',
970
971 -- Freeform text. Interpreted as edit history comments.
972 log_comment varchar(255) NOT NULL default '',
973
974 -- LF separated list of miscellaneous parameters
975 log_params blob NOT NULL default '',
976
977 KEY type_time (log_type, log_timestamp),
978 KEY user_time (log_user, log_timestamp),
979 KEY page_time (log_namespace, log_title, log_timestamp)
980
981 ) TYPE=InnoDB
982 ENDS;
983 $fields = array(
984 'log_type' => MW_UPGRADE_COPY,
985 'log_action' => MW_UPGRADE_COPY,
986 'log_timestamp' => MW_UPGRADE_COPY,
987 'log_user' => MW_UPGRADE_COPY,
988 'log_namespace' => MW_UPGRADE_COPY,
989 'log_title' => MW_UPGRADE_ENCODE,
990 'log_comment' => MW_UPGRADE_ENCODE,
991 'log_params' => MW_UPGRADE_ENCODE );
992 $this->copyTable( 'logging', $tabledef, $fields );
993 }
994
995 function upgradeArchive() {
996 $tabledef = <<<ENDS
997 CREATE TABLE $1 (
998 ar_namespace int NOT NULL default '0',
999 ar_title varchar(255) binary NOT NULL default '',
1000 ar_text mediumblob NOT NULL default '',
1001
1002 ar_comment tinyblob NOT NULL default '',
1003 ar_user int(5) unsigned NOT NULL default '0',
1004 ar_user_text varchar(255) binary NOT NULL,
1005 ar_timestamp char(14) binary NOT NULL default '',
1006 ar_minor_edit tinyint(1) NOT NULL default '0',
1007
1008 ar_flags tinyblob NOT NULL default '',
1009
1010 ar_rev_id int(8) unsigned,
1011 ar_text_id int(8) unsigned,
1012
1013 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
1014
1015 ) TYPE=InnoDB
1016 ENDS;
1017 $fields = array(
1018 'ar_namespace' => MW_UPGRADE_COPY,
1019 'ar_title' => MW_UPGRADE_ENCODE,
1020 'ar_text' => MW_UPGRADE_COPY,
1021 'ar_comment' => MW_UPGRADE_ENCODE,
1022 'ar_user' => MW_UPGRADE_COPY,
1023 'ar_user_text' => MW_UPGRADE_ENCODE,
1024 'ar_timestamp' => MW_UPGRADE_COPY,
1025 'ar_minor_edit' => MW_UPGRADE_COPY,
1026 'ar_flags' => MW_UPGRADE_COPY,
1027 'ar_rev_id' => MW_UPGRADE_NULL,
1028 'ar_text_id' => MW_UPGRADE_NULL );
1029 $this->copyTable( 'archive', $tabledef, $fields );
1030 }
1031
1032 function upgradeImagelinks() {
1033 global $wgUseLatin1;
1034 if ( $wgUseLatin1 ) {
1035 $tabledef = <<<ENDS
1036 CREATE TABLE $1 (
1037 -- Key to page_id of the page containing the image / media link.
1038 il_from int(8) unsigned NOT NULL default '0',
1039
1040 -- Filename of target image.
1041 -- This is also the page_title of the file's description page;
1042 -- all such pages are in namespace 6 (NS_FILE).
1043 il_to varchar(255) binary NOT NULL default '',
1044
1045 UNIQUE KEY il_from(il_from,il_to),
1046 KEY (il_to)
1047
1048 ) TYPE=InnoDB
1049 ENDS;
1050 $fields = array(
1051 'il_from' => MW_UPGRADE_COPY,
1052 'il_to' => MW_UPGRADE_ENCODE );
1053 $this->copyTable( 'imagelinks', $tabledef, $fields );
1054 }
1055 }
1056
1057 function upgradeCategorylinks() {
1058 global $wgUseLatin1;
1059 if ( $wgUseLatin1 ) {
1060 $tabledef = <<<ENDS
1061 CREATE TABLE $1 (
1062 cl_from int(8) unsigned NOT NULL default '0',
1063 cl_to varchar(255) binary NOT NULL default '',
1064 cl_sortkey varchar(86) binary NOT NULL default '',
1065 cl_timestamp timestamp NOT NULL,
1066
1067 UNIQUE KEY cl_from(cl_from,cl_to),
1068 KEY cl_sortkey(cl_to,cl_sortkey),
1069 KEY cl_timestamp(cl_to,cl_timestamp)
1070 ) TYPE=InnoDB
1071 ENDS;
1072 $fields = array(
1073 'cl_from' => MW_UPGRADE_COPY,
1074 'cl_to' => MW_UPGRADE_ENCODE,
1075 'cl_sortkey' => MW_UPGRADE_ENCODE,
1076 'cl_timestamp' => MW_UPGRADE_COPY );
1077 $this->copyTable( 'categorylinks', $tabledef, $fields );
1078 }
1079 }
1080
1081 function upgradeIpblocks() {
1082 global $wgUseLatin1;
1083 if ( $wgUseLatin1 ) {
1084 $tabledef = <<<ENDS
1085 CREATE TABLE $1 (
1086 ipb_id int(8) NOT NULL auto_increment,
1087 ipb_address varchar(40) binary NOT NULL default '',
1088 ipb_user int(8) unsigned NOT NULL default '0',
1089 ipb_by int(8) unsigned NOT NULL default '0',
1090 ipb_reason tinyblob NOT NULL default '',
1091 ipb_timestamp char(14) binary NOT NULL default '',
1092 ipb_auto tinyint(1) NOT NULL default '0',
1093 ipb_expiry char(14) binary NOT NULL default '',
1094
1095 PRIMARY KEY ipb_id (ipb_id),
1096 INDEX ipb_address (ipb_address),
1097 INDEX ipb_user (ipb_user)
1098
1099 ) TYPE=InnoDB
1100 ENDS;
1101 $fields = array(
1102 'ipb_id' => MW_UPGRADE_COPY,
1103 'ipb_address' => MW_UPGRADE_COPY,
1104 'ipb_user' => MW_UPGRADE_COPY,
1105 'ipb_by' => MW_UPGRADE_COPY,
1106 'ipb_reason' => MW_UPGRADE_ENCODE,
1107 'ipb_timestamp' => MW_UPGRADE_COPY,
1108 'ipb_auto' => MW_UPGRADE_COPY,
1109 'ipb_expiry' => MW_UPGRADE_COPY );
1110 $this->copyTable( 'ipblocks', $tabledef, $fields );
1111 }
1112 }
1113
1114 function upgradeRecentchanges() {
1115 // There's a format change in the namespace field
1116 $tabledef = <<<ENDS
1117 CREATE TABLE $1 (
1118 rc_id int(8) NOT NULL auto_increment,
1119 rc_timestamp varchar(14) binary NOT NULL default '',
1120 rc_cur_time varchar(14) binary NOT NULL default '',
1121
1122 rc_user int(10) unsigned NOT NULL default '0',
1123 rc_user_text varchar(255) binary NOT NULL default '',
1124
1125 rc_namespace int NOT NULL default '0',
1126 rc_title varchar(255) binary NOT NULL default '',
1127
1128 rc_comment varchar(255) binary NOT NULL default '',
1129 rc_minor tinyint(3) unsigned NOT NULL default '0',
1130
1131 rc_bot tinyint(3) unsigned NOT NULL default '0',
1132 rc_new tinyint(3) unsigned NOT NULL default '0',
1133
1134 rc_cur_id int(10) unsigned NOT NULL default '0',
1135 rc_this_oldid int(10) unsigned NOT NULL default '0',
1136 rc_last_oldid int(10) unsigned NOT NULL default '0',
1137
1138 rc_type tinyint(3) unsigned NOT NULL default '0',
1139 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
1140 rc_moved_to_title varchar(255) binary NOT NULL default '',
1141
1142 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1143
1144 rc_ip char(15) NOT NULL default '',
1145
1146 PRIMARY KEY rc_id (rc_id),
1147 INDEX rc_timestamp (rc_timestamp),
1148 INDEX rc_namespace_title (rc_namespace, rc_title),
1149 INDEX rc_cur_id (rc_cur_id),
1150 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
1151 INDEX rc_ip (rc_ip)
1152
1153 ) TYPE=InnoDB
1154 ENDS;
1155 $fields = array(
1156 'rc_id' => MW_UPGRADE_COPY,
1157 'rc_timestamp' => MW_UPGRADE_COPY,
1158 'rc_cur_time' => MW_UPGRADE_COPY,
1159 'rc_user' => MW_UPGRADE_COPY,
1160 'rc_user_text' => MW_UPGRADE_ENCODE,
1161 'rc_namespace' => MW_UPGRADE_COPY,
1162 'rc_title' => MW_UPGRADE_ENCODE,
1163 'rc_comment' => MW_UPGRADE_ENCODE,
1164 'rc_minor' => MW_UPGRADE_COPY,
1165 'rc_bot' => MW_UPGRADE_COPY,
1166 'rc_new' => MW_UPGRADE_COPY,
1167 'rc_cur_id' => MW_UPGRADE_COPY,
1168 'rc_this_oldid' => MW_UPGRADE_COPY,
1169 'rc_last_oldid' => MW_UPGRADE_COPY,
1170 'rc_type' => MW_UPGRADE_COPY,
1171 'rc_moved_to_ns' => MW_UPGRADE_COPY,
1172 'rc_moved_to_title' => MW_UPGRADE_ENCODE,
1173 'rc_patrolled' => MW_UPGRADE_COPY,
1174 'rc_ip' => MW_UPGRADE_COPY );
1175 $this->copyTable( 'recentchanges', $tabledef, $fields );
1176 }
1177
1178 function upgradeQuerycache() {
1179 // There's a format change in the namespace field
1180 $tabledef = <<<ENDS
1181 CREATE TABLE $1 (
1182 -- A key name, generally the base name of of the special page.
1183 qc_type char(32) NOT NULL,
1184
1185 -- Some sort of stored value. Sizes, counts...
1186 qc_value int(5) unsigned NOT NULL default '0',
1187
1188 -- Target namespace+title
1189 qc_namespace int NOT NULL default '0',
1190 qc_title char(255) binary NOT NULL default '',
1191
1192 KEY (qc_type,qc_value)
1193
1194 ) TYPE=InnoDB
1195 ENDS;
1196 $fields = array(
1197 'qc_type' => MW_UPGRADE_COPY,
1198 'qc_value' => MW_UPGRADE_COPY,
1199 'qc_namespace' => MW_UPGRADE_COPY,
1200 'qc_title' => MW_UPGRADE_ENCODE );
1201 $this->copyTable( 'querycache', $tabledef, $fields );
1202 }
1203
1204 /**
1205 * Check for duplicate rows in "cur" table and move duplicates entries in
1206 * "old" table.
1207 *
1208 * This was in cleanupDupes.inc before.
1209 */
1210 function checkDupes() {
1211 $dbw = wfGetDB( DB_MASTER );
1212 if ( $dbw->indexExists( 'cur', 'name_title' ) &&
1213 $dbw->indexUnique( 'cur', 'name_title' ) ) {
1214 echo wfWikiID() . ": cur table has the current unique index; no duplicate entries.\n";
1215 return;
1216 } elseif ( $dbw->indexExists( 'cur', 'name_title_dup_prevention' ) ) {
1217 echo wfWikiID() . ": cur table has a temporary name_title_dup_prevention unique index; no duplicate entries.\n";
1218 return;
1219 }
1220
1221 echo wfWikiID() . ": cur table has the old non-unique index and may have duplicate entries.\n";
1222
1223 $dbw = wfGetDB( DB_MASTER );
1224 $cur = $dbw->tableName( 'cur' );
1225 $old = $dbw->tableName( 'old' );
1226 $dbw->query( "LOCK TABLES $cur WRITE, $old WRITE" );
1227 echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n";
1228 $res = $dbw->query( <<<END
1229 SELECT cur_namespace,cur_title,count(*) as c,min(cur_id) as id
1230 FROM $cur
1231 GROUP BY cur_namespace,cur_title
1232 HAVING c > 1
1233 END
1234 );
1235 $n = $dbw->numRows( $res );
1236 echo "Found $n titles with duplicate entries.\n";
1237 if ( $n > 0 ) {
1238 echo "Correcting...\n";
1239 foreach ( $res as $row ) {
1240 $ns = intval( $row->cur_namespace );
1241 $title = $dbw->addQuotes( $row->cur_title );
1242
1243 # Get the first responding ID; that'll be the one we keep.
1244 $id = $dbw->selectField( 'cur', 'cur_id', array(
1245 'cur_namespace' => $row->cur_namespace,
1246 'cur_title' => $row->cur_title ) );
1247
1248 echo "$ns:$row->cur_title (canonical ID $id)\n";
1249 if ( $id != $row->id ) {
1250 echo " ** minimum ID $row->id; ";
1251 $timeMin = $dbw->selectField( 'cur', 'cur_timestamp', array(
1252 'cur_id' => $row->id ) );
1253 $timeFirst = $dbw->selectField( 'cur', 'cur_timestamp', array(
1254 'cur_id' => $id ) );
1255 if ( $timeMin == $timeFirst ) {
1256 echo "timestamps match at $timeFirst; ok\n";
1257 } else {
1258 echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
1259 if ( $timeMin > $timeFirst ) {
1260 $id = $row->id;
1261 echo "keeping minimum: $id\n";
1262 } else {
1263 echo "keeping first: $id\n";
1264 }
1265 }
1266 }
1267
1268 $dbw->query( <<<END
1269 INSERT
1270 INTO $old
1271 (old_namespace, old_title, old_text,
1272 old_comment, old_user, old_user_text,
1273 old_timestamp, old_minor_edit, old_flags,
1274 inverse_timestamp)
1275 SELECT cur_namespace, cur_title, cur_text,
1276 cur_comment, cur_user, cur_user_text,
1277 cur_timestamp, cur_minor_edit, '',
1278 inverse_timestamp
1279 FROM $cur
1280 WHERE cur_namespace=$ns
1281 AND cur_title=$title
1282 AND cur_id != $id
1283 END
1284 );
1285 $dbw->query( <<<END
1286 DELETE
1287 FROM $cur
1288 WHERE cur_namespace=$ns
1289 AND cur_title=$title
1290 AND cur_id != $id
1291 END
1292 );
1293 }
1294 }
1295 $dbw->query( 'UNLOCK TABLES' );
1296 echo "Done.\n";
1297 }
1298
1299 /**
1300 * Rename all our temporary tables into final place.
1301 * We've left things in place so a read-only wiki can continue running
1302 * on the old code during all this.
1303 */
1304 function upgradeCleanup() {
1305 $this->renameTable( 'old', 'text' );
1306
1307 foreach ( $this->cleanupSwaps as $table ) {
1308 $this->swap( $table );
1309 }
1310 }
1311
1312 function renameTable( $from, $to ) {
1313 $this->log( "Renaming $from to $to..." );
1314
1315 $fromtable = $this->dbw->tableName( $from );
1316 $totable = $this->dbw->tableName( $to );
1317 $this->dbw->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1318 }
1319
1320 function swap( $base ) {
1321 $this->renameTable( $base, "{$base}_old" );
1322 $this->renameTable( "{$base}_temp", $base );
1323 }
1324
1325 }
1326
1327 $maintClass = 'FiveUpgrade';
1328 require( RUN_MAINTENANCE_IF_MAIN );