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->step
= $this->getOption( 'step', null );
107 function doing( $step ) {
108 return is_null( $this->step
) ||
$step == $this->step
;
112 * Open a connection to the master server with the admin rights.
116 function newConnection() {
117 $lb = wfGetLBFactory()->newMainLB();
118 $db = $lb->getConnection( DB_MASTER
);
120 $this->loadBalancers
[] = $lb;
125 * Commit transactions and close the connections when we're done...
128 foreach ( $this->loadBalancers
as $lb ) {
129 $lb->commitMasterChanges();
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
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" );
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
161 function prepareWindows1252() {
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
199 for ( $i = 0; $i < 0x100; $i++
) {
200 $unicode = isset( $cp1252[$i] ) ?
$cp1252[$i] : $i;
201 $pairs[chr( $i )] = codepointToUtf8( $unicode );
207 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
208 * @param string $text
212 function conv( $text ) {
214 return is_null( $text )
217 ?
strtr( $text, $this->conversionTables
)
222 * Dump timestamp and message to output
223 * @param $message String
226 function log( $message ) {
227 $this->output( wfWikiID() . ' ' . wfTimestamp( TS_DB
) . ': ' . $message . "\n" );
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.
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
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;
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()
265 function addChunk( &$chunk, $key = null ) {
266 if ( count( $chunk ) >= $this->chunkSize
) {
267 $this->insertChunk( $chunk );
269 $this->chunkCount +
= count( $chunk );
271 $delta = $now - $this->chunkStartTime
;
272 $rate = $this->chunkCount
/ $delta;
274 if ( is_null( $key ) ) {
275 $completed = $this->chunkCount
;
279 $portion = $completed / $this->chunkFinal
;
281 $estimatedTotalTime = $delta / $portion;
282 $eta = $this->chunkStartTime +
$estimatedTotalTime;
284 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
285 wfTimestamp( TS_DB
, intval( $now ) ),
288 wfTimestamp( TS_DB
, intval( $eta ) ),
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.
303 function lastChunk( &$chunk ) {
304 $n = count( $chunk );
306 $this->insertChunk( $chunk );
308 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
312 * Chunked inserts: perform an insert.
313 * @param array &$chunk -- This will be emptied if an insert is done.
316 function insertChunk( &$chunk ) {
317 // Give slaves a chance to catch up
319 $this->dbw
->insert( $this->chunkTable
, $chunk, $this->chunkFunction
, $this->chunkOptions
);
323 * Helper function for copyTable array_filter
325 static private function notUpgradeNull( $x ) {
326 return $x !== MW_UPGRADE_NULL
;
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
342 function copyTable( $name, $tabledef, $fields, $callback = null ) {
343 $name_temp = $name . '_temp';
344 $this->log( "Migrating $name table to $name_temp..." );
346 $table_temp = $this->dbw
->tableName( $name_temp );
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__
);
353 $numRecords = $this->dbw
->selectField( $name, 'COUNT(*)', '', __METHOD__
);
354 $this->setChunkScale( 100, $numRecords, $name_temp, __METHOD__
);
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,
364 foreach ( $result as $row ) {
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;
374 $this->log( "Unknown field copy type: $field => $source" );
377 if ( is_callable( $callback ) ) {
378 $copy = call_user_func( $callback, $row, $copy );
381 $this->addChunk( $add );
383 $this->lastChunk( $add );
385 $this->log( "Done converting $name." );
386 $this->cleanupSwaps
[] = $name;
389 function upgradePage() {
392 if ( $this->dbw
->tableExists( 'page' ) ) {
393 $this->error( 'Page table already exists.', true );
396 $this->log( "Checking cur table for unique title index and applying if necessary" );
399 $this->log( "...converting from cur/old to page/revision/text DB structure." );
401 list ( $cur, $old, $page, $revision, $text ) = $this->dbw
->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
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,
417 PRIMARY KEY page_id (page_id),
418 UNIQUE INDEX name_title (page_namespace,page_title),
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',
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__
);
441 $maxold = intval( $this->dbw
->selectField( 'old', 'max(old_id)', '', __METHOD__
) );
442 $this->log( "Last old record is {$maxold}" );
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'";
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';
459 $maxcur = $this->dbw
->selectField( 'cur', 'max(cur_id)', '', __METHOD__
);
460 $this->log( "Last cur entry is $maxcur" );
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.
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
473 ORDER BY cur_id", __METHOD__
);
475 foreach ( $result as $row ) {
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
);
488 $this->lastChunk( $add );
491 * Copy revision metadata from old into revision.
492 * We'll also do UTF-8 conversion of usernames and comments.
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__
);
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",
508 foreach ( $result as $row ) {
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 );
520 $this->lastChunk( $add );
524 * Copy page metadata from cur into page.
525 * We'll also do UTF-8 conversion of titles.
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
533 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
534 ORDER BY cur_id", __METHOD__
);
536 foreach ( $result as $row ) {
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 );
552 $this->lastChunk( $add );
554 $this->log( "...done with cur/old -> page/revision." );
557 function upgradeLinks() {
559 list ( $links, $brokenlinks, $pagelinks, $cur ) = $this->dbw
->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
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.' );
566 $this->log( 'adding iw_trans...' );
567 $this->dbw
->sourceFile( $IP . '/maintenance/archives/patch-interwiki-trans.sql' );
568 $this->log( 'added iw_trans.' );
571 $this->log( 'Creating pagelinks table...' );
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',
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
581 pl_namespace int NOT NULL default '0',
582 pl_title varchar(255) binary NOT NULL default '',
584 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
585 KEY (pl_namespace,pl_title)
589 $this->log( 'Importing live links -> pagelinks' );
590 $nlinks = $this->dbw
->selectField( 'links', 'count(*)', '', __METHOD__
);
592 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', __METHOD__
);
593 $result = $this->dbr
->query( "
594 SELECT l_from,cur_namespace,cur_title
596 WHERE l_to=cur_id", __METHOD__
);
598 foreach ( $result as $row ) {
600 'pl_from' => $row->l_from
,
601 'pl_namespace' => $row->cur_namespace
,
602 'pl_title' => $this->conv( $row->cur_title
) );
603 $this->addChunk( $add );
605 $this->lastChunk( $add );
607 $this->log( 'no links!' );
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",
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')" );
625 'pl_from' => $row->bl_from
,
626 'pl_namespace' => $title->getNamespace(),
627 'pl_title' => $title->getDBkey() );
628 $this->addChunk( $add );
631 $this->lastChunk( $add );
633 $this->log( 'no brokenlinks!' );
636 $this->log( 'Done with links.' );
639 function userDupeCallback( $str ) {
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." );
649 $this->log( "Clearing user duplicates..." );
650 if ( !$duper->clearDupes() ) {
651 $this->log( "WARNING: Duplicate user accounts, may explode!" );
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,
670 PRIMARY KEY user_id (user_id),
671 UNIQUE INDEX user_name (user_name),
672 INDEX (user_email_token)
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' ) );
693 function userCallback( $row, $copy ) {
694 $now = $this->dbw
->timestamp();
695 $copy['user_touched'] = $now;
696 $copy['user_email_authenticated'] = $this->emailAuth ?
$now : null;
700 function upgradeImage() {
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 '',
717 PRIMARY KEY img_name (img_name),
718 INDEX img_size (img_size),
719 INDEX img_timestamp (img_timestamp)
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' ) );
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
);
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'];
754 // If doing UTF8 conversion the file must be renamed
755 $this->renameFile( $row->img_name
, 'wfImageDir' );
760 function imageInfo( $filename ) {
769 $magic = MimeMagic
::singleton();
770 $mime = $magic->guessMimeType( $filename, true );
771 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
773 $info['media'] = $magic->getMediaType( $filename, $mime );
775 $image = UnregisteredLocalFile
::newFromPath( $filename, $mime );
777 $info['width'] = $image->getWidth();
778 $info['height'] = $image->getHeight();
780 $gis = $image->getImageSize( $filename );
781 if ( isset( $gis['bits'] ) ) {
782 $info['bits'] = $gis['bits'];
791 * @param string $table The table name to be truncated
793 function clearTable( $table ) {
794 print "Clearing $table...\n";
795 $tableName = $this->db
->tableName( $table );
796 $this->db
->query( "TRUNCATE $tableName" );
800 * Rename a given image or archived image file to the converted filename,
801 * leaving a symlink for URL compatibility.
803 * @param string $oldname pre-conversion filename
804 * @param string $basename pre-conversion base filename for dir hashing, if an archive
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.
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;
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!" );
827 $this->log( "... rename failed!" );
832 function upgradeOldImage() {
835 -- Base filename: key to image.img_name
836 oi_name varchar(255) binary NOT NULL default '',
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 '',
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 '',
852 INDEX oi_name (oi_name(10))
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' ) );
871 function oldimageCallback( $row, $copy ) {
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' ];
881 // If doing UTF8 conversion the file must be renamed
882 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
888 function upgradeWatchlist() {
891 list ( $watchlist, $watchlist_temp ) = $this->dbw
->tableNamesN( 'watchlist', 'watchlist_temp' );
893 $this->log( 'Migrating watchlist table to watchlist_temp...' );
895 "CREATE TABLE $watchlist_temp (
897 wl_user int(5) unsigned NOT NULL,
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 '',
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',
910 UNIQUE KEY (wl_user, wl_namespace, wl_title),
911 KEY namespace_title (wl_namespace,wl_title)
913 ) TYPE=InnoDB;", __METHOD__
);
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__
);
919 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', __METHOD__
);
920 $result = $this->dbr
->select( 'watchlist',
929 foreach ( $result as $row ) {
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 );
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 );
944 $this->lastChunk( $add );
946 $this->log( 'Done converting watchlist.' );
947 $this->cleanupSwaps
[] = 'watchlist';
950 function upgradeLogging() {
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 '',
960 log_timestamp char(14) NOT NULL default '19700101000000',
962 -- The user who performed this action; key to user_id
963 log_user int unsigned NOT NULL default 0,
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 '',
970 -- Freeform text. Interpreted as edit history comments.
971 log_comment varchar(255) NOT NULL default '',
973 -- LF separated list of miscellaneous parameters
974 log_params blob NOT NULL default '',
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)
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 );
994 function upgradeArchive() {
997 ar_namespace int NOT NULL default '0',
998 ar_title varchar(255) binary NOT NULL default '',
999 ar_text mediumblob NOT NULL default '',
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',
1007 ar_flags tinyblob NOT NULL default '',
1009 ar_rev_id int(8) unsigned,
1010 ar_text_id int(8) unsigned,
1012 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
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 );
1031 function upgradeImagelinks() {
1032 global $wgUseLatin1;
1033 if ( $wgUseLatin1 ) {
1036 -- Key to page_id of the page containing the image / media link.
1037 il_from int(8) unsigned NOT NULL default '0',
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 '',
1044 UNIQUE KEY il_from(il_from,il_to),
1050 'il_from' => MW_UPGRADE_COPY
,
1051 'il_to' => MW_UPGRADE_ENCODE
);
1052 $this->copyTable( 'imagelinks', $tabledef, $fields );
1056 function upgradeCategorylinks() {
1057 global $wgUseLatin1;
1058 if ( $wgUseLatin1 ) {
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,
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)
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 );
1080 function upgradeIpblocks() {
1081 global $wgUseLatin1;
1082 if ( $wgUseLatin1 ) {
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 '',
1094 PRIMARY KEY ipb_id (ipb_id),
1095 INDEX ipb_address (ipb_address),
1096 INDEX ipb_user (ipb_user)
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 );
1113 function upgradeRecentchanges() {
1114 // There's a format change in the namespace field
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 '',
1121 rc_user int(10) unsigned NOT NULL default '0',
1122 rc_user_text varchar(255) binary NOT NULL default '',
1124 rc_namespace int NOT NULL default '0',
1125 rc_title varchar(255) binary NOT NULL default '',
1127 rc_comment varchar(255) binary NOT NULL default '',
1128 rc_minor tinyint(3) unsigned NOT NULL default '0',
1130 rc_bot tinyint(3) unsigned NOT NULL default '0',
1131 rc_new tinyint(3) unsigned NOT NULL default '0',
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',
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 '',
1141 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1143 rc_ip char(15) NOT NULL default '',
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),
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 );
1177 function upgradeQuerycache() {
1178 // There's a format change in the namespace field
1181 -- A key name, generally the base name of of the special page.
1182 qc_type char(32) NOT NULL,
1184 -- Some sort of stored value. Sizes, counts...
1185 qc_value int(5) unsigned NOT NULL default '0',
1187 -- Target namespace+title
1188 qc_namespace int NOT NULL default '0',
1189 qc_title char(255) binary NOT NULL default '',
1191 KEY (qc_type,qc_value)
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 );
1204 * Check for duplicate rows in "cur" table and move duplicates entries in
1207 * This was in cleanupDupes.inc before.
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";
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";
1220 echo wfWikiID() . ": cur table has the old non-unique index and may have duplicate entries.\n";
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
1230 GROUP BY cur_namespace,cur_title
1234 $n = $dbw->numRows( $res );
1235 echo "Found $n titles with duplicate entries.\n";
1237 echo "Correcting...\n";
1238 foreach ( $res as $row ) {
1239 $ns = intval( $row->cur_namespace
);
1240 $title = $dbw->addQuotes( $row->cur_title
);
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
) );
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";
1257 echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
1258 if ( $timeMin > $timeFirst ) {
1260 echo "keeping minimum: $id\n";
1262 echo "keeping first: $id\n";
1270 (old_namespace, old_title, old_text,
1271 old_comment, old_user, old_user_text,
1272 old_timestamp, old_minor_edit, old_flags,
1274 SELECT cur_namespace, cur_title, cur_text,
1275 cur_comment, cur_user, cur_user_text,
1276 cur_timestamp, cur_minor_edit, '',
1279 WHERE cur_namespace=$ns
1280 AND cur_title=$title
1287 WHERE cur_namespace=$ns
1288 AND cur_title=$title
1294 $dbw->query( 'UNLOCK TABLES' );
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.
1303 function upgradeCleanup() {
1304 $this->renameTable( 'old', 'text' );
1306 foreach ( $this->cleanupSwaps
as $table ) {
1307 $this->swap( $table );
1311 function renameTable( $from, $to ) {
1312 $this->log( "Renaming $from to $to..." );
1314 $fromtable = $this->dbw
->tableName( $from );
1315 $totable = $this->dbw
->tableName( $to );
1316 $this->dbw
->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1319 function swap( $base ) {
1320 $this->renameTable( $base, "{$base}_old" );
1321 $this->renameTable( "{$base}_temp", $base );
1326 $maintClass = 'FiveUpgrade';
1327 require( RUN_MAINTENANCE_IF_MAIN
);