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