3 // Alternate 1.4 -> 1.5 schema upgrade
4 // This does only the main tables + UTF-8
5 // and is designed to allow upgrades to interleave
6 // with other updates on the replication stream so
7 // that large wikis can be upgraded without disrupting
10 // Note: this script DOES NOT apply every update, nor
11 // will it probably handle much older versions, etc.
12 // Run this, FOLLOWED BY update.php, for upgrading
13 // from 1.4.5 release to 1.5.
15 $options = array( 'step', 'noimages' );
17 require_once( 'commandLine.inc' );
18 require_once( 'cleanupDupes.inc' );
19 require_once( 'userDupes.inc' );
20 require_once( 'updaters.inc' );
22 define( 'MW_UPGRADE_COPY', false );
23 define( 'MW_UPGRADE_ENCODE', true );
24 define( 'MW_UPGRADE_NULL', null );
25 define( 'MW_UPGRADE_CALLBACK', null ); // for self-documentation only
28 function FiveUpgrade() {
30 $this->conversionTables
= $this->prepareWindows1252();
32 $this->dbw
=& $this->newConnection();
33 $this->dbr
=& $this->streamConnection();
35 $this->cleanupSwaps
= array();
36 $this->emailAuth
= false; # don't preauthenticate emails
37 $this->maxLag
= 10; # if slaves are lagged more than 10 secs, wait
40 function doing( $step ) {
41 return is_null( $this->step
) ||
$step == $this->step
;
44 function upgrade( $step ) {
61 foreach( $tables as $table ) {
62 if( $this->doing( $table ) ) {
63 $method = 'upgrade' . ucfirst( $table );
68 if( $this->doing( 'cleanup' ) ) {
69 $this->upgradeCleanup();
75 * Open a connection to the master server with the admin rights.
79 function &newConnection() {
80 global $wgDBadminuser, $wgDBadminpassword;
81 global $wgDBserver, $wgDBname;
82 $db =& new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
87 * Open a second connection to the master server, with buffering off.
88 * This will let us stream large datasets in and write in chunks on the
93 function &streamConnection() {
95 $db =& $this->newConnection();
96 $db->bufferResults( false );
97 $db->query( "SET net_read_timeout=$timeout" );
98 $db->query( "SET net_write_timeout=$timeout" );
103 * Prepare a conversion array for converting Windows Code Page 1252 to
104 * UTF-8. This should provide proper conversion of text that was miscoded
105 * as Windows-1252 by naughty user-agents, and doesn't rely on an outside
111 function prepareWindows1252() {
113 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
114 static $cp1252 = array(
115 0x80 => 0x20AC, #EURO SIGN
116 0x81 => UNICODE_REPLACEMENT
,
117 0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
118 0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
119 0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
120 0x85 => 0x2026, #HORIZONTAL ELLIPSIS
121 0x86 => 0x2020, #DAGGER
122 0x87 => 0x2021, #DOUBLE DAGGER
123 0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
124 0x89 => 0x2030, #PER MILLE SIGN
125 0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
126 0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
127 0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
128 0x8D => UNICODE_REPLACEMENT
,
129 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
130 0x8F => UNICODE_REPLACEMENT
,
131 0x90 => UNICODE_REPLACEMENT
,
132 0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
133 0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
134 0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
135 0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
136 0x95 => 0x2022, #BULLET
137 0x96 => 0x2013, #EN DASH
138 0x97 => 0x2014, #EM DASH
139 0x98 => 0x02DC, #SMALL TILDE
140 0x99 => 0x2122, #TRADE MARK SIGN
141 0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
142 0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
143 0x9C => 0x0153, #LATIN SMALL LIGATURE OE
144 0x9D => UNICODE_REPLACEMENT
,
145 0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
146 0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
149 for( $i = 0; $i < 0x100; $i++
) {
150 $unicode = isset( $cp1252[$i] ) ?
$cp1252[$i] : $i;
151 $pairs[chr( $i )] = codepointToUtf8( $unicode );
157 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
158 * @param string $text
162 function conv( $text ) {
164 return is_null( $text )
167 ?
strtr( $text, $this->conversionTables
)
172 * Dump timestamp and message to output
173 * @param string $message
176 function log( $message ) {
177 echo wfTimestamp( TS_DB
) . ': ' . $message . "\n";
182 * Initialize the chunked-insert system.
183 * Rows will be inserted in chunks of the given number, rather
184 * than in a giant INSERT...SELECT query, to keep the serialized
185 * MySQL database replication from getting hung up. This way other
186 * things can be going on during conversion without waiting for
187 * slaves to catch up as badly.
189 * @param int $chunksize Number of rows to insert at once
190 * @param int $final Total expected number of rows / id of last row,
191 * used for progress reports.
192 * @param string $table to insert on
193 * @param string $fname function name to report in SQL
196 function setChunkScale( $chunksize, $final, $table, $fname ) {
197 $this->chunkSize
= $chunksize;
198 $this->chunkFinal
= $final;
199 $this->chunkCount
= 0;
200 $this->chunkStartTime
= wfTime();
201 $this->chunkOptions
= array( 'IGNORE' );
202 $this->chunkTable
= $table;
203 $this->chunkFunction
= $fname;
207 * Chunked inserts: perform an insert if we've reached the chunk limit.
208 * Prints a progress report with estimated completion time.
209 * @param array &$chunk -- This will be emptied if an insert is done.
210 * @param int $key A key identifier to use in progress estimation in
211 * place of the number of rows inserted. Use this if
212 * you provided a max key number instead of a count
213 * as the final chunk number in setChunkScale()
216 function addChunk( &$chunk, $key = null ) {
217 if( count( $chunk ) >= $this->chunkSize
) {
218 $this->insertChunk( $chunk );
220 $this->chunkCount +
= count( $chunk );
222 $delta = $now - $this->chunkStartTime
;
223 $rate = $this->chunkCount
/ $delta;
225 if( is_null( $key ) ) {
226 $completed = $this->chunkCount
;
230 $portion = $completed / $this->chunkFinal
;
232 $estimatedTotalTime = $delta / $portion;
233 $eta = $this->chunkStartTime +
$estimatedTotalTime;
235 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
236 wfTimestamp( TS_DB
, intval( $now ) ),
239 wfTimestamp( TS_DB
, intval( $eta ) ),
250 * Chunked inserts: perform an insert unconditionally, at the end, and log.
251 * @param array &$chunk -- This will be emptied if an insert is done.
254 function lastChunk( &$chunk ) {
255 $n = count( $chunk );
257 $this->insertChunk( $chunk );
259 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
263 * Chunked inserts: perform an insert.
264 * @param array &$chunk -- This will be emptied if an insert is done.
267 function insertChunk( &$chunk ) {
268 // Give slaves a chance to catch up
269 wfWaitForSlaves( $this->maxLag
);
270 $this->dbw
->insert( $this->chunkTable
, $chunk, $this->chunkFunction
, $this->chunkOptions
);
275 * Copy and transcode a table to table_temp.
276 * @param string $name Base name of the source table
277 * @param string $tabledef CREATE TABLE definition, w/ $1 for the name
278 * @param array $fields set of destination fields to these constants:
279 * MW_UPGRADE_COPY - straight copy
280 * MW_UPGRADE_ENCODE - for old Latin1 wikis, conv to UTF-8
281 * MW_UPGRADE_NULL - just put NULL
282 * @param callable $callback An optional callback to modify the data
283 * or perform other processing. Func should be
284 * ( object $row, array $copy ) and return $copy
287 function copyTable( $name, $tabledef, $fields, $callback = null ) {
288 $fname = 'FiveUpgrade::copyTable';
290 $name_temp = $name . '_temp';
291 $this->log( "Migrating $name table to $name_temp..." );
293 $table = $this->dbw
->tableName( $name );
294 $table_temp = $this->dbw
->tableName( $name_temp );
296 // Create temporary table; we're going to copy everything in there,
297 // then at the end rename the final tables into place.
298 $def = str_replace( '$1', $table_temp, $tabledef );
299 $this->dbw
->query( $def, $fname );
301 $numRecords = $this->dbw
->selectField( $name, 'COUNT(*)', '', $fname );
302 $this->setChunkScale( 100, $numRecords, $name_temp, $fname );
304 // Pull all records from the second, streaming database connection.
305 $sourceFields = array_keys( array_filter( $fields,
306 create_function( '$x', 'return $x !== MW_UPGRADE_NULL;' ) ) );
307 $result = $this->dbr
->select( $name,
313 while( $row = $this->dbr
->fetchObject( $result ) ) {
315 foreach( $fields as $field => $source ) {
316 if( $source === MW_UPGRADE_COPY
) {
317 $copy[$field] = $row->$field;
318 } elseif( $source === MW_UPGRADE_ENCODE
) {
319 $copy[$field] = $this->conv( $row->$field );
320 } elseif( $source === MW_UPGRADE_NULL
) {
321 $copy[$field] = null;
323 $this->log( "Unknown field copy type: $field => $source" );
326 if( is_callable( $callback ) ) {
327 $copy = call_user_func( $callback, $row, $copy );
330 $this->addChunk( $add );
332 $this->lastChunk( $add );
333 $this->dbr
->freeResult( $result );
335 $this->log( "Done converting $name." );
336 $this->cleanupSwaps
[] = $name;
339 function upgradePage() {
340 $fname = "FiveUpgrade::upgradePage";
343 if( $this->dbw
->tableExists( 'page' ) ) {
344 $this->log( 'Page table already exists; aborting.' );
348 $this->log( "Checking cur table for unique title index and applying if necessary" );
351 $this->log( "...converting from cur/old to page/revision/text DB structure." );
353 extract( $this->dbw
->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
355 $this->log( "Creating page and revision tables..." );
356 $this->dbw
->query("CREATE TABLE $page (
357 page_id int(8) unsigned NOT NULL auto_increment,
358 page_namespace int NOT NULL,
359 page_title varchar(255) binary NOT NULL,
360 page_restrictions tinyblob NOT NULL default '',
361 page_counter bigint(20) unsigned NOT NULL default '0',
362 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
363 page_is_new tinyint(1) unsigned NOT NULL default '0',
364 page_random real unsigned NOT NULL,
365 page_touched char(14) binary NOT NULL default '',
366 page_latest int(8) unsigned NOT NULL,
367 page_len int(8) unsigned NOT NULL,
369 PRIMARY KEY page_id (page_id),
370 UNIQUE INDEX name_title (page_namespace,page_title),
373 ) TYPE=InnoDB", $fname );
374 $this->dbw
->query("CREATE TABLE $revision (
375 rev_id int(8) unsigned NOT NULL auto_increment,
376 rev_page int(8) unsigned NOT NULL,
377 rev_text_id int(8) unsigned NOT NULL,
378 rev_comment tinyblob NOT NULL default '',
379 rev_user int(5) unsigned NOT NULL default '0',
380 rev_user_text varchar(255) binary NOT NULL default '',
381 rev_timestamp char(14) binary NOT NULL default '',
382 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
383 rev_deleted tinyint(1) unsigned NOT NULL default '0',
385 PRIMARY KEY rev_page_id (rev_page, rev_id),
386 UNIQUE INDEX rev_id (rev_id),
387 INDEX rev_timestamp (rev_timestamp),
388 INDEX page_timestamp (rev_page,rev_timestamp),
389 INDEX user_timestamp (rev_user,rev_timestamp),
390 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
391 ) TYPE=InnoDB", $fname );
393 $maxold = IntVal( $this->dbw
->selectField( 'old', 'max(old_id)', '', $fname ) );
394 $this->log( "Last old record is {$maxold}" );
396 global $wgLegacySchemaConversion;
397 if( $wgLegacySchemaConversion ) {
398 // Create HistoryBlobCurStub entries.
399 // Text will be pulled from the leftover 'cur' table at runtime.
400 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
401 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
402 $cur_flags = "'object'";
404 // Copy all cur text in immediately: this may take longer but avoids
405 // having to keep an extra table around.
406 echo "......Moving text from cur.\n";
407 $cur_text = 'cur_text';
411 $maxcur = $this->dbw
->selectField( 'cur', 'max(cur_id)', '', $fname );
412 $this->log( "Last cur entry is $maxcur" );
415 * Copy placeholder records for each page's current version into old
416 * Don't do any conversion here; text records are converted at runtime
417 * based on the flags (and may be originally binary!) while the meta
418 * fields will be converted in the old -> rev and cur -> page steps.
420 $this->setChunkScale( $chunksize, $maxcur, 'old', $fname );
421 $result = $this->dbr
->query(
422 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
423 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
425 ORDER BY cur_id", $fname );
427 while( $row = $this->dbr
->fetchObject( $result ) ) {
429 'old_namespace' => $row->cur_namespace
,
430 'old_title' => $row->cur_title
,
431 'old_text' => $row->text
,
432 'old_comment' => $row->cur_comment
,
433 'old_user' => $row->cur_user
,
434 'old_user_text' => $row->cur_user_text
,
435 'old_timestamp' => $row->cur_timestamp
,
436 'old_minor_edit' => $row->cur_minor_edit
,
437 'old_flags' => $row->flags
);
438 $this->addChunk( $add, $row->cur_id
);
440 $this->lastChunk( $add );
441 $this->dbr
->freeResult( $result );
444 * Copy revision metadata from old into revision.
445 * We'll also do UTF-8 conversion of usernames and comments.
447 #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
448 #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
449 $countold = $this->dbw
->selectField( 'old', 'count(old_id)', '', $fname );
450 $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
452 $this->log( "......Setting up revision table." );
453 $result = $this->dbr
->query(
454 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
455 old_timestamp, old_minor_edit
456 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
460 while( $row = $this->dbr
->fetchObject( $result ) ) {
462 'rev_id' => $row->old_id
,
463 'rev_page' => $row->cur_id
,
464 'rev_text_id' => $row->old_id
,
465 'rev_comment' => $this->conv( $row->old_comment
),
466 'rev_user' => $row->old_user
,
467 'rev_user_text' => $this->conv( $row->old_user_text
),
468 'rev_timestamp' => $row->old_timestamp
,
469 'rev_minor_edit' => $row->old_minor_edit
);
470 $this->addChunk( $add );
472 $this->lastChunk( $add );
473 $this->dbr
->freeResult( $result );
477 * Copy page metadata from cur into page.
478 * We'll also do UTF-8 conversion of titles.
480 $this->log( "......Setting up page table." );
481 $this->setChunkScale( $chunksize, $maxcur, 'page', $fname );
482 $result = $this->dbr
->query( "
483 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
484 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
486 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
487 ORDER BY cur_id", $fname );
489 while( $row = $this->dbr
->fetchObject( $result ) ) {
491 'page_id' => $row->cur_id
,
492 'page_namespace' => $row->cur_namespace
,
493 'page_title' => $this->conv( $row->cur_title
),
494 'page_restrictions' => $row->cur_restrictions
,
495 'page_counter' => $row->cur_counter
,
496 'page_is_redirect' => $row->cur_is_redirect
,
497 'page_is_new' => $row->cur_is_new
,
498 'page_random' => $row->cur_random
,
499 'page_touched' => $this->dbw
->timestamp(),
500 'page_latest' => $row->rev_id
,
501 'page_len' => $row->len
);
502 $this->addChunk( $add, $row->cur_id
);
504 $this->lastChunk( $add );
505 $this->dbr
->freeResult( $result );
507 $this->log( "...done with cur/old -> page/revision." );
510 function upgradeLinks() {
511 $fname = 'FiveUpgrade::upgradeLinks';
513 extract( $this->dbw
->tableNames( 'links', 'brokenlinks', 'pagelinks', 'cur' ) );
515 $this->log( 'Creating pagelinks table...' );
517 CREATE TABLE $pagelinks (
518 -- Key to the page_id of the page containing the link.
519 pl_from int(8) unsigned NOT NULL default '0',
521 -- Key to page_namespace/page_title of the target page.
522 -- The target page may or may not exist, and due to renames
523 -- and deletions may refer to different page records as time
525 pl_namespace int NOT NULL default '0',
526 pl_title varchar(255) binary NOT NULL default '',
528 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
529 KEY (pl_namespace,pl_title)
533 $this->log( 'Importing live links -> pagelinks' );
534 $nlinks = $this->dbw
->selectField( 'links', 'count(*)', '', $fname );
536 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
537 $result = $this->dbr
->query( "
538 SELECT l_from,cur_namespace,cur_title
540 WHERE l_to=cur_id", $fname );
542 while( $row = $this->dbr
->fetchObject( $result ) ) {
544 'pl_from' => $row->l_from
,
545 'pl_namespace' => $row->cur_namespace
,
546 'pl_title' => $row->cur_title
);
547 $this->addChunk( $add );
549 $this->lastChunk( $add );
551 $this->log( 'no links!' );
554 $this->log( 'Importing brokenlinks -> pagelinks' );
555 $nbrokenlinks = $this->dbw
->selectField( 'brokenlinks', 'count(*)', '', $fname );
556 if( $nbrokenlinks ) {
557 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
558 $result = $this->dbr
->query(
559 "SELECT bl_from, bl_to FROM $brokenlinks",
562 while( $row = $this->dbr
->fetchObject( $result ) ) {
563 $pagename = $this->conv( $row->bl_to
);
564 $title = Title
::newFromText( $pagename );
565 if( is_null( $title ) ) {
566 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
569 'pl_from' => $row->bl_from
,
570 'pl_namespace' => $title->getNamespace(),
571 'pl_title' => $title->getDBkey() );
572 $this->addChunk( $add );
575 $this->lastChunk( $add );
577 $this->log( 'no brokenlinks!' );
580 $this->log( 'Done with links.' );
583 function upgradeUser() {
584 // Apply unique index, if necessary:
585 $duper = new UserDupes( $this->dbw
);
586 if( $duper->hasUniqueIndex() ) {
587 $this->log( "Already have unique user_name index." );
589 $this->log( "Clearing user duplicates..." );
590 if( !$duper->clearDupes() ) {
591 $this->log( "WARNING: Duplicate user accounts, may explode!" );
597 user_id int(5) unsigned NOT NULL auto_increment,
598 user_name varchar(255) binary NOT NULL default '',
599 user_real_name varchar(255) binary NOT NULL default '',
600 user_password tinyblob NOT NULL default '',
601 user_newpassword tinyblob NOT NULL default '',
602 user_email tinytext NOT NULL default '',
603 user_options blob NOT NULL default '',
604 user_touched char(14) binary NOT NULL default '',
605 user_token char(32) binary NOT NULL default '',
606 user_email_authenticated CHAR(14) BINARY,
607 user_email_token CHAR(32) BINARY,
608 user_email_token_expires CHAR(14) BINARY,
610 PRIMARY KEY user_id (user_id),
611 UNIQUE INDEX user_name (user_name),
612 INDEX (user_email_token)
617 'user_id' => MW_UPGRADE_COPY
,
618 'user_name' => MW_UPGRADE_ENCODE
,
619 'user_real_name' => MW_UPGRADE_ENCODE
,
620 'user_password' => MW_UPGRADE_COPY
,
621 'user_newpassword' => MW_UPGRADE_COPY
,
622 'user_email' => MW_UPGRADE_ENCODE
,
623 'user_options' => MW_UPGRADE_ENCODE
,
624 'user_touched' => MW_UPGRADE_CALLBACK
,
625 'user_token' => MW_UPGRADE_COPY
,
626 'user_email_authenticated' => MW_UPGRADE_CALLBACK
,
627 'user_email_token' => MW_UPGRADE_NULL
,
628 'user_email_token_expires' => MW_UPGRADE_NULL
);
629 $this->copyTable( 'user', $tabledef, $fields,
630 array( &$this, 'userCallback' ) );
633 function userCallback( $row, $copy ) {
634 $now = $this->dbw
->timestamp();
635 $copy['user_touched'] = $now;
636 $copy['user_email_authenticated'] = $this->emailAuth ?
$now : null;
640 function upgradeImage() {
643 img_name varchar(255) binary NOT NULL default '',
644 img_size int(8) unsigned NOT NULL default '0',
645 img_width int(5) NOT NULL default '0',
646 img_height int(5) NOT NULL default '0',
647 img_metadata mediumblob NOT NULL,
648 img_bits int(3) NOT NULL default '0',
649 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
650 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
651 img_minor_mime varchar(32) NOT NULL default "unknown",
652 img_description tinyblob NOT NULL default '',
653 img_user int(5) unsigned NOT NULL default '0',
654 img_user_text varchar(255) binary NOT NULL default '',
655 img_timestamp char(14) binary NOT NULL default '',
657 PRIMARY KEY img_name (img_name),
658 INDEX img_size (img_size),
659 INDEX img_timestamp (img_timestamp)
663 'img_name' => MW_UPGRADE_ENCODE
,
664 'img_size' => MW_UPGRADE_COPY
,
665 'img_width' => MW_UPGRADE_CALLBACK
,
666 'img_height' => MW_UPGRADE_CALLBACK
,
667 'img_metadata' => MW_UPGRADE_CALLBACK
,
668 'img_bits' => MW_UPGRADE_CALLBACK
,
669 'img_media_type' => MW_UPGRADE_CALLBACK
,
670 'img_major_mime' => MW_UPGRADE_CALLBACK
,
671 'img_minor_mime' => MW_UPGRADE_CALLBACK
,
672 'img_description' => MW_UPGRADE_ENCODE
,
673 'img_user' => MW_UPGRADE_COPY
,
674 'img_user_text' => MW_UPGRADE_ENCODE
,
675 'img_timestamp' => MW_UPGRADE_COPY
);
676 $this->copyTable( 'image', $tabledef, $fields,
677 array( &$this, 'imageCallback' ) );
680 function imageCallback( $row, $copy ) {
682 if( !isset( $options['noimage'] ) ) {
683 // Fill in the new image info fields
684 $info = $this->imageInfo( $row->img_name
);
686 $copy['img_width' ] = $info['width'];
687 $copy['img_height' ] = $info['height'];
688 $copy['img_metadata' ] = ""; // loaded on-demand
689 $copy['img_bits' ] = $info['bits'];
690 $copy['img_media_type'] = $info['media'];
691 $copy['img_major_mime'] = $info['major'];
692 $copy['img_minor_mime'] = $info['minor'];
695 // If doing UTF8 conversion the file must be renamed
696 $this->renameFile( $row->img_name
, 'wfImageDir' );
701 function imageInfo( $name, $subdirCallback='wfImageDir', $basename = null ) {
702 if( is_null( $basename ) ) $basename = $name;
703 $dir = call_user_func( $subdirCallback, $basename );
704 $filename = $dir . '/' . $name;
713 $magic =& wfGetMimeMagic();
714 $mime = $magic->guessMimeType( $filename, true );
715 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
717 $info['media'] = $magic->getMediaType( $filename, $mime );
721 if( $mime == 'image/svg' ) {
722 $gis = wfGetSVGsize( $this->imagePath
);
723 } elseif( $magic->isPHPImageType( $mime ) ) {
724 $gis = getimagesize( $filename );
726 $this->log( "Surprising mime type: $mime" );
729 $info['width' ] = $gis[0];
730 $info['height'] = $gis[1];
732 if( isset( $gis['bits'] ) ) {
733 $info['bits'] = $gis['bits'];
742 * @param string $table The table name to be truncated
744 function clearTable( $table ) {
745 print "Clearing $table...\n";
746 $tableName = $this->db
->tableName( $table );
747 $this->db
->query( 'TRUNCATE $tableName' );
751 * Rename a given image or archived image file to the converted filename,
752 * leaving a symlink for URL compatibility.
754 * @param string $oldname pre-conversion filename
755 * @param string $basename pre-conversion base filename for dir hashing, if an archive
758 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
759 $newname = $this->conv( $oldname );
760 if( $newname == $oldname ) {
761 // No need to rename; another field triggered this row.
765 if( is_null( $basename ) ) $basename = $oldname;
766 $ubasename = $this->conv( $basename );
767 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
768 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
770 $this->log( "$oldpath -> $newpath" );
771 if( rename( $oldpath, $newpath ) ) {
772 $relpath = $this->relativize( $newpath, dirname( $oldpath ) );
773 if( !symlink( $relpath, $oldpath ) ) {
774 $this->log( "... symlink failed!" );
777 $this->log( "... rename failed!" );
782 * Generate a relative path name to the given file.
783 * Assumes Unix-style paths, separators, and semantics.
785 * @param string $path Absolute destination path including target filename
786 * @param string $from Absolute source path, directory only
791 function relativize( $path, $from ) {
792 $pieces = explode( '/', dirname( $path ) );
793 $against = explode( '/', $from );
795 // Trim off common prefix
796 while( count( $pieces ) && count( $against )
797 && $pieces[0] == $against[0] ) {
798 array_shift( $pieces );
799 array_shift( $against );
802 // relative dots to bump us to the parent
803 while( count( $against ) ) {
804 array_unshift( $pieces, '..' );
805 array_shift( $against );
808 array_push( $pieces, basename( $path ) );
810 return implode( '/', $pieces );
813 function upgradeOldImage() {
816 -- Base filename: key to image.img_name
817 oi_name varchar(255) binary NOT NULL default '',
819 -- Filename of the archived file.
820 -- This is generally a timestamp and '!' prepended to the base name.
821 oi_archive_name varchar(255) binary NOT NULL default '',
823 -- Other fields as in image...
824 oi_size int(8) unsigned NOT NULL default 0,
825 oi_width int(5) NOT NULL default 0,
826 oi_height int(5) NOT NULL default 0,
827 oi_bits int(3) NOT NULL default 0,
828 oi_description tinyblob NOT NULL default '',
829 oi_user int(5) unsigned NOT NULL default '0',
830 oi_user_text varchar(255) binary NOT NULL default '',
831 oi_timestamp char(14) binary NOT NULL default '',
833 INDEX oi_name (oi_name(10))
838 'oi_name' => MW_UPGRADE_ENCODE
,
839 'oi_archive_name' => MW_UPGRADE_ENCODE
,
840 'oi_size' => MW_UPGRADE_COPY
,
841 'oi_width' => MW_UPGRADE_CALLBACK
,
842 'oi_height' => MW_UPGRADE_CALLBACK
,
843 'oi_bits' => MW_UPGRADE_CALLBACK
,
844 'oi_description' => MW_UPGRADE_ENCODE
,
845 'oi_user' => MW_UPGRADE_COPY
,
846 'oi_user_text' => MW_UPGRADE_ENCODE
,
847 'oi_timestamp' => MW_UPGRADE_COPY
);
848 $this->copyTable( 'oldimage', $tabledef, $fields,
849 array( &$this, 'oldimageCallback' ) );
852 function oldimageCallback( $row, $copy ) {
854 if( !isset( $options['noimage'] ) ) {
855 // Fill in the new image info fields
856 $info = $this->imageInfo( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
857 $copy['oi_width' ] = $info['width' ];
858 $copy['oi_height'] = $info['height'];
859 $copy['oi_bits' ] = $info['bits' ];
862 // If doing UTF8 conversion the file must be renamed
863 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
869 function upgradeWatchlist() {
870 $fname = 'FiveUpgrade::upgradeWatchlist';
873 extract( $this->dbw
->tableNames( 'watchlist', 'watchlist_temp' ) );
875 $this->log( 'Migrating watchlist table to watchlist_temp...' );
877 "CREATE TABLE $watchlist_temp (
879 wl_user int(5) unsigned NOT NULL,
881 -- Key to page_namespace/page_title
882 -- Note that users may watch patches which do not exist yet,
883 -- or existed in the past but have been deleted.
884 wl_namespace int NOT NULL default '0',
885 wl_title varchar(255) binary NOT NULL default '',
887 -- Timestamp when user was last sent a notification e-mail;
888 -- cleared when the user visits the page.
889 -- FIXME: add proper null support etc
890 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
892 UNIQUE KEY (wl_user, wl_namespace, wl_title),
893 KEY namespace_title (wl_namespace,wl_title)
895 ) TYPE=InnoDB;", $fname );
897 // Fix encoding for Latin-1 upgrades, add some fields,
898 // and double article to article+talk pairs
899 $numwatched = $this->dbw
->selectField( 'watchlist', 'count(*)', '', $fname );
901 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
902 $result = $this->dbr
->select( 'watchlist',
911 while( $row = $this->dbr
->fetchObject( $result ) ) {
912 $now = $this->dbw
->timestamp();
914 'wl_user' => $row->wl_user
,
915 'wl_namespace' => Namespace::getSubject( $row->wl_namespace
),
916 'wl_title' => $this->conv( $row->wl_title
),
917 'wl_notificationtimestamp' => '0' );
918 $this->addChunk( $add );
921 'wl_user' => $row->wl_user
,
922 'wl_namespace' => Namespace::getTalk( $row->wl_namespace
),
923 'wl_title' => $this->conv( $row->wl_title
),
924 'wl_notificationtimestamp' => '0' );
925 $this->addChunk( $add );
927 $this->lastChunk( $add );
928 $this->dbr
->freeResult( $result );
930 $this->log( 'Done converting watchlist.' );
931 $this->cleanupSwaps
[] = 'watchlist';
934 function upgradeLogging() {
937 -- Symbolic keys for the general log type and the action type
938 -- within the log. The output format will be controlled by the
939 -- action field, but only the type controls categorization.
940 log_type char(10) NOT NULL default '',
941 log_action char(10) NOT NULL default '',
944 log_timestamp char(14) NOT NULL default '19700101000000',
946 -- The user who performed this action; key to user_id
947 log_user int unsigned NOT NULL default 0,
949 -- Key to the page affected. Where a user is the target,
950 -- this will point to the user page.
951 log_namespace int NOT NULL default 0,
952 log_title varchar(255) binary NOT NULL default '',
954 -- Freeform text. Interpreted as edit history comments.
955 log_comment varchar(255) NOT NULL default '',
957 -- LF separated list of miscellaneous parameters
958 log_params blob NOT NULL default '',
960 KEY type_time (log_type, log_timestamp),
961 KEY user_time (log_user, log_timestamp),
962 KEY page_time (log_namespace, log_title, log_timestamp)
967 'log_type' => MW_UPGRADE_COPY
,
968 'log_action' => MW_UPGRADE_COPY
,
969 'log_timestamp' => MW_UPGRADE_COPY
,
970 'log_user' => MW_UPGRADE_COPY
,
971 'log_namespace' => MW_UPGRADE_COPY
,
972 'log_title' => MW_UPGRADE_ENCODE
,
973 'log_comment' => MW_UPGRADE_ENCODE
,
974 'log_params' => MW_UPGRADE_ENCODE
);
975 $this->copyTable( 'logging', $tabledef, $fields );
978 function upgradeArchive() {
981 ar_namespace int NOT NULL default '0',
982 ar_title varchar(255) binary NOT NULL default '',
983 ar_text mediumblob NOT NULL default '',
985 ar_comment tinyblob NOT NULL default '',
986 ar_user int(5) unsigned NOT NULL default '0',
987 ar_user_text varchar(255) binary NOT NULL,
988 ar_timestamp char(14) binary NOT NULL default '',
989 ar_minor_edit tinyint(1) NOT NULL default '0',
991 ar_flags tinyblob NOT NULL default '',
993 ar_rev_id int(8) unsigned,
994 ar_text_id int(8) unsigned,
996 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
1001 'ar_namespace' => MW_UPGRADE_COPY
,
1002 'ar_title' => MW_UPGRADE_ENCODE
,
1003 'ar_text' => MW_UPGRADE_COPY
,
1004 'ar_comment' => MW_UPGRADE_ENCODE
,
1005 'ar_user' => MW_UPGRADE_COPY
,
1006 'ar_user_text' => MW_UPGRADE_ENCODE
,
1007 'ar_timestamp' => MW_UPGRADE_COPY
,
1008 'ar_minor_edit' => MW_UPGRADE_COPY
,
1009 'ar_flags' => MW_UPGRADE_COPY
,
1010 'ar_rev_id' => MW_UPGRADE_NULL
,
1011 'ar_text_id' => MW_UPGRADE_NULL
);
1012 $this->copyTable( 'archive', $tabledef, $fields );
1015 function upgradeImagelinks() {
1016 global $wgUseLatin1;
1017 if( $wgUseLatin1 ) {
1020 -- Key to page_id of the page containing the image / media link.
1021 il_from int(8) unsigned NOT NULL default '0',
1023 -- Filename of target image.
1024 -- This is also the page_title of the file's description page;
1025 -- all such pages are in namespace 6 (NS_IMAGE).
1026 il_to varchar(255) binary NOT NULL default '',
1028 UNIQUE KEY il_from(il_from,il_to),
1034 'il_from' => MW_UPGRADE_COPY
,
1035 'il_to' => MW_UPGRADE_ENCODE
);
1036 $this->copyTable( 'imagelinks', $tabledef, $fields );
1040 function upgradeCategorylinks() {
1041 global $wgUseLatin1;
1042 if( $wgUseLatin1 ) {
1045 cl_from int(8) unsigned NOT NULL default '0',
1046 cl_to varchar(255) binary NOT NULL default '',
1047 cl_sortkey varchar(86) binary NOT NULL default '',
1048 cl_timestamp timestamp NOT NULL,
1050 UNIQUE KEY cl_from(cl_from,cl_to),
1051 KEY cl_sortkey(cl_to,cl_sortkey),
1052 KEY cl_timestamp(cl_to,cl_timestamp)
1056 'cl_from' => MW_UPGRADE_COPY
,
1057 'cl_to' => MW_UPGRADE_ENCODE
,
1058 'cl_sortkey' => MW_UPGRADE_ENCODE
,
1059 'cl_timestamp' => MW_UPGRADE_COPY
);
1060 $this->copyTable( 'categorylinks', $tabledef, $fields );
1064 function upgradeIpblocks() {
1065 global $wgUseLatin1;
1066 if( $wgUseLatin1 ) {
1069 ipb_id int(8) NOT NULL auto_increment,
1070 ipb_address varchar(40) binary NOT NULL default '',
1071 ipb_user int(8) unsigned NOT NULL default '0',
1072 ipb_by int(8) unsigned NOT NULL default '0',
1073 ipb_reason tinyblob NOT NULL default '',
1074 ipb_timestamp char(14) binary NOT NULL default '',
1075 ipb_auto tinyint(1) NOT NULL default '0',
1076 ipb_expiry char(14) binary NOT NULL default '',
1078 PRIMARY KEY ipb_id (ipb_id),
1079 INDEX ipb_address (ipb_address),
1080 INDEX ipb_user (ipb_user)
1085 'ipb_id' => MW_UPGRADE_COPY
,
1086 'ipb_address' => MW_UPGRADE_COPY
,
1087 'ipb_user' => MW_UPGRADE_COPY
,
1088 'ipb_by' => MW_UPGRADE_COPY
,
1089 'ipb_reason' => MW_UPGRADE_ENCODE
,
1090 'ipb_timestamp' => MW_UPGRADE_COPY
,
1091 'ipb_auto' => MW_UPGRADE_COPY
,
1092 'ipb_expiry' => MW_UPGRADE_COPY
);
1093 $this->copyTable( 'ipblocks', $tabledef, $fields );
1097 function upgradeRecentchanges() {
1098 // There's a format change in the namespace field
1101 rc_id int(8) NOT NULL auto_increment,
1102 rc_timestamp varchar(14) binary NOT NULL default '',
1103 rc_cur_time varchar(14) binary NOT NULL default '',
1105 rc_user int(10) unsigned NOT NULL default '0',
1106 rc_user_text varchar(255) binary NOT NULL default '',
1108 rc_namespace int NOT NULL default '0',
1109 rc_title varchar(255) binary NOT NULL default '',
1111 rc_comment varchar(255) binary NOT NULL default '',
1112 rc_minor tinyint(3) unsigned NOT NULL default '0',
1114 rc_bot tinyint(3) unsigned NOT NULL default '0',
1115 rc_new tinyint(3) unsigned NOT NULL default '0',
1117 rc_cur_id int(10) unsigned NOT NULL default '0',
1118 rc_this_oldid int(10) unsigned NOT NULL default '0',
1119 rc_last_oldid int(10) unsigned NOT NULL default '0',
1121 rc_type tinyint(3) unsigned NOT NULL default '0',
1122 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
1123 rc_moved_to_title varchar(255) binary NOT NULL default '',
1125 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1127 rc_ip char(15) NOT NULL default '',
1129 PRIMARY KEY rc_id (rc_id),
1130 INDEX rc_timestamp (rc_timestamp),
1131 INDEX rc_namespace_title (rc_namespace, rc_title),
1132 INDEX rc_cur_id (rc_cur_id),
1133 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
1139 'rc_id' => MW_UPGRADE_COPY
,
1140 'rc_timestamp' => MW_UPGRADE_COPY
,
1141 'rc_cur_time' => MW_UPGRADE_COPY
,
1142 'rc_user' => MW_UPGRADE_COPY
,
1143 'rc_user_text' => MW_UPGRADE_ENCODE
,
1144 'rc_namespace' => MW_UPGRADE_COPY
,
1145 'rc_title' => MW_UPGRADE_ENCODE
,
1146 'rc_comment' => MW_UPGRADE_ENCODE
,
1147 'rc_minor' => MW_UPGRADE_COPY
,
1148 'rc_bot' => MW_UPGRADE_COPY
,
1149 'rc_new' => MW_UPGRADE_COPY
,
1150 'rc_cur_id' => MW_UPGRADE_COPY
,
1151 'rc_this_oldid' => MW_UPGRADE_COPY
,
1152 'rc_last_oldid' => MW_UPGRADE_COPY
,
1153 'rc_type' => MW_UPGRADE_COPY
,
1154 'rc_moved_to_ns' => MW_UPGRADE_COPY
,
1155 'rc_moved_to_title' => MW_UPGRADE_ENCODE
,
1156 'rc_patrolled' => MW_UPGRADE_COPY
,
1157 'rc_ip' => MW_UPGRADE_COPY
);
1158 $this->copyTable( 'recentchanges', $tabledef, $fields );
1161 function upgradeQuerycache() {
1162 // There's a format change in the namespace field
1165 -- A key name, generally the base name of of the special page.
1166 qc_type char(32) NOT NULL,
1168 -- Some sort of stored value. Sizes, counts...
1169 qc_value int(5) unsigned NOT NULL default '0',
1171 -- Target namespace+title
1172 qc_namespace int NOT NULL default '0',
1173 qc_title char(255) binary NOT NULL default '',
1175 KEY (qc_type,qc_value)
1180 'qc_type' => MW_UPGRADE_COPY
,
1181 'qc_value' => MW_UPGRADE_COPY
,
1182 'qc_namespace' => MW_UPGRADE_COPY
,
1183 'qc_title' => MW_UPGRADE_ENCODE
);
1184 $this->copyTable( 'querycache', $tabledef, $fields );
1188 * Rename all our temporary tables into final place.
1189 * We've left things in place so a read-only wiki can continue running
1190 * on the old code during all this.
1192 function upgradeCleanup() {
1193 $this->renameTable( 'old', 'text' );
1195 foreach( $this->cleanupSwaps
as $table ) {
1196 $this->swap( $table );
1200 function renameTable( $from, $to ) {
1201 $this->log( "Renaming $from to $to..." );
1203 $fromtable = $this->dbw
->tableName( $from );
1204 $totable = $this->dbw
->tableName( $to );
1205 $this->dbw
->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1208 function swap( $base ) {
1209 $this->renameTable( $base, "{$base}_old" );
1210 $this->renameTable( "{$base}_temp", $base );
1215 $upgrade = new FiveUpgrade();
1216 $step = isset( $options['step'] ) ?
$options['step'] : null;
1217 $upgrade->upgrade( $step );