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();
31 $this->dbw
=& $this->newConnection();
32 $this->dbr
=& $this->newConnection();
33 $this->dbr
->bufferResults( false );
34 $this->cleanupSwaps
= array();
36 $this->emailAuth
= false; # don't preauthenticate emails
39 function doing( $step ) {
40 return is_null( $this->step
) ||
$step == $this->step
;
43 function upgrade( $step ) {
59 foreach( $tables as $table ) {
60 if( $this->doing( $table ) ) {
61 $method = 'upgrade' . ucfirst( $table );
66 if( $this->doing( 'cleanup' ) ) {
67 $this->upgradeCleanup();
73 * Open a second connection to the master server, with buffering off.
74 * This will let us stream large datasets in and write in chunks on the
79 function &newConnection() {
80 global $wgDBadminuser, $wgDBadminpassword;
81 global $wgDBserver, $wgDBname;
82 $db =& new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
87 * Prepare a conversion array for converting Windows Code Page 1252 to
88 * UTF-8. This should provide proper conversion of text that was miscoded
89 * as Windows-1252 by naughty user-agents, and doesn't rely on an outside
95 function prepareWindows1252() {
97 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
98 static $cp1252 = array(
99 0x80 => 0x20AC, #EURO SIGN
100 0x81 => UNICODE_REPLACEMENT
,
101 0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
102 0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
103 0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
104 0x85 => 0x2026, #HORIZONTAL ELLIPSIS
105 0x86 => 0x2020, #DAGGER
106 0x87 => 0x2021, #DOUBLE DAGGER
107 0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
108 0x89 => 0x2030, #PER MILLE SIGN
109 0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
110 0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
111 0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
112 0x8D => UNICODE_REPLACEMENT
,
113 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
114 0x8F => UNICODE_REPLACEMENT
,
115 0x90 => UNICODE_REPLACEMENT
,
116 0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
117 0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
118 0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
119 0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
120 0x95 => 0x2022, #BULLET
121 0x96 => 0x2013, #EN DASH
122 0x97 => 0x2014, #EM DASH
123 0x98 => 0x02DC, #SMALL TILDE
124 0x99 => 0x2122, #TRADE MARK SIGN
125 0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
126 0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
127 0x9C => 0x0153, #LATIN SMALL LIGATURE OE
128 0x9D => UNICODE_REPLACEMENT
,
129 0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
130 0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
133 for( $i = 0; $i < 0x100; $i++
) {
134 $unicode = isset( $cp1252[$i] ) ?
$cp1252[$i] : $i;
135 $pairs[chr( $i )] = codepointToUtf8( $unicode );
141 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
142 * @param string $text
146 function conv( $text ) {
148 return is_null( $text )
151 ?
strtr( $text, $this->conversionTables
)
156 * Dump timestamp and message to output
157 * @param string $message
160 function log( $message ) {
161 echo wfTimestamp( TS_DB
) . ': ' . $message . "\n";
166 * Initialize the chunked-insert system.
167 * Rows will be inserted in chunks of the given number, rather
168 * than in a giant INSERT...SELECT query, to keep the serialized
169 * MySQL database replication from getting hung up. This way other
170 * things can be going on during conversion without waiting for
171 * slaves to catch up as badly.
173 * @param int $chunksize Number of rows to insert at once
174 * @param int $final Total expected number of rows / id of last row,
175 * used for progress reports.
176 * @param string $table to insert on
177 * @param string $fname function name to report in SQL
180 function setChunkScale( $chunksize, $final, $table, $fname ) {
181 $this->chunkSize
= $chunksize;
182 $this->chunkFinal
= $final;
183 $this->chunkCount
= 0;
184 $this->chunkStartTime
= wfTime();
185 $this->chunkOptions
= array();
186 $this->chunkTable
= $table;
187 $this->chunkFunction
= $fname;
191 * Chunked inserts: perform an insert if we've reached the chunk limit.
192 * Prints a progress report with estimated completion time.
193 * @param array &$chunk -- This will be emptied if an insert is done.
194 * @param int $key A key identifier to use in progress estimation in
195 * place of the number of rows inserted. Use this if
196 * you provided a max key number instead of a count
197 * as the final chunk number in setChunkScale()
200 function addChunk( &$chunk, $key = null ) {
201 if( count( $chunk ) >= $this->chunkSize
) {
202 $this->insertChunk( $chunk );
204 $this->chunkCount +
= count( $chunk );
206 $delta = $now - $this->chunkStartTime
;
207 $rate = $this->chunkCount
/ $delta;
209 if( is_null( $key ) ) {
210 $completed = $this->chunkCount
;
214 $portion = $completed / $this->chunkFinal
;
216 $estimatedTotalTime = $delta / $portion;
217 $eta = $this->chunkStartTime +
$estimatedTotalTime;
219 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
220 wfTimestamp( TS_DB
, intval( $now ) ),
223 wfTimestamp( TS_DB
, intval( $eta ) ),
234 * Chunked inserts: perform an insert unconditionally, at the end, and log.
235 * @param array &$chunk -- This will be emptied if an insert is done.
238 function lastChunk( &$chunk ) {
239 $n = count( $chunk );
241 $this->insertChunk( $chunk );
243 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
247 * Chunked inserts: perform an insert.
248 * @param array &$chunk -- This will be emptied if an insert is done.
251 function insertChunk( &$chunk ) {
252 $this->dbw
->insert( $this->chunkTable
, $chunk, $this->chunkFunction
, $this->chunkOptions
);
257 * Copy and transcode a table to table_temp.
258 * @param string $name Base name of the source table
259 * @param string $tabledef CREATE TABLE definition, w/ $1 for the name
260 * @param array $fields set of destination fields to these constants:
261 * MW_UPGRADE_COPY - straight copy
262 * MW_UPGRADE_ENCODE - for old Latin1 wikis, conv to UTF-8
263 * MW_UPGRADE_NULL - just put NULL
264 * @param callable $callback An optional callback to modify the data
265 * or perform other processing. Func should be
266 * ( object $row, array $copy ) and return $copy
269 function copyTable( $name, $tabledef, $fields, $callback = null ) {
270 $fname = 'FiveUpgrade::copyTable';
272 $name_temp = $name . '_temp';
273 $this->log( "Migrating $name table to $name_temp..." );
275 $table = $this->dbw
->tableName( $name );
276 $table_temp = $this->dbw
->tableName( $name_temp );
278 // Create temporary table; we're going to copy everything in there,
279 // then at the end rename the final tables into place.
280 $def = str_replace( '$1', $table_temp, $tabledef );
281 $this->dbw
->query( $def, $fname );
283 $numRecords = $this->dbw
->selectField( $name, 'COUNT(*)', '', $fname );
284 $this->setChunkScale( 100, $numRecords, $name_temp, $fname );
286 // Pull all records from the second, streaming database connection.
287 $sourceFields = array_keys( array_filter( $fields,
288 create_function( '$x', 'return $x !== MW_UPGRADE_NULL;' ) ) );
289 $result = $this->dbr
->select( $name,
295 while( $row = $this->dbr
->fetchObject( $result ) ) {
297 foreach( $fields as $field => $source ) {
298 if( $source === MW_UPGRADE_COPY
) {
299 $copy[$field] = $row->$field;
300 } elseif( $source === MW_UPGRADE_ENCODE
) {
301 $copy[$field] = $this->conv( $row->$field );
302 } elseif( $source === MW_UPGRADE_NULL
) {
303 $copy[$field] = null;
305 $this->log( "Unknown field copy type: $field => $source" );
308 if( is_callable( $callback ) ) {
309 $copy = call_user_func( $callback, $row, $copy );
312 $this->addChunk( $add );
314 $this->lastChunk( $add );
315 $this->dbr
->freeResult( $result );
317 $this->log( "Done converting $name." );
318 $this->cleanupSwaps
[] = $name;
321 function upgradePage() {
322 $fname = "FiveUpgrade::upgradePage";
326 $this->log( "Checking cur table for unique title index and applying if necessary" );
329 $this->log( "...converting from cur/old to page/revision/text DB structure." );
331 extract( $this->dbw
->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
333 $this->log( "Creating page and revision tables..." );
334 $this->dbw
->query("CREATE TABLE $page (
335 page_id int(8) unsigned NOT NULL auto_increment,
336 page_namespace int NOT NULL,
337 page_title varchar(255) binary NOT NULL,
338 page_restrictions tinyblob NOT NULL default '',
339 page_counter bigint(20) unsigned NOT NULL default '0',
340 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
341 page_is_new tinyint(1) unsigned NOT NULL default '0',
342 page_random real unsigned NOT NULL,
343 page_touched char(14) binary NOT NULL default '',
344 page_latest int(8) unsigned NOT NULL,
345 page_len int(8) unsigned NOT NULL,
347 PRIMARY KEY page_id (page_id),
348 UNIQUE INDEX name_title (page_namespace,page_title),
351 ) TYPE=InnoDB", $fname );
352 $this->dbw
->query("CREATE TABLE $revision (
353 rev_id int(8) unsigned NOT NULL auto_increment,
354 rev_page int(8) unsigned NOT NULL,
355 rev_text_id int(8) unsigned NOT NULL,
356 rev_comment tinyblob NOT NULL default '',
357 rev_user int(5) unsigned NOT NULL default '0',
358 rev_user_text varchar(255) binary NOT NULL default '',
359 rev_timestamp char(14) binary NOT NULL default '',
360 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
361 rev_deleted tinyint(1) unsigned NOT NULL default '0',
363 PRIMARY KEY rev_page_id (rev_page, rev_id),
364 UNIQUE INDEX rev_id (rev_id),
365 INDEX rev_timestamp (rev_timestamp),
366 INDEX page_timestamp (rev_page,rev_timestamp),
367 INDEX user_timestamp (rev_user,rev_timestamp),
368 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
369 ) TYPE=InnoDB", $fname );
371 $maxold = $this->dbw
->selectField( 'old', 'max(old_id)', '', $fname );
372 $this->log( "Last old record is {$maxold}" );
374 global $wgLegacySchemaConversion;
375 if( $wgLegacySchemaConversion ) {
376 // Create HistoryBlobCurStub entries.
377 // Text will be pulled from the leftover 'cur' table at runtime.
378 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
379 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
380 $cur_flags = "'object'";
382 // Copy all cur text in immediately: this may take longer but avoids
383 // having to keep an extra table around.
384 echo "......Moving text from cur.\n";
385 $cur_text = 'cur_text';
389 $maxcur = $this->dbw
->selectField( 'cur', 'max(cur_id)', '', $fname );
390 $this->log( "Last cur entry is $maxcur" );
393 * Copy placeholder records for each page's current version into old
394 * Don't do any conversion here; text records are converted at runtime
395 * based on the flags (and may be originally binary!) while the meta
396 * fields will be converted in the old -> rev and cur -> page steps.
398 $this->setChunkScale( $chunksize, $maxcur, 'old', $fname );
399 $result = $this->dbr
->query(
400 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
401 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
403 ORDER BY cur_id", $fname );
405 while( $row = $this->dbr
->fetchObject( $result ) ) {
407 'old_namespace' => $row->cur_namespace
,
408 'old_title' => $row->cur_title
,
409 'old_text' => $row->text
,
410 'old_comment' => $row->cur_comment
,
411 'old_user' => $row->cur_user
,
412 'old_user_text' => $row->cur_user_text
,
413 'old_timestamp' => $row->cur_timestamp
,
414 'old_minor_edit' => $row->cur_minor_edit
,
415 'old_flags' => $row->flags
);
416 $this->addChunk( $add, $row->cur_id
);
418 $this->lastChunk( $add );
419 $this->dbr
->freeResult( $result );
422 * Copy revision metadata from old into revision.
423 * We'll also do UTF-8 conversion of usernames and comments.
425 #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
426 #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
427 $countold = $this->dbw
->selectField( 'old', 'count(old_id)', '', $fname );
428 $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
430 $this->log( "......Setting up revision table." );
431 $result = $this->dbr
->query(
432 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
433 old_timestamp, old_minor_edit
434 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
438 while( $row = $this->dbr
->fetchObject( $result ) ) {
440 'rev_id' => $row->old_id
,
441 'rev_page' => $row->cur_id
,
442 'rev_text_id' => $row->old_id
,
443 'rev_comment' => $this->conv( $row->old_comment
),
444 'rev_user' => $row->old_user
,
445 'rev_user_text' => $this->conv( $row->old_user_text
),
446 'rev_timestamp' => $row->old_timestamp
,
447 'rev_minor_edit' => $row->old_minor_edit
);
448 $this->addChunk( $add );
450 $this->lastChunk( $add );
451 $this->dbr
->freeResult( $result );
455 * Copy page metadata from cur into page.
456 * We'll also do UTF-8 conversion of titles.
458 $this->log( "......Setting up page table." );
459 $this->setChunkScale( $chunksize, $maxcur, 'page', $fname );
460 $result = $this->dbr
->query( "
461 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
462 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
464 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
465 ORDER BY cur_id", $fname );
467 while( $row = $this->dbr
->fetchObject( $result ) ) {
469 'page_id' => $row->cur_id
,
470 'page_namespace' => $row->cur_namespace
,
471 'page_title' => $this->conv( $row->cur_title
),
472 'page_restrictions' => $row->cur_restrictions
,
473 'page_counter' => $row->cur_counter
,
474 'page_is_redirect' => $row->cur_is_redirect
,
475 'page_is_new' => $row->cur_is_new
,
476 'page_random' => $row->cur_random
,
477 'page_touched' => $this->dbw
->timestamp(),
478 'page_latest' => $row->rev_id
,
479 'page_len' => $row->len
);
480 $this->addChunk( $add, $row->cur_id
);
482 $this->lastChunk( $add );
483 $this->dbr
->freeResult( $result );
485 $this->log( "...done with cur/old -> page/revision." );
488 function upgradeLinks() {
489 $fname = 'FiveUpgrade::upgradeLinks';
491 extract( $this->dbw
->tableNames( 'links', 'brokenlinks', 'pagelinks', 'page' ) );
493 $this->log( 'Creating pagelinks table...' );
495 CREATE TABLE $pagelinks (
496 -- Key to the page_id of the page containing the link.
497 pl_from int(8) unsigned NOT NULL default '0',
499 -- Key to page_namespace/page_title of the target page.
500 -- The target page may or may not exist, and due to renames
501 -- and deletions may refer to different page records as time
503 pl_namespace int NOT NULL default '0',
504 pl_title varchar(255) binary NOT NULL default '',
506 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
507 KEY (pl_namespace,pl_title)
511 $this->log( 'Importing live links -> pagelinks' );
512 $nlinks = $this->dbw
->selectField( 'links', 'count(*)', '', $fname );
514 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
515 $result = $this->dbr
->query( "
516 SELECT l_from,page_namespace,page_title
518 WHERE l_to=page_id", $fname );
520 while( $row = $this->dbr
->fetchObject( $result ) ) {
522 'pl_from' => $row->l_from
,
523 'pl_namespace' => $row->page_namespace
,
524 'pl_title' => $row->page_title
);
525 $this->addChunk( $add );
527 $this->lastChunk( $add );
529 $this->log( 'no links!' );
532 $this->log( 'Importing brokenlinks -> pagelinks' );
533 $nbrokenlinks = $this->dbw
->selectField( 'brokenlinks', 'count(*)', '', $fname );
534 if( $nbrokenlinks ) {
535 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
536 $this->chunkOptions
= array( 'IGNORE' );
537 $result = $this->dbr
->query(
538 "SELECT bl_from, bl_to FROM $brokenlinks",
541 while( $row = $this->dbr
->fetchObject( $result ) ) {
542 $pagename = $this->conv( $row->bl_to
);
543 $title = Title
::newFromText( $pagename );
544 if( is_null( $title ) ) {
545 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
548 'pl_from' => $row->bl_from
,
549 'pl_namespace' => $title->getNamespace(),
550 'pl_title' => $title->getDBkey() );
551 $this->addChunk( $add );
554 $this->lastChunk( $add );
556 $this->log( 'no brokenlinks!' );
559 $this->log( 'Done with links.' );
562 function upgradeUser() {
563 // Apply unique index, if necessary:
564 $duper = new UserDupes( $this->dbw
);
565 if( $duper->hasUniqueIndex() ) {
566 $this->log( "Already have unique user_name index." );
568 $this->log( "Clearing user duplicates..." );
569 if( !$duper->clearDupes() ) {
570 $this->log( "WARNING: Duplicate user accounts, may explode!" );
576 user_id int(5) unsigned NOT NULL auto_increment,
577 user_name varchar(255) binary NOT NULL default '',
578 user_real_name varchar(255) binary NOT NULL default '',
579 user_password tinyblob NOT NULL default '',
580 user_newpassword tinyblob NOT NULL default '',
581 user_email tinytext NOT NULL default '',
582 user_options blob NOT NULL default '',
583 user_touched char(14) binary NOT NULL default '',
584 user_token char(32) binary NOT NULL default '',
585 user_email_authenticated CHAR(14) BINARY,
586 user_email_token CHAR(32) BINARY,
587 user_email_token_expires CHAR(14) BINARY,
589 PRIMARY KEY user_id (user_id),
590 UNIQUE INDEX user_name (user_name),
591 INDEX (user_email_token)
596 'user_id' => MW_UPGRADE_COPY
,
597 'user_name' => MW_UPGRADE_ENCODE
,
598 'user_real_name' => MW_UPGRADE_ENCODE
,
599 'user_password' => MW_UPGRADE_COPY
,
600 'user_newpassword' => MW_UPGRADE_COPY
,
601 'user_email' => MW_UPGRADE_ENCODE
,
602 'user_options' => MW_UPGRADE_ENCODE
,
603 'user_touched' => MW_UPGRADE_CALLBACK
,
604 'user_token' => MW_UPGRADE_COPY
,
605 'user_email_authenticated' => MW_UPGRADE_CALLBACK
,
606 'user_email_token' => MW_UPGRADE_NULL
,
607 'user_email_token_expires' => MW_UPGRADE_NULL
);
608 $this->copyTable( 'user', $tabledef, $fields,
609 array( &$this, 'userCallback' ) );
612 function userCallback( $row, $copy ) {
613 $now = $this->dbw
->timestamp();
614 $copy['user_touched'] = $now;
615 $copy['user_email_authenticated'] = $this->emailAuth ?
$now : null;
619 function upgradeImage() {
622 img_name varchar(255) binary NOT NULL default '',
623 img_size int(8) unsigned NOT NULL default '0',
624 img_width int(5) NOT NULL default '0',
625 img_height int(5) NOT NULL default '0',
626 img_metadata mediumblob NOT NULL,
627 img_bits int(3) NOT NULL default '0',
628 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
629 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
630 img_minor_mime varchar(32) NOT NULL default "unknown",
631 img_description tinyblob NOT NULL default '',
632 img_user int(5) unsigned NOT NULL default '0',
633 img_user_text varchar(255) binary NOT NULL default '',
634 img_timestamp char(14) binary NOT NULL default '',
636 PRIMARY KEY img_name (img_name),
637 INDEX img_size (img_size),
638 INDEX img_timestamp (img_timestamp)
642 'img_name' => MW_UPGRADE_ENCODE
,
643 'img_size' => MW_UPGRADE_COPY
,
644 'img_width' => MW_UPGRADE_CALLBACK
,
645 'img_height' => MW_UPGRADE_CALLBACK
,
646 'img_metadata' => MW_UPGRADE_CALLBACK
,
647 'img_bits' => MW_UPGRADE_CALLBACK
,
648 'img_media_type' => MW_UPGRADE_CALLBACK
,
649 'img_major_mime' => MW_UPGRADE_CALLBACK
,
650 'img_minor_mime' => MW_UPGRADE_CALLBACK
,
651 'img_description' => MW_UPGRADE_ENCODE
,
652 'img_user' => MW_UPGRADE_COPY
,
653 'img_user_text' => MW_UPGRADE_ENCODE
,
654 'img_timestamp' => MW_UPGRADE_COPY
);
655 $this->copyTable( 'image', $tabledef, $fields,
656 array( &$this, 'imageCallback' ) );
659 function imageCallback( $row, $copy ) {
660 // Fill in the new image info fields
661 $info = $this->imageInfo( $row->img_name
);
663 $copy['img_width' ] = $info['width'];
664 $copy['img_height' ] = $info['height'];
665 $copy['img_metadata' ] = ""; // loaded on-demand
666 $copy['img_bits' ] = $info['bits'];
667 $copy['img_media_type'] = $info['media'];
668 $copy['img_major_mime'] = $info['major'];
669 $copy['img_minor_mime'] = $info['minor'];
671 // If doing UTF8 conversion the file must be renamed
672 $this->renameFile( $row->img_name
, 'wfImageDir' );
677 function imageInfo( $name, $subdirCallback='wfImageDir', $basename = null ) {
678 if( is_null( $basename ) ) $basename = $name;
679 $dir = call_user_func( $subdirCallback, $basename );
680 $filename = $dir . '/' . $name;
689 $magic =& wfGetMimeMagic();
690 $mime = $magic->guessMimeType( $filename, true );
691 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
693 $info['media'] = $magic->getMediaType( $filename, $mime );
697 if( $mime == 'image/svg' ) {
698 $gis = wfGetSVGsize( $this->imagePath
);
699 } elseif( $magic->isPHPImageType( $mime ) ) {
700 $gis = getimagesize( $filename );
702 $this->log( "Surprising mime type: $mime" );
705 $info['width' ] = $gis[0];
706 $info['height'] = $gis[1];
708 if( isset( $gis['bits'] ) ) {
709 $info['bits'] = $gis['bits'];
718 * @param string $table The table name to be truncated
720 function clearTable( $table ) {
721 print "Clearing $table...\n";
722 $tableName = $this->db
->tableName( $table );
723 $this->db
->query( 'TRUNCATE $tableName' );
727 * Rename a given image or archived image file to the converted filename,
728 * leaving a symlink for URL compatibility.
730 * @param string $oldname pre-conversion filename
731 * @param string $basename pre-conversion base filename for dir hashing, if an archive
734 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
735 $newname = $this->conv( $oldname );
736 if( $newname == $oldname ) {
737 // No need to rename; another field triggered this row.
741 if( is_null( $basename ) ) $basename = $oldname;
742 $ubasename = $this->conv( $basename );
743 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
744 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
746 $this->log( "$oldpath -> $newpath" );
747 if( rename( $oldpath, $newpath ) ) {
748 $relpath = $this->relativize( $newpath, dirname( $oldpath ) );
749 if( !symlink( $relpath, $oldpath ) ) {
750 $this->log( "... symlink failed!" );
753 $this->log( "... rename failed!" );
758 * Generate a relative path name to the given file.
759 * Assumes Unix-style paths, separators, and semantics.
761 * @param string $path Absolute destination path including target filename
762 * @param string $from Absolute source path, directory only
767 function relativize( $path, $from ) {
768 $pieces = explode( '/', dirname( $path ) );
769 $against = explode( '/', $from );
771 // Trim off common prefix
772 while( count( $pieces ) && count( $against )
773 && $pieces[0] == $against[0] ) {
774 array_shift( $pieces );
775 array_shift( $against );
778 // relative dots to bump us to the parent
779 while( count( $against ) ) {
780 array_unshift( $pieces, '..' );
781 array_shift( $against );
784 array_push( $pieces, basename( $path ) );
786 return implode( '/', $pieces );
789 function upgradeOldImage() {
792 -- Base filename: key to image.img_name
793 oi_name varchar(255) binary NOT NULL default '',
795 -- Filename of the archived file.
796 -- This is generally a timestamp and '!' prepended to the base name.
797 oi_archive_name varchar(255) binary NOT NULL default '',
799 -- Other fields as in image...
800 oi_size int(8) unsigned NOT NULL default 0,
801 oi_width int(5) NOT NULL default 0,
802 oi_height int(5) NOT NULL default 0,
803 oi_bits int(3) NOT NULL default 0,
804 oi_description tinyblob NOT NULL default '',
805 oi_user int(5) unsigned NOT NULL default '0',
806 oi_user_text varchar(255) binary NOT NULL default '',
807 oi_timestamp char(14) binary NOT NULL default '',
809 INDEX oi_name (oi_name(10))
814 'oi_name' => MW_UPGRADE_ENCODE
,
815 'oi_archive_name' => MW_UPGRADE_ENCODE
,
816 'oi_size' => MW_UPGRADE_COPY
,
817 'oi_width' => MW_UPGRADE_CALLBACK
,
818 'oi_height' => MW_UPGRADE_CALLBACK
,
819 'oi_bits' => MW_UPGRADE_CALLBACK
,
820 'oi_description' => MW_UPGRADE_ENCODE
,
821 'oi_user' => MW_UPGRADE_COPY
,
822 'oi_user_text' => MW_UPGRADE_ENCODE
,
823 'oi_timestamp' => MW_UPGRADE_COPY
);
824 $this->copyTable( 'oldimage', $tabledef, $fields,
825 array( &$this, 'oldimageCallback' ) );
828 function oldimageCallback( $row, $copy ) {
829 // Fill in the new image info fields
830 $info = $this->imageInfo( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
831 $copy['oi_width' ] = $info['width' ];
832 $copy['oi_height'] = $info['height'];
833 $copy['oi_bits' ] = $info['bits' ];
835 // If doing UTF8 conversion the file must be renamed
836 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
842 function upgradeWatchlist() {
843 $fname = 'FiveUpgrade::upgradeWatchlist';
846 extract( $this->dbw
->tableNames( 'watchlist', 'watchlist_temp' ) );
848 $this->log( 'Migrating watchlist table to watchlist_temp...' );
850 "CREATE TABLE $watchlist_temp (
852 wl_user int(5) unsigned NOT NULL,
854 -- Key to page_namespace/page_title
855 -- Note that users may watch patches which do not exist yet,
856 -- or existed in the past but have been deleted.
857 wl_namespace int NOT NULL default '0',
858 wl_title varchar(255) binary NOT NULL default '',
860 -- Timestamp when user was last sent a notification e-mail;
861 -- cleared when the user visits the page.
862 -- FIXME: add proper null support etc
863 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
865 UNIQUE KEY (wl_user, wl_namespace, wl_title),
866 KEY namespace_title (wl_namespace,wl_title)
868 ) TYPE=InnoDB;", $fname );
870 // Fix encoding for Latin-1 upgrades, add some fields,
871 // and double article to article+talk pairs
872 $numwatched = $this->dbw
->selectField( 'watchlist', 'count(*)', '', $fname );
874 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
875 $result = $this->dbr
->select( 'watchlist',
884 while( $row = $this->dbr
->fetchObject( $result ) ) {
885 $now = $this->dbw
->timestamp();
887 'wl_user' => $row->wl_user
,
888 'wl_namespace' => Namespace::getSubject( $row->wl_namespace
),
889 'wl_title' => $this->conv( $row->wl_title
),
890 'wl_notificationtimestamp' => '0' );
891 $this->addChunk( $add );
894 'wl_user' => $row->wl_user
,
895 'wl_namespace' => Namespace::getTalk( $row->wl_namespace
),
896 'wl_title' => $this->conv( $row->wl_title
),
897 'wl_notificationtimestamp' => '0' );
898 $this->addChunk( $add );
900 $this->lastChunk( $add );
901 $this->dbr
->freeResult( $result );
903 $this->log( 'Done converting watchlist.' );
904 $this->cleanupSwaps
[] = 'watchlist';
907 function upgradeLogging() {
910 -- Symbolic keys for the general log type and the action type
911 -- within the log. The output format will be controlled by the
912 -- action field, but only the type controls categorization.
913 log_type char(10) NOT NULL default '',
914 log_action char(10) NOT NULL default '',
917 log_timestamp char(14) NOT NULL default '19700101000000',
919 -- The user who performed this action; key to user_id
920 log_user int unsigned NOT NULL default 0,
922 -- Key to the page affected. Where a user is the target,
923 -- this will point to the user page.
924 log_namespace int NOT NULL default 0,
925 log_title varchar(255) binary NOT NULL default '',
927 -- Freeform text. Interpreted as edit history comments.
928 log_comment varchar(255) NOT NULL default '',
930 -- LF separated list of miscellaneous parameters
931 log_params blob NOT NULL default '',
933 KEY type_time (log_type, log_timestamp),
934 KEY user_time (log_user, log_timestamp),
935 KEY page_time (log_namespace, log_title, log_timestamp)
940 'log_type' => MW_UPGRADE_COPY
,
941 'log_action' => MW_UPGRADE_COPY
,
942 'log_timestamp' => MW_UPGRADE_COPY
,
943 'log_user' => MW_UPGRADE_COPY
,
944 'log_namespace' => MW_UPGRADE_COPY
,
945 'log_title' => MW_UPGRADE_ENCODE
,
946 'log_comment' => MW_UPGRADE_ENCODE
,
947 'log_params' => MW_UPGRADE_ENCODE
);
948 $this->copyTable( 'logging', $tabledef, $fields );
951 function upgradeArchive() {
954 ar_namespace int NOT NULL default '0',
955 ar_title varchar(255) binary NOT NULL default '',
956 ar_text mediumblob NOT NULL default '',
958 ar_comment tinyblob NOT NULL default '',
959 ar_user int(5) unsigned NOT NULL default '0',
960 ar_user_text varchar(255) binary NOT NULL,
961 ar_timestamp char(14) binary NOT NULL default '',
962 ar_minor_edit tinyint(1) NOT NULL default '0',
964 ar_flags tinyblob NOT NULL default '',
966 ar_rev_id int(8) unsigned,
967 ar_text_id int(8) unsigned,
969 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
974 'ar_namespace' => MW_UPGRADE_COPY
,
975 'ar_title' => MW_UPGRADE_ENCODE
,
976 'ar_text' => MW_UPGRADE_COPY
,
977 'ar_comment' => MW_UPGRADE_ENCODE
,
978 'ar_user' => MW_UPGRADE_COPY
,
979 'ar_user_text' => MW_UPGRADE_ENCODE
,
980 'ar_timestamp' => MW_UPGRADE_COPY
,
981 'ar_minor_edit' => MW_UPGRADE_COPY
,
982 'ar_flags' => MW_UPGRADE_COPY
,
983 'ar_rev_id' => MW_UPGRADE_NULL
,
984 'ar_text_id' => MW_UPGRADE_NULL
);
985 $this->copyTable( 'archive', $tabledef, $fields );
988 function upgradeImagelinks() {
993 -- Key to page_id of the page containing the image / media link.
994 il_from int(8) unsigned NOT NULL default '0',
996 -- Filename of target image.
997 -- This is also the page_title of the file's description page;
998 -- all such pages are in namespace 6 (NS_IMAGE).
999 il_to varchar(255) binary NOT NULL default '',
1001 UNIQUE KEY il_from(il_from,il_to),
1007 'il_from' => MW_UPGRADE_COPY
,
1008 'il_to' => MW_UPGRADE_ENCODE
);
1009 $this->copyTable( 'imagelinks', $tabledef, $fields );
1013 function upgradeCategorylinks() {
1014 global $wgUseLatin1;
1015 if( $wgUseLatin1 ) {
1018 cl_from int(8) unsigned NOT NULL default '0',
1019 cl_to varchar(255) binary NOT NULL default '',
1020 cl_sortkey varchar(86) binary NOT NULL default '',
1021 cl_timestamp timestamp NOT NULL,
1023 UNIQUE KEY cl_from(cl_from,cl_to),
1024 KEY cl_sortkey(cl_to,cl_sortkey),
1025 KEY cl_timestamp(cl_to,cl_timestamp)
1029 'cl_from' => MW_UPGRADE_COPY
,
1030 'cl_to' => MW_UPGRADE_ENCODE
,
1031 'cl_sortkey' => MW_UPGRADE_ENCODE
,
1032 'cl_timestamp' => MW_UPGRADE_COPY
);
1033 $this->copyTable( 'categorylinks', $tabledef, $fields );
1037 function upgradeIpblocks() {
1038 global $wgUseLatin1;
1039 if( $wgUseLatin1 ) {
1042 ipb_id int(8) NOT NULL auto_increment,
1043 ipb_address varchar(40) binary NOT NULL default '',
1044 ipb_user int(8) unsigned NOT NULL default '0',
1045 ipb_by int(8) unsigned NOT NULL default '0',
1046 ipb_reason tinyblob NOT NULL default '',
1047 ipb_timestamp char(14) binary NOT NULL default '',
1048 ipb_auto tinyint(1) NOT NULL default '0',
1049 ipb_expiry char(14) binary NOT NULL default '',
1051 PRIMARY KEY ipb_id (ipb_id),
1052 INDEX ipb_address (ipb_address),
1053 INDEX ipb_user (ipb_user)
1058 'ipb_id' => MW_UPGRADE_COPY
,
1059 'ipb_address' => MW_UPGRADE_COPY
,
1060 'ipb_user' => MW_UPGRADE_COPY
,
1061 'ipb_by' => MW_UPGRADE_COPY
,
1062 'ipb_reason' => MW_UPGRADE_ENCODE
,
1063 'ipb_timestamp' => MW_UPGRADE_COPY
,
1064 'ipb_auto' => MW_UPGRADE_COPY
,
1065 'ipb_expiry' => MW_UPGRADE_COPY
);
1066 $this->copyTable( 'ipblocks', $tabledef, $fields );
1070 function upgradeRecentchanges() {
1071 // There's a format change in the namespace field
1074 rc_id int(8) NOT NULL auto_increment,
1075 rc_timestamp varchar(14) binary NOT NULL default '',
1076 rc_cur_time varchar(14) binary NOT NULL default '',
1078 rc_user int(10) unsigned NOT NULL default '0',
1079 rc_user_text varchar(255) binary NOT NULL default '',
1081 rc_namespace int NOT NULL default '0',
1082 rc_title varchar(255) binary NOT NULL default '',
1084 rc_comment varchar(255) binary NOT NULL default '',
1085 rc_minor tinyint(3) unsigned NOT NULL default '0',
1087 rc_bot tinyint(3) unsigned NOT NULL default '0',
1088 rc_new tinyint(3) unsigned NOT NULL default '0',
1090 rc_cur_id int(10) unsigned NOT NULL default '0',
1091 rc_this_oldid int(10) unsigned NOT NULL default '0',
1092 rc_last_oldid int(10) unsigned NOT NULL default '0',
1094 rc_type tinyint(3) unsigned NOT NULL default '0',
1095 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
1096 rc_moved_to_title varchar(255) binary NOT NULL default '',
1098 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1100 rc_ip char(15) NOT NULL default '',
1102 PRIMARY KEY rc_id (rc_id),
1103 INDEX rc_timestamp (rc_timestamp),
1104 INDEX rc_namespace_title (rc_namespace, rc_title),
1105 INDEX rc_cur_id (rc_cur_id),
1106 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
1112 'rc_id' => MW_UPGRADE_COPY
,
1113 'rc_timestamp' => MW_UPGRADE_COPY
,
1114 'rc_cur_time' => MW_UPGRADE_COPY
,
1115 'rc_user' => MW_UPGRADE_COPY
,
1116 'rc_user_text' => MW_UPGRADE_ENCODE
,
1117 'rc_namespace' => MW_UPGRADE_COPY
,
1118 'rc_title' => MW_UPGRADE_ENCODE
,
1119 'rc_comment' => MW_UPGRADE_ENCODE
,
1120 'rc_minor' => MW_UPGRADE_COPY
,
1121 'rc_bot' => MW_UPGRADE_COPY
,
1122 'rc_new' => MW_UPGRADE_COPY
,
1123 'rc_cur_id' => MW_UPGRADE_COPY
,
1124 'rc_this_oldid' => MW_UPGRADE_COPY
,
1125 'rc_last_oldid' => MW_UPGRADE_COPY
,
1126 'rc_type' => MW_UPGRADE_COPY
,
1127 'rc_moved_to_ns' => MW_UPGRADE_COPY
,
1128 'rc_moved_to_title' => MW_UPGRADE_ENCODE
,
1129 'rc_patrolled' => MW_UPGRADE_COPY
,
1130 'rc_ip' => MW_UPGRADE_COPY
);
1131 $this->copyTable( 'recentchanges', $tabledef, $fields );
1135 * Rename all our temporary tables into final place.
1136 * We've left things in place so a read-only wiki can continue running
1137 * on the old code during all this.
1139 function upgradeCleanup() {
1140 $this->renameTable( 'old', 'text' );
1142 foreach( $this->cleanupSwaps
as $table ) {
1143 $this->swap( $table );
1147 function renameTable( $from, $to ) {
1148 $this->log( "Renaming $from to $to..." );
1150 $fromtable = $this->dbw
->tableName( $from );
1151 $totable = $this->dbw
->tableName( $to );
1152 $this->dbw
->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1155 function swap( $base ) {
1156 $this->renameTable( $base, "{$base}_old" );
1157 $this->renameTable( "{$base}_temp", $base );
1162 $upgrade = new FiveUpgrade();
1163 $step = isset( $options['step'] ) ?
$options['step'] : null;
1164 $upgrade->upgrade( $step );