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' );
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();
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', 'page' ) );
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,page_namespace,page_title
540 WHERE l_to=page_id", $fname );
542 while( $row = $this->dbr
->fetchObject( $result ) ) {
544 'pl_from' => $row->l_from
,
545 'pl_namespace' => $row->page_namespace
,
546 'pl_title' => $row->page_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 $this->chunkOptions
= array( 'IGNORE' );
559 $result = $this->dbr
->query(
560 "SELECT bl_from, bl_to FROM $brokenlinks",
563 while( $row = $this->dbr
->fetchObject( $result ) ) {
564 $pagename = $this->conv( $row->bl_to
);
565 $title = Title
::newFromText( $pagename );
566 if( is_null( $title ) ) {
567 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
570 'pl_from' => $row->bl_from
,
571 'pl_namespace' => $title->getNamespace(),
572 'pl_title' => $title->getDBkey() );
573 $this->addChunk( $add );
576 $this->lastChunk( $add );
578 $this->log( 'no brokenlinks!' );
581 $this->log( 'Done with links.' );
584 function upgradeUser() {
585 // Apply unique index, if necessary:
586 $duper = new UserDupes( $this->dbw
);
587 if( $duper->hasUniqueIndex() ) {
588 $this->log( "Already have unique user_name index." );
590 $this->log( "Clearing user duplicates..." );
591 if( !$duper->clearDupes() ) {
592 $this->log( "WARNING: Duplicate user accounts, may explode!" );
598 user_id int(5) unsigned NOT NULL auto_increment,
599 user_name varchar(255) binary NOT NULL default '',
600 user_real_name varchar(255) binary NOT NULL default '',
601 user_password tinyblob NOT NULL default '',
602 user_newpassword tinyblob NOT NULL default '',
603 user_email tinytext NOT NULL default '',
604 user_options blob NOT NULL default '',
605 user_touched char(14) binary NOT NULL default '',
606 user_token char(32) binary NOT NULL default '',
607 user_email_authenticated CHAR(14) BINARY,
608 user_email_token CHAR(32) BINARY,
609 user_email_token_expires CHAR(14) BINARY,
611 PRIMARY KEY user_id (user_id),
612 UNIQUE INDEX user_name (user_name),
613 INDEX (user_email_token)
618 'user_id' => MW_UPGRADE_COPY
,
619 'user_name' => MW_UPGRADE_ENCODE
,
620 'user_real_name' => MW_UPGRADE_ENCODE
,
621 'user_password' => MW_UPGRADE_COPY
,
622 'user_newpassword' => MW_UPGRADE_COPY
,
623 'user_email' => MW_UPGRADE_ENCODE
,
624 'user_options' => MW_UPGRADE_ENCODE
,
625 'user_touched' => MW_UPGRADE_CALLBACK
,
626 'user_token' => MW_UPGRADE_COPY
,
627 'user_email_authenticated' => MW_UPGRADE_CALLBACK
,
628 'user_email_token' => MW_UPGRADE_NULL
,
629 'user_email_token_expires' => MW_UPGRADE_NULL
);
630 $this->copyTable( 'user', $tabledef, $fields,
631 array( &$this, 'userCallback' ) );
634 function userCallback( $row, $copy ) {
635 $now = $this->dbw
->timestamp();
636 $copy['user_touched'] = $now;
637 $copy['user_email_authenticated'] = $this->emailAuth ?
$now : null;
641 function upgradeImage() {
644 img_name varchar(255) binary NOT NULL default '',
645 img_size int(8) unsigned NOT NULL default '0',
646 img_width int(5) NOT NULL default '0',
647 img_height int(5) NOT NULL default '0',
648 img_metadata mediumblob NOT NULL,
649 img_bits int(3) NOT NULL default '0',
650 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
651 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
652 img_minor_mime varchar(32) NOT NULL default "unknown",
653 img_description tinyblob NOT NULL default '',
654 img_user int(5) unsigned NOT NULL default '0',
655 img_user_text varchar(255) binary NOT NULL default '',
656 img_timestamp char(14) binary NOT NULL default '',
658 PRIMARY KEY img_name (img_name),
659 INDEX img_size (img_size),
660 INDEX img_timestamp (img_timestamp)
664 'img_name' => MW_UPGRADE_ENCODE
,
665 'img_size' => MW_UPGRADE_COPY
,
666 'img_width' => MW_UPGRADE_CALLBACK
,
667 'img_height' => MW_UPGRADE_CALLBACK
,
668 'img_metadata' => MW_UPGRADE_CALLBACK
,
669 'img_bits' => MW_UPGRADE_CALLBACK
,
670 'img_media_type' => MW_UPGRADE_CALLBACK
,
671 'img_major_mime' => MW_UPGRADE_CALLBACK
,
672 'img_minor_mime' => MW_UPGRADE_CALLBACK
,
673 'img_description' => MW_UPGRADE_ENCODE
,
674 'img_user' => MW_UPGRADE_COPY
,
675 'img_user_text' => MW_UPGRADE_ENCODE
,
676 'img_timestamp' => MW_UPGRADE_COPY
);
677 $this->copyTable( 'image', $tabledef, $fields,
678 array( &$this, 'imageCallback' ) );
681 function imageCallback( $row, $copy ) {
682 // Fill in the new image info fields
683 $info = $this->imageInfo( $row->img_name
);
685 $copy['img_width' ] = $info['width'];
686 $copy['img_height' ] = $info['height'];
687 $copy['img_metadata' ] = ""; // loaded on-demand
688 $copy['img_bits' ] = $info['bits'];
689 $copy['img_media_type'] = $info['media'];
690 $copy['img_major_mime'] = $info['major'];
691 $copy['img_minor_mime'] = $info['minor'];
693 // If doing UTF8 conversion the file must be renamed
694 $this->renameFile( $row->img_name
, 'wfImageDir' );
699 function imageInfo( $name, $subdirCallback='wfImageDir', $basename = null ) {
700 if( is_null( $basename ) ) $basename = $name;
701 $dir = call_user_func( $subdirCallback, $basename );
702 $filename = $dir . '/' . $name;
711 $magic =& wfGetMimeMagic();
712 $mime = $magic->guessMimeType( $filename, true );
713 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
715 $info['media'] = $magic->getMediaType( $filename, $mime );
719 if( $mime == 'image/svg' ) {
720 $gis = wfGetSVGsize( $this->imagePath
);
721 } elseif( $magic->isPHPImageType( $mime ) ) {
722 $gis = getimagesize( $filename );
724 $this->log( "Surprising mime type: $mime" );
727 $info['width' ] = $gis[0];
728 $info['height'] = $gis[1];
730 if( isset( $gis['bits'] ) ) {
731 $info['bits'] = $gis['bits'];
740 * @param string $table The table name to be truncated
742 function clearTable( $table ) {
743 print "Clearing $table...\n";
744 $tableName = $this->db
->tableName( $table );
745 $this->db
->query( 'TRUNCATE $tableName' );
749 * Rename a given image or archived image file to the converted filename,
750 * leaving a symlink for URL compatibility.
752 * @param string $oldname pre-conversion filename
753 * @param string $basename pre-conversion base filename for dir hashing, if an archive
756 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
757 $newname = $this->conv( $oldname );
758 if( $newname == $oldname ) {
759 // No need to rename; another field triggered this row.
763 if( is_null( $basename ) ) $basename = $oldname;
764 $ubasename = $this->conv( $basename );
765 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
766 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
768 $this->log( "$oldpath -> $newpath" );
769 if( rename( $oldpath, $newpath ) ) {
770 $relpath = $this->relativize( $newpath, dirname( $oldpath ) );
771 if( !symlink( $relpath, $oldpath ) ) {
772 $this->log( "... symlink failed!" );
775 $this->log( "... rename failed!" );
780 * Generate a relative path name to the given file.
781 * Assumes Unix-style paths, separators, and semantics.
783 * @param string $path Absolute destination path including target filename
784 * @param string $from Absolute source path, directory only
789 function relativize( $path, $from ) {
790 $pieces = explode( '/', dirname( $path ) );
791 $against = explode( '/', $from );
793 // Trim off common prefix
794 while( count( $pieces ) && count( $against )
795 && $pieces[0] == $against[0] ) {
796 array_shift( $pieces );
797 array_shift( $against );
800 // relative dots to bump us to the parent
801 while( count( $against ) ) {
802 array_unshift( $pieces, '..' );
803 array_shift( $against );
806 array_push( $pieces, basename( $path ) );
808 return implode( '/', $pieces );
811 function upgradeOldImage() {
814 -- Base filename: key to image.img_name
815 oi_name varchar(255) binary NOT NULL default '',
817 -- Filename of the archived file.
818 -- This is generally a timestamp and '!' prepended to the base name.
819 oi_archive_name varchar(255) binary NOT NULL default '',
821 -- Other fields as in image...
822 oi_size int(8) unsigned NOT NULL default 0,
823 oi_width int(5) NOT NULL default 0,
824 oi_height int(5) NOT NULL default 0,
825 oi_bits int(3) NOT NULL default 0,
826 oi_description tinyblob NOT NULL default '',
827 oi_user int(5) unsigned NOT NULL default '0',
828 oi_user_text varchar(255) binary NOT NULL default '',
829 oi_timestamp char(14) binary NOT NULL default '',
831 INDEX oi_name (oi_name(10))
836 'oi_name' => MW_UPGRADE_ENCODE
,
837 'oi_archive_name' => MW_UPGRADE_ENCODE
,
838 'oi_size' => MW_UPGRADE_COPY
,
839 'oi_width' => MW_UPGRADE_CALLBACK
,
840 'oi_height' => MW_UPGRADE_CALLBACK
,
841 'oi_bits' => MW_UPGRADE_CALLBACK
,
842 'oi_description' => MW_UPGRADE_ENCODE
,
843 'oi_user' => MW_UPGRADE_COPY
,
844 'oi_user_text' => MW_UPGRADE_ENCODE
,
845 'oi_timestamp' => MW_UPGRADE_COPY
);
846 $this->copyTable( 'oldimage', $tabledef, $fields,
847 array( &$this, 'oldimageCallback' ) );
850 function oldimageCallback( $row, $copy ) {
851 // Fill in the new image info fields
852 $info = $this->imageInfo( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
853 $copy['oi_width' ] = $info['width' ];
854 $copy['oi_height'] = $info['height'];
855 $copy['oi_bits' ] = $info['bits' ];
857 // If doing UTF8 conversion the file must be renamed
858 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
864 function upgradeWatchlist() {
865 $fname = 'FiveUpgrade::upgradeWatchlist';
868 extract( $this->dbw
->tableNames( 'watchlist', 'watchlist_temp' ) );
870 $this->log( 'Migrating watchlist table to watchlist_temp...' );
872 "CREATE TABLE $watchlist_temp (
874 wl_user int(5) unsigned NOT NULL,
876 -- Key to page_namespace/page_title
877 -- Note that users may watch patches which do not exist yet,
878 -- or existed in the past but have been deleted.
879 wl_namespace int NOT NULL default '0',
880 wl_title varchar(255) binary NOT NULL default '',
882 -- Timestamp when user was last sent a notification e-mail;
883 -- cleared when the user visits the page.
884 -- FIXME: add proper null support etc
885 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
887 UNIQUE KEY (wl_user, wl_namespace, wl_title),
888 KEY namespace_title (wl_namespace,wl_title)
890 ) TYPE=InnoDB;", $fname );
892 // Fix encoding for Latin-1 upgrades, add some fields,
893 // and double article to article+talk pairs
894 $numwatched = $this->dbw
->selectField( 'watchlist', 'count(*)', '', $fname );
896 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
897 $result = $this->dbr
->select( 'watchlist',
906 while( $row = $this->dbr
->fetchObject( $result ) ) {
907 $now = $this->dbw
->timestamp();
909 'wl_user' => $row->wl_user
,
910 'wl_namespace' => Namespace::getSubject( $row->wl_namespace
),
911 'wl_title' => $this->conv( $row->wl_title
),
912 'wl_notificationtimestamp' => '0' );
913 $this->addChunk( $add );
916 'wl_user' => $row->wl_user
,
917 'wl_namespace' => Namespace::getTalk( $row->wl_namespace
),
918 'wl_title' => $this->conv( $row->wl_title
),
919 'wl_notificationtimestamp' => '0' );
920 $this->addChunk( $add );
922 $this->lastChunk( $add );
923 $this->dbr
->freeResult( $result );
925 $this->log( 'Done converting watchlist.' );
926 $this->cleanupSwaps
[] = 'watchlist';
929 function upgradeLogging() {
932 -- Symbolic keys for the general log type and the action type
933 -- within the log. The output format will be controlled by the
934 -- action field, but only the type controls categorization.
935 log_type char(10) NOT NULL default '',
936 log_action char(10) NOT NULL default '',
939 log_timestamp char(14) NOT NULL default '19700101000000',
941 -- The user who performed this action; key to user_id
942 log_user int unsigned NOT NULL default 0,
944 -- Key to the page affected. Where a user is the target,
945 -- this will point to the user page.
946 log_namespace int NOT NULL default 0,
947 log_title varchar(255) binary NOT NULL default '',
949 -- Freeform text. Interpreted as edit history comments.
950 log_comment varchar(255) NOT NULL default '',
952 -- LF separated list of miscellaneous parameters
953 log_params blob NOT NULL default '',
955 KEY type_time (log_type, log_timestamp),
956 KEY user_time (log_user, log_timestamp),
957 KEY page_time (log_namespace, log_title, log_timestamp)
962 'log_type' => MW_UPGRADE_COPY
,
963 'log_action' => MW_UPGRADE_COPY
,
964 'log_timestamp' => MW_UPGRADE_COPY
,
965 'log_user' => MW_UPGRADE_COPY
,
966 'log_namespace' => MW_UPGRADE_COPY
,
967 'log_title' => MW_UPGRADE_ENCODE
,
968 'log_comment' => MW_UPGRADE_ENCODE
,
969 'log_params' => MW_UPGRADE_ENCODE
);
970 $this->copyTable( 'logging', $tabledef, $fields );
973 function upgradeArchive() {
976 ar_namespace int NOT NULL default '0',
977 ar_title varchar(255) binary NOT NULL default '',
978 ar_text mediumblob NOT NULL default '',
980 ar_comment tinyblob NOT NULL default '',
981 ar_user int(5) unsigned NOT NULL default '0',
982 ar_user_text varchar(255) binary NOT NULL,
983 ar_timestamp char(14) binary NOT NULL default '',
984 ar_minor_edit tinyint(1) NOT NULL default '0',
986 ar_flags tinyblob NOT NULL default '',
988 ar_rev_id int(8) unsigned,
989 ar_text_id int(8) unsigned,
991 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
996 'ar_namespace' => MW_UPGRADE_COPY
,
997 'ar_title' => MW_UPGRADE_ENCODE
,
998 'ar_text' => MW_UPGRADE_COPY
,
999 'ar_comment' => MW_UPGRADE_ENCODE
,
1000 'ar_user' => MW_UPGRADE_COPY
,
1001 'ar_user_text' => MW_UPGRADE_ENCODE
,
1002 'ar_timestamp' => MW_UPGRADE_COPY
,
1003 'ar_minor_edit' => MW_UPGRADE_COPY
,
1004 'ar_flags' => MW_UPGRADE_COPY
,
1005 'ar_rev_id' => MW_UPGRADE_NULL
,
1006 'ar_text_id' => MW_UPGRADE_NULL
);
1007 $this->copyTable( 'archive', $tabledef, $fields );
1010 function upgradeImagelinks() {
1011 global $wgUseLatin1;
1012 if( $wgUseLatin1 ) {
1015 -- Key to page_id of the page containing the image / media link.
1016 il_from int(8) unsigned NOT NULL default '0',
1018 -- Filename of target image.
1019 -- This is also the page_title of the file's description page;
1020 -- all such pages are in namespace 6 (NS_IMAGE).
1021 il_to varchar(255) binary NOT NULL default '',
1023 UNIQUE KEY il_from(il_from,il_to),
1029 'il_from' => MW_UPGRADE_COPY
,
1030 'il_to' => MW_UPGRADE_ENCODE
);
1031 $this->copyTable( 'imagelinks', $tabledef, $fields );
1035 function upgradeCategorylinks() {
1036 global $wgUseLatin1;
1037 if( $wgUseLatin1 ) {
1040 cl_from int(8) unsigned NOT NULL default '0',
1041 cl_to varchar(255) binary NOT NULL default '',
1042 cl_sortkey varchar(86) binary NOT NULL default '',
1043 cl_timestamp timestamp NOT NULL,
1045 UNIQUE KEY cl_from(cl_from,cl_to),
1046 KEY cl_sortkey(cl_to,cl_sortkey),
1047 KEY cl_timestamp(cl_to,cl_timestamp)
1051 'cl_from' => MW_UPGRADE_COPY
,
1052 'cl_to' => MW_UPGRADE_ENCODE
,
1053 'cl_sortkey' => MW_UPGRADE_ENCODE
,
1054 'cl_timestamp' => MW_UPGRADE_COPY
);
1055 $this->copyTable( 'categorylinks', $tabledef, $fields );
1059 function upgradeIpblocks() {
1060 global $wgUseLatin1;
1061 if( $wgUseLatin1 ) {
1064 ipb_id int(8) NOT NULL auto_increment,
1065 ipb_address varchar(40) binary NOT NULL default '',
1066 ipb_user int(8) unsigned NOT NULL default '0',
1067 ipb_by int(8) unsigned NOT NULL default '0',
1068 ipb_reason tinyblob NOT NULL default '',
1069 ipb_timestamp char(14) binary NOT NULL default '',
1070 ipb_auto tinyint(1) NOT NULL default '0',
1071 ipb_expiry char(14) binary NOT NULL default '',
1073 PRIMARY KEY ipb_id (ipb_id),
1074 INDEX ipb_address (ipb_address),
1075 INDEX ipb_user (ipb_user)
1080 'ipb_id' => MW_UPGRADE_COPY
,
1081 'ipb_address' => MW_UPGRADE_COPY
,
1082 'ipb_user' => MW_UPGRADE_COPY
,
1083 'ipb_by' => MW_UPGRADE_COPY
,
1084 'ipb_reason' => MW_UPGRADE_ENCODE
,
1085 'ipb_timestamp' => MW_UPGRADE_COPY
,
1086 'ipb_auto' => MW_UPGRADE_COPY
,
1087 'ipb_expiry' => MW_UPGRADE_COPY
);
1088 $this->copyTable( 'ipblocks', $tabledef, $fields );
1092 function upgradeRecentchanges() {
1093 // There's a format change in the namespace field
1096 rc_id int(8) NOT NULL auto_increment,
1097 rc_timestamp varchar(14) binary NOT NULL default '',
1098 rc_cur_time varchar(14) binary NOT NULL default '',
1100 rc_user int(10) unsigned NOT NULL default '0',
1101 rc_user_text varchar(255) binary NOT NULL default '',
1103 rc_namespace int NOT NULL default '0',
1104 rc_title varchar(255) binary NOT NULL default '',
1106 rc_comment varchar(255) binary NOT NULL default '',
1107 rc_minor tinyint(3) unsigned NOT NULL default '0',
1109 rc_bot tinyint(3) unsigned NOT NULL default '0',
1110 rc_new tinyint(3) unsigned NOT NULL default '0',
1112 rc_cur_id int(10) unsigned NOT NULL default '0',
1113 rc_this_oldid int(10) unsigned NOT NULL default '0',
1114 rc_last_oldid int(10) unsigned NOT NULL default '0',
1116 rc_type tinyint(3) unsigned NOT NULL default '0',
1117 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
1118 rc_moved_to_title varchar(255) binary NOT NULL default '',
1120 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1122 rc_ip char(15) NOT NULL default '',
1124 PRIMARY KEY rc_id (rc_id),
1125 INDEX rc_timestamp (rc_timestamp),
1126 INDEX rc_namespace_title (rc_namespace, rc_title),
1127 INDEX rc_cur_id (rc_cur_id),
1128 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
1134 'rc_id' => MW_UPGRADE_COPY
,
1135 'rc_timestamp' => MW_UPGRADE_COPY
,
1136 'rc_cur_time' => MW_UPGRADE_COPY
,
1137 'rc_user' => MW_UPGRADE_COPY
,
1138 'rc_user_text' => MW_UPGRADE_ENCODE
,
1139 'rc_namespace' => MW_UPGRADE_COPY
,
1140 'rc_title' => MW_UPGRADE_ENCODE
,
1141 'rc_comment' => MW_UPGRADE_ENCODE
,
1142 'rc_minor' => MW_UPGRADE_COPY
,
1143 'rc_bot' => MW_UPGRADE_COPY
,
1144 'rc_new' => MW_UPGRADE_COPY
,
1145 'rc_cur_id' => MW_UPGRADE_COPY
,
1146 'rc_this_oldid' => MW_UPGRADE_COPY
,
1147 'rc_last_oldid' => MW_UPGRADE_COPY
,
1148 'rc_type' => MW_UPGRADE_COPY
,
1149 'rc_moved_to_ns' => MW_UPGRADE_COPY
,
1150 'rc_moved_to_title' => MW_UPGRADE_ENCODE
,
1151 'rc_patrolled' => MW_UPGRADE_COPY
,
1152 'rc_ip' => MW_UPGRADE_COPY
);
1153 $this->copyTable( 'recentchanges', $tabledef, $fields );
1156 function upgradeQuerycache() {
1157 // There's a format change in the namespace field
1160 -- A key name, generally the base name of of the special page.
1161 qc_type char(32) NOT NULL,
1163 -- Some sort of stored value. Sizes, counts...
1164 qc_value int(5) unsigned NOT NULL default '0',
1166 -- Target namespace+title
1167 qc_namespace int NOT NULL default '0',
1168 qc_title char(255) binary NOT NULL default '',
1170 KEY (qc_type,qc_value)
1175 'qc_type' => MW_UPGRADE_COPY
,
1176 'qc_value' => MW_UPGRADE_COPY
,
1177 'qc_namespace' => MW_UPGRADE_COPY
,
1178 'qc_title' => MW_UPGRADE_ENCODE
);
1179 $this->copyTable( 'querycache', $tabledef, $fields );
1183 * Rename all our temporary tables into final place.
1184 * We've left things in place so a read-only wiki can continue running
1185 * on the old code during all this.
1187 function upgradeCleanup() {
1188 $this->renameTable( 'old', 'text' );
1190 foreach( $this->cleanupSwaps
as $table ) {
1191 $this->swap( $table );
1195 function renameTable( $from, $to ) {
1196 $this->log( "Renaming $from to $to..." );
1198 $fromtable = $this->dbw
->tableName( $from );
1199 $totable = $this->dbw
->tableName( $to );
1200 $this->dbw
->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1203 function swap( $base ) {
1204 $this->renameTable( $base, "{$base}_old" );
1205 $this->renameTable( "{$base}_temp", $base );
1210 $upgrade = new FiveUpgrade();
1211 $step = isset( $options['step'] ) ?
$options['step'] : null;
1212 $upgrade->upgrade( $step );