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
);
325 * Copy and transcode a table to table_temp.
326 * @param string $name Base name of the source table
327 * @param string $tabledef CREATE TABLE definition, w/ $1 for the name
328 * @param array $fields set of destination fields to these constants:
329 * MW_UPGRADE_COPY - straight copy
330 * MW_UPGRADE_ENCODE - for old Latin1 wikis, conv to UTF-8
331 * MW_UPGRADE_NULL - just put NULL
332 * @param callable $callback An optional callback to modify the data
333 * or perform other processing. Func should be
334 * ( object $row, array $copy ) and return $copy
337 function copyTable( $name, $tabledef, $fields, $callback = null ) {
338 $name_temp = $name . '_temp';
339 $this->log( "Migrating $name table to $name_temp..." );
341 $table_temp = $this->dbw
->tableName( $name_temp );
343 // Create temporary table; we're going to copy everything in there,
344 // then at the end rename the final tables into place.
345 $def = str_replace( '$1', $table_temp, $tabledef );
346 $this->dbw
->query( $def, __METHOD__
);
348 $numRecords = $this->dbw
->selectField( $name, 'COUNT(*)', '', __METHOD__
);
349 $this->setChunkScale( 100, $numRecords, $name_temp, __METHOD__
);
351 // Pull all records from the second, streaming database connection.
352 $sourceFields = array_keys( array_filter( $fields,
353 create_function( '$x', 'return $x !== MW_UPGRADE_NULL;' ) ) );
354 $result = $this->dbr
->select( $name,
360 foreach ( $result as $row ) {
362 foreach ( $fields as $field => $source ) {
363 if ( $source === MW_UPGRADE_COPY
) {
364 $copy[$field] = $row->$field;
365 } elseif ( $source === MW_UPGRADE_ENCODE
) {
366 $copy[$field] = $this->conv( $row->$field );
367 } elseif ( $source === MW_UPGRADE_NULL
) {
368 $copy[$field] = null;
370 $this->log( "Unknown field copy type: $field => $source" );
373 if ( is_callable( $callback ) ) {
374 $copy = call_user_func( $callback, $row, $copy );
377 $this->addChunk( $add );
379 $this->lastChunk( $add );
381 $this->log( "Done converting $name." );
382 $this->cleanupSwaps
[] = $name;
385 function upgradePage() {
388 if ( $this->dbw
->tableExists( 'page' ) ) {
389 $this->error( 'Page table already exists.', true );
392 $this->log( "Checking cur table for unique title index and applying if necessary" );
395 $this->log( "...converting from cur/old to page/revision/text DB structure." );
397 list ( $cur, $old, $page, $revision, $text ) = $this->dbw
->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
399 $this->log( "Creating page and revision tables..." );
400 $this->dbw
->query( "CREATE TABLE $page (
401 page_id int(8) unsigned NOT NULL auto_increment,
402 page_namespace int NOT NULL,
403 page_title varchar(255) binary NOT NULL,
404 page_restrictions tinyblob NOT NULL default '',
405 page_counter bigint(20) unsigned NOT NULL default '0',
406 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
407 page_is_new tinyint(1) unsigned NOT NULL default '0',
408 page_random real unsigned NOT NULL,
409 page_touched char(14) binary NOT NULL default '',
410 page_latest int(8) unsigned NOT NULL,
411 page_len int(8) unsigned NOT NULL,
413 PRIMARY KEY page_id (page_id),
414 UNIQUE INDEX name_title (page_namespace,page_title),
417 ) TYPE=InnoDB", __METHOD__
);
418 $this->dbw
->query( "CREATE TABLE $revision (
419 rev_id int(8) unsigned NOT NULL auto_increment,
420 rev_page int(8) unsigned NOT NULL,
421 rev_text_id int(8) unsigned NOT NULL,
422 rev_comment tinyblob NOT NULL default '',
423 rev_user int(5) unsigned NOT NULL default '0',
424 rev_user_text varchar(255) binary NOT NULL default '',
425 rev_timestamp char(14) binary NOT NULL default '',
426 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
427 rev_deleted tinyint(1) unsigned NOT NULL default '0',
429 PRIMARY KEY rev_page_id (rev_page, rev_id),
430 UNIQUE INDEX rev_id (rev_id),
431 INDEX rev_timestamp (rev_timestamp),
432 INDEX page_timestamp (rev_page,rev_timestamp),
433 INDEX user_timestamp (rev_user,rev_timestamp),
434 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
435 ) TYPE=InnoDB", __METHOD__
);
437 $maxold = intval( $this->dbw
->selectField( 'old', 'max(old_id)', '', __METHOD__
) );
438 $this->log( "Last old record is {$maxold}" );
440 global $wgLegacySchemaConversion;
441 if ( $wgLegacySchemaConversion ) {
442 // Create HistoryBlobCurStub entries.
443 // Text will be pulled from the leftover 'cur' table at runtime.
444 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
445 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
446 $cur_flags = "'object'";
448 // Copy all cur text in immediately: this may take longer but avoids
449 // having to keep an extra table around.
450 echo "......Moving text from cur.\n";
451 $cur_text = 'cur_text';
455 $maxcur = $this->dbw
->selectField( 'cur', 'max(cur_id)', '', __METHOD__
);
456 $this->log( "Last cur entry is $maxcur" );
459 * Copy placeholder records for each page's current version into old
460 * Don't do any conversion here; text records are converted at runtime
461 * based on the flags (and may be originally binary!) while the meta
462 * fields will be converted in the old -> rev and cur -> page steps.
464 $this->setChunkScale( $chunksize, $maxcur, 'old', __METHOD__
);
465 $result = $this->dbr
->query(
466 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
467 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
469 ORDER BY cur_id", __METHOD__
);
471 foreach ( $result as $row ) {
473 'old_namespace' => $row->cur_namespace
,
474 'old_title' => $row->cur_title
,
475 'old_text' => $row->text
,
476 'old_comment' => $row->cur_comment
,
477 'old_user' => $row->cur_user
,
478 'old_user_text' => $row->cur_user_text
,
479 'old_timestamp' => $row->cur_timestamp
,
480 'old_minor_edit' => $row->cur_minor_edit
,
481 'old_flags' => $row->flags
);
482 $this->addChunk( $add, $row->cur_id
);
484 $this->lastChunk( $add );
487 * Copy revision metadata from old into revision.
488 * We'll also do UTF-8 conversion of usernames and comments.
490 # $newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', __METHOD__ );
491 # $this->setChunkScale( $chunksize, $newmaxold, 'revision', __METHOD__ );
492 # $countold = $this->dbw->selectField( 'old', 'count(old_id)', '', __METHOD__ );
493 $countold = $this->dbw
->selectField( 'old', 'max(old_id)', '', __METHOD__
);
494 $this->setChunkScale( $chunksize, $countold, 'revision', __METHOD__
);
496 $this->log( "......Setting up revision table." );
497 $result = $this->dbr
->query(
498 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
499 old_timestamp, old_minor_edit
500 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
504 foreach ( $result as $row ) {
506 'rev_id' => $row->old_id
,
507 'rev_page' => $row->cur_id
,
508 'rev_text_id' => $row->old_id
,
509 'rev_comment' => $this->conv( $row->old_comment
),
510 'rev_user' => $row->old_user
,
511 'rev_user_text' => $this->conv( $row->old_user_text
),
512 'rev_timestamp' => $row->old_timestamp
,
513 'rev_minor_edit' => $row->old_minor_edit
);
514 $this->addChunk( $add );
516 $this->lastChunk( $add );
520 * Copy page metadata from cur into page.
521 * We'll also do UTF-8 conversion of titles.
523 $this->log( "......Setting up page table." );
524 $this->setChunkScale( $chunksize, $maxcur, 'page', __METHOD__
);
525 $result = $this->dbr
->query( "
526 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
527 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
529 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
530 ORDER BY cur_id", __METHOD__
);
532 foreach ( $result as $row ) {
534 'page_id' => $row->cur_id
,
535 'page_namespace' => $row->cur_namespace
,
536 'page_title' => $this->conv( $row->cur_title
),
537 'page_restrictions' => $row->cur_restrictions
,
538 'page_counter' => $row->cur_counter
,
539 'page_is_redirect' => $row->cur_is_redirect
,
540 'page_is_new' => $row->cur_is_new
,
541 'page_random' => $row->cur_random
,
542 'page_touched' => $this->dbw
->timestamp(),
543 'page_latest' => $row->rev_id
,
544 'page_len' => $row->len
);
545 # $this->addChunk( $add, $row->cur_id );
546 $this->addChunk( $add );
548 $this->lastChunk( $add );
550 $this->log( "...done with cur/old -> page/revision." );
553 function upgradeLinks() {
555 list ( $links, $brokenlinks, $pagelinks, $cur ) = $this->dbw
->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
557 $this->log( 'Checking for interwiki table change in case of bogus items...' );
558 if ( $this->dbw
->fieldExists( 'interwiki', 'iw_trans' ) ) {
559 $this->log( 'interwiki has iw_trans.' );
562 $this->log( 'adding iw_trans...' );
563 $this->dbw
->sourceFile( $IP . '/maintenance/archives/patch-interwiki-trans.sql' );
564 $this->log( 'added iw_trans.' );
567 $this->log( 'Creating pagelinks table...' );
569 CREATE TABLE $pagelinks (
570 -- Key to the page_id of the page containing the link.
571 pl_from int(8) unsigned NOT NULL default '0',
573 -- Key to page_namespace/page_title of the target page.
574 -- The target page may or may not exist, and due to renames
575 -- and deletions may refer to different page records as time
577 pl_namespace int NOT NULL default '0',
578 pl_title varchar(255) binary NOT NULL default '',
580 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
581 KEY (pl_namespace,pl_title)
585 $this->log( 'Importing live links -> pagelinks' );
586 $nlinks = $this->dbw
->selectField( 'links', 'count(*)', '', __METHOD__
);
588 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', __METHOD__
);
589 $result = $this->dbr
->query( "
590 SELECT l_from,cur_namespace,cur_title
592 WHERE l_to=cur_id", __METHOD__
);
594 foreach ( $result as $row ) {
596 'pl_from' => $row->l_from
,
597 'pl_namespace' => $row->cur_namespace
,
598 'pl_title' => $this->conv( $row->cur_title
) );
599 $this->addChunk( $add );
601 $this->lastChunk( $add );
603 $this->log( 'no links!' );
606 $this->log( 'Importing brokenlinks -> pagelinks' );
607 $nbrokenlinks = $this->dbw
->selectField( 'brokenlinks', 'count(*)', '', __METHOD__
);
608 if ( $nbrokenlinks ) {
609 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', __METHOD__
);
610 $result = $this->dbr
->query(
611 "SELECT bl_from, bl_to FROM $brokenlinks",
614 foreach ( $result as $row ) {
615 $pagename = $this->conv( $row->bl_to
);
616 $title = Title
::newFromText( $pagename );
617 if ( is_null( $title ) ) {
618 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
621 'pl_from' => $row->bl_from
,
622 'pl_namespace' => $title->getNamespace(),
623 'pl_title' => $title->getDBkey() );
624 $this->addChunk( $add );
627 $this->lastChunk( $add );
629 $this->log( 'no brokenlinks!' );
632 $this->log( 'Done with links.' );
635 function upgradeUser() {
636 // Apply unique index, if necessary:
637 $duper = new UserDupes( $this->dbw
);
638 if ( $duper->hasUniqueIndex() ) {
639 $this->log( "Already have unique user_name index." );
641 $this->log( "Clearing user duplicates..." );
642 if ( !$duper->clearDupes() ) {
643 $this->log( "WARNING: Duplicate user accounts, may explode!" );
649 user_id int(5) unsigned NOT NULL auto_increment,
650 user_name varchar(255) binary NOT NULL default '',
651 user_real_name varchar(255) binary NOT NULL default '',
652 user_password tinyblob NOT NULL default '',
653 user_newpassword tinyblob NOT NULL default '',
654 user_email tinytext NOT NULL default '',
655 user_options blob NOT NULL default '',
656 user_touched char(14) binary NOT NULL default '',
657 user_token char(32) binary NOT NULL default '',
658 user_email_authenticated CHAR(14) BINARY,
659 user_email_token CHAR(32) BINARY,
660 user_email_token_expires CHAR(14) BINARY,
662 PRIMARY KEY user_id (user_id),
663 UNIQUE INDEX user_name (user_name),
664 INDEX (user_email_token)
669 'user_id' => MW_UPGRADE_COPY
,
670 'user_name' => MW_UPGRADE_ENCODE
,
671 'user_real_name' => MW_UPGRADE_ENCODE
,
672 'user_password' => MW_UPGRADE_COPY
,
673 'user_newpassword' => MW_UPGRADE_COPY
,
674 'user_email' => MW_UPGRADE_ENCODE
,
675 'user_options' => MW_UPGRADE_ENCODE
,
676 'user_touched' => MW_UPGRADE_CALLBACK
,
677 'user_token' => MW_UPGRADE_COPY
,
678 'user_email_authenticated' => MW_UPGRADE_CALLBACK
,
679 'user_email_token' => MW_UPGRADE_NULL
,
680 'user_email_token_expires' => MW_UPGRADE_NULL
);
681 $this->copyTable( 'user', $tabledef, $fields,
682 array( &$this, 'userCallback' ) );
685 function userCallback( $row, $copy ) {
686 $now = $this->dbw
->timestamp();
687 $copy['user_touched'] = $now;
688 $copy['user_email_authenticated'] = $this->emailAuth ?
$now : null;
692 function upgradeImage() {
695 img_name varchar(255) binary NOT NULL default '',
696 img_size int(8) unsigned NOT NULL default '0',
697 img_width int(5) NOT NULL default '0',
698 img_height int(5) NOT NULL default '0',
699 img_metadata mediumblob NOT NULL,
700 img_bits int(3) NOT NULL default '0',
701 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
702 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
703 img_minor_mime varchar(32) NOT NULL default "unknown",
704 img_description tinyblob NOT NULL default '',
705 img_user int(5) unsigned NOT NULL default '0',
706 img_user_text varchar(255) binary NOT NULL default '',
707 img_timestamp char(14) binary NOT NULL default '',
709 PRIMARY KEY img_name (img_name),
710 INDEX img_size (img_size),
711 INDEX img_timestamp (img_timestamp)
715 'img_name' => MW_UPGRADE_ENCODE
,
716 'img_size' => MW_UPGRADE_COPY
,
717 'img_width' => MW_UPGRADE_CALLBACK
,
718 'img_height' => MW_UPGRADE_CALLBACK
,
719 'img_metadata' => MW_UPGRADE_CALLBACK
,
720 'img_bits' => MW_UPGRADE_CALLBACK
,
721 'img_media_type' => MW_UPGRADE_CALLBACK
,
722 'img_major_mime' => MW_UPGRADE_CALLBACK
,
723 'img_minor_mime' => MW_UPGRADE_CALLBACK
,
724 'img_description' => MW_UPGRADE_ENCODE
,
725 'img_user' => MW_UPGRADE_COPY
,
726 'img_user_text' => MW_UPGRADE_ENCODE
,
727 'img_timestamp' => MW_UPGRADE_COPY
);
728 $this->copyTable( 'image', $tabledef, $fields,
729 array( &$this, 'imageCallback' ) );
732 function imageCallback( $row, $copy ) {
733 if ( !$this->hasOption( 'noimage' ) ) {
734 // Fill in the new image info fields
735 $info = $this->imageInfo( $row->img_name
);
737 $copy['img_width' ] = $info['width'];
738 $copy['img_height' ] = $info['height'];
739 $copy['img_metadata' ] = ""; // loaded on-demand
740 $copy['img_bits' ] = $info['bits'];
741 $copy['img_media_type'] = $info['media'];
742 $copy['img_major_mime'] = $info['major'];
743 $copy['img_minor_mime'] = $info['minor'];
746 // If doing UTF8 conversion the file must be renamed
747 $this->renameFile( $row->img_name
, 'wfImageDir' );
752 function imageInfo( $filename ) {
761 $magic = MimeMagic
::singleton();
762 $mime = $magic->guessMimeType( $filename, true );
763 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
765 $info['media'] = $magic->getMediaType( $filename, $mime );
767 $image = UnregisteredLocalFile
::newFromPath( $filename, $mime );
769 $info['width'] = $image->getWidth();
770 $info['height'] = $image->getHeight();
772 $gis = $image->getImageSize( $filename );
773 if ( isset( $gis['bits'] ) ) {
774 $info['bits'] = $gis['bits'];
783 * @param string $table The table name to be truncated
785 function clearTable( $table ) {
786 print "Clearing $table...\n";
787 $tableName = $this->db
->tableName( $table );
788 $this->db
->query( "TRUNCATE $tableName" );
792 * Rename a given image or archived image file to the converted filename,
793 * leaving a symlink for URL compatibility.
795 * @param string $oldname pre-conversion filename
796 * @param string $basename pre-conversion base filename for dir hashing, if an archive
799 function renameFile( $oldname, $subdirCallback = 'wfImageDir', $basename = null ) {
800 $newname = $this->conv( $oldname );
801 if ( $newname == $oldname ) {
802 // No need to rename; another field triggered this row.
806 if ( is_null( $basename ) ) $basename = $oldname;
807 $ubasename = $this->conv( $basename );
808 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
809 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
811 $this->log( "$oldpath -> $newpath" );
812 if ( rename( $oldpath, $newpath ) ) {
813 $relpath = wfRelativePath( $newpath, dirname( $oldpath ) );
814 if ( !symlink( $relpath, $oldpath ) ) {
815 $this->log( "... symlink failed!" );
819 $this->log( "... rename failed!" );
824 function upgradeOldImage() {
827 -- Base filename: key to image.img_name
828 oi_name varchar(255) binary NOT NULL default '',
830 -- Filename of the archived file.
831 -- This is generally a timestamp and '!' prepended to the base name.
832 oi_archive_name varchar(255) binary NOT NULL default '',
834 -- Other fields as in image...
835 oi_size int(8) unsigned NOT NULL default 0,
836 oi_width int(5) NOT NULL default 0,
837 oi_height int(5) NOT NULL default 0,
838 oi_bits int(3) NOT NULL default 0,
839 oi_description tinyblob NOT NULL default '',
840 oi_user int(5) unsigned NOT NULL default '0',
841 oi_user_text varchar(255) binary NOT NULL default '',
842 oi_timestamp char(14) binary NOT NULL default '',
844 INDEX oi_name (oi_name(10))
849 'oi_name' => MW_UPGRADE_ENCODE
,
850 'oi_archive_name' => MW_UPGRADE_ENCODE
,
851 'oi_size' => MW_UPGRADE_COPY
,
852 'oi_width' => MW_UPGRADE_CALLBACK
,
853 'oi_height' => MW_UPGRADE_CALLBACK
,
854 'oi_bits' => MW_UPGRADE_CALLBACK
,
855 'oi_description' => MW_UPGRADE_ENCODE
,
856 'oi_user' => MW_UPGRADE_COPY
,
857 'oi_user_text' => MW_UPGRADE_ENCODE
,
858 'oi_timestamp' => MW_UPGRADE_COPY
);
859 $this->copyTable( 'oldimage', $tabledef, $fields,
860 array( &$this, 'oldimageCallback' ) );
863 function oldimageCallback( $row, $copy ) {
865 if ( !isset( $options['noimage'] ) ) {
866 // Fill in the new image info fields
867 $info = $this->imageInfo( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
868 $copy['oi_width' ] = $info['width' ];
869 $copy['oi_height'] = $info['height'];
870 $copy['oi_bits' ] = $info['bits' ];
873 // If doing UTF8 conversion the file must be renamed
874 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
880 function upgradeWatchlist() {
883 list ( $watchlist, $watchlist_temp ) = $this->dbw
->tableNamesN( 'watchlist', 'watchlist_temp' );
885 $this->log( 'Migrating watchlist table to watchlist_temp...' );
887 "CREATE TABLE $watchlist_temp (
889 wl_user int(5) unsigned NOT NULL,
891 -- Key to page_namespace/page_title
892 -- Note that users may watch patches which do not exist yet,
893 -- or existed in the past but have been deleted.
894 wl_namespace int NOT NULL default '0',
895 wl_title varchar(255) binary NOT NULL default '',
897 -- Timestamp when user was last sent a notification e-mail;
898 -- cleared when the user visits the page.
899 -- FIXME: add proper null support etc
900 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
902 UNIQUE KEY (wl_user, wl_namespace, wl_title),
903 KEY namespace_title (wl_namespace,wl_title)
905 ) TYPE=InnoDB;", __METHOD__
);
907 // Fix encoding for Latin-1 upgrades, add some fields,
908 // and double article to article+talk pairs
909 $numwatched = $this->dbw
->selectField( 'watchlist', 'count(*)', '', __METHOD__
);
911 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', __METHOD__
);
912 $result = $this->dbr
->select( 'watchlist',
921 foreach ( $result as $row ) {
923 'wl_user' => $row->wl_user
,
924 'wl_namespace' => MWNamespace
::getSubject( $row->wl_namespace
),
925 'wl_title' => $this->conv( $row->wl_title
),
926 'wl_notificationtimestamp' => '0' );
927 $this->addChunk( $add );
930 'wl_user' => $row->wl_user
,
931 'wl_namespace' => MWNamespace
::getTalk( $row->wl_namespace
),
932 'wl_title' => $this->conv( $row->wl_title
),
933 'wl_notificationtimestamp' => '0' );
934 $this->addChunk( $add );
936 $this->lastChunk( $add );
938 $this->log( 'Done converting watchlist.' );
939 $this->cleanupSwaps
[] = 'watchlist';
942 function upgradeLogging() {
945 -- Symbolic keys for the general log type and the action type
946 -- within the log. The output format will be controlled by the
947 -- action field, but only the type controls categorization.
948 log_type char(10) NOT NULL default '',
949 log_action char(10) NOT NULL default '',
952 log_timestamp char(14) NOT NULL default '19700101000000',
954 -- The user who performed this action; key to user_id
955 log_user int unsigned NOT NULL default 0,
957 -- Key to the page affected. Where a user is the target,
958 -- this will point to the user page.
959 log_namespace int NOT NULL default 0,
960 log_title varchar(255) binary NOT NULL default '',
962 -- Freeform text. Interpreted as edit history comments.
963 log_comment varchar(255) NOT NULL default '',
965 -- LF separated list of miscellaneous parameters
966 log_params blob NOT NULL default '',
968 KEY type_time (log_type, log_timestamp),
969 KEY user_time (log_user, log_timestamp),
970 KEY page_time (log_namespace, log_title, log_timestamp)
975 'log_type' => MW_UPGRADE_COPY
,
976 'log_action' => MW_UPGRADE_COPY
,
977 'log_timestamp' => MW_UPGRADE_COPY
,
978 'log_user' => MW_UPGRADE_COPY
,
979 'log_namespace' => MW_UPGRADE_COPY
,
980 'log_title' => MW_UPGRADE_ENCODE
,
981 'log_comment' => MW_UPGRADE_ENCODE
,
982 'log_params' => MW_UPGRADE_ENCODE
);
983 $this->copyTable( 'logging', $tabledef, $fields );
986 function upgradeArchive() {
989 ar_namespace int NOT NULL default '0',
990 ar_title varchar(255) binary NOT NULL default '',
991 ar_text mediumblob NOT NULL default '',
993 ar_comment tinyblob NOT NULL default '',
994 ar_user int(5) unsigned NOT NULL default '0',
995 ar_user_text varchar(255) binary NOT NULL,
996 ar_timestamp char(14) binary NOT NULL default '',
997 ar_minor_edit tinyint(1) NOT NULL default '0',
999 ar_flags tinyblob NOT NULL default '',
1001 ar_rev_id int(8) unsigned,
1002 ar_text_id int(8) unsigned,
1004 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
1009 'ar_namespace' => MW_UPGRADE_COPY
,
1010 'ar_title' => MW_UPGRADE_ENCODE
,
1011 'ar_text' => MW_UPGRADE_COPY
,
1012 'ar_comment' => MW_UPGRADE_ENCODE
,
1013 'ar_user' => MW_UPGRADE_COPY
,
1014 'ar_user_text' => MW_UPGRADE_ENCODE
,
1015 'ar_timestamp' => MW_UPGRADE_COPY
,
1016 'ar_minor_edit' => MW_UPGRADE_COPY
,
1017 'ar_flags' => MW_UPGRADE_COPY
,
1018 'ar_rev_id' => MW_UPGRADE_NULL
,
1019 'ar_text_id' => MW_UPGRADE_NULL
);
1020 $this->copyTable( 'archive', $tabledef, $fields );
1023 function upgradeImagelinks() {
1024 global $wgUseLatin1;
1025 if ( $wgUseLatin1 ) {
1028 -- Key to page_id of the page containing the image / media link.
1029 il_from int(8) unsigned NOT NULL default '0',
1031 -- Filename of target image.
1032 -- This is also the page_title of the file's description page;
1033 -- all such pages are in namespace 6 (NS_FILE).
1034 il_to varchar(255) binary NOT NULL default '',
1036 UNIQUE KEY il_from(il_from,il_to),
1042 'il_from' => MW_UPGRADE_COPY
,
1043 'il_to' => MW_UPGRADE_ENCODE
);
1044 $this->copyTable( 'imagelinks', $tabledef, $fields );
1048 function upgradeCategorylinks() {
1049 global $wgUseLatin1;
1050 if ( $wgUseLatin1 ) {
1053 cl_from int(8) unsigned NOT NULL default '0',
1054 cl_to varchar(255) binary NOT NULL default '',
1055 cl_sortkey varchar(86) binary NOT NULL default '',
1056 cl_timestamp timestamp NOT NULL,
1058 UNIQUE KEY cl_from(cl_from,cl_to),
1059 KEY cl_sortkey(cl_to,cl_sortkey),
1060 KEY cl_timestamp(cl_to,cl_timestamp)
1064 'cl_from' => MW_UPGRADE_COPY
,
1065 'cl_to' => MW_UPGRADE_ENCODE
,
1066 'cl_sortkey' => MW_UPGRADE_ENCODE
,
1067 'cl_timestamp' => MW_UPGRADE_COPY
);
1068 $this->copyTable( 'categorylinks', $tabledef, $fields );
1072 function upgradeIpblocks() {
1073 global $wgUseLatin1;
1074 if ( $wgUseLatin1 ) {
1077 ipb_id int(8) NOT NULL auto_increment,
1078 ipb_address varchar(40) binary NOT NULL default '',
1079 ipb_user int(8) unsigned NOT NULL default '0',
1080 ipb_by int(8) unsigned NOT NULL default '0',
1081 ipb_reason tinyblob NOT NULL default '',
1082 ipb_timestamp char(14) binary NOT NULL default '',
1083 ipb_auto tinyint(1) NOT NULL default '0',
1084 ipb_expiry char(14) binary NOT NULL default '',
1086 PRIMARY KEY ipb_id (ipb_id),
1087 INDEX ipb_address (ipb_address),
1088 INDEX ipb_user (ipb_user)
1093 'ipb_id' => MW_UPGRADE_COPY
,
1094 'ipb_address' => MW_UPGRADE_COPY
,
1095 'ipb_user' => MW_UPGRADE_COPY
,
1096 'ipb_by' => MW_UPGRADE_COPY
,
1097 'ipb_reason' => MW_UPGRADE_ENCODE
,
1098 'ipb_timestamp' => MW_UPGRADE_COPY
,
1099 'ipb_auto' => MW_UPGRADE_COPY
,
1100 'ipb_expiry' => MW_UPGRADE_COPY
);
1101 $this->copyTable( 'ipblocks', $tabledef, $fields );
1105 function upgradeRecentchanges() {
1106 // There's a format change in the namespace field
1109 rc_id int(8) NOT NULL auto_increment,
1110 rc_timestamp varchar(14) binary NOT NULL default '',
1111 rc_cur_time varchar(14) binary NOT NULL default '',
1113 rc_user int(10) unsigned NOT NULL default '0',
1114 rc_user_text varchar(255) binary NOT NULL default '',
1116 rc_namespace int NOT NULL default '0',
1117 rc_title varchar(255) binary NOT NULL default '',
1119 rc_comment varchar(255) binary NOT NULL default '',
1120 rc_minor tinyint(3) unsigned NOT NULL default '0',
1122 rc_bot tinyint(3) unsigned NOT NULL default '0',
1123 rc_new tinyint(3) unsigned NOT NULL default '0',
1125 rc_cur_id int(10) unsigned NOT NULL default '0',
1126 rc_this_oldid int(10) unsigned NOT NULL default '0',
1127 rc_last_oldid int(10) unsigned NOT NULL default '0',
1129 rc_type tinyint(3) unsigned NOT NULL default '0',
1130 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
1131 rc_moved_to_title varchar(255) binary NOT NULL default '',
1133 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1135 rc_ip char(15) NOT NULL default '',
1137 PRIMARY KEY rc_id (rc_id),
1138 INDEX rc_timestamp (rc_timestamp),
1139 INDEX rc_namespace_title (rc_namespace, rc_title),
1140 INDEX rc_cur_id (rc_cur_id),
1141 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
1147 'rc_id' => MW_UPGRADE_COPY
,
1148 'rc_timestamp' => MW_UPGRADE_COPY
,
1149 'rc_cur_time' => MW_UPGRADE_COPY
,
1150 'rc_user' => MW_UPGRADE_COPY
,
1151 'rc_user_text' => MW_UPGRADE_ENCODE
,
1152 'rc_namespace' => MW_UPGRADE_COPY
,
1153 'rc_title' => MW_UPGRADE_ENCODE
,
1154 'rc_comment' => MW_UPGRADE_ENCODE
,
1155 'rc_minor' => MW_UPGRADE_COPY
,
1156 'rc_bot' => MW_UPGRADE_COPY
,
1157 'rc_new' => MW_UPGRADE_COPY
,
1158 'rc_cur_id' => MW_UPGRADE_COPY
,
1159 'rc_this_oldid' => MW_UPGRADE_COPY
,
1160 'rc_last_oldid' => MW_UPGRADE_COPY
,
1161 'rc_type' => MW_UPGRADE_COPY
,
1162 'rc_moved_to_ns' => MW_UPGRADE_COPY
,
1163 'rc_moved_to_title' => MW_UPGRADE_ENCODE
,
1164 'rc_patrolled' => MW_UPGRADE_COPY
,
1165 'rc_ip' => MW_UPGRADE_COPY
);
1166 $this->copyTable( 'recentchanges', $tabledef, $fields );
1169 function upgradeQuerycache() {
1170 // There's a format change in the namespace field
1173 -- A key name, generally the base name of of the special page.
1174 qc_type char(32) NOT NULL,
1176 -- Some sort of stored value. Sizes, counts...
1177 qc_value int(5) unsigned NOT NULL default '0',
1179 -- Target namespace+title
1180 qc_namespace int NOT NULL default '0',
1181 qc_title char(255) binary NOT NULL default '',
1183 KEY (qc_type,qc_value)
1188 'qc_type' => MW_UPGRADE_COPY
,
1189 'qc_value' => MW_UPGRADE_COPY
,
1190 'qc_namespace' => MW_UPGRADE_COPY
,
1191 'qc_title' => MW_UPGRADE_ENCODE
);
1192 $this->copyTable( 'querycache', $tabledef, $fields );
1196 * Check for duplicate rows in "cur" table and move duplicates entries in
1199 * This was in cleanupDupes.inc before.
1201 function checkDupes() {
1202 $dbw = wfGetDB( DB_MASTER
);
1203 if ( $dbw->indexExists( 'cur', 'name_title' ) &&
1204 $dbw->indexUnique( 'cur', 'name_title' ) ) {
1205 echo wfWikiID() . ": cur table has the current unique index; no duplicate entries.\n";
1207 } elseif ( $dbw->indexExists( 'cur', 'name_title_dup_prevention' ) ) {
1208 echo wfWikiID() . ": cur table has a temporary name_title_dup_prevention unique index; no duplicate entries.\n";
1212 echo wfWikiID() . ": cur table has the old non-unique index and may have duplicate entries.\n";
1214 $dbw = wfGetDB( DB_MASTER
);
1215 $cur = $dbw->tableName( 'cur' );
1216 $old = $dbw->tableName( 'old' );
1217 $dbw->query( "LOCK TABLES $cur WRITE, $old WRITE" );
1218 echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n";
1219 $res = $dbw->query( <<<END
1220 SELECT cur_namespace,cur_title,count(*) as c,min(cur_id) as id
1222 GROUP BY cur_namespace,cur_title
1226 $n = $dbw->numRows( $res );
1227 echo "Found $n titles with duplicate entries.\n";
1229 echo "Correcting...\n";
1230 foreach ( $res as $row ) {
1231 $ns = intval( $row->cur_namespace
);
1232 $title = $dbw->addQuotes( $row->cur_title
);
1234 # Get the first responding ID; that'll be the one we keep.
1235 $id = $dbw->selectField( 'cur', 'cur_id', array(
1236 'cur_namespace' => $row->cur_namespace
,
1237 'cur_title' => $row->cur_title
) );
1239 echo "$ns:$row->cur_title (canonical ID $id)\n";
1240 if ( $id != $row->id
) {
1241 echo " ** minimum ID $row->id; ";
1242 $timeMin = $dbw->selectField( 'cur', 'cur_timestamp', array(
1243 'cur_id' => $row->id
) );
1244 $timeFirst = $dbw->selectField( 'cur', 'cur_timestamp', array(
1245 'cur_id' => $id ) );
1246 if ( $timeMin == $timeFirst ) {
1247 echo "timestamps match at $timeFirst; ok\n";
1249 echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
1250 if ( $timeMin > $timeFirst ) {
1252 echo "keeping minimum: $id\n";
1254 echo "keeping first: $id\n";
1262 (old_namespace, old_title, old_text,
1263 old_comment, old_user, old_user_text,
1264 old_timestamp, old_minor_edit, old_flags,
1266 SELECT cur_namespace, cur_title, cur_text,
1267 cur_comment, cur_user, cur_user_text,
1268 cur_timestamp, cur_minor_edit, '',
1271 WHERE cur_namespace=$ns
1272 AND cur_title=$title
1279 WHERE cur_namespace=$ns
1280 AND cur_title=$title
1286 $dbw->query( 'UNLOCK TABLES' );
1291 * Rename all our temporary tables into final place.
1292 * We've left things in place so a read-only wiki can continue running
1293 * on the old code during all this.
1295 function upgradeCleanup() {
1296 $this->renameTable( 'old', 'text' );
1298 foreach ( $this->cleanupSwaps
as $table ) {
1299 $this->swap( $table );
1303 function renameTable( $from, $to ) {
1304 $this->log( "Renaming $from to $to..." );
1306 $fromtable = $this->dbw
->tableName( $from );
1307 $totable = $this->dbw
->tableName( $to );
1308 $this->dbw
->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1311 function swap( $base ) {
1312 $this->renameTable( $base, "{$base}_old" );
1313 $this->renameTable( "{$base}_temp", $base );
1318 $maintClass = 'FiveUpgrade';
1319 require( DO_MAINTENANCE
);