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.
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.
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.
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.
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
28 * @ingroup Maintenance
31 require_once( dirname( __FILE__
) . '/Maintenance.php' );
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
39 * @ingroup Maintenance
41 class FiveUpgrade
extends Maintenance
{
42 function __construct() {
43 parent
::__construct();
45 $this->mDescription
= 'Script for upgrades from 1.4 to 1.5 (NOT 1.15) in very special cases.';
47 $this->addOption( 'upgrade', 'Really run the script' );
48 $this->addOption( 'noimage', '' );
49 $this->addOption( 'step', 'Only do a specific step', false, true );
52 public function getDbType() {
53 return Maintenance
::DB_ADMIN
;
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" );
60 if ( !$this->hasOption( 'upgrade' ) ) {
61 $this->output( "Please run this script with --upgrade key to actually run the updater.\n" );
83 foreach ( $tables as $table ) {
84 if ( $this->doing( $table ) ) {
85 $method = 'upgrade' . ucfirst( $table );
90 if ( $this->doing( 'cleanup' ) ) {
91 $this->upgradeCleanup();
95 protected function setMembers() {
96 $this->conversionTables
= $this->prepareWindows1252();
98 $this->loadBalancers
= array();
99 $this->dbw
= wfGetDB( DB_MASTER
);
100 $this->dbr
= $this->streamConnection();
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 );
108 function doing( $step ) {
109 return is_null( $this->step
) ||
$step == $this->step
;
113 * Open a connection to the master server with the admin rights.
117 function newConnection() {
118 $lb = wfGetLBFactory()->newMainLB();
119 $db = $lb->getConnection( DB_MASTER
);
121 $this->loadBalancers
[] = $lb;
126 * Commit transactions and close the connections when we're done...
129 foreach ( $this->loadBalancers
as $lb ) {
130 $lb->commitMasterChanges();
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
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" );
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
162 function prepareWindows1252() {
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
200 for ( $i = 0; $i < 0x100; $i++
) {
201 $unicode = isset( $cp1252[$i] ) ?
$cp1252[$i] : $i;
202 $pairs[chr( $i )] = codepointToUtf8( $unicode );
208 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
209 * @param string $text
213 function conv( $text ) {
215 return is_null( $text )
218 ?
strtr( $text, $this->conversionTables
)
223 * Dump timestamp and message to output
224 * @param $message String
227 function log( $message ) {
228 $this->output( wfWikiID() . ' ' . wfTimestamp( TS_DB
) . ': ' . $message . "\n" );
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.
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
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;
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()
266 function addChunk( &$chunk, $key = null ) {
267 if ( count( $chunk ) >= $this->chunkSize
) {
268 $this->insertChunk( $chunk );
270 $this->chunkCount +
= count( $chunk );
272 $delta = $now - $this->chunkStartTime
;
273 $rate = $this->chunkCount
/ $delta;
275 if ( is_null( $key ) ) {
276 $completed = $this->chunkCount
;
280 $portion = $completed / $this->chunkFinal
;
282 $estimatedTotalTime = $delta / $portion;
283 $eta = $this->chunkStartTime +
$estimatedTotalTime;
285 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
286 wfTimestamp( TS_DB
, intval( $now ) ),
289 wfTimestamp( TS_DB
, intval( $eta ) ),
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.
304 function lastChunk( &$chunk ) {
305 $n = count( $chunk );
307 $this->insertChunk( $chunk );
309 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
313 * Chunked inserts: perform an insert.
314 * @param array &$chunk -- This will be emptied if an insert is done.
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
);
324 * Helper function for copyTable array_filter
326 static private function notUpgradeNull( $x ) {
327 return $x !== MW_UPGRADE_NULL
;
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
343 function copyTable( $name, $tabledef, $fields, $callback = null ) {
344 $name_temp = $name . '_temp';
345 $this->log( "Migrating $name table to $name_temp..." );
347 $table_temp = $this->dbw
->tableName( $name_temp );
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__
);
354 $numRecords = $this->dbw
->selectField( $name, 'COUNT(*)', '', __METHOD__
);
355 $this->setChunkScale( 100, $numRecords, $name_temp, __METHOD__
);
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,
365 foreach ( $result as $row ) {
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;
375 $this->log( "Unknown field copy type: $field => $source" );
378 if ( is_callable( $callback ) ) {
379 $copy = call_user_func( $callback, $row, $copy );
382 $this->addChunk( $add );
384 $this->lastChunk( $add );
386 $this->log( "Done converting $name." );
387 $this->cleanupSwaps
[] = $name;
390 function upgradePage() {
393 if ( $this->dbw
->tableExists( 'page' ) ) {
394 $this->error( 'Page table already exists.', true );
397 $this->log( "Checking cur table for unique title index and applying if necessary" );
400 $this->log( "...converting from cur/old to page/revision/text DB structure." );
402 list ( $cur, $old, $page, $revision, $text ) = $this->dbw
->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
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,
418 PRIMARY KEY page_id (page_id),
419 UNIQUE INDEX name_title (page_namespace,page_title),
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',
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__
);
442 $maxold = intval( $this->dbw
->selectField( 'old', 'max(old_id)', '', __METHOD__
) );
443 $this->log( "Last old record is {$maxold}" );
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'";
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';
460 $maxcur = $this->dbw
->selectField( 'cur', 'max(cur_id)', '', __METHOD__
);
461 $this->log( "Last cur entry is $maxcur" );
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.
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
474 ORDER BY cur_id", __METHOD__
);
476 foreach ( $result as $row ) {
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
);
489 $this->lastChunk( $add );
492 * Copy revision metadata from old into revision.
493 * We'll also do UTF-8 conversion of usernames and comments.
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__
);
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",
509 foreach ( $result as $row ) {
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 );
521 $this->lastChunk( $add );
525 * Copy page metadata from cur into page.
526 * We'll also do UTF-8 conversion of titles.
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
534 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
535 ORDER BY cur_id", __METHOD__
);
537 foreach ( $result as $row ) {
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 );
553 $this->lastChunk( $add );
555 $this->log( "...done with cur/old -> page/revision." );
558 function upgradeLinks() {
560 list ( $links, $brokenlinks, $pagelinks, $cur ) = $this->dbw
->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
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.' );
567 $this->log( 'adding iw_trans...' );
568 $this->dbw
->sourceFile( $IP . '/maintenance/archives/patch-interwiki-trans.sql' );
569 $this->log( 'added iw_trans.' );
572 $this->log( 'Creating pagelinks table...' );
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',
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
582 pl_namespace int NOT NULL default '0',
583 pl_title varchar(255) binary NOT NULL default '',
585 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
586 KEY (pl_namespace,pl_title)
590 $this->log( 'Importing live links -> pagelinks' );
591 $nlinks = $this->dbw
->selectField( 'links', 'count(*)', '', __METHOD__
);
593 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', __METHOD__
);
594 $result = $this->dbr
->query( "
595 SELECT l_from,cur_namespace,cur_title
597 WHERE l_to=cur_id", __METHOD__
);
599 foreach ( $result as $row ) {
601 'pl_from' => $row->l_from
,
602 'pl_namespace' => $row->cur_namespace
,
603 'pl_title' => $this->conv( $row->cur_title
) );
604 $this->addChunk( $add );
606 $this->lastChunk( $add );
608 $this->log( 'no links!' );
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",
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')" );
626 'pl_from' => $row->bl_from
,
627 'pl_namespace' => $title->getNamespace(),
628 'pl_title' => $title->getDBkey() );
629 $this->addChunk( $add );
632 $this->lastChunk( $add );
634 $this->log( 'no brokenlinks!' );
637 $this->log( 'Done with links.' );
640 function userDupeCallback( $str ) {
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." );
650 $this->log( "Clearing user duplicates..." );
651 if ( !$duper->clearDupes() ) {
652 $this->log( "WARNING: Duplicate user accounts, may explode!" );
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,
671 PRIMARY KEY user_id (user_id),
672 UNIQUE INDEX user_name (user_name),
673 INDEX (user_email_token)
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' ) );
694 function userCallback( $row, $copy ) {
695 $now = $this->dbw
->timestamp();
696 $copy['user_touched'] = $now;
697 $copy['user_email_authenticated'] = $this->emailAuth ?
$now : null;
701 function upgradeImage() {
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 '',
718 PRIMARY KEY img_name (img_name),
719 INDEX img_size (img_size),
720 INDEX img_timestamp (img_timestamp)
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' ) );
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
);
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'];
755 // If doing UTF8 conversion the file must be renamed
756 $this->renameFile( $row->img_name
, 'wfImageDir' );
761 function imageInfo( $filename ) {
770 $magic = MimeMagic
::singleton();
771 $mime = $magic->guessMimeType( $filename, true );
772 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
774 $info['media'] = $magic->getMediaType( $filename, $mime );
776 $image = UnregisteredLocalFile
::newFromPath( $filename, $mime );
778 $info['width'] = $image->getWidth();
779 $info['height'] = $image->getHeight();
781 $gis = $image->getImageSize( $filename );
782 if ( isset( $gis['bits'] ) ) {
783 $info['bits'] = $gis['bits'];
792 * @param string $table The table name to be truncated
794 function clearTable( $table ) {
795 print "Clearing $table...\n";
796 $tableName = $this->db
->tableName( $table );
797 $this->db
->query( "TRUNCATE $tableName" );
801 * Rename a given image or archived image file to the converted filename,
802 * leaving a symlink for URL compatibility.
804 * @param string $oldname pre-conversion filename
805 * @param string $basename pre-conversion base filename for dir hashing, if an archive
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.
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;
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!" );
828 $this->log( "... rename failed!" );
833 function upgradeOldImage() {
836 -- Base filename: key to image.img_name
837 oi_name varchar(255) binary NOT NULL default '',
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 '',
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 '',
853 INDEX oi_name (oi_name(10))
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' ) );
872 function oldimageCallback( $row, $copy ) {
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' ];
882 // If doing UTF8 conversion the file must be renamed
883 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
889 function upgradeWatchlist() {
892 list ( $watchlist, $watchlist_temp ) = $this->dbw
->tableNamesN( 'watchlist', 'watchlist_temp' );
894 $this->log( 'Migrating watchlist table to watchlist_temp...' );
896 "CREATE TABLE $watchlist_temp (
898 wl_user int(5) unsigned NOT NULL,
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 '',
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',
911 UNIQUE KEY (wl_user, wl_namespace, wl_title),
912 KEY namespace_title (wl_namespace,wl_title)
914 ) TYPE=InnoDB;", __METHOD__
);
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__
);
920 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', __METHOD__
);
921 $result = $this->dbr
->select( 'watchlist',
930 foreach ( $result as $row ) {
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 );
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 );
945 $this->lastChunk( $add );
947 $this->log( 'Done converting watchlist.' );
948 $this->cleanupSwaps
[] = 'watchlist';
951 function upgradeLogging() {
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 '',
961 log_timestamp char(14) NOT NULL default '19700101000000',
963 -- The user who performed this action; key to user_id
964 log_user int unsigned NOT NULL default 0,
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 '',
971 -- Freeform text. Interpreted as edit history comments.
972 log_comment varchar(255) NOT NULL default '',
974 -- LF separated list of miscellaneous parameters
975 log_params blob NOT NULL default '',
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)
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 );
995 function upgradeArchive() {
998 ar_namespace int NOT NULL default '0',
999 ar_title varchar(255) binary NOT NULL default '',
1000 ar_text mediumblob NOT NULL default '',
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',
1008 ar_flags tinyblob NOT NULL default '',
1010 ar_rev_id int(8) unsigned,
1011 ar_text_id int(8) unsigned,
1013 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
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 );
1032 function upgradeImagelinks() {
1033 global $wgUseLatin1;
1034 if ( $wgUseLatin1 ) {
1037 -- Key to page_id of the page containing the image / media link.
1038 il_from int(8) unsigned NOT NULL default '0',
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 '',
1045 UNIQUE KEY il_from(il_from,il_to),
1051 'il_from' => MW_UPGRADE_COPY
,
1052 'il_to' => MW_UPGRADE_ENCODE
);
1053 $this->copyTable( 'imagelinks', $tabledef, $fields );
1057 function upgradeCategorylinks() {
1058 global $wgUseLatin1;
1059 if ( $wgUseLatin1 ) {
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,
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)
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 );
1081 function upgradeIpblocks() {
1082 global $wgUseLatin1;
1083 if ( $wgUseLatin1 ) {
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 '',
1095 PRIMARY KEY ipb_id (ipb_id),
1096 INDEX ipb_address (ipb_address),
1097 INDEX ipb_user (ipb_user)
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 );
1114 function upgradeRecentchanges() {
1115 // There's a format change in the namespace field
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 '',
1122 rc_user int(10) unsigned NOT NULL default '0',
1123 rc_user_text varchar(255) binary NOT NULL default '',
1125 rc_namespace int NOT NULL default '0',
1126 rc_title varchar(255) binary NOT NULL default '',
1128 rc_comment varchar(255) binary NOT NULL default '',
1129 rc_minor tinyint(3) unsigned NOT NULL default '0',
1131 rc_bot tinyint(3) unsigned NOT NULL default '0',
1132 rc_new tinyint(3) unsigned NOT NULL default '0',
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',
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 '',
1142 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1144 rc_ip char(15) NOT NULL default '',
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),
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 );
1178 function upgradeQuerycache() {
1179 // There's a format change in the namespace field
1182 -- A key name, generally the base name of of the special page.
1183 qc_type char(32) NOT NULL,
1185 -- Some sort of stored value. Sizes, counts...
1186 qc_value int(5) unsigned NOT NULL default '0',
1188 -- Target namespace+title
1189 qc_namespace int NOT NULL default '0',
1190 qc_title char(255) binary NOT NULL default '',
1192 KEY (qc_type,qc_value)
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 );
1205 * Check for duplicate rows in "cur" table and move duplicates entries in
1208 * This was in cleanupDupes.inc before.
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";
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";
1221 echo wfWikiID() . ": cur table has the old non-unique index and may have duplicate entries.\n";
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
1231 GROUP BY cur_namespace,cur_title
1235 $n = $dbw->numRows( $res );
1236 echo "Found $n titles with duplicate entries.\n";
1238 echo "Correcting...\n";
1239 foreach ( $res as $row ) {
1240 $ns = intval( $row->cur_namespace
);
1241 $title = $dbw->addQuotes( $row->cur_title
);
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
) );
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";
1258 echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
1259 if ( $timeMin > $timeFirst ) {
1261 echo "keeping minimum: $id\n";
1263 echo "keeping first: $id\n";
1271 (old_namespace, old_title, old_text,
1272 old_comment, old_user, old_user_text,
1273 old_timestamp, old_minor_edit, old_flags,
1275 SELECT cur_namespace, cur_title, cur_text,
1276 cur_comment, cur_user, cur_user_text,
1277 cur_timestamp, cur_minor_edit, '',
1280 WHERE cur_namespace=$ns
1281 AND cur_title=$title
1288 WHERE cur_namespace=$ns
1289 AND cur_title=$title
1295 $dbw->query( 'UNLOCK TABLES' );
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.
1304 function upgradeCleanup() {
1305 $this->renameTable( 'old', 'text' );
1307 foreach ( $this->cleanupSwaps
as $table ) {
1308 $this->swap( $table );
1312 function renameTable( $from, $to ) {
1313 $this->log( "Renaming $from to $to..." );
1315 $fromtable = $this->dbw
->tableName( $from );
1316 $totable = $this->dbw
->tableName( $to );
1317 $this->dbw
->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1320 function swap( $base ) {
1321 $this->renameTable( $base, "{$base}_old" );
1322 $this->renameTable( "{$base}_temp", $base );
1327 $maintClass = 'FiveUpgrade';
1328 require( DO_MAINTENANCE
);