00001 <?php
00007 require_once( 'cleanupDupes.inc' );
00008 require_once( 'userDupes.inc' );
00009 require_once( 'updaters.inc' );
00010
00011 define( 'MW_UPGRADE_COPY', false );
00012 define( 'MW_UPGRADE_ENCODE', true );
00013 define( 'MW_UPGRADE_NULL', null );
00014 define( 'MW_UPGRADE_CALLBACK', null );
00015
00019 class FiveUpgrade {
00020 function FiveUpgrade() {
00021 $this->conversionTables = $this->prepareWindows1252();
00022
00023 $this->loadBalancers = array();
00024 $this->dbw = wfGetDB( DB_MASTER );
00025 $this->dbr = $this->streamConnection();
00026
00027 $this->cleanupSwaps = array();
00028 $this->emailAuth = false; # don't preauthenticate emails
00029 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
00030 }
00031
00032 function doing( $step ) {
00033 return is_null( $this->step ) || $step == $this->step;
00034 }
00035
00036 function upgrade( $step ) {
00037 $this->step = $step;
00038
00039 $tables = array(
00040 'page',
00041 'links',
00042 'user',
00043 'image',
00044 'oldimage',
00045 'watchlist',
00046 'logging',
00047 'archive',
00048 'imagelinks',
00049 'categorylinks',
00050 'ipblocks',
00051 'recentchanges',
00052 'querycache' );
00053 foreach( $tables as $table ) {
00054 if( $this->doing( $table ) ) {
00055 $method = 'upgrade' . ucfirst( $table );
00056 $this->$method();
00057 }
00058 }
00059
00060 if( $this->doing( 'cleanup' ) ) {
00061 $this->upgradeCleanup();
00062 }
00063 }
00064
00065
00071 function newConnection() {
00072 $lb = wfGetLBFactory()->newMainLB();
00073 $db = $lb->getConnection( DB_MASTER );
00074
00075 $this->loadBalancers[] = $lb;
00076 return $db;
00077 }
00078
00082 function close() {
00083 foreach( $this->loadBalancers as $lb ) {
00084 $lb->commitMasterChanges();
00085 $lb->closeAll();
00086 }
00087 }
00088
00096 function streamConnection() {
00097 global $wgDBtype;
00098
00099 $timeout = 3600 * 24;
00100 $db = $this->newConnection();
00101 $db->bufferResults( false );
00102 if ($wgDBtype == 'mysql') {
00103 $db->query( "SET net_read_timeout=$timeout" );
00104 $db->query( "SET net_write_timeout=$timeout" );
00105 }
00106 return $db;
00107 }
00108
00118 function prepareWindows1252() {
00119 # Mappings from:
00120 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
00121 static $cp1252 = array(
00122 0x80 => 0x20AC, #EURO SIGN
00123 0x81 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
00124 0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
00125 0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
00126 0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
00127 0x85 => 0x2026, #HORIZONTAL ELLIPSIS
00128 0x86 => 0x2020, #DAGGER
00129 0x87 => 0x2021, #DOUBLE DAGGER
00130 0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
00131 0x89 => 0x2030, #PER MILLE SIGN
00132 0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
00133 0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
00134 0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
00135 0x8D => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
00136 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
00137 0x8F => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
00138 0x90 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
00139 0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
00140 0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
00141 0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
00142 0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
00143 0x95 => 0x2022, #BULLET
00144 0x96 => 0x2013, #EN DASH
00145 0x97 => 0x2014, #EM DASH
00146 0x98 => 0x02DC, #SMALL TILDE
00147 0x99 => 0x2122, #TRADE MARK SIGN
00148 0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
00149 0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
00150 0x9C => 0x0153, #LATIN SMALL LIGATURE OE
00151 0x9D => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
00152 0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
00153 0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
00154 );
00155 $pairs = array();
00156 for( $i = 0; $i < 0x100; $i++ ) {
00157 $unicode = isset( $cp1252[$i] ) ? $cp1252[$i] : $i;
00158 $pairs[chr( $i )] = codepointToUtf8( $unicode );
00159 }
00160 return $pairs;
00161 }
00162
00169 function conv( $text ) {
00170 global $wgUseLatin1;
00171 return is_null( $text )
00172 ? null
00173 : ( $wgUseLatin1
00174 ? strtr( $text, $this->conversionTables )
00175 : $text );
00176 }
00177
00183 function log( $message ) {
00184 echo wfWikiID() . ' ' . wfTimestamp( TS_DB ) . ': ' . $message . "\n";
00185 flush();
00186 }
00187
00203 function setChunkScale( $chunksize, $final, $table, $fname ) {
00204 $this->chunkSize = $chunksize;
00205 $this->chunkFinal = $final;
00206 $this->chunkCount = 0;
00207 $this->chunkStartTime = wfTime();
00208 $this->chunkOptions = array( 'IGNORE' );
00209 $this->chunkTable = $table;
00210 $this->chunkFunction = $fname;
00211 }
00212
00223 function addChunk( &$chunk, $key = null ) {
00224 if( count( $chunk ) >= $this->chunkSize ) {
00225 $this->insertChunk( $chunk );
00226
00227 $this->chunkCount += count( $chunk );
00228 $now = wfTime();
00229 $delta = $now - $this->chunkStartTime;
00230 $rate = $this->chunkCount / $delta;
00231
00232 if( is_null( $key ) ) {
00233 $completed = $this->chunkCount;
00234 } else {
00235 $completed = $key;
00236 }
00237 $portion = $completed / $this->chunkFinal;
00238
00239 $estimatedTotalTime = $delta / $portion;
00240 $eta = $this->chunkStartTime + $estimatedTotalTime;
00241
00242 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
00243 wfTimestamp( TS_DB, intval( $now ) ),
00244 $portion * 100.0,
00245 $this->chunkTable,
00246 wfTimestamp( TS_DB, intval( $eta ) ),
00247 $completed,
00248 $this->chunkFinal,
00249 $rate );
00250 flush();
00251
00252 $chunk = array();
00253 }
00254 }
00255
00261 function lastChunk( &$chunk ) {
00262 $n = count( $chunk );
00263 if( $n > 0 ) {
00264 $this->insertChunk( $chunk );
00265 }
00266 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
00267 }
00268
00274 function insertChunk( &$chunk ) {
00275 // Give slaves a chance to catch up
00276 wfWaitForSlaves( $this->maxLag );
00277 $this->dbw->insert( $this->chunkTable, $chunk, $this->chunkFunction, $this->chunkOptions );
00278 }
00279
00280
00294 function copyTable( $name, $tabledef, $fields, $callback = null ) {
00295 $fname = 'FiveUpgrade::copyTable';
00296
00297 $name_temp = $name . '_temp';
00298 $this->log( "Migrating $name table to $name_temp..." );
00299
00300 $table_temp = $this->dbw->tableName( $name_temp );
00301
00302 // Create temporary table; we're going to copy everything in there,
00303
00304 $def = str_replace( '$1', $table_temp, $tabledef );
00305 $this->dbw->query( $def, $fname );
00306
00307 $numRecords = $this->dbw->selectField( $name, 'COUNT(*)', '', $fname );
00308 $this->setChunkScale( 100, $numRecords, $name_temp, $fname );
00309
00310
00311 $sourceFields = array_keys( array_filter( $fields,
00312 create_function( '$x', 'return $x !== MW_UPGRADE_NULL;' ) ) );
00313 $result = $this->dbr->select( $name,
00314 $sourceFields,
00315 '',
00316 $fname );
00317
00318 $add = array();
00319 while( $row = $this->dbr->fetchObject( $result ) ) {
00320 $copy = array();
00321 foreach( $fields as $field => $source ) {
00322 if( $source === MW_UPGRADE_COPY ) {
00323 $copy[$field] = $row->$field;
00324 } elseif( $source === MW_UPGRADE_ENCODE ) {
00325 $copy[$field] = $this->conv( $row->$field );
00326 } elseif( $source === MW_UPGRADE_NULL ) {
00327 $copy[$field] = null;
00328 } else {
00329 $this->log( "Unknown field copy type: $field => $source" );
00330 }
00331 }
00332 if( is_callable( $callback ) ) {
00333 $copy = call_user_func( $callback, $row, $copy );
00334 }
00335 $add[] = $copy;
00336 $this->addChunk( $add );
00337 }
00338 $this->lastChunk( $add );
00339 $this->dbr->freeResult( $result );
00340
00341 $this->log( "Done converting $name." );
00342 $this->cleanupSwaps[] = $name;
00343 }
00344
00345 function upgradePage() {
00346 $fname = "FiveUpgrade::upgradePage";
00347 $chunksize = 100;
00348
00349 if( $this->dbw->tableExists( 'page' ) ) {
00350 $this->log( 'Page table already exists; aborting.' );
00351 die( -1 );
00352 }
00353
00354 $this->log( "Checking cur table for unique title index and applying if necessary" );
00355 checkDupes( true );
00356
00357 $this->log( "...converting from cur/old to page/revision/text DB structure." );
00358
00359 list ($cur, $old, $page, $revision, $text) = $this->dbw->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
00360
00361 $this->log( "Creating page and revision tables..." );
00362 $this->dbw->query("CREATE TABLE $page (
00363 page_id int(8) unsigned NOT NULL auto_increment,
00364 page_namespace int NOT NULL,
00365 page_title varchar(255) binary NOT NULL,
00366 page_restrictions tinyblob NOT NULL default '',
00367 page_counter bigint(20) unsigned NOT NULL default '0',
00368 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
00369 page_is_new tinyint(1) unsigned NOT NULL default '0',
00370 page_random real unsigned NOT NULL,
00371 page_touched char(14) binary NOT NULL default '',
00372 page_latest int(8) unsigned NOT NULL,
00373 page_len int(8) unsigned NOT NULL,
00374
00375 PRIMARY KEY page_id (page_id),
00376 UNIQUE INDEX name_title (page_namespace,page_title),
00377 INDEX (page_random),
00378 INDEX (page_len)
00379 ) TYPE=InnoDB", $fname );
00380 $this->dbw->query("CREATE TABLE $revision (
00381 rev_id int(8) unsigned NOT NULL auto_increment,
00382 rev_page int(8) unsigned NOT NULL,
00383 rev_text_id int(8) unsigned NOT NULL,
00384 rev_comment tinyblob NOT NULL default '',
00385 rev_user int(5) unsigned NOT NULL default '0',
00386 rev_user_text varchar(255) binary NOT NULL default '',
00387 rev_timestamp char(14) binary NOT NULL default '',
00388 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
00389 rev_deleted tinyint(1) unsigned NOT NULL default '0',
00390
00391 PRIMARY KEY rev_page_id (rev_page, rev_id),
00392 UNIQUE INDEX rev_id (rev_id),
00393 INDEX rev_timestamp (rev_timestamp),
00394 INDEX page_timestamp (rev_page,rev_timestamp),
00395 INDEX user_timestamp (rev_user,rev_timestamp),
00396 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
00397 ) TYPE=InnoDB", $fname );
00398
00399 $maxold = intval( $this->dbw->selectField( 'old', 'max(old_id)', '', $fname ) );
00400 $this->log( "Last old record is {$maxold}" );
00401
00402 global $wgLegacySchemaConversion;
00403 if( $wgLegacySchemaConversion ) {
00404
00405
00406 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
00407 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
00408 $cur_flags = "'object'";
00409 } else {
00410
00411
00412 echo "......Moving text from cur.\n";
00413 $cur_text = 'cur_text';
00414 $cur_flags = "''";
00415 }
00416
00417 $maxcur = $this->dbw->selectField( 'cur', 'max(cur_id)', '', $fname );
00418 $this->log( "Last cur entry is $maxcur" );
00419
00426 $this->setChunkScale( $chunksize, $maxcur, 'old', $fname );
00427 $result = $this->dbr->query(
00428 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
00429 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
00430 FROM $cur
00431 ORDER BY cur_id", $fname );
00432 $add = array();
00433 while( $row = $this->dbr->fetchObject( $result ) ) {
00434 $add[] = array(
00435 'old_namespace' => $row->cur_namespace,
00436 'old_title' => $row->cur_title,
00437 'old_text' => $row->text,
00438 'old_comment' => $row->cur_comment,
00439 'old_user' => $row->cur_user,
00440 'old_user_text' => $row->cur_user_text,
00441 'old_timestamp' => $row->cur_timestamp,
00442 'old_minor_edit' => $row->cur_minor_edit,
00443 'old_flags' => $row->flags );
00444 $this->addChunk( $add, $row->cur_id );
00445 }
00446 $this->lastChunk( $add );
00447 $this->dbr->freeResult( $result );
00448
00453 #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
00454 #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
00455 #$countold = $this->dbw->selectField( 'old', 'count(old_id)', '', $fname );
00456 $countold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
00457 $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
00458
00459 $this->log( "......Setting up revision table." );
00460 $result = $this->dbr->query(
00461 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
00462 old_timestamp, old_minor_edit
00463 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
00464 $fname );
00465
00466 $add = array();
00467 while( $row = $this->dbr->fetchObject( $result ) ) {
00468 $add[] = array(
00469 'rev_id' => $row->old_id,
00470 'rev_page' => $row->cur_id,
00471 'rev_text_id' => $row->old_id,
00472 'rev_comment' => $this->conv( $row->old_comment ),
00473 'rev_user' => $row->old_user,
00474 'rev_user_text' => $this->conv( $row->old_user_text ),
00475 'rev_timestamp' => $row->old_timestamp,
00476 'rev_minor_edit' => $row->old_minor_edit );
00477 $this->addChunk( $add );
00478 }
00479 $this->lastChunk( $add );
00480 $this->dbr->freeResult( $result );
00481
00482
00487 $this->log( "......Setting up page table." );
00488 $this->setChunkScale( $chunksize, $maxcur, 'page', $fname );
00489 $result = $this->dbr->query( "
00490 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
00491 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
00492 FROM $cur,$revision
00493 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
00494 ORDER BY cur_id", $fname );
00495 $add = array();
00496 while( $row = $this->dbr->fetchObject( $result ) ) {
00497 $add[] = array(
00498 'page_id' => $row->cur_id,
00499 'page_namespace' => $row->cur_namespace,
00500 'page_title' => $this->conv( $row->cur_title ),
00501 'page_restrictions' => $row->cur_restrictions,
00502 'page_counter' => $row->cur_counter,
00503 'page_is_redirect' => $row->cur_is_redirect,
00504 'page_is_new' => $row->cur_is_new,
00505 'page_random' => $row->cur_random,
00506 'page_touched' => $this->dbw->timestamp(),
00507 'page_latest' => $row->rev_id,
00508 'page_len' => $row->len );
00509 #$this->addChunk( $add, $row->cur_id );
00510 $this->addChunk( $add );
00511 }
00512 $this->lastChunk( $add );
00513 $this->dbr->freeResult( $result );
00514
00515 $this->log( "...done with cur/old -> page/revision." );
00516 }
00517
00518 function upgradeLinks() {
00519 $fname = 'FiveUpgrade::upgradeLinks';
00520 $chunksize = 200;
00521 list ($links, $brokenlinks, $pagelinks, $cur) = $this->dbw->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
00522
00523 $this->log( 'Checking for interwiki table change in case of bogus items...' );
00524 if( $this->dbw->fieldExists( 'interwiki', 'iw_trans' ) ) {
00525 $this->log( 'interwiki has iw_trans.' );
00526 } else {
00527 global $IP;
00528 $this->log( 'adding iw_trans...' );
00529 dbsource( $IP . '/maintenance/archives/patch-interwiki-trans.sql', $this->dbw );
00530 $this->log( 'added iw_trans.' );
00531 }
00532
00533 $this->log( 'Creating pagelinks table...' );
00534 $this->dbw->query( "
00535 CREATE TABLE $pagelinks (
00536 -- Key to the page_id of the page containing the link.
00537 pl_from int(8) unsigned NOT NULL default '0',
00538
00539 -- Key to page_namespace/page_title of the target page.
00540 -- The target page may or may not exist, and due to renames
00541 -- and deletions may refer to different page records as time
00542 -- goes by.
00543 pl_namespace int NOT NULL default '0',
00544 pl_title varchar(255) binary NOT NULL default '',
00545
00546 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
00547 KEY (pl_namespace,pl_title)
00548
00549 ) TYPE=InnoDB" );
00550
00551 $this->log( 'Importing live links -> pagelinks' );
00552 $nlinks = $this->dbw->selectField( 'links', 'count(*)', '', $fname );
00553 if( $nlinks ) {
00554 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
00555 $result = $this->dbr->query( "
00556 SELECT l_from,cur_namespace,cur_title
00557 FROM $links, $cur
00558 WHERE l_to=cur_id", $fname );
00559 $add = array();
00560 while( $row = $this->dbr->fetchObject( $result ) ) {
00561 $add[] = array(
00562 'pl_from' => $row->l_from,
00563 'pl_namespace' => $row->cur_namespace,
00564 'pl_title' => $this->conv( $row->cur_title ) );
00565 $this->addChunk( $add );
00566 }
00567 $this->lastChunk( $add );
00568 } else {
00569 $this->log( 'no links!' );
00570 }
00571
00572 $this->log( 'Importing brokenlinks -> pagelinks' );
00573 $nbrokenlinks = $this->dbw->selectField( 'brokenlinks', 'count(*)', '', $fname );
00574 if( $nbrokenlinks ) {
00575 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
00576 $result = $this->dbr->query(
00577 "SELECT bl_from, bl_to FROM $brokenlinks",
00578 $fname );
00579 $add = array();
00580 while( $row = $this->dbr->fetchObject( $result ) ) {
00581 $pagename = $this->conv( $row->bl_to );
00582 $title = Title::newFromText( $pagename );
00583 if( is_null( $title ) ) {
00584 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
00585 } else {
00586 $add[] = array(
00587 'pl_from' => $row->bl_from,
00588 'pl_namespace' => $title->getNamespace(),
00589 'pl_title' => $title->getDBkey() );
00590 $this->addChunk( $add );
00591 }
00592 }
00593 $this->lastChunk( $add );
00594 } else {
00595 $this->log( 'no brokenlinks!' );
00596 }
00597
00598 $this->log( 'Done with links.' );
00599 }
00600
00601 function upgradeUser() {
00602
00603 $duper = new UserDupes( $this->dbw );
00604 if( $duper->hasUniqueIndex() ) {
00605 $this->log( "Already have unique user_name index." );
00606 } else {
00607 $this->log( "Clearing user duplicates..." );
00608 if( !$duper->clearDupes() ) {
00609 $this->log( "WARNING: Duplicate user accounts, may explode!" );
00610 }
00611 }
00612
00613 $tabledef = <<<END
00614 CREATE TABLE $1 (
00615 user_id int(5) unsigned NOT NULL auto_increment,
00616 user_name varchar(255) binary NOT NULL default '',
00617 user_real_name varchar(255) binary NOT NULL default '',
00618 user_password tinyblob NOT NULL default '',
00619 user_newpassword tinyblob NOT NULL default '',
00620 user_email tinytext NOT NULL default '',
00621 user_options blob NOT NULL default '',
00622 user_touched char(14) binary NOT NULL default '',
00623 user_token char(32) binary NOT NULL default '',
00624 user_email_authenticated CHAR(14) BINARY,
00625 user_email_token CHAR(32) BINARY,
00626 user_email_token_expires CHAR(14) BINARY,
00627
00628 PRIMARY KEY user_id (user_id),
00629 UNIQUE INDEX user_name (user_name),
00630 INDEX (user_email_token)
00631
00632 ) TYPE=InnoDB
00633 END;
00634 $fields = array(
00635 'user_id' => MW_UPGRADE_COPY,
00636 'user_name' => MW_UPGRADE_ENCODE,
00637 'user_real_name' => MW_UPGRADE_ENCODE,
00638 'user_password' => MW_UPGRADE_COPY,
00639 'user_newpassword' => MW_UPGRADE_COPY,
00640 'user_email' => MW_UPGRADE_ENCODE,
00641 'user_options' => MW_UPGRADE_ENCODE,
00642 'user_touched' => MW_UPGRADE_CALLBACK,
00643 'user_token' => MW_UPGRADE_COPY,
00644 'user_email_authenticated' => MW_UPGRADE_CALLBACK,
00645 'user_email_token' => MW_UPGRADE_NULL,
00646 'user_email_token_expires' => MW_UPGRADE_NULL );
00647 $this->copyTable( 'user', $tabledef, $fields,
00648 array( &$this, 'userCallback' ) );
00649 }
00650
00651 function userCallback( $row, $copy ) {
00652 $now = $this->dbw->timestamp();
00653 $copy['user_touched'] = $now;
00654 $copy['user_email_authenticated'] = $this->emailAuth ? $now : null;
00655 return $copy;
00656 }
00657
00658 function upgradeImage() {
00659 $tabledef = <<<END
00660 CREATE TABLE $1 (
00661 img_name varchar(255) binary NOT NULL default '',
00662 img_size int(8) unsigned NOT NULL default '0',
00663 img_width int(5) NOT NULL default '0',
00664 img_height int(5) NOT NULL default '0',
00665 img_metadata mediumblob NOT NULL,
00666 img_bits int(3) NOT NULL default '0',
00667 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
00668 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
00669 img_minor_mime varchar(32) NOT NULL default "unknown",
00670 img_description tinyblob NOT NULL default '',
00671 img_user int(5) unsigned NOT NULL default '0',
00672 img_user_text varchar(255) binary NOT NULL default '',
00673 img_timestamp char(14) binary NOT NULL default '',
00674
00675 PRIMARY KEY img_name (img_name),
00676 INDEX img_size (img_size),
00677 INDEX img_timestamp (img_timestamp)
00678 ) TYPE=InnoDB
00679 END;
00680 $fields = array(
00681 'img_name' => MW_UPGRADE_ENCODE,
00682 'img_size' => MW_UPGRADE_COPY,
00683 'img_width' => MW_UPGRADE_CALLBACK,
00684 'img_height' => MW_UPGRADE_CALLBACK,
00685 'img_metadata' => MW_UPGRADE_CALLBACK,
00686 'img_bits' => MW_UPGRADE_CALLBACK,
00687 'img_media_type' => MW_UPGRADE_CALLBACK,
00688 'img_major_mime' => MW_UPGRADE_CALLBACK,
00689 'img_minor_mime' => MW_UPGRADE_CALLBACK,
00690 'img_description' => MW_UPGRADE_ENCODE,
00691 'img_user' => MW_UPGRADE_COPY,
00692 'img_user_text' => MW_UPGRADE_ENCODE,
00693 'img_timestamp' => MW_UPGRADE_COPY );
00694 $this->copyTable( 'image', $tabledef, $fields,
00695 array( &$this, 'imageCallback' ) );
00696 }
00697
00698 function imageCallback( $row, $copy ) {
00699 global $options;
00700 if( !isset( $options['noimage'] ) ) {
00701
00702 $info = $this->imageInfo( $row->img_name );
00703
00704 $copy['img_width' ] = $info['width'];
00705 $copy['img_height' ] = $info['height'];
00706 $copy['img_metadata' ] = "";
00707 $copy['img_bits' ] = $info['bits'];
00708 $copy['img_media_type'] = $info['media'];
00709 $copy['img_major_mime'] = $info['major'];
00710 $copy['img_minor_mime'] = $info['minor'];
00711 }
00712
00713
00714 $this->renameFile( $row->img_name, 'wfImageDir' );
00715
00716 return $copy;
00717 }
00718
00719 function imageInfo( $filename ) {
00720 $info = array(
00721 'width' => 0,
00722 'height' => 0,
00723 'bits' => 0,
00724 'media' => '',
00725 'major' => '',
00726 'minor' => '' );
00727
00728 $magic = MimeMagic::singleton();
00729 $mime = $magic->guessMimeType( $filename, true );
00730 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
00731
00732 $info['media'] = $magic->getMediaType( $filename, $mime );
00733
00734 $image = UnregisteredLocalFile::newFromPath( $filename, $mime );
00735
00736 $info['width'] = $image->getWidth();
00737 $info['height'] = $image->getHeight();
00738
00739 $gis = $image->getImageSize();
00740 if ( isset( $gis['bits'] ) ) {
00741 $info['bits'] = $gis['bits'];
00742 }
00743
00744 return $info;
00745 }
00746
00747
00752 function clearTable( $table ) {
00753 print "Clearing $table...\n";
00754 $tableName = $this->db->tableName( $table );
00755 $this->db->query( "TRUNCATE $tableName" );
00756 }
00757
00766 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
00767 $newname = $this->conv( $oldname );
00768 if( $newname == $oldname ) {
00769
00770 return false;
00771 }
00772
00773 if( is_null( $basename ) ) $basename = $oldname;
00774 $ubasename = $this->conv( $basename );
00775 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
00776 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
00777
00778 $this->log( "$oldpath -> $newpath" );
00779 if( rename( $oldpath, $newpath ) ) {
00780 $relpath = wfRelativePath( $newpath, dirname( $oldpath ) );
00781 if( !symlink( $relpath, $oldpath ) ) {
00782 $this->log( "... symlink failed!" );
00783 }
00784 return $newname;
00785 } else {
00786 $this->log( "... rename failed!" );
00787 return false;
00788 }
00789 }
00790
00791 function upgradeOldImage() {
00792 $tabledef = <<<END
00793 CREATE TABLE $1 (
00794 -- Base filename: key to image.img_name
00795 oi_name varchar(255) binary NOT NULL default '',
00796
00797 -- Filename of the archived file.
00798 -- This is generally a timestamp and '!' prepended to the base name.
00799 oi_archive_name varchar(255) binary NOT NULL default '',
00800
00801 -- Other fields as in image...
00802 oi_size int(8) unsigned NOT NULL default 0,
00803 oi_width int(5) NOT NULL default 0,
00804 oi_height int(5) NOT NULL default 0,
00805 oi_bits int(3) NOT NULL default 0,
00806 oi_description tinyblob NOT NULL default '',
00807 oi_user int(5) unsigned NOT NULL default '0',
00808 oi_user_text varchar(255) binary NOT NULL default '',
00809 oi_timestamp char(14) binary NOT NULL default '',
00810
00811 INDEX oi_name (oi_name(10))
00812
00813 ) TYPE=InnoDB;
00814 END;
00815 $fields = array(
00816 'oi_name' => MW_UPGRADE_ENCODE,
00817 'oi_archive_name' => MW_UPGRADE_ENCODE,
00818 'oi_size' => MW_UPGRADE_COPY,
00819 'oi_width' => MW_UPGRADE_CALLBACK,
00820 'oi_height' => MW_UPGRADE_CALLBACK,
00821 'oi_bits' => MW_UPGRADE_CALLBACK,
00822 'oi_description' => MW_UPGRADE_ENCODE,
00823 'oi_user' => MW_UPGRADE_COPY,
00824 'oi_user_text' => MW_UPGRADE_ENCODE,
00825 'oi_timestamp' => MW_UPGRADE_COPY );
00826 $this->copyTable( 'oldimage', $tabledef, $fields,
00827 array( &$this, 'oldimageCallback' ) );
00828 }
00829
00830 function oldimageCallback( $row, $copy ) {
00831 global $options;
00832 if( !isset( $options['noimage'] ) ) {
00833
00834 $info = $this->imageInfo( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
00835 $copy['oi_width' ] = $info['width' ];
00836 $copy['oi_height'] = $info['height'];
00837 $copy['oi_bits' ] = $info['bits' ];
00838 }
00839
00840
00841 $this->renameFile( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
00842
00843 return $copy;
00844 }
00845
00846
00847 function upgradeWatchlist() {
00848 $fname = 'FiveUpgrade::upgradeWatchlist';
00849 $chunksize = 100;
00850
00851 list ($watchlist, $watchlist_temp) = $this->dbw->tableNamesN( 'watchlist', 'watchlist_temp' );
00852
00853 $this->log( 'Migrating watchlist table to watchlist_temp...' );
00854 $this->dbw->query(
00855 "CREATE TABLE $watchlist_temp (
00856 -- Key to user_id
00857 wl_user int(5) unsigned NOT NULL,
00858
00859 -- Key to page_namespace/page_title
00860 -- Note that users may watch patches which do not exist yet,
00861 -- or existed in the past but have been deleted.
00862 wl_namespace int NOT NULL default '0',
00863 wl_title varchar(255) binary NOT NULL default '',
00864
00865 -- Timestamp when user was last sent a notification e-mail;
00866 -- cleared when the user visits the page.
00867 -- FIXME: add proper null support etc
00868 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
00869
00870 UNIQUE KEY (wl_user, wl_namespace, wl_title),
00871 KEY namespace_title (wl_namespace,wl_title)
00872
00873 ) TYPE=InnoDB;", $fname );
00874
00875
00876
00877 $numwatched = $this->dbw->selectField( 'watchlist', 'count(*)', '', $fname );
00878
00879 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
00880 $result = $this->dbr->select( 'watchlist',
00881 array(
00882 'wl_user',
00883 'wl_namespace',
00884 'wl_title' ),
00885 '',
00886 $fname );
00887
00888 $add = array();
00889 while( $row = $this->dbr->fetchObject( $result ) ) {
00890 $add[] = array(
00891 'wl_user' => $row->wl_user,
00892 'wl_namespace' => MWNamespace::getSubject( $row->wl_namespace ),
00893 'wl_title' => $this->conv( $row->wl_title ),
00894 'wl_notificationtimestamp' => '0' );
00895 $this->addChunk( $add );
00896
00897 $add[] = array(
00898 'wl_user' => $row->wl_user,
00899 'wl_namespace' => MWNamespace::getTalk( $row->wl_namespace ),
00900 'wl_title' => $this->conv( $row->wl_title ),
00901 'wl_notificationtimestamp' => '0' );
00902 $this->addChunk( $add );
00903 }
00904 $this->lastChunk( $add );
00905 $this->dbr->freeResult( $result );
00906
00907 $this->log( 'Done converting watchlist.' );
00908 $this->cleanupSwaps[] = 'watchlist';
00909 }
00910
00911 function upgradeLogging() {
00912 $tabledef = <<<ENDS
00913 CREATE TABLE $1 (
00914 -- Symbolic keys for the general log type and the action type
00915 -- within the log. The output format will be controlled by the
00916 -- action field, but only the type controls categorization.
00917 log_type char(10) NOT NULL default '',
00918 log_action char(10) NOT NULL default '',
00919
00920 -- Timestamp. Duh.
00921 log_timestamp char(14) NOT NULL default '19700101000000',
00922
00923 -- The user who performed this action; key to user_id
00924 log_user int unsigned NOT NULL default 0,
00925
00926 -- Key to the page affected. Where a user is the target,
00927 -- this will point to the user page.
00928 log_namespace int NOT NULL default 0,
00929 log_title varchar(255) binary NOT NULL default '',
00930
00931 -- Freeform text. Interpreted as edit history comments.
00932 log_comment varchar(255) NOT NULL default '',
00933
00934 -- LF separated list of miscellaneous parameters
00935 log_params blob NOT NULL default '',
00936
00937 KEY type_time (log_type, log_timestamp),
00938 KEY user_time (log_user, log_timestamp),
00939 KEY page_time (log_namespace, log_title, log_timestamp)
00940
00941 ) TYPE=InnoDB
00942 ENDS;
00943 $fields = array(
00944 'log_type' => MW_UPGRADE_COPY,
00945 'log_action' => MW_UPGRADE_COPY,
00946 'log_timestamp' => MW_UPGRADE_COPY,
00947 'log_user' => MW_UPGRADE_COPY,
00948 'log_namespace' => MW_UPGRADE_COPY,
00949 'log_title' => MW_UPGRADE_ENCODE,
00950 'log_comment' => MW_UPGRADE_ENCODE,
00951 'log_params' => MW_UPGRADE_ENCODE );
00952 $this->copyTable( 'logging', $tabledef, $fields );
00953 }
00954
00955 function upgradeArchive() {
00956 $tabledef = <<<ENDS
00957 CREATE TABLE $1 (
00958 ar_namespace int NOT NULL default '0',
00959 ar_title varchar(255) binary NOT NULL default '',
00960 ar_text mediumblob NOT NULL default '',
00961
00962 ar_comment tinyblob NOT NULL default '',
00963 ar_user int(5) unsigned NOT NULL default '0',
00964 ar_user_text varchar(255) binary NOT NULL,
00965 ar_timestamp char(14) binary NOT NULL default '',
00966 ar_minor_edit tinyint(1) NOT NULL default '0',
00967
00968 ar_flags tinyblob NOT NULL default '',
00969
00970 ar_rev_id int(8) unsigned,
00971 ar_text_id int(8) unsigned,
00972
00973 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
00974
00975 ) TYPE=InnoDB
00976 ENDS;
00977 $fields = array(
00978 'ar_namespace' => MW_UPGRADE_COPY,
00979 'ar_title' => MW_UPGRADE_ENCODE,
00980 'ar_text' => MW_UPGRADE_COPY,
00981 'ar_comment' => MW_UPGRADE_ENCODE,
00982 'ar_user' => MW_UPGRADE_COPY,
00983 'ar_user_text' => MW_UPGRADE_ENCODE,
00984 'ar_timestamp' => MW_UPGRADE_COPY,
00985 'ar_minor_edit' => MW_UPGRADE_COPY,
00986 'ar_flags' => MW_UPGRADE_COPY,
00987 'ar_rev_id' => MW_UPGRADE_NULL,
00988 'ar_text_id' => MW_UPGRADE_NULL );
00989 $this->copyTable( 'archive', $tabledef, $fields );
00990 }
00991
00992 function upgradeImagelinks() {
00993 global $wgUseLatin1;
00994 if( $wgUseLatin1 ) {
00995 $tabledef = <<<ENDS
00996 CREATE TABLE $1 (
00997 -- Key to page_id of the page containing the image / media link.
00998 il_from int(8) unsigned NOT NULL default '0',
00999
01000 -- Filename of target image.
01001 -- This is also the page_title of the file's description page;
01002 -- all such pages are in namespace 6 (NS_FILE).
01003 il_to varchar(255) binary NOT NULL default '',
01004
01005 UNIQUE KEY il_from(il_from,il_to),
01006 KEY (il_to)
01007
01008 ) TYPE=InnoDB
01009 ENDS;
01010 $fields = array(
01011 'il_from' => MW_UPGRADE_COPY,
01012 'il_to' => MW_UPGRADE_ENCODE );
01013 $this->copyTable( 'imagelinks', $tabledef, $fields );
01014 }
01015 }
01016
01017 function upgradeCategorylinks() {
01018 global $wgUseLatin1;
01019 if( $wgUseLatin1 ) {
01020 $tabledef = <<<ENDS
01021 CREATE TABLE $1 (
01022 cl_from int(8) unsigned NOT NULL default '0',
01023 cl_to varchar(255) binary NOT NULL default '',
01024 cl_sortkey varchar(86) binary NOT NULL default '',
01025 cl_timestamp timestamp NOT NULL,
01026
01027 UNIQUE KEY cl_from(cl_from,cl_to),
01028 KEY cl_sortkey(cl_to,cl_sortkey),
01029 KEY cl_timestamp(cl_to,cl_timestamp)
01030 ) TYPE=InnoDB
01031 ENDS;
01032 $fields = array(
01033 'cl_from' => MW_UPGRADE_COPY,
01034 'cl_to' => MW_UPGRADE_ENCODE,
01035 'cl_sortkey' => MW_UPGRADE_ENCODE,
01036 'cl_timestamp' => MW_UPGRADE_COPY );
01037 $this->copyTable( 'categorylinks', $tabledef, $fields );
01038 }
01039 }
01040
01041 function upgradeIpblocks() {
01042 global $wgUseLatin1;
01043 if( $wgUseLatin1 ) {
01044 $tabledef = <<<ENDS
01045 CREATE TABLE $1 (
01046 ipb_id int(8) NOT NULL auto_increment,
01047 ipb_address varchar(40) binary NOT NULL default '',
01048 ipb_user int(8) unsigned NOT NULL default '0',
01049 ipb_by int(8) unsigned NOT NULL default '0',
01050 ipb_reason tinyblob NOT NULL default '',
01051 ipb_timestamp char(14) binary NOT NULL default '',
01052 ipb_auto tinyint(1) NOT NULL default '0',
01053 ipb_expiry char(14) binary NOT NULL default '',
01054
01055 PRIMARY KEY ipb_id (ipb_id),
01056 INDEX ipb_address (ipb_address),
01057 INDEX ipb_user (ipb_user)
01058
01059 ) TYPE=InnoDB
01060 ENDS;
01061 $fields = array(
01062 'ipb_id' => MW_UPGRADE_COPY,
01063 'ipb_address' => MW_UPGRADE_COPY,
01064 'ipb_user' => MW_UPGRADE_COPY,
01065 'ipb_by' => MW_UPGRADE_COPY,
01066 'ipb_reason' => MW_UPGRADE_ENCODE,
01067 'ipb_timestamp' => MW_UPGRADE_COPY,
01068 'ipb_auto' => MW_UPGRADE_COPY,
01069 'ipb_expiry' => MW_UPGRADE_COPY );
01070 $this->copyTable( 'ipblocks', $tabledef, $fields );
01071 }
01072 }
01073
01074 function upgradeRecentchanges() {
01075
01076 $tabledef = <<<ENDS
01077 CREATE TABLE $1 (
01078 rc_id int(8) NOT NULL auto_increment,
01079 rc_timestamp varchar(14) binary NOT NULL default '',
01080 rc_cur_time varchar(14) binary NOT NULL default '',
01081
01082 rc_user int(10) unsigned NOT NULL default '0',
01083 rc_user_text varchar(255) binary NOT NULL default '',
01084
01085 rc_namespace int NOT NULL default '0',
01086 rc_title varchar(255) binary NOT NULL default '',
01087
01088 rc_comment varchar(255) binary NOT NULL default '',
01089 rc_minor tinyint(3) unsigned NOT NULL default '0',
01090
01091 rc_bot tinyint(3) unsigned NOT NULL default '0',
01092 rc_new tinyint(3) unsigned NOT NULL default '0',
01093
01094 rc_cur_id int(10) unsigned NOT NULL default '0',
01095 rc_this_oldid int(10) unsigned NOT NULL default '0',
01096 rc_last_oldid int(10) unsigned NOT NULL default '0',
01097
01098 rc_type tinyint(3) unsigned NOT NULL default '0',
01099 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
01100 rc_moved_to_title varchar(255) binary NOT NULL default '',
01101
01102 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
01103
01104 rc_ip char(15) NOT NULL default '',
01105
01106 PRIMARY KEY rc_id (rc_id),
01107 INDEX rc_timestamp (rc_timestamp),
01108 INDEX rc_namespace_title (rc_namespace, rc_title),
01109 INDEX rc_cur_id (rc_cur_id),
01110 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
01111 INDEX rc_ip (rc_ip)
01112
01113 ) TYPE=InnoDB
01114 ENDS;
01115 $fields = array(
01116 'rc_id' => MW_UPGRADE_COPY,
01117 'rc_timestamp' => MW_UPGRADE_COPY,
01118 'rc_cur_time' => MW_UPGRADE_COPY,
01119 'rc_user' => MW_UPGRADE_COPY,
01120 'rc_user_text' => MW_UPGRADE_ENCODE,
01121 'rc_namespace' => MW_UPGRADE_COPY,
01122 'rc_title' => MW_UPGRADE_ENCODE,
01123 'rc_comment' => MW_UPGRADE_ENCODE,
01124 'rc_minor' => MW_UPGRADE_COPY,
01125 'rc_bot' => MW_UPGRADE_COPY,
01126 'rc_new' => MW_UPGRADE_COPY,
01127 'rc_cur_id' => MW_UPGRADE_COPY,
01128 'rc_this_oldid' => MW_UPGRADE_COPY,
01129 'rc_last_oldid' => MW_UPGRADE_COPY,
01130 'rc_type' => MW_UPGRADE_COPY,
01131 'rc_moved_to_ns' => MW_UPGRADE_COPY,
01132 'rc_moved_to_title' => MW_UPGRADE_ENCODE,
01133 'rc_patrolled' => MW_UPGRADE_COPY,
01134 'rc_ip' => MW_UPGRADE_COPY );
01135 $this->copyTable( 'recentchanges', $tabledef, $fields );
01136 }
01137
01138 function upgradeQuerycache() {
01139
01140 $tabledef = <<<ENDS
01141 CREATE TABLE $1 (
01142 -- A key name, generally the base name of of the special page.
01143 qc_type char(32) NOT NULL,
01144
01145 -- Some sort of stored value. Sizes, counts...
01146 qc_value int(5) unsigned NOT NULL default '0',
01147
01148 -- Target namespace+title
01149 qc_namespace int NOT NULL default '0',
01150 qc_title char(255) binary NOT NULL default '',
01151
01152 KEY (qc_type,qc_value)
01153
01154 ) TYPE=InnoDB
01155 ENDS;
01156 $fields = array(
01157 'qc_type' => MW_UPGRADE_COPY,
01158 'qc_value' => MW_UPGRADE_COPY,
01159 'qc_namespace' => MW_UPGRADE_COPY,
01160 'qc_title' => MW_UPGRADE_ENCODE );
01161 $this->copyTable( 'querycache', $tabledef, $fields );
01162 }
01163
01169 function upgradeCleanup() {
01170 $this->renameTable( 'old', 'text' );
01171
01172 foreach( $this->cleanupSwaps as $table ) {
01173 $this->swap( $table );
01174 }
01175 }
01176
01177 function renameTable( $from, $to ) {
01178 $this->log( "Renaming $from to $to..." );
01179
01180 $fromtable = $this->dbw->tableName( $from );
01181 $totable = $this->dbw->tableName( $to );
01182 $this->dbw->query( "ALTER TABLE $fromtable RENAME TO $totable" );
01183 }
01184
01185 function swap( $base ) {
01186 $this->renameTable( $base, "{$base}_old" );
01187 $this->renameTable( "{$base}_temp", $base );
01188 }
01189
01190 }