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->newConnection();
34 $this->dbr
->bufferResults( false );
35 $this->slave
=& wfGetDB( DB_SLAVE
);
37 $this->cleanupSwaps
= array();
38 $this->emailAuth
= false; # don't preauthenticate emails
39 $this->maxLag
= 10; # if slaves are lagged more than 10 secs, wait
42 function doing( $step ) {
43 return is_null( $this->step
) ||
$step == $this->step
;
46 function upgrade( $step ) {
63 foreach( $tables as $table ) {
64 if( $this->doing( $table ) ) {
65 $method = 'upgrade' . ucfirst( $table );
70 if( $this->doing( 'cleanup' ) ) {
71 $this->upgradeCleanup();
77 * Open a second connection to the master server, with buffering off.
78 * This will let us stream large datasets in and write in chunks on the
83 function &newConnection() {
84 global $wgDBadminuser, $wgDBadminpassword;
85 global $wgDBserver, $wgDBname;
86 $db =& new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
91 * Prepare a conversion array for converting Windows Code Page 1252 to
92 * UTF-8. This should provide proper conversion of text that was miscoded
93 * as Windows-1252 by naughty user-agents, and doesn't rely on an outside
99 function prepareWindows1252() {
101 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
102 static $cp1252 = array(
103 0x80 => 0x20AC, #EURO SIGN
104 0x81 => UNICODE_REPLACEMENT
,
105 0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
106 0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
107 0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
108 0x85 => 0x2026, #HORIZONTAL ELLIPSIS
109 0x86 => 0x2020, #DAGGER
110 0x87 => 0x2021, #DOUBLE DAGGER
111 0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
112 0x89 => 0x2030, #PER MILLE SIGN
113 0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
114 0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
115 0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
116 0x8D => UNICODE_REPLACEMENT
,
117 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
118 0x8F => UNICODE_REPLACEMENT
,
119 0x90 => UNICODE_REPLACEMENT
,
120 0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
121 0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
122 0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
123 0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
124 0x95 => 0x2022, #BULLET
125 0x96 => 0x2013, #EN DASH
126 0x97 => 0x2014, #EM DASH
127 0x98 => 0x02DC, #SMALL TILDE
128 0x99 => 0x2122, #TRADE MARK SIGN
129 0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
130 0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
131 0x9C => 0x0153, #LATIN SMALL LIGATURE OE
132 0x9D => UNICODE_REPLACEMENT
,
133 0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
134 0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
137 for( $i = 0; $i < 0x100; $i++
) {
138 $unicode = isset( $cp1252[$i] ) ?
$cp1252[$i] : $i;
139 $pairs[chr( $i )] = codepointToUtf8( $unicode );
145 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
146 * @param string $text
150 function conv( $text ) {
152 return is_null( $text )
155 ?
strtr( $text, $this->conversionTables
)
160 * Dump timestamp and message to output
161 * @param string $message
164 function log( $message ) {
165 echo wfTimestamp( TS_DB
) . ': ' . $message . "\n";
170 * Initialize the chunked-insert system.
171 * Rows will be inserted in chunks of the given number, rather
172 * than in a giant INSERT...SELECT query, to keep the serialized
173 * MySQL database replication from getting hung up. This way other
174 * things can be going on during conversion without waiting for
175 * slaves to catch up as badly.
177 * @param int $chunksize Number of rows to insert at once
178 * @param int $final Total expected number of rows / id of last row,
179 * used for progress reports.
180 * @param string $table to insert on
181 * @param string $fname function name to report in SQL
184 function setChunkScale( $chunksize, $final, $table, $fname ) {
185 $this->chunkSize
= $chunksize;
186 $this->chunkFinal
= $final;
187 $this->chunkCount
= 0;
188 $this->chunkStartTime
= wfTime();
189 $this->chunkOptions
= array();
190 $this->chunkTable
= $table;
191 $this->chunkFunction
= $fname;
195 * Chunked inserts: perform an insert if we've reached the chunk limit.
196 * Prints a progress report with estimated completion time.
197 * @param array &$chunk -- This will be emptied if an insert is done.
198 * @param int $key A key identifier to use in progress estimation in
199 * place of the number of rows inserted. Use this if
200 * you provided a max key number instead of a count
201 * as the final chunk number in setChunkScale()
204 function addChunk( &$chunk, $key = null ) {
205 if( count( $chunk ) >= $this->chunkSize
) {
206 $this->insertChunk( $chunk );
208 $this->chunkCount +
= count( $chunk );
210 $delta = $now - $this->chunkStartTime
;
211 $rate = $this->chunkCount
/ $delta;
213 if( is_null( $key ) ) {
214 $completed = $this->chunkCount
;
218 $portion = $completed / $this->chunkFinal
;
220 $estimatedTotalTime = $delta / $portion;
221 $eta = $this->chunkStartTime +
$estimatedTotalTime;
223 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
224 wfTimestamp( TS_DB
, intval( $now ) ),
227 wfTimestamp( TS_DB
, intval( $eta ) ),
238 * Chunked inserts: perform an insert unconditionally, at the end, and log.
239 * @param array &$chunk -- This will be emptied if an insert is done.
242 function lastChunk( &$chunk ) {
243 $n = count( $chunk );
245 $this->insertChunk( $chunk );
247 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
251 * Chunked inserts: perform an insert.
252 * @param array &$chunk -- This will be emptied if an insert is done.
255 function insertChunk( &$chunk ) {
256 // Give slaves a chance to catch up
257 wfWaitForSlaves( $this->maxLag
);
258 $this->dbw
->insert( $this->chunkTable
, $chunk, $this->chunkFunction
, $this->chunkOptions
);
263 * Copy and transcode a table to table_temp.
264 * @param string $name Base name of the source table
265 * @param string $tabledef CREATE TABLE definition, w/ $1 for the name
266 * @param array $fields set of destination fields to these constants:
267 * MW_UPGRADE_COPY - straight copy
268 * MW_UPGRADE_ENCODE - for old Latin1 wikis, conv to UTF-8
269 * MW_UPGRADE_NULL - just put NULL
270 * @param callable $callback An optional callback to modify the data
271 * or perform other processing. Func should be
272 * ( object $row, array $copy ) and return $copy
275 function copyTable( $name, $tabledef, $fields, $callback = null ) {
276 $fname = 'FiveUpgrade::copyTable';
278 $name_temp = $name . '_temp';
279 $this->log( "Migrating $name table to $name_temp..." );
281 $table = $this->dbw
->tableName( $name );
282 $table_temp = $this->dbw
->tableName( $name_temp );
284 // Create temporary table; we're going to copy everything in there,
285 // then at the end rename the final tables into place.
286 $def = str_replace( '$1', $table_temp, $tabledef );
287 $this->dbw
->query( $def, $fname );
289 $numRecords = $this->dbw
->selectField( $name, 'COUNT(*)', '', $fname );
290 $this->setChunkScale( 100, $numRecords, $name_temp, $fname );
292 // Pull all records from the second, streaming database connection.
293 $sourceFields = array_keys( array_filter( $fields,
294 create_function( '$x', 'return $x !== MW_UPGRADE_NULL;' ) ) );
295 $result = $this->dbr
->select( $name,
301 while( $row = $this->dbr
->fetchObject( $result ) ) {
303 foreach( $fields as $field => $source ) {
304 if( $source === MW_UPGRADE_COPY
) {
305 $copy[$field] = $row->$field;
306 } elseif( $source === MW_UPGRADE_ENCODE
) {
307 $copy[$field] = $this->conv( $row->$field );
308 } elseif( $source === MW_UPGRADE_NULL
) {
309 $copy[$field] = null;
311 $this->log( "Unknown field copy type: $field => $source" );
314 if( is_callable( $callback ) ) {
315 $copy = call_user_func( $callback, $row, $copy );
318 $this->addChunk( $add );
320 $this->lastChunk( $add );
321 $this->dbr
->freeResult( $result );
323 $this->log( "Done converting $name." );
324 $this->cleanupSwaps
[] = $name;
327 function upgradePage() {
328 $fname = "FiveUpgrade::upgradePage";
331 if( $this->dbw
->tableExists( 'page' ) ) {
332 $this->log( 'Page table already exists; aborting.' );
336 $this->log( "Checking cur table for unique title index and applying if necessary" );
339 $this->log( "...converting from cur/old to page/revision/text DB structure." );
341 extract( $this->dbw
->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
343 $this->log( "Creating page and revision tables..." );
344 $this->dbw
->query("CREATE TABLE $page (
345 page_id int(8) unsigned NOT NULL auto_increment,
346 page_namespace int NOT NULL,
347 page_title varchar(255) binary NOT NULL,
348 page_restrictions tinyblob NOT NULL default '',
349 page_counter bigint(20) unsigned NOT NULL default '0',
350 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
351 page_is_new tinyint(1) unsigned NOT NULL default '0',
352 page_random real unsigned NOT NULL,
353 page_touched char(14) binary NOT NULL default '',
354 page_latest int(8) unsigned NOT NULL,
355 page_len int(8) unsigned NOT NULL,
357 PRIMARY KEY page_id (page_id),
358 UNIQUE INDEX name_title (page_namespace,page_title),
361 ) TYPE=InnoDB", $fname );
362 $this->dbw
->query("CREATE TABLE $revision (
363 rev_id int(8) unsigned NOT NULL auto_increment,
364 rev_page int(8) unsigned NOT NULL,
365 rev_text_id int(8) unsigned NOT NULL,
366 rev_comment tinyblob NOT NULL default '',
367 rev_user int(5) unsigned NOT NULL default '0',
368 rev_user_text varchar(255) binary NOT NULL default '',
369 rev_timestamp char(14) binary NOT NULL default '',
370 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
371 rev_deleted tinyint(1) unsigned NOT NULL default '0',
373 PRIMARY KEY rev_page_id (rev_page, rev_id),
374 UNIQUE INDEX rev_id (rev_id),
375 INDEX rev_timestamp (rev_timestamp),
376 INDEX page_timestamp (rev_page,rev_timestamp),
377 INDEX user_timestamp (rev_user,rev_timestamp),
378 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
379 ) TYPE=InnoDB", $fname );
381 $maxold = $this->dbw
->selectField( 'old', 'max(old_id)', '', $fname );
382 $this->log( "Last old record is {$maxold}" );
384 global $wgLegacySchemaConversion;
385 if( $wgLegacySchemaConversion ) {
386 // Create HistoryBlobCurStub entries.
387 // Text will be pulled from the leftover 'cur' table at runtime.
388 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
389 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
390 $cur_flags = "'object'";
392 // Copy all cur text in immediately: this may take longer but avoids
393 // having to keep an extra table around.
394 echo "......Moving text from cur.\n";
395 $cur_text = 'cur_text';
399 $maxcur = $this->dbw
->selectField( 'cur', 'max(cur_id)', '', $fname );
400 $this->log( "Last cur entry is $maxcur" );
403 * Copy placeholder records for each page's current version into old
404 * Don't do any conversion here; text records are converted at runtime
405 * based on the flags (and may be originally binary!) while the meta
406 * fields will be converted in the old -> rev and cur -> page steps.
408 $this->setChunkScale( $chunksize, $maxcur, 'old', $fname );
409 $result = $this->dbr
->query(
410 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
411 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
413 ORDER BY cur_id", $fname );
415 while( $row = $this->dbr
->fetchObject( $result ) ) {
417 'old_namespace' => $row->cur_namespace
,
418 'old_title' => $row->cur_title
,
419 'old_text' => $row->text
,
420 'old_comment' => $row->cur_comment
,
421 'old_user' => $row->cur_user
,
422 'old_user_text' => $row->cur_user_text
,
423 'old_timestamp' => $row->cur_timestamp
,
424 'old_minor_edit' => $row->cur_minor_edit
,
425 'old_flags' => $row->flags
);
426 $this->addChunk( $add, $row->cur_id
);
428 $this->lastChunk( $add );
429 $this->dbr
->freeResult( $result );
432 * Copy revision metadata from old into revision.
433 * We'll also do UTF-8 conversion of usernames and comments.
435 #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
436 #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
437 $countold = $this->dbw
->selectField( 'old', 'count(old_id)', '', $fname );
438 $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
440 $this->log( "......Setting up revision table." );
441 $result = $this->dbr
->query(
442 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
443 old_timestamp, old_minor_edit
444 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
448 while( $row = $this->dbr
->fetchObject( $result ) ) {
450 'rev_id' => $row->old_id
,
451 'rev_page' => $row->cur_id
,
452 'rev_text_id' => $row->old_id
,
453 'rev_comment' => $this->conv( $row->old_comment
),
454 'rev_user' => $row->old_user
,
455 'rev_user_text' => $this->conv( $row->old_user_text
),
456 'rev_timestamp' => $row->old_timestamp
,
457 'rev_minor_edit' => $row->old_minor_edit
);
458 $this->addChunk( $add );
460 $this->lastChunk( $add );
461 $this->dbr
->freeResult( $result );
465 * Copy page metadata from cur into page.
466 * We'll also do UTF-8 conversion of titles.
468 $this->log( "......Setting up page table." );
469 $this->setChunkScale( $chunksize, $maxcur, 'page', $fname );
470 $result = $this->dbr
->query( "
471 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
472 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
474 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
475 ORDER BY cur_id", $fname );
477 while( $row = $this->dbr
->fetchObject( $result ) ) {
479 'page_id' => $row->cur_id
,
480 'page_namespace' => $row->cur_namespace
,
481 'page_title' => $this->conv( $row->cur_title
),
482 'page_restrictions' => $row->cur_restrictions
,
483 'page_counter' => $row->cur_counter
,
484 'page_is_redirect' => $row->cur_is_redirect
,
485 'page_is_new' => $row->cur_is_new
,
486 'page_random' => $row->cur_random
,
487 'page_touched' => $this->dbw
->timestamp(),
488 'page_latest' => $row->rev_id
,
489 'page_len' => $row->len
);
490 $this->addChunk( $add, $row->cur_id
);
492 $this->lastChunk( $add );
493 $this->dbr
->freeResult( $result );
495 $this->log( "...done with cur/old -> page/revision." );
498 function upgradeLinks() {
499 $fname = 'FiveUpgrade::upgradeLinks';
501 extract( $this->dbw
->tableNames( 'links', 'brokenlinks', 'pagelinks', 'page' ) );
503 $this->log( 'Creating pagelinks table...' );
505 CREATE TABLE $pagelinks (
506 -- Key to the page_id of the page containing the link.
507 pl_from int(8) unsigned NOT NULL default '0',
509 -- Key to page_namespace/page_title of the target page.
510 -- The target page may or may not exist, and due to renames
511 -- and deletions may refer to different page records as time
513 pl_namespace int NOT NULL default '0',
514 pl_title varchar(255) binary NOT NULL default '',
516 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
517 KEY (pl_namespace,pl_title)
521 $this->log( 'Importing live links -> pagelinks' );
522 $nlinks = $this->dbw
->selectField( 'links', 'count(*)', '', $fname );
524 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
525 $result = $this->dbr
->query( "
526 SELECT l_from,page_namespace,page_title
528 WHERE l_to=page_id", $fname );
530 while( $row = $this->dbr
->fetchObject( $result ) ) {
532 'pl_from' => $row->l_from
,
533 'pl_namespace' => $row->page_namespace
,
534 'pl_title' => $row->page_title
);
535 $this->addChunk( $add );
537 $this->lastChunk( $add );
539 $this->log( 'no links!' );
542 $this->log( 'Importing brokenlinks -> pagelinks' );
543 $nbrokenlinks = $this->dbw
->selectField( 'brokenlinks', 'count(*)', '', $fname );
544 if( $nbrokenlinks ) {
545 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
546 $this->chunkOptions
= array( 'IGNORE' );
547 $result = $this->dbr
->query(
548 "SELECT bl_from, bl_to FROM $brokenlinks",
551 while( $row = $this->dbr
->fetchObject( $result ) ) {
552 $pagename = $this->conv( $row->bl_to
);
553 $title = Title
::newFromText( $pagename );
554 if( is_null( $title ) ) {
555 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
558 'pl_from' => $row->bl_from
,
559 'pl_namespace' => $title->getNamespace(),
560 'pl_title' => $title->getDBkey() );
561 $this->addChunk( $add );
564 $this->lastChunk( $add );
566 $this->log( 'no brokenlinks!' );
569 $this->log( 'Done with links.' );
572 function upgradeUser() {
573 // Apply unique index, if necessary:
574 $duper = new UserDupes( $this->dbw
);
575 if( $duper->hasUniqueIndex() ) {
576 $this->log( "Already have unique user_name index." );
578 $this->log( "Clearing user duplicates..." );
579 if( !$duper->clearDupes() ) {
580 $this->log( "WARNING: Duplicate user accounts, may explode!" );
586 user_id int(5) unsigned NOT NULL auto_increment,
587 user_name varchar(255) binary NOT NULL default '',
588 user_real_name varchar(255) binary NOT NULL default '',
589 user_password tinyblob NOT NULL default '',
590 user_newpassword tinyblob NOT NULL default '',
591 user_email tinytext NOT NULL default '',
592 user_options blob NOT NULL default '',
593 user_touched char(14) binary NOT NULL default '',
594 user_token char(32) binary NOT NULL default '',
595 user_email_authenticated CHAR(14) BINARY,
596 user_email_token CHAR(32) BINARY,
597 user_email_token_expires CHAR(14) BINARY,
599 PRIMARY KEY user_id (user_id),
600 UNIQUE INDEX user_name (user_name),
601 INDEX (user_email_token)
606 'user_id' => MW_UPGRADE_COPY
,
607 'user_name' => MW_UPGRADE_ENCODE
,
608 'user_real_name' => MW_UPGRADE_ENCODE
,
609 'user_password' => MW_UPGRADE_COPY
,
610 'user_newpassword' => MW_UPGRADE_COPY
,
611 'user_email' => MW_UPGRADE_ENCODE
,
612 'user_options' => MW_UPGRADE_ENCODE
,
613 'user_touched' => MW_UPGRADE_CALLBACK
,
614 'user_token' => MW_UPGRADE_COPY
,
615 'user_email_authenticated' => MW_UPGRADE_CALLBACK
,
616 'user_email_token' => MW_UPGRADE_NULL
,
617 'user_email_token_expires' => MW_UPGRADE_NULL
);
618 $this->copyTable( 'user', $tabledef, $fields,
619 array( &$this, 'userCallback' ) );
622 function userCallback( $row, $copy ) {
623 $now = $this->dbw
->timestamp();
624 $copy['user_touched'] = $now;
625 $copy['user_email_authenticated'] = $this->emailAuth ?
$now : null;
629 function upgradeImage() {
632 img_name varchar(255) binary NOT NULL default '',
633 img_size int(8) unsigned NOT NULL default '0',
634 img_width int(5) NOT NULL default '0',
635 img_height int(5) NOT NULL default '0',
636 img_metadata mediumblob NOT NULL,
637 img_bits int(3) NOT NULL default '0',
638 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
639 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
640 img_minor_mime varchar(32) NOT NULL default "unknown",
641 img_description tinyblob NOT NULL default '',
642 img_user int(5) unsigned NOT NULL default '0',
643 img_user_text varchar(255) binary NOT NULL default '',
644 img_timestamp char(14) binary NOT NULL default '',
646 PRIMARY KEY img_name (img_name),
647 INDEX img_size (img_size),
648 INDEX img_timestamp (img_timestamp)
652 'img_name' => MW_UPGRADE_ENCODE
,
653 'img_size' => MW_UPGRADE_COPY
,
654 'img_width' => MW_UPGRADE_CALLBACK
,
655 'img_height' => MW_UPGRADE_CALLBACK
,
656 'img_metadata' => MW_UPGRADE_CALLBACK
,
657 'img_bits' => MW_UPGRADE_CALLBACK
,
658 'img_media_type' => MW_UPGRADE_CALLBACK
,
659 'img_major_mime' => MW_UPGRADE_CALLBACK
,
660 'img_minor_mime' => MW_UPGRADE_CALLBACK
,
661 'img_description' => MW_UPGRADE_ENCODE
,
662 'img_user' => MW_UPGRADE_COPY
,
663 'img_user_text' => MW_UPGRADE_ENCODE
,
664 'img_timestamp' => MW_UPGRADE_COPY
);
665 $this->copyTable( 'image', $tabledef, $fields,
666 array( &$this, 'imageCallback' ) );
669 function imageCallback( $row, $copy ) {
670 // Fill in the new image info fields
671 $info = $this->imageInfo( $row->img_name
);
673 $copy['img_width' ] = $info['width'];
674 $copy['img_height' ] = $info['height'];
675 $copy['img_metadata' ] = ""; // loaded on-demand
676 $copy['img_bits' ] = $info['bits'];
677 $copy['img_media_type'] = $info['media'];
678 $copy['img_major_mime'] = $info['major'];
679 $copy['img_minor_mime'] = $info['minor'];
681 // If doing UTF8 conversion the file must be renamed
682 $this->renameFile( $row->img_name
, 'wfImageDir' );
687 function imageInfo( $name, $subdirCallback='wfImageDir', $basename = null ) {
688 if( is_null( $basename ) ) $basename = $name;
689 $dir = call_user_func( $subdirCallback, $basename );
690 $filename = $dir . '/' . $name;
699 $magic =& wfGetMimeMagic();
700 $mime = $magic->guessMimeType( $filename, true );
701 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
703 $info['media'] = $magic->getMediaType( $filename, $mime );
707 if( $mime == 'image/svg' ) {
708 $gis = wfGetSVGsize( $this->imagePath
);
709 } elseif( $magic->isPHPImageType( $mime ) ) {
710 $gis = getimagesize( $filename );
712 $this->log( "Surprising mime type: $mime" );
715 $info['width' ] = $gis[0];
716 $info['height'] = $gis[1];
718 if( isset( $gis['bits'] ) ) {
719 $info['bits'] = $gis['bits'];
728 * @param string $table The table name to be truncated
730 function clearTable( $table ) {
731 print "Clearing $table...\n";
732 $tableName = $this->db
->tableName( $table );
733 $this->db
->query( 'TRUNCATE $tableName' );
737 * Rename a given image or archived image file to the converted filename,
738 * leaving a symlink for URL compatibility.
740 * @param string $oldname pre-conversion filename
741 * @param string $basename pre-conversion base filename for dir hashing, if an archive
744 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
745 $newname = $this->conv( $oldname );
746 if( $newname == $oldname ) {
747 // No need to rename; another field triggered this row.
751 if( is_null( $basename ) ) $basename = $oldname;
752 $ubasename = $this->conv( $basename );
753 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
754 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
756 $this->log( "$oldpath -> $newpath" );
757 if( rename( $oldpath, $newpath ) ) {
758 $relpath = $this->relativize( $newpath, dirname( $oldpath ) );
759 if( !symlink( $relpath, $oldpath ) ) {
760 $this->log( "... symlink failed!" );
763 $this->log( "... rename failed!" );
768 * Generate a relative path name to the given file.
769 * Assumes Unix-style paths, separators, and semantics.
771 * @param string $path Absolute destination path including target filename
772 * @param string $from Absolute source path, directory only
777 function relativize( $path, $from ) {
778 $pieces = explode( '/', dirname( $path ) );
779 $against = explode( '/', $from );
781 // Trim off common prefix
782 while( count( $pieces ) && count( $against )
783 && $pieces[0] == $against[0] ) {
784 array_shift( $pieces );
785 array_shift( $against );
788 // relative dots to bump us to the parent
789 while( count( $against ) ) {
790 array_unshift( $pieces, '..' );
791 array_shift( $against );
794 array_push( $pieces, basename( $path ) );
796 return implode( '/', $pieces );
799 function upgradeOldImage() {
802 -- Base filename: key to image.img_name
803 oi_name varchar(255) binary NOT NULL default '',
805 -- Filename of the archived file.
806 -- This is generally a timestamp and '!' prepended to the base name.
807 oi_archive_name varchar(255) binary NOT NULL default '',
809 -- Other fields as in image...
810 oi_size int(8) unsigned NOT NULL default 0,
811 oi_width int(5) NOT NULL default 0,
812 oi_height int(5) NOT NULL default 0,
813 oi_bits int(3) NOT NULL default 0,
814 oi_description tinyblob NOT NULL default '',
815 oi_user int(5) unsigned NOT NULL default '0',
816 oi_user_text varchar(255) binary NOT NULL default '',
817 oi_timestamp char(14) binary NOT NULL default '',
819 INDEX oi_name (oi_name(10))
824 'oi_name' => MW_UPGRADE_ENCODE
,
825 'oi_archive_name' => MW_UPGRADE_ENCODE
,
826 'oi_size' => MW_UPGRADE_COPY
,
827 'oi_width' => MW_UPGRADE_CALLBACK
,
828 'oi_height' => MW_UPGRADE_CALLBACK
,
829 'oi_bits' => MW_UPGRADE_CALLBACK
,
830 'oi_description' => MW_UPGRADE_ENCODE
,
831 'oi_user' => MW_UPGRADE_COPY
,
832 'oi_user_text' => MW_UPGRADE_ENCODE
,
833 'oi_timestamp' => MW_UPGRADE_COPY
);
834 $this->copyTable( 'oldimage', $tabledef, $fields,
835 array( &$this, 'oldimageCallback' ) );
838 function oldimageCallback( $row, $copy ) {
839 // Fill in the new image info fields
840 $info = $this->imageInfo( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
841 $copy['oi_width' ] = $info['width' ];
842 $copy['oi_height'] = $info['height'];
843 $copy['oi_bits' ] = $info['bits' ];
845 // If doing UTF8 conversion the file must be renamed
846 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
852 function upgradeWatchlist() {
853 $fname = 'FiveUpgrade::upgradeWatchlist';
856 extract( $this->dbw
->tableNames( 'watchlist', 'watchlist_temp' ) );
858 $this->log( 'Migrating watchlist table to watchlist_temp...' );
860 "CREATE TABLE $watchlist_temp (
862 wl_user int(5) unsigned NOT NULL,
864 -- Key to page_namespace/page_title
865 -- Note that users may watch patches which do not exist yet,
866 -- or existed in the past but have been deleted.
867 wl_namespace int NOT NULL default '0',
868 wl_title varchar(255) binary NOT NULL default '',
870 -- Timestamp when user was last sent a notification e-mail;
871 -- cleared when the user visits the page.
872 -- FIXME: add proper null support etc
873 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
875 UNIQUE KEY (wl_user, wl_namespace, wl_title),
876 KEY namespace_title (wl_namespace,wl_title)
878 ) TYPE=InnoDB;", $fname );
880 // Fix encoding for Latin-1 upgrades, add some fields,
881 // and double article to article+talk pairs
882 $numwatched = $this->dbw
->selectField( 'watchlist', 'count(*)', '', $fname );
884 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
885 $result = $this->dbr
->select( 'watchlist',
894 while( $row = $this->dbr
->fetchObject( $result ) ) {
895 $now = $this->dbw
->timestamp();
897 'wl_user' => $row->wl_user
,
898 'wl_namespace' => Namespace::getSubject( $row->wl_namespace
),
899 'wl_title' => $this->conv( $row->wl_title
),
900 'wl_notificationtimestamp' => '0' );
901 $this->addChunk( $add );
904 'wl_user' => $row->wl_user
,
905 'wl_namespace' => Namespace::getTalk( $row->wl_namespace
),
906 'wl_title' => $this->conv( $row->wl_title
),
907 'wl_notificationtimestamp' => '0' );
908 $this->addChunk( $add );
910 $this->lastChunk( $add );
911 $this->dbr
->freeResult( $result );
913 $this->log( 'Done converting watchlist.' );
914 $this->cleanupSwaps
[] = 'watchlist';
917 function upgradeLogging() {
920 -- Symbolic keys for the general log type and the action type
921 -- within the log. The output format will be controlled by the
922 -- action field, but only the type controls categorization.
923 log_type char(10) NOT NULL default '',
924 log_action char(10) NOT NULL default '',
927 log_timestamp char(14) NOT NULL default '19700101000000',
929 -- The user who performed this action; key to user_id
930 log_user int unsigned NOT NULL default 0,
932 -- Key to the page affected. Where a user is the target,
933 -- this will point to the user page.
934 log_namespace int NOT NULL default 0,
935 log_title varchar(255) binary NOT NULL default '',
937 -- Freeform text. Interpreted as edit history comments.
938 log_comment varchar(255) NOT NULL default '',
940 -- LF separated list of miscellaneous parameters
941 log_params blob NOT NULL default '',
943 KEY type_time (log_type, log_timestamp),
944 KEY user_time (log_user, log_timestamp),
945 KEY page_time (log_namespace, log_title, log_timestamp)
950 'log_type' => MW_UPGRADE_COPY
,
951 'log_action' => MW_UPGRADE_COPY
,
952 'log_timestamp' => MW_UPGRADE_COPY
,
953 'log_user' => MW_UPGRADE_COPY
,
954 'log_namespace' => MW_UPGRADE_COPY
,
955 'log_title' => MW_UPGRADE_ENCODE
,
956 'log_comment' => MW_UPGRADE_ENCODE
,
957 'log_params' => MW_UPGRADE_ENCODE
);
958 $this->copyTable( 'logging', $tabledef, $fields );
961 function upgradeArchive() {
964 ar_namespace int NOT NULL default '0',
965 ar_title varchar(255) binary NOT NULL default '',
966 ar_text mediumblob NOT NULL default '',
968 ar_comment tinyblob NOT NULL default '',
969 ar_user int(5) unsigned NOT NULL default '0',
970 ar_user_text varchar(255) binary NOT NULL,
971 ar_timestamp char(14) binary NOT NULL default '',
972 ar_minor_edit tinyint(1) NOT NULL default '0',
974 ar_flags tinyblob NOT NULL default '',
976 ar_rev_id int(8) unsigned,
977 ar_text_id int(8) unsigned,
979 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
984 'ar_namespace' => MW_UPGRADE_COPY
,
985 'ar_title' => MW_UPGRADE_ENCODE
,
986 'ar_text' => MW_UPGRADE_COPY
,
987 'ar_comment' => MW_UPGRADE_ENCODE
,
988 'ar_user' => MW_UPGRADE_COPY
,
989 'ar_user_text' => MW_UPGRADE_ENCODE
,
990 'ar_timestamp' => MW_UPGRADE_COPY
,
991 'ar_minor_edit' => MW_UPGRADE_COPY
,
992 'ar_flags' => MW_UPGRADE_COPY
,
993 'ar_rev_id' => MW_UPGRADE_NULL
,
994 'ar_text_id' => MW_UPGRADE_NULL
);
995 $this->copyTable( 'archive', $tabledef, $fields );
998 function upgradeImagelinks() {
1000 if( $wgUseLatin1 ) {
1003 -- Key to page_id of the page containing the image / media link.
1004 il_from int(8) unsigned NOT NULL default '0',
1006 -- Filename of target image.
1007 -- This is also the page_title of the file's description page;
1008 -- all such pages are in namespace 6 (NS_IMAGE).
1009 il_to varchar(255) binary NOT NULL default '',
1011 UNIQUE KEY il_from(il_from,il_to),
1017 'il_from' => MW_UPGRADE_COPY
,
1018 'il_to' => MW_UPGRADE_ENCODE
);
1019 $this->copyTable( 'imagelinks', $tabledef, $fields );
1023 function upgradeCategorylinks() {
1024 global $wgUseLatin1;
1025 if( $wgUseLatin1 ) {
1028 cl_from int(8) unsigned NOT NULL default '0',
1029 cl_to varchar(255) binary NOT NULL default '',
1030 cl_sortkey varchar(86) binary NOT NULL default '',
1031 cl_timestamp timestamp NOT NULL,
1033 UNIQUE KEY cl_from(cl_from,cl_to),
1034 KEY cl_sortkey(cl_to,cl_sortkey),
1035 KEY cl_timestamp(cl_to,cl_timestamp)
1039 'cl_from' => MW_UPGRADE_COPY
,
1040 'cl_to' => MW_UPGRADE_ENCODE
,
1041 'cl_sortkey' => MW_UPGRADE_ENCODE
,
1042 'cl_timestamp' => MW_UPGRADE_COPY
);
1043 $this->copyTable( 'categorylinks', $tabledef, $fields );
1047 function upgradeIpblocks() {
1048 global $wgUseLatin1;
1049 if( $wgUseLatin1 ) {
1052 ipb_id int(8) NOT NULL auto_increment,
1053 ipb_address varchar(40) binary NOT NULL default '',
1054 ipb_user int(8) unsigned NOT NULL default '0',
1055 ipb_by int(8) unsigned NOT NULL default '0',
1056 ipb_reason tinyblob NOT NULL default '',
1057 ipb_timestamp char(14) binary NOT NULL default '',
1058 ipb_auto tinyint(1) NOT NULL default '0',
1059 ipb_expiry char(14) binary NOT NULL default '',
1061 PRIMARY KEY ipb_id (ipb_id),
1062 INDEX ipb_address (ipb_address),
1063 INDEX ipb_user (ipb_user)
1068 'ipb_id' => MW_UPGRADE_COPY
,
1069 'ipb_address' => MW_UPGRADE_COPY
,
1070 'ipb_user' => MW_UPGRADE_COPY
,
1071 'ipb_by' => MW_UPGRADE_COPY
,
1072 'ipb_reason' => MW_UPGRADE_ENCODE
,
1073 'ipb_timestamp' => MW_UPGRADE_COPY
,
1074 'ipb_auto' => MW_UPGRADE_COPY
,
1075 'ipb_expiry' => MW_UPGRADE_COPY
);
1076 $this->copyTable( 'ipblocks', $tabledef, $fields );
1080 function upgradeRecentchanges() {
1081 // There's a format change in the namespace field
1084 rc_id int(8) NOT NULL auto_increment,
1085 rc_timestamp varchar(14) binary NOT NULL default '',
1086 rc_cur_time varchar(14) binary NOT NULL default '',
1088 rc_user int(10) unsigned NOT NULL default '0',
1089 rc_user_text varchar(255) binary NOT NULL default '',
1091 rc_namespace int NOT NULL default '0',
1092 rc_title varchar(255) binary NOT NULL default '',
1094 rc_comment varchar(255) binary NOT NULL default '',
1095 rc_minor tinyint(3) unsigned NOT NULL default '0',
1097 rc_bot tinyint(3) unsigned NOT NULL default '0',
1098 rc_new tinyint(3) unsigned NOT NULL default '0',
1100 rc_cur_id int(10) unsigned NOT NULL default '0',
1101 rc_this_oldid int(10) unsigned NOT NULL default '0',
1102 rc_last_oldid int(10) unsigned NOT NULL default '0',
1104 rc_type tinyint(3) unsigned NOT NULL default '0',
1105 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
1106 rc_moved_to_title varchar(255) binary NOT NULL default '',
1108 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1110 rc_ip char(15) NOT NULL default '',
1112 PRIMARY KEY rc_id (rc_id),
1113 INDEX rc_timestamp (rc_timestamp),
1114 INDEX rc_namespace_title (rc_namespace, rc_title),
1115 INDEX rc_cur_id (rc_cur_id),
1116 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
1122 'rc_id' => MW_UPGRADE_COPY
,
1123 'rc_timestamp' => MW_UPGRADE_COPY
,
1124 'rc_cur_time' => MW_UPGRADE_COPY
,
1125 'rc_user' => MW_UPGRADE_COPY
,
1126 'rc_user_text' => MW_UPGRADE_ENCODE
,
1127 'rc_namespace' => MW_UPGRADE_COPY
,
1128 'rc_title' => MW_UPGRADE_ENCODE
,
1129 'rc_comment' => MW_UPGRADE_ENCODE
,
1130 'rc_minor' => MW_UPGRADE_COPY
,
1131 'rc_bot' => MW_UPGRADE_COPY
,
1132 'rc_new' => MW_UPGRADE_COPY
,
1133 'rc_cur_id' => MW_UPGRADE_COPY
,
1134 'rc_this_oldid' => MW_UPGRADE_COPY
,
1135 'rc_last_oldid' => MW_UPGRADE_COPY
,
1136 'rc_type' => MW_UPGRADE_COPY
,
1137 'rc_moved_to_ns' => MW_UPGRADE_COPY
,
1138 'rc_moved_to_title' => MW_UPGRADE_ENCODE
,
1139 'rc_patrolled' => MW_UPGRADE_COPY
,
1140 'rc_ip' => MW_UPGRADE_COPY
);
1141 $this->copyTable( 'recentchanges', $tabledef, $fields );
1144 function upgradeQuerycache() {
1145 // There's a format change in the namespace field
1148 -- A key name, generally the base name of of the special page.
1149 qc_type char(32) NOT NULL,
1151 -- Some sort of stored value. Sizes, counts...
1152 qc_value int(5) unsigned NOT NULL default '0',
1154 -- Target namespace+title
1155 qc_namespace int NOT NULL default '0',
1156 qc_title char(255) binary NOT NULL default '',
1158 KEY (qc_type,qc_value)
1163 'qc_type' => MW_UPGRADE_COPY
,
1164 'qc_value' => MW_UPGRADE_COPY
,
1165 'qc_namespace' => MW_UPGRADE_COPY
,
1166 'qc_title' => MW_UPGRADE_ENCODE
);
1167 $this->copyTable( 'querycache', $tabledef, $fields );
1171 * Rename all our temporary tables into final place.
1172 * We've left things in place so a read-only wiki can continue running
1173 * on the old code during all this.
1175 function upgradeCleanup() {
1176 $this->renameTable( 'old', 'text' );
1178 foreach( $this->cleanupSwaps
as $table ) {
1179 $this->swap( $table );
1183 function renameTable( $from, $to ) {
1184 $this->log( "Renaming $from to $to..." );
1186 $fromtable = $this->dbw
->tableName( $from );
1187 $totable = $this->dbw
->tableName( $to );
1188 $this->dbw
->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1191 function swap( $base ) {
1192 $this->renameTable( $base, "{$base}_old" );
1193 $this->renameTable( "{$base}_temp", $base );
1198 $upgrade = new FiveUpgrade();
1199 $step = isset( $options['step'] ) ?
$options['step'] : null;
1200 $upgrade->upgrade( $step );