3 require_once( 'cleanupDupes.inc' );
4 require_once( 'userDupes.inc' );
5 require_once( 'updaters.inc' );
7 define( 'MW_UPGRADE_COPY', false );
8 define( 'MW_UPGRADE_ENCODE', true );
9 define( 'MW_UPGRADE_NULL', null );
10 define( 'MW_UPGRADE_CALLBACK', null ); // for self-documentation only
13 function FiveUpgrade() {
14 $this->conversionTables = $this->prepareWindows1252();
16 $this->dbw =& $this->newConnection();
17 $this->dbr =& $this->streamConnection();
19 $this->cleanupSwaps = array();
20 $this->emailAuth = false; # don't preauthenticate emails
21 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
24 function doing( $step ) {
25 return is_null( $this->step ) || $step == $this->step;
28 function upgrade( $step ) {
45 foreach( $tables as $table ) {
46 if( $this->doing( $table ) ) {
47 $method = 'upgrade' . ucfirst( $table );
52 if( $this->doing( 'cleanup' ) ) {
53 $this->upgradeCleanup();
59 * Open a connection to the master server with the admin rights.
63 function &newConnection() {
64 global $wgDBadminuser, $wgDBadminpassword;
65 global $wgDBserver, $wgDBname;
66 $db = new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
71 * Open a second connection to the master server, with buffering off.
72 * This will let us stream large datasets in and write in chunks on the
77 function &streamConnection() {
79 $db =& $this->newConnection();
80 $db->bufferResults( false );
81 $db->query( "SET net_read_timeout=$timeout" );
82 $db->query( "SET net_write_timeout=$timeout" );
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 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
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 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
113 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
114 0x8F => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
115 0x90 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
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 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
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 wfWikiID() . ' ' . 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( 'IGNORE' );
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 // Give slaves a chance to catch up
253 wfWaitForSlaves( $this->maxLag );
254 $this->dbw->insert( $this->chunkTable, $chunk, $this->chunkFunction, $this->chunkOptions );
259 * Copy and transcode a table to table_temp.
260 * @param string $name Base name of the source table
261 * @param string $tabledef CREATE TABLE definition, w/ $1 for the name
262 * @param array $fields set of destination fields to these constants:
263 * MW_UPGRADE_COPY - straight copy
264 * MW_UPGRADE_ENCODE - for old Latin1 wikis, conv to UTF-8
265 * MW_UPGRADE_NULL - just put NULL
266 * @param callable $callback An optional callback to modify the data
267 * or perform other processing. Func should be
268 * ( object $row, array $copy ) and return $copy
271 function copyTable( $name, $tabledef, $fields, $callback = null ) {
272 $fname = 'FiveUpgrade::copyTable';
274 $name_temp = $name . '_temp';
275 $this->log( "Migrating $name table to $name_temp..." );
277 $table_temp = $this->dbw->tableName( $name_temp );
279 // Create temporary table; we're going to copy everything in there,
280 // then at the end rename the final tables into place.
281 $def = str_replace( '$1', $table_temp, $tabledef );
282 $this->dbw->query( $def, $fname );
284 $numRecords = $this->dbw->selectField( $name, 'COUNT(*)', '', $fname );
285 $this->setChunkScale( 100, $numRecords, $name_temp, $fname );
287 // Pull all records from the second, streaming database connection.
288 $sourceFields = array_keys( array_filter( $fields,
289 create_function( '$x', 'return $x !== MW_UPGRADE_NULL;' ) ) );
290 $result = $this->dbr->select( $name,
296 while( $row = $this->dbr->fetchObject( $result ) ) {
298 foreach( $fields as $field => $source ) {
299 if( $source === MW_UPGRADE_COPY ) {
300 $copy[$field] = $row->$field;
301 } elseif( $source === MW_UPGRADE_ENCODE ) {
302 $copy[$field] = $this->conv( $row->$field );
303 } elseif( $source === MW_UPGRADE_NULL ) {
304 $copy[$field] = null;
306 $this->log( "Unknown field copy type: $field => $source" );
309 if( is_callable( $callback ) ) {
310 $copy = call_user_func( $callback, $row, $copy );
313 $this->addChunk( $add );
315 $this->lastChunk( $add );
316 $this->dbr->freeResult( $result );
318 $this->log( "Done converting $name." );
319 $this->cleanupSwaps[] = $name;
322 function upgradePage() {
323 $fname = "FiveUpgrade::upgradePage";
326 if( $this->dbw->tableExists( 'page' ) ) {
327 $this->log( 'Page table already exists; aborting.' );
331 $this->log( "Checking cur table for unique title index and applying if necessary" );
334 $this->log( "...converting from cur/old to page/revision/text DB structure." );
336 list ($cur, $old, $page, $revision, $text) = $this->dbw->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
338 $this->log( "Creating page and revision tables..." );
339 $this->dbw->query("CREATE TABLE $page (
340 page_id int(8) unsigned NOT NULL auto_increment,
341 page_namespace int NOT NULL,
342 page_title varchar(255) binary NOT NULL,
343 page_restrictions tinyblob NOT NULL default '',
344 page_counter bigint(20) unsigned NOT NULL default '0',
345 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
346 page_is_new tinyint(1) unsigned NOT NULL default '0',
347 page_random real unsigned NOT NULL,
348 page_touched char(14) binary NOT NULL default '',
349 page_latest int(8) unsigned NOT NULL,
350 page_len int(8) unsigned NOT NULL,
352 PRIMARY KEY page_id (page_id),
353 UNIQUE INDEX name_title (page_namespace,page_title),
356 ) TYPE=InnoDB", $fname );
357 $this->dbw->query("CREATE TABLE $revision (
358 rev_id int(8) unsigned NOT NULL auto_increment,
359 rev_page int(8) unsigned NOT NULL,
360 rev_text_id int(8) unsigned NOT NULL,
361 rev_comment tinyblob NOT NULL default '',
362 rev_user int(5) unsigned NOT NULL default '0',
363 rev_user_text varchar(255) binary NOT NULL default '',
364 rev_timestamp char(14) binary NOT NULL default '',
365 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
366 rev_deleted tinyint(1) unsigned NOT NULL default '0',
368 PRIMARY KEY rev_page_id (rev_page, rev_id),
369 UNIQUE INDEX rev_id (rev_id),
370 INDEX rev_timestamp (rev_timestamp),
371 INDEX page_timestamp (rev_page,rev_timestamp),
372 INDEX user_timestamp (rev_user,rev_timestamp),
373 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
374 ) TYPE=InnoDB", $fname );
376 $maxold = intval( $this->dbw->selectField( 'old', 'max(old_id)', '', $fname ) );
377 $this->log( "Last old record is {$maxold}" );
379 global $wgLegacySchemaConversion;
380 if( $wgLegacySchemaConversion ) {
381 // Create HistoryBlobCurStub entries.
382 // Text will be pulled from the leftover 'cur' table at runtime.
383 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
384 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
385 $cur_flags = "'object'";
387 // Copy all cur text in immediately: this may take longer but avoids
388 // having to keep an extra table around.
389 echo "......Moving text from cur.\n";
390 $cur_text = 'cur_text';
394 $maxcur = $this->dbw->selectField( 'cur', 'max(cur_id)', '', $fname );
395 $this->log( "Last cur entry is $maxcur" );
398 * Copy placeholder records for each page's current version into old
399 * Don't do any conversion here; text records are converted at runtime
400 * based on the flags (and may be originally binary!) while the meta
401 * fields will be converted in the old -> rev and cur -> page steps.
403 $this->setChunkScale( $chunksize, $maxcur, 'old', $fname );
404 $result = $this->dbr->query(
405 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
406 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
408 ORDER BY cur_id", $fname );
410 while( $row = $this->dbr->fetchObject( $result ) ) {
412 'old_namespace' => $row->cur_namespace,
413 'old_title' => $row->cur_title,
414 'old_text' => $row->text,
415 'old_comment' => $row->cur_comment,
416 'old_user' => $row->cur_user,
417 'old_user_text' => $row->cur_user_text,
418 'old_timestamp' => $row->cur_timestamp,
419 'old_minor_edit' => $row->cur_minor_edit,
420 'old_flags' => $row->flags );
421 $this->addChunk( $add, $row->cur_id );
423 $this->lastChunk( $add );
424 $this->dbr->freeResult( $result );
427 * Copy revision metadata from old into revision.
428 * We'll also do UTF-8 conversion of usernames and comments.
430 #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
431 #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
432 #$countold = $this->dbw->selectField( 'old', 'count(old_id)', '', $fname );
433 $countold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
434 $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
436 $this->log( "......Setting up revision table." );
437 $result = $this->dbr->query(
438 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
439 old_timestamp, old_minor_edit
440 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
444 while( $row = $this->dbr->fetchObject( $result ) ) {
446 'rev_id' => $row->old_id,
447 'rev_page' => $row->cur_id,
448 'rev_text_id' => $row->old_id,
449 'rev_comment' => $this->conv( $row->old_comment ),
450 'rev_user' => $row->old_user,
451 'rev_user_text' => $this->conv( $row->old_user_text ),
452 'rev_timestamp' => $row->old_timestamp,
453 'rev_minor_edit' => $row->old_minor_edit );
454 $this->addChunk( $add );
456 $this->lastChunk( $add );
457 $this->dbr->freeResult( $result );
461 * Copy page metadata from cur into page.
462 * We'll also do UTF-8 conversion of titles.
464 $this->log( "......Setting up page table." );
465 $this->setChunkScale( $chunksize, $maxcur, 'page', $fname );
466 $result = $this->dbr->query( "
467 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
468 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
470 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
471 ORDER BY cur_id", $fname );
473 while( $row = $this->dbr->fetchObject( $result ) ) {
475 'page_id' => $row->cur_id,
476 'page_namespace' => $row->cur_namespace,
477 'page_title' => $this->conv( $row->cur_title ),
478 'page_restrictions' => $row->cur_restrictions,
479 'page_counter' => $row->cur_counter,
480 'page_is_redirect' => $row->cur_is_redirect,
481 'page_is_new' => $row->cur_is_new,
482 'page_random' => $row->cur_random,
483 'page_touched' => $this->dbw->timestamp(),
484 'page_latest' => $row->rev_id,
485 'page_len' => $row->len );
486 #$this->addChunk( $add, $row->cur_id );
487 $this->addChunk( $add );
489 $this->lastChunk( $add );
490 $this->dbr->freeResult( $result );
492 $this->log( "...done with cur/old -> page/revision." );
495 function upgradeLinks() {
496 $fname = 'FiveUpgrade::upgradeLinks';
498 list ($links, $brokenlinks, $pagelinks, $cur) = $this->dbw->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
500 $this->log( 'Checking for interwiki table change in case of bogus items...' );
501 if( $this->dbw->fieldExists( 'interwiki', 'iw_trans' ) ) {
502 $this->log( 'interwiki has iw_trans.' );
505 $this->log( 'adding iw_trans...' );
506 dbsource( $IP . '/maintenance/archives/patch-interwiki-trans.sql', $this->dbw );
507 $this->log( 'added iw_trans.' );
510 $this->log( 'Creating pagelinks table...' );
512 CREATE TABLE $pagelinks (
513 -- Key to the page_id of the page containing the link.
514 pl_from int(8) unsigned NOT NULL default '0',
516 -- Key to page_namespace/page_title of the target page.
517 -- The target page may or may not exist, and due to renames
518 -- and deletions may refer to different page records as time
520 pl_namespace int NOT NULL default '0',
521 pl_title varchar(255) binary NOT NULL default '',
523 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
524 KEY (pl_namespace,pl_title)
528 $this->log( 'Importing live links -> pagelinks' );
529 $nlinks = $this->dbw->selectField( 'links', 'count(*)', '', $fname );
531 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
532 $result = $this->dbr->query( "
533 SELECT l_from,cur_namespace,cur_title
535 WHERE l_to=cur_id", $fname );
537 while( $row = $this->dbr->fetchObject( $result ) ) {
539 'pl_from' => $row->l_from,
540 'pl_namespace' => $row->cur_namespace,
541 'pl_title' => $this->conv( $row->cur_title ) );
542 $this->addChunk( $add );
544 $this->lastChunk( $add );
546 $this->log( 'no links!' );
549 $this->log( 'Importing brokenlinks -> pagelinks' );
550 $nbrokenlinks = $this->dbw->selectField( 'brokenlinks', 'count(*)', '', $fname );
551 if( $nbrokenlinks ) {
552 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
553 $result = $this->dbr->query(
554 "SELECT bl_from, bl_to FROM $brokenlinks",
557 while( $row = $this->dbr->fetchObject( $result ) ) {
558 $pagename = $this->conv( $row->bl_to );
559 $title = Title::newFromText( $pagename );
560 if( is_null( $title ) ) {
561 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
564 'pl_from' => $row->bl_from,
565 'pl_namespace' => $title->getNamespace(),
566 'pl_title' => $title->getDBkey() );
567 $this->addChunk( $add );
570 $this->lastChunk( $add );
572 $this->log( 'no brokenlinks!' );
575 $this->log( 'Done with links.' );
578 function upgradeUser() {
579 // Apply unique index, if necessary:
580 $duper = new UserDupes( $this->dbw );
581 if( $duper->hasUniqueIndex() ) {
582 $this->log( "Already have unique user_name index." );
584 $this->log( "Clearing user duplicates..." );
585 if( !$duper->clearDupes() ) {
586 $this->log( "WARNING: Duplicate user accounts, may explode!" );
592 user_id int(5) unsigned NOT NULL auto_increment,
593 user_name varchar(255) binary NOT NULL default '',
594 user_real_name varchar(255) binary NOT NULL default '',
595 user_password tinyblob NOT NULL default '',
596 user_newpassword tinyblob NOT NULL default '',
597 user_email tinytext NOT NULL default '',
598 user_options blob NOT NULL default '',
599 user_touched char(14) binary NOT NULL default '',
600 user_token char(32) binary NOT NULL default '',
601 user_email_authenticated CHAR(14) BINARY,
602 user_email_token CHAR(32) BINARY,
603 user_email_token_expires CHAR(14) BINARY,
605 PRIMARY KEY user_id (user_id),
606 UNIQUE INDEX user_name (user_name),
607 INDEX (user_email_token)
612 'user_id' => MW_UPGRADE_COPY,
613 'user_name' => MW_UPGRADE_ENCODE,
614 'user_real_name' => MW_UPGRADE_ENCODE,
615 'user_password' => MW_UPGRADE_COPY,
616 'user_newpassword' => MW_UPGRADE_COPY,
617 'user_email' => MW_UPGRADE_ENCODE,
618 'user_options' => MW_UPGRADE_ENCODE,
619 'user_touched' => MW_UPGRADE_CALLBACK,
620 'user_token' => MW_UPGRADE_COPY,
621 'user_email_authenticated' => MW_UPGRADE_CALLBACK,
622 'user_email_token' => MW_UPGRADE_NULL,
623 'user_email_token_expires' => MW_UPGRADE_NULL );
624 $this->copyTable( 'user', $tabledef, $fields,
625 array( &$this, 'userCallback' ) );
628 function userCallback( $row, $copy ) {
629 $now = $this->dbw->timestamp();
630 $copy['user_touched'] = $now;
631 $copy['user_email_authenticated'] = $this->emailAuth ? $now : null;
635 function upgradeImage() {
638 img_name varchar(255) binary NOT NULL default '',
639 img_size int(8) unsigned NOT NULL default '0',
640 img_width int(5) NOT NULL default '0',
641 img_height int(5) NOT NULL default '0',
642 img_metadata mediumblob NOT NULL,
643 img_bits int(3) NOT NULL default '0',
644 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
645 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
646 img_minor_mime varchar(32) NOT NULL default "unknown",
647 img_description tinyblob NOT NULL default '',
648 img_user int(5) unsigned NOT NULL default '0',
649 img_user_text varchar(255) binary NOT NULL default '',
650 img_timestamp char(14) binary NOT NULL default '',
652 PRIMARY KEY img_name (img_name),
653 INDEX img_size (img_size),
654 INDEX img_timestamp (img_timestamp)
658 'img_name' => MW_UPGRADE_ENCODE,
659 'img_size' => MW_UPGRADE_COPY,
660 'img_width' => MW_UPGRADE_CALLBACK,
661 'img_height' => MW_UPGRADE_CALLBACK,
662 'img_metadata' => MW_UPGRADE_CALLBACK,
663 'img_bits' => MW_UPGRADE_CALLBACK,
664 'img_media_type' => MW_UPGRADE_CALLBACK,
665 'img_major_mime' => MW_UPGRADE_CALLBACK,
666 'img_minor_mime' => MW_UPGRADE_CALLBACK,
667 'img_description' => MW_UPGRADE_ENCODE,
668 'img_user' => MW_UPGRADE_COPY,
669 'img_user_text' => MW_UPGRADE_ENCODE,
670 'img_timestamp' => MW_UPGRADE_COPY );
671 $this->copyTable( 'image', $tabledef, $fields,
672 array( &$this, 'imageCallback' ) );
675 function imageCallback( $row, $copy ) {
677 if( !isset( $options['noimage'] ) ) {
678 // Fill in the new image info fields
679 $info = $this->imageInfo( $row->img_name );
681 $copy['img_width' ] = $info['width'];
682 $copy['img_height' ] = $info['height'];
683 $copy['img_metadata' ] = ""; // loaded on-demand
684 $copy['img_bits' ] = $info['bits'];
685 $copy['img_media_type'] = $info['media'];
686 $copy['img_major_mime'] = $info['major'];
687 $copy['img_minor_mime'] = $info['minor'];
690 // If doing UTF8 conversion the file must be renamed
691 $this->renameFile( $row->img_name, 'wfImageDir' );
696 function imageInfo( $name, $subdirCallback='wfImageDir', $basename = null ) {
697 if( is_null( $basename ) ) $basename = $name;
698 $dir = call_user_func( $subdirCallback, $basename );
699 $filename = $dir . '/' . $name;
708 $magic =& wfGetMimeMagic();
709 $mime = $magic->guessMimeType( $filename, true );
710 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
712 $info['media'] = $magic->getMediaType( $filename, $mime );
716 if( $mime == 'image/svg' ) {
717 $gis = wfGetSVGsize( $filename );
718 } elseif( $magic->isPHPImageType( $mime ) ) {
719 $gis = getimagesize( $filename );
721 $this->log( "Surprising mime type: $mime" );
724 $info['width' ] = $gis[0];
725 $info['height'] = $gis[1];
727 if( isset( $gis['bits'] ) ) {
728 $info['bits'] = $gis['bits'];
737 * @param string $table The table name to be truncated
739 function clearTable( $table ) {
740 print "Clearing $table...\n";
741 $tableName = $this->db->tableName( $table );
742 $this->db->query( "TRUNCATE $tableName" );
746 * Rename a given image or archived image file to the converted filename,
747 * leaving a symlink for URL compatibility.
749 * @param string $oldname pre-conversion filename
750 * @param string $basename pre-conversion base filename for dir hashing, if an archive
753 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
754 $newname = $this->conv( $oldname );
755 if( $newname == $oldname ) {
756 // No need to rename; another field triggered this row.
760 if( is_null( $basename ) ) $basename = $oldname;
761 $ubasename = $this->conv( $basename );
762 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
763 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
765 $this->log( "$oldpath -> $newpath" );
766 if( rename( $oldpath, $newpath ) ) {
767 $relpath = wfRelativePath( $newpath, dirname( $oldpath ) );
768 if( !symlink( $relpath, $oldpath ) ) {
769 $this->log( "... symlink failed!" );
773 $this->log( "... rename failed!" );
778 function upgradeOldImage() {
781 -- Base filename: key to image.img_name
782 oi_name varchar(255) binary NOT NULL default '',
784 -- Filename of the archived file.
785 -- This is generally a timestamp and '!' prepended to the base name.
786 oi_archive_name varchar(255) binary NOT NULL default '',
788 -- Other fields as in image...
789 oi_size int(8) unsigned NOT NULL default 0,
790 oi_width int(5) NOT NULL default 0,
791 oi_height int(5) NOT NULL default 0,
792 oi_bits int(3) NOT NULL default 0,
793 oi_description tinyblob NOT NULL default '',
794 oi_user int(5) unsigned NOT NULL default '0',
795 oi_user_text varchar(255) binary NOT NULL default '',
796 oi_timestamp char(14) binary NOT NULL default '',
798 INDEX oi_name (oi_name(10))
803 'oi_name' => MW_UPGRADE_ENCODE,
804 'oi_archive_name' => MW_UPGRADE_ENCODE,
805 'oi_size' => MW_UPGRADE_COPY,
806 'oi_width' => MW_UPGRADE_CALLBACK,
807 'oi_height' => MW_UPGRADE_CALLBACK,
808 'oi_bits' => MW_UPGRADE_CALLBACK,
809 'oi_description' => MW_UPGRADE_ENCODE,
810 'oi_user' => MW_UPGRADE_COPY,
811 'oi_user_text' => MW_UPGRADE_ENCODE,
812 'oi_timestamp' => MW_UPGRADE_COPY );
813 $this->copyTable( 'oldimage', $tabledef, $fields,
814 array( &$this, 'oldimageCallback' ) );
817 function oldimageCallback( $row, $copy ) {
819 if( !isset( $options['noimage'] ) ) {
820 // Fill in the new image info fields
821 $info = $this->imageInfo( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
822 $copy['oi_width' ] = $info['width' ];
823 $copy['oi_height'] = $info['height'];
824 $copy['oi_bits' ] = $info['bits' ];
827 // If doing UTF8 conversion the file must be renamed
828 $this->renameFile( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
834 function upgradeWatchlist() {
835 $fname = 'FiveUpgrade::upgradeWatchlist';
838 list ($watchlist, $watchlist_temp) = $this->dbw->tableNamesN( 'watchlist', 'watchlist_temp' );
840 $this->log( 'Migrating watchlist table to watchlist_temp...' );
842 "CREATE TABLE $watchlist_temp (
844 wl_user int(5) unsigned NOT NULL,
846 -- Key to page_namespace/page_title
847 -- Note that users may watch patches which do not exist yet,
848 -- or existed in the past but have been deleted.
849 wl_namespace int NOT NULL default '0',
850 wl_title varchar(255) binary NOT NULL default '',
852 -- Timestamp when user was last sent a notification e-mail;
853 -- cleared when the user visits the page.
854 -- FIXME: add proper null support etc
855 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
857 UNIQUE KEY (wl_user, wl_namespace, wl_title),
858 KEY namespace_title (wl_namespace,wl_title)
860 ) TYPE=InnoDB;", $fname );
862 // Fix encoding for Latin-1 upgrades, add some fields,
863 // and double article to article+talk pairs
864 $numwatched = $this->dbw->selectField( 'watchlist', 'count(*)', '', $fname );
866 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
867 $result = $this->dbr->select( 'watchlist',
876 while( $row = $this->dbr->fetchObject( $result ) ) {
878 'wl_user' => $row->wl_user,
879 'wl_namespace' => Namespace::getSubject( $row->wl_namespace ),
880 'wl_title' => $this->conv( $row->wl_title ),
881 'wl_notificationtimestamp' => '0' );
882 $this->addChunk( $add );
885 'wl_user' => $row->wl_user,
886 'wl_namespace' => Namespace::getTalk( $row->wl_namespace ),
887 'wl_title' => $this->conv( $row->wl_title ),
888 'wl_notificationtimestamp' => '0' );
889 $this->addChunk( $add );
891 $this->lastChunk( $add );
892 $this->dbr->freeResult( $result );
894 $this->log( 'Done converting watchlist.' );
895 $this->cleanupSwaps[] = 'watchlist';
898 function upgradeLogging() {
901 -- Symbolic keys for the general log type and the action type
902 -- within the log. The output format will be controlled by the
903 -- action field, but only the type controls categorization.
904 log_type char(10) NOT NULL default '',
905 log_action char(10) NOT NULL default '',
908 log_timestamp char(14) NOT NULL default '19700101000000',
910 -- The user who performed this action; key to user_id
911 log_user int unsigned NOT NULL default 0,
913 -- Key to the page affected. Where a user is the target,
914 -- this will point to the user page.
915 log_namespace int NOT NULL default 0,
916 log_title varchar(255) binary NOT NULL default '',
918 -- Freeform text. Interpreted as edit history comments.
919 log_comment varchar(255) NOT NULL default '',
921 -- LF separated list of miscellaneous parameters
922 log_params blob NOT NULL default '',
924 KEY type_time (log_type, log_timestamp),
925 KEY user_time (log_user, log_timestamp),
926 KEY page_time (log_namespace, log_title, log_timestamp)
931 'log_type' => MW_UPGRADE_COPY,
932 'log_action' => MW_UPGRADE_COPY,
933 'log_timestamp' => MW_UPGRADE_COPY,
934 'log_user' => MW_UPGRADE_COPY,
935 'log_namespace' => MW_UPGRADE_COPY,
936 'log_title' => MW_UPGRADE_ENCODE,
937 'log_comment' => MW_UPGRADE_ENCODE,
938 'log_params' => MW_UPGRADE_ENCODE );
939 $this->copyTable( 'logging', $tabledef, $fields );
942 function upgradeArchive() {
945 ar_namespace int NOT NULL default '0',
946 ar_title varchar(255) binary NOT NULL default '',
947 ar_text mediumblob NOT NULL default '',
949 ar_comment tinyblob NOT NULL default '',
950 ar_user int(5) unsigned NOT NULL default '0',
951 ar_user_text varchar(255) binary NOT NULL,
952 ar_timestamp char(14) binary NOT NULL default '',
953 ar_minor_edit tinyint(1) NOT NULL default '0',
955 ar_flags tinyblob NOT NULL default '',
957 ar_rev_id int(8) unsigned,
958 ar_text_id int(8) unsigned,
960 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
965 'ar_namespace' => MW_UPGRADE_COPY,
966 'ar_title' => MW_UPGRADE_ENCODE,
967 'ar_text' => MW_UPGRADE_COPY,
968 'ar_comment' => MW_UPGRADE_ENCODE,
969 'ar_user' => MW_UPGRADE_COPY,
970 'ar_user_text' => MW_UPGRADE_ENCODE,
971 'ar_timestamp' => MW_UPGRADE_COPY,
972 'ar_minor_edit' => MW_UPGRADE_COPY,
973 'ar_flags' => MW_UPGRADE_COPY,
974 'ar_rev_id' => MW_UPGRADE_NULL,
975 'ar_text_id' => MW_UPGRADE_NULL );
976 $this->copyTable( 'archive', $tabledef, $fields );
979 function upgradeImagelinks() {
984 -- Key to page_id of the page containing the image / media link.
985 il_from int(8) unsigned NOT NULL default '0',
987 -- Filename of target image.
988 -- This is also the page_title of the file's description page;
989 -- all such pages are in namespace 6 (NS_IMAGE).
990 il_to varchar(255) binary NOT NULL default '',
992 UNIQUE KEY il_from(il_from,il_to),
998 'il_from' => MW_UPGRADE_COPY,
999 'il_to' => MW_UPGRADE_ENCODE );
1000 $this->copyTable( 'imagelinks', $tabledef, $fields );
1004 function upgradeCategorylinks() {
1005 global $wgUseLatin1;
1006 if( $wgUseLatin1 ) {
1009 cl_from int(8) unsigned NOT NULL default '0',
1010 cl_to varchar(255) binary NOT NULL default '',
1011 cl_sortkey varchar(86) binary NOT NULL default '',
1012 cl_timestamp timestamp NOT NULL,
1014 UNIQUE KEY cl_from(cl_from,cl_to),
1015 KEY cl_sortkey(cl_to,cl_sortkey),
1016 KEY cl_timestamp(cl_to,cl_timestamp)
1020 'cl_from' => MW_UPGRADE_COPY,
1021 'cl_to' => MW_UPGRADE_ENCODE,
1022 'cl_sortkey' => MW_UPGRADE_ENCODE,
1023 'cl_timestamp' => MW_UPGRADE_COPY );
1024 $this->copyTable( 'categorylinks', $tabledef, $fields );
1028 function upgradeIpblocks() {
1029 global $wgUseLatin1;
1030 if( $wgUseLatin1 ) {
1033 ipb_id int(8) NOT NULL auto_increment,
1034 ipb_address varchar(40) binary NOT NULL default '',
1035 ipb_user int(8) unsigned NOT NULL default '0',
1036 ipb_by int(8) unsigned NOT NULL default '0',
1037 ipb_reason tinyblob NOT NULL default '',
1038 ipb_timestamp char(14) binary NOT NULL default '',
1039 ipb_auto tinyint(1) NOT NULL default '0',
1040 ipb_expiry char(14) binary NOT NULL default '',
1042 PRIMARY KEY ipb_id (ipb_id),
1043 INDEX ipb_address (ipb_address),
1044 INDEX ipb_user (ipb_user)
1049 'ipb_id' => MW_UPGRADE_COPY,
1050 'ipb_address' => MW_UPGRADE_COPY,
1051 'ipb_user' => MW_UPGRADE_COPY,
1052 'ipb_by' => MW_UPGRADE_COPY,
1053 'ipb_reason' => MW_UPGRADE_ENCODE,
1054 'ipb_timestamp' => MW_UPGRADE_COPY,
1055 'ipb_auto' => MW_UPGRADE_COPY,
1056 'ipb_expiry' => MW_UPGRADE_COPY );
1057 $this->copyTable( 'ipblocks', $tabledef, $fields );
1061 function upgradeRecentchanges() {
1062 // There's a format change in the namespace field
1065 rc_id int(8) NOT NULL auto_increment,
1066 rc_timestamp varchar(14) binary NOT NULL default '',
1067 rc_cur_time varchar(14) binary NOT NULL default '',
1069 rc_user int(10) unsigned NOT NULL default '0',
1070 rc_user_text varchar(255) binary NOT NULL default '',
1072 rc_namespace int NOT NULL default '0',
1073 rc_title varchar(255) binary NOT NULL default '',
1075 rc_comment varchar(255) binary NOT NULL default '',
1076 rc_minor tinyint(3) unsigned NOT NULL default '0',
1078 rc_bot tinyint(3) unsigned NOT NULL default '0',
1079 rc_new tinyint(3) unsigned NOT NULL default '0',
1081 rc_cur_id int(10) unsigned NOT NULL default '0',
1082 rc_this_oldid int(10) unsigned NOT NULL default '0',
1083 rc_last_oldid int(10) unsigned NOT NULL default '0',
1085 rc_type tinyint(3) unsigned NOT NULL default '0',
1086 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
1087 rc_moved_to_title varchar(255) binary NOT NULL default '',
1089 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1091 rc_ip char(15) NOT NULL default '',
1093 PRIMARY KEY rc_id (rc_id),
1094 INDEX rc_timestamp (rc_timestamp),
1095 INDEX rc_namespace_title (rc_namespace, rc_title),
1096 INDEX rc_cur_id (rc_cur_id),
1097 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
1103 'rc_id' => MW_UPGRADE_COPY,
1104 'rc_timestamp' => MW_UPGRADE_COPY,
1105 'rc_cur_time' => MW_UPGRADE_COPY,
1106 'rc_user' => MW_UPGRADE_COPY,
1107 'rc_user_text' => MW_UPGRADE_ENCODE,
1108 'rc_namespace' => MW_UPGRADE_COPY,
1109 'rc_title' => MW_UPGRADE_ENCODE,
1110 'rc_comment' => MW_UPGRADE_ENCODE,
1111 'rc_minor' => MW_UPGRADE_COPY,
1112 'rc_bot' => MW_UPGRADE_COPY,
1113 'rc_new' => MW_UPGRADE_COPY,
1114 'rc_cur_id' => MW_UPGRADE_COPY,
1115 'rc_this_oldid' => MW_UPGRADE_COPY,
1116 'rc_last_oldid' => MW_UPGRADE_COPY,
1117 'rc_type' => MW_UPGRADE_COPY,
1118 'rc_moved_to_ns' => MW_UPGRADE_COPY,
1119 'rc_moved_to_title' => MW_UPGRADE_ENCODE,
1120 'rc_patrolled' => MW_UPGRADE_COPY,
1121 'rc_ip' => MW_UPGRADE_COPY );
1122 $this->copyTable( 'recentchanges', $tabledef, $fields );
1125 function upgradeQuerycache() {
1126 // There's a format change in the namespace field
1129 -- A key name, generally the base name of of the special page.
1130 qc_type char(32) NOT NULL,
1132 -- Some sort of stored value. Sizes, counts...
1133 qc_value int(5) unsigned NOT NULL default '0',
1135 -- Target namespace+title
1136 qc_namespace int NOT NULL default '0',
1137 qc_title char(255) binary NOT NULL default '',
1139 KEY (qc_type,qc_value)
1144 'qc_type' => MW_UPGRADE_COPY,
1145 'qc_value' => MW_UPGRADE_COPY,
1146 'qc_namespace' => MW_UPGRADE_COPY,
1147 'qc_title' => MW_UPGRADE_ENCODE );
1148 $this->copyTable( 'querycache', $tabledef, $fields );
1152 * Rename all our temporary tables into final place.
1153 * We've left things in place so a read-only wiki can continue running
1154 * on the old code during all this.
1156 function upgradeCleanup() {
1157 $this->renameTable( 'old', 'text' );
1159 foreach( $this->cleanupSwaps as $table ) {
1160 $this->swap( $table );
1164 function renameTable( $from, $to ) {
1165 $this->log( "Renaming $from to $to..." );
1167 $fromtable = $this->dbw->tableName( $from );
1168 $totable = $this->dbw->tableName( $to );
1169 $this->dbw->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1172 function swap( $base ) {
1173 $this->renameTable( $base, "{$base}_old" );
1174 $this->renameTable( "{$base}_temp", $base );