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 $upgrade = new FiveUpgrade();
23 $step = isset( $options['step'] ) ?
$options['step'] : null;
24 $upgrade->upgrade( $step );
27 function FiveUpgrade() {
29 $this->conversionTables
= $this->prepareWindows1252();
30 $this->dbw
=& $this->newConnection();
31 $this->dbr
=& $this->newConnection();
32 $this->dbr
->bufferResults( false );
35 function doing( $step ) {
36 return is_null( $this->step
) ||
$step == $this->step
;
39 function upgrade( $step ) {
41 if( $this->doing( 'page' ) )
43 if( $this->doing( 'links' ) )
44 $this->upgradeLinks();
45 if( $this->doing( 'user' ) )
47 if( $this->doing( 'image' ) )
48 $this->upgradeImage();
49 if( $this->doing( 'oldimage' ) )
50 $this->upgradeOldImage();
51 if( $this->doing( 'watchlist' ) )
52 $this->upgradeWatchlist();
54 if( $this->doing( 'cleanup' ) )
55 $this->upgradeCleanup();
60 * Open a second connection to the master server, with buffering off.
61 * This will let us stream large datasets in and write in chunks on the
66 function &newConnection() {
67 global $wgDBadminuser, $wgDBadminpassword;
68 global $wgDBserver, $wgDBname;
69 $db =& new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
74 * Prepare a conversion array for converting Windows Code Page 1252 to
75 * UTF-8. This should provide proper conversion of text that was miscoded
76 * as Windows-1252 by naughty user-agents, and doesn't rely on an outside
82 function prepareWindows1252() {
84 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
85 static $cp1252 = array(
86 0x80 => 0x20AC, #EURO SIGN
87 0x81 => UNICODE_REPLACEMENT
,
88 0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
89 0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
90 0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
91 0x85 => 0x2026, #HORIZONTAL ELLIPSIS
92 0x86 => 0x2020, #DAGGER
93 0x87 => 0x2021, #DOUBLE DAGGER
94 0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
95 0x89 => 0x2030, #PER MILLE SIGN
96 0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
97 0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
98 0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
99 0x8D => UNICODE_REPLACEMENT
,
100 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
101 0x8F => UNICODE_REPLACEMENT
,
102 0x90 => UNICODE_REPLACEMENT
,
103 0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
104 0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
105 0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
106 0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
107 0x95 => 0x2022, #BULLET
108 0x96 => 0x2013, #EN DASH
109 0x97 => 0x2014, #EM DASH
110 0x98 => 0x02DC, #SMALL TILDE
111 0x99 => 0x2122, #TRADE MARK SIGN
112 0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
113 0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
114 0x9C => 0x0153, #LATIN SMALL LIGATURE OE
115 0x9D => UNICODE_REPLACEMENT
,
116 0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
117 0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
120 for( $i = 0; $i < 0x100; $i++
) {
121 $unicode = isset( $cp1252[$i] ) ?
$cp1252[$i] : $i;
122 $pairs[chr( $i )] = codepointToUtf8( $unicode );
128 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
129 * @param string $text
133 function conv( $text ) {
136 return strtr( $text, $this->conversionTables
);
143 * Dump timestamp and message to output
144 * @param string $message
147 function log( $message ) {
148 echo wfTimestamp( TS_DB
) . ': ' . $message . "\n";
153 * Initialize the chunked-insert system.
154 * Rows will be inserted in chunks of the given number, rather
155 * than in a giant INSERT...SELECT query, to keep the serialized
156 * MySQL database replication from getting hung up. This way other
157 * things can be going on during conversion without waiting for
158 * slaves to catch up as badly.
160 * @param int $chunksize Number of rows to insert at once
161 * @param int $final Total expected number of rows / id of last row,
162 * used for progress reports.
163 * @param string $table to insert on
164 * @param string $fname function name to report in SQL
167 function setChunkScale( $chunksize, $final, $table, $fname ) {
168 $this->chunkSize
= $chunksize;
169 $this->chunkFinal
= $final;
170 $this->chunkCount
= 0;
171 $this->chunkStartTime
= wfTime();
172 $this->chunkOptions
= array();
173 $this->chunkTable
= $table;
174 $this->chunkFunction
= $fname;
178 * Chunked inserts: perform an insert if we've reached the chunk limit.
179 * Prints a progress report with estimated completion time.
180 * @param array &$chunk -- This will be emptied if an insert is done.
181 * @param int $key A key identifier to use in progress estimation in
182 * place of the number of rows inserted. Use this if
183 * you provided a max key number instead of a count
184 * as the final chunk number in setChunkScale()
187 function addChunk( &$chunk, $key = null ) {
188 if( count( $chunk ) >= $this->chunkSize
) {
189 $this->insertChunk( $chunk );
191 $this->chunkCount +
= count( $chunk );
193 $delta = $now - $this->chunkStartTime
;
194 $rate = $this->chunkCount
/ $delta;
196 if( is_null( $key ) ) {
197 $completed = $this->chunkCount
;
201 $portion = $completed / $this->chunkFinal
;
203 $estimatedTotalTime = $delta / $portion;
204 $eta = $this->chunkStartTime +
$estimatedTotalTime;
206 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
207 wfTimestamp( TS_DB
, intval( $now ) ),
210 wfTimestamp( TS_DB
, intval( $eta ) ),
221 * Chunked inserts: perform an insert unconditionally, at the end, and log.
222 * @param array &$chunk -- This will be emptied if an insert is done.
225 function lastChunk( &$chunk ) {
226 $n = count( $chunk );
228 $this->insertChunk( $chunk );
230 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
234 * Chunked inserts: perform an insert.
235 * @param array &$chunk -- This will be emptied if an insert is done.
238 function insertChunk( &$chunk ) {
239 $this->dbw
->insert( $this->chunkTable
, $chunk, $this->chunkFunction
, $this->chunkOptions
);
243 function upgradePage() {
244 $fname = "FiveUpgrade::upgradePage";
248 $this->log( "Checking cur table for unique title index and applying if necessary" );
251 $this->log( "...converting from cur/old to page/revision/text DB structure." );
253 extract( $this->dbw
->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
255 $this->log( "Creating page and revision tables..." );
256 $this->dbw
->query("CREATE TABLE $page (
257 page_id int(8) unsigned NOT NULL auto_increment,
258 page_namespace int NOT NULL,
259 page_title varchar(255) binary NOT NULL,
260 page_restrictions tinyblob NOT NULL default '',
261 page_counter bigint(20) unsigned NOT NULL default '0',
262 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
263 page_is_new tinyint(1) unsigned NOT NULL default '0',
264 page_random real unsigned NOT NULL,
265 page_touched char(14) binary NOT NULL default '',
266 page_latest int(8) unsigned NOT NULL,
267 page_len int(8) unsigned NOT NULL,
269 PRIMARY KEY page_id (page_id),
270 UNIQUE INDEX name_title (page_namespace,page_title),
273 ) TYPE=InnoDB", $fname );
274 $this->dbw
->query("CREATE TABLE $revision (
275 rev_id int(8) unsigned NOT NULL auto_increment,
276 rev_page int(8) unsigned NOT NULL,
277 rev_comment tinyblob NOT NULL default '',
278 rev_user int(5) unsigned NOT NULL default '0',
279 rev_user_text varchar(255) binary NOT NULL default '',
280 rev_timestamp char(14) binary NOT NULL default '',
281 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
282 rev_deleted tinyint(1) unsigned NOT NULL default '0',
284 PRIMARY KEY rev_page_id (rev_page, rev_id),
285 UNIQUE INDEX rev_id (rev_id),
286 INDEX rev_timestamp (rev_timestamp),
287 INDEX page_timestamp (rev_page,rev_timestamp),
288 INDEX user_timestamp (rev_user,rev_timestamp),
289 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
290 ) TYPE=InnoDB", $fname );
292 $maxold = $this->dbw
->selectField( 'old', 'max(old_id)', '', $fname );
293 $this->log( "Last old record is {$maxold}" );
295 global $wgLegacySchemaConversion;
296 if( $wgLegacySchemaConversion ) {
297 // Create HistoryBlobCurStub entries.
298 // Text will be pulled from the leftover 'cur' table at runtime.
299 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
300 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
301 $cur_flags = "'object'";
303 // Copy all cur text in immediately: this may take longer but avoids
304 // having to keep an extra table around.
305 echo "......Moving text from cur.\n";
306 $cur_text = 'cur_text';
310 $maxcur = $this->dbw
->selectField( 'cur', 'max(cur_id)', '', $fname );
311 $this->log( "Last cur entry is $maxcur" );
314 * Copy placeholder records for each page's current version into old
315 * Don't do any conversion here; text records are converted at runtime
316 * based on the flags (and may be originally binary!) while the meta
317 * fields will be converted in the old -> rev and cur -> page steps.
319 $this->setChunkScale( $chunksize, $maxcur, 'old', $fname );
320 $result = $this->dbr
->query(
321 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
322 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
324 ORDER BY cur_id", $fname );
326 while( $row = $this->dbr
->fetchObject( $result ) ) {
328 'old_namespace' => $row->cur_namespace
,
329 'old_title' => $row->cur_title
,
330 'old_text' => $row->text
,
331 'old_comment' => $row->cur_comment
,
332 'old_user' => $row->cur_user
,
333 'old_user_text' => $row->cur_user_text
,
334 'old_timestamp' => $row->cur_timestamp
,
335 'old_minor_edit' => $row->cur_minor_edit
,
336 'old_flags' => $row->flags
);
337 $this->addChunk( $add, $row->cur_id
);
339 $this->lastChunk( $add );
340 $this->dbr
->freeResult( $result );
343 * Copy revision metadata from old into revision.
344 * We'll also do UTF-8 conversion of usernames and comments.
346 #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
347 #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
348 $countold = $this->dbw
->selectField( 'old', 'count(old_id)', '', $fname );
349 $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
351 $this->log( "......Setting up revision table." );
352 $result = $this->dbr
->query(
353 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
354 old_timestamp, old_minor_edit
355 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
359 while( $row = $this->dbr
->fetchObject( $result ) ) {
361 'rev_id' => $row->old_id
,
362 'rev_page' => $row->cur_id
,
363 'rev_comment' => $this->conv( $row->old_comment
),
364 'rev_user' => $row->old_user
,
365 'rev_user_text' => $this->conv( $row->old_user_text
),
366 'rev_timestamp' => $row->old_timestamp
,
367 'rev_minor_edit' => $row->old_minor_edit
);
368 $this->addChunk( $add );
370 $this->lastChunk( $add );
371 $this->dbr
->freeResult( $result );
375 * Copy page metadata from cur into page.
376 * We'll also do UTF-8 conversion of titles.
378 $this->log( "......Setting up page table." );
379 $this->setChunkScale( $chunksize, $maxcur, 'page', $fname );
380 $result = $this->dbr
->query( "
381 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
382 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
384 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
385 ORDER BY cur_id", $fname );
387 while( $row = $this->dbr
->fetchObject( $result ) ) {
389 'page_id' => $row->cur_id
,
390 'page_namespace' => $row->cur_namespace
,
391 'page_title' => $this->conv( $row->cur_title
),
392 'page_restrictions' => $row->cur_restrictions
,
393 'page_counter' => $row->cur_counter
,
394 'page_is_redirect' => $row->cur_is_redirect
,
395 'page_is_new' => $row->cur_is_new
,
396 'page_random' => $row->cur_random
,
397 'page_touched' => $this->dbw
->timestamp(),
398 'page_latest' => $row->rev_id
,
399 'page_len' => $row->len
);
400 $this->addChunk( $add, $row->cur_id
);
402 $this->lastChunk( $add );
403 $this->dbr
->freeResult( $result );
405 $this->log( "...done with cur/old -> page/revision." );
408 function upgradeLinks() {
409 $fname = 'FiveUpgrade::upgradeLinks';
411 extract( $this->dbw
->tableNames( 'links', 'brokenlinks', 'pagelinks', 'page' ) );
413 $this->log( 'Creating pagelinks table...' );
415 CREATE TABLE $pagelinks (
416 -- Key to the page_id of the page containing the link.
417 pl_from int(8) unsigned NOT NULL default '0',
419 -- Key to page_namespace/page_title of the target page.
420 -- The target page may or may not exist, and due to renames
421 -- and deletions may refer to different page records as time
423 pl_namespace int NOT NULL default '0',
424 pl_title varchar(255) binary NOT NULL default '',
426 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
427 KEY (pl_namespace,pl_title)
431 $this->log( 'Importing live links -> pagelinks' );
432 $nlinks = $this->dbw
->selectField( 'links', 'count(*)', '', $fname );
434 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
435 $result = $this->dbr
->query( "
436 SELECT l_from,page_namespace,page_title
438 WHERE l_to=page_id", $fname );
440 while( $row = $this->dbr
->fetchObject( $result ) ) {
442 'pl_from' => $row->l_from
,
443 'pl_namespace' => $row->page_namespace
,
444 'pl_title' => $row->page_title
);
445 $this->addChunk( $add );
447 $this->lastChunk( $add );
449 $this->log( 'no links!' );
452 $this->log( 'Importing brokenlinks -> pagelinks' );
453 $nbrokenlinks = $this->dbw
->selectField( 'brokenlinks', 'count(*)', '', $fname );
454 if( $nbrokenlinks ) {
455 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
456 $this->chunkOptions
= array( 'IGNORE' );
457 $result = $this->dbr
->query(
458 "SELECT bl_from, bl_to FROM $brokenlinks",
461 while( $row = $this->dbr
->fetchObject( $result ) ) {
462 $pagename = $this->conv( $row->bl_to
);
463 $title = Title
::newFromText( $pagename );
464 if( is_null( $title ) ) {
465 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
468 'pl_from' => $row->bl_from
,
469 'pl_namespace' => $title->getNamespace(),
470 'pl_title' => $title->getDBkey() );
471 $this->addChunk( $add );
474 $this->lastChunk( $add );
476 $this->log( 'no brokenlinks!' );
479 $this->log( 'Done with links.' );
482 function upgradeUser() {
483 $fname = 'FiveUpgrade::upgradeUser';
487 // Apply unique index, if necessary:
488 $duper = new UserDupes( $this->dbw
);
489 if( $duper->hasUniqueIndex() ) {
490 $this->log( "Already have unique user_name index." );
492 $this->log( "Clearing user duplicates..." );
493 if( !$duper->clearDupes() ) {
494 $this->log( "WARNING: Duplicate user accounts, may explode!" );
498 /** Convert encoding on options, etc */
499 extract( $this->dbw
->tableNames( 'user', 'user_temp', 'user_old' ) );
501 $this->log( 'Migrating user table to user_temp...' );
502 $this->dbw
->query( "CREATE TABLE $user_temp (
503 user_id int(5) unsigned NOT NULL auto_increment,
504 user_name varchar(255) binary NOT NULL default '',
505 user_real_name varchar(255) binary NOT NULL default '',
506 user_password tinyblob NOT NULL default '',
507 user_newpassword tinyblob NOT NULL default '',
508 user_email tinytext NOT NULL default '',
509 user_options blob NOT NULL default '',
510 user_touched char(14) binary NOT NULL default '',
511 user_token char(32) binary NOT NULL default '',
512 user_email_authenticated CHAR(14) BINARY,
513 user_email_token CHAR(32) BINARY,
514 user_email_token_expires CHAR(14) BINARY,
516 PRIMARY KEY user_id (user_id),
517 UNIQUE INDEX user_name (user_name),
518 INDEX (user_email_token)
520 ) TYPE=InnoDB", $fname );
522 // Fix encoding for Latin-1 upgrades, and add some fields.
523 $numusers = $this->dbw
->selectField( 'user', 'count(*)', '', $fname );
524 $this->setChunkScale( $chunksize, $numusers, 'user_temp', $fname );
525 $result = $this->dbr
->select( 'user',
539 while( $row = $this->dbr
->fetchObject( $result ) ) {
540 $now = $this->dbw
->timestamp();
542 'user_id' => $row->user_id
,
543 'user_name' => $this->conv( $row->user_name
),
544 'user_real_name' => $this->conv( $row->user_real_name
),
545 'user_password' => $row->user_password
,
546 'user_newpassword' => $row->user_newpassword
,
547 'user_email' => $this->conv( $row->user_email
),
548 'user_options' => $this->conv( $row->user_options
),
549 'user_touched' => $now,
550 'user_token' => $row->user_token
,
551 'user_email_authenticated' => $preauth ?
$now : null,
552 'user_email_token' => null,
553 'user_email_token_expires' => null );
554 $this->addChunk( $add );
556 $this->lastChunk( $add );
557 $this->dbr
->freeResult( $result );
560 function upgradeImage() {
561 $fname = 'FiveUpgrade::upgradeImage';
564 extract( $this->dbw
->tableNames( 'image', 'image_temp', 'image_old' ) );
565 $this->log( 'Creating temporary image_temp to merge into...' );
566 $this->dbw
->query( <<<END
567 CREATE TABLE $image_temp (
568 img_name varchar(255) binary NOT NULL default '',
569 img_size int(8) unsigned NOT NULL default '0',
570 img_width int(5) NOT NULL default '0',
571 img_height int(5) NOT NULL default '0',
572 img_metadata mediumblob NOT NULL,
573 img_bits int(3) NOT NULL default '0',
574 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
575 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
576 img_minor_mime varchar(32) NOT NULL default "unknown",
577 img_description tinyblob NOT NULL default '',
578 img_user int(5) unsigned NOT NULL default '0',
579 img_user_text varchar(255) binary NOT NULL default '',
580 img_timestamp char(14) binary NOT NULL default '',
582 PRIMARY KEY img_name (img_name),
583 INDEX img_size (img_size),
584 INDEX img_timestamp (img_timestamp)
589 $numimages = $this->dbw
->selectField( 'image', 'count(*)', '', $fname );
590 $result = $this->dbr
->select( 'image',
601 $this->setChunkScale( $chunksize, $numimages, 'image_temp', $fname );
602 while( $row = $this->dbr
->fetchObject( $result ) ) {
603 // Fill in the new image info fields
604 $info = $this->imageInfo( $row->img_name
);
606 // Update and convert encoding
608 'img_name' => $this->conv( $row->img_name
),
609 'img_size' => $row->img_size
,
610 'img_width' => $info['width'],
611 'img_height' => $info['height'],
612 'img_metadata' => "", // loaded on-demand
613 'img_bits' => $info['bits'],
614 'img_media_type' => $info['media'],
615 'img_major_mime' => $info['major'],
616 'img_minor_mime' => $info['minor'],
617 'img_description' => $this->conv( $row->img_description
),
618 'img_user' => $row->img_user
,
619 'img_user_text' => $this->conv( $row->img_user_text
),
620 'img_timestamp' => $row->img_timestamp
);
622 // If doing UTF8 conversion the file must be renamed
623 $this->renameFile( $row->img_name
, 'wfImageDir' );
625 $this->lastChunk( $add );
627 $this->log( 'done with image table.' );
630 function imageInfo( $name, $subdirCallback='wfImageDir', $basename = null ) {
631 if( is_null( $basename ) ) $basename = $name;
632 $dir = call_user_func( $subdirCallback, $basename );
633 $filename = $dir . '/' . $name;
642 $magic =& wfGetMimeMagic();
643 $mime = $magic->guessMimeType( $filename, true );
644 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
646 $info['media'] = $magic->getMediaType( $filename, $mime );
650 if( $mime == 'image/svg' ) {
651 $gis = wfGetSVGsize( $this->imagePath
);
652 } elseif( $magic->isPHPImageType( $mime ) ) {
653 $gis = getimagesize( $filename );
655 $this->log( "Surprising mime type: $mime" );
658 $info['width' ] = $gis[0];
659 $info['height'] = $gis[1];
661 if( isset( $gis['bits'] ) ) {
662 $info['bits'] = $gis['bits'];
671 * @param string $table The table name to be truncated
673 function clearTable( $table ) {
674 print "Clearing $table...\n";
675 $tableName = $this->db
->tableName( $table );
676 $this->db
->query( 'TRUNCATE $tableName' );
680 * Rename a given image or archived image file to the converted filename,
681 * leaving a symlink for URL compatibility.
683 * @param string $oldname pre-conversion filename
684 * @param string $basename pre-conversion base filename for dir hashing, if an archive
687 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
688 $newname = $this->conv( $oldname );
689 if( $newname == $oldname ) {
690 // No need to rename; another field triggered this row.
694 if( is_null( $basename ) ) $basename = $oldname;
695 $ubasename = $this->conv( $basename );
696 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
697 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
699 $this->log( "$oldpath -> $newpath" );
700 if( rename( $oldpath, $newpath ) ) {
701 $relpath = $this->relativize( $newpath, dirname( $oldpath ) );
702 if( !symlink( $relpath, $oldpath ) ) {
703 $this->log( "... symlink failed!" );
706 $this->log( "... rename failed!" );
711 * Generate a relative path name to the given file.
712 * Assumes Unix-style paths, separators, and semantics.
714 * @param string $path Absolute destination path including target filename
715 * @param string $from Absolute source path, directory only
720 function relativize( $path, $from ) {
721 $pieces = explode( '/', dirname( $path ) );
722 $against = explode( '/', $from );
724 // Trim off common prefix
725 while( count( $pieces ) && count( $against )
726 && $pieces[0] == $against[0] ) {
727 array_shift( $pieces );
728 array_shift( $against );
731 // relative dots to bump us to the parent
732 while( count( $against ) ) {
733 array_unshift( $pieces, '..' );
734 array_shift( $against );
737 array_push( $pieces, basename( $path ) );
739 return implode( '/', $pieces );
742 function upgradeOldImage() {
743 $fname = 'FiveUpgrade::upgradeOldImage';
746 extract( $this->dbw
->tableNames( 'oldimage', 'oldimage_temp', 'oldimage_old' ) );
747 $this->log( 'Creating temporary oldimage_temp to merge into...' );
748 $this->dbw
->query( <<<END
749 CREATE TABLE $oldimage_temp (
750 -- Base filename: key to image.img_name
751 oi_name varchar(255) binary NOT NULL default '',
753 -- Filename of the archived file.
754 -- This is generally a timestamp and '!' prepended to the base name.
755 oi_archive_name varchar(255) binary NOT NULL default '',
757 -- Other fields as in image...
758 oi_size int(8) unsigned NOT NULL default 0,
759 oi_width int(5) NOT NULL default 0,
760 oi_height int(5) NOT NULL default 0,
761 oi_bits int(3) NOT NULL default 0,
762 oi_description tinyblob NOT NULL default '',
763 oi_user int(5) unsigned NOT NULL default '0',
764 oi_user_text varchar(255) binary NOT NULL default '',
765 oi_timestamp char(14) binary NOT NULL default '',
767 INDEX oi_name (oi_name(10))
773 $numimages = $this->dbw
->selectField( 'oldimage', 'count(*)', '', $fname );
774 $result = $this->dbr
->select( 'oldimage',
786 $this->setChunkScale( $chunksize, $numimages, 'oldimage_temp', $fname );
787 while( $row = $this->dbr
->fetchObject( $result ) ) {
788 // Fill in the new image info fields
789 $info = $this->imageInfo( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
791 // Update and convert encoding
793 'oi_name' => $this->conv( $row->oi_name
),
794 'oi_archive_name' => $this->conv( $row->oi_archive_name
),
795 'oi_size' => $row->oi_size
,
796 'oi_width' => $info['width'],
797 'oi_height' => $info['height'],
798 'oi_bits' => $info['bits'],
799 'oi_description' => $this->conv( $row->oi_description
),
800 'oi_user' => $row->oi_user
,
801 'oi_user_text' => $this->conv( $row->oi_user_text
),
802 'oi_timestamp' => $row->oi_timestamp
);
804 // If doing UTF8 conversion the file must be renamed
805 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
807 $this->lastChunk( $add );
809 $this->log( 'done with oldimage table.' );
813 function upgradeWatchlist() {
814 $fname = 'FiveUpgrade::upgradeWatchlist';
817 extract( $this->dbw
->tableNames( 'watchlist', 'watchlist_temp' ) );
819 $this->log( 'Migrating watchlist table to watchlist_temp...' );
821 "CREATE TABLE $watchlist_temp (
823 wl_user int(5) unsigned NOT NULL,
825 -- Key to page_namespace/page_title
826 -- Note that users may watch patches which do not exist yet,
827 -- or existed in the past but have been deleted.
828 wl_namespace int NOT NULL default '0',
829 wl_title varchar(255) binary NOT NULL default '',
831 -- Timestamp when user was last sent a notification e-mail;
832 -- cleared when the user visits the page.
833 -- FIXME: add proper null support etc
834 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
836 UNIQUE KEY (wl_user, wl_namespace, wl_title),
837 KEY namespace_title (wl_namespace,wl_title)
839 ) TYPE=InnoDB;", $fname );
841 // Fix encoding for Latin-1 upgrades, add some fields,
842 // and double article to article+talk pairs
843 $numwatched = $this->dbw
->selectField( 'watchlist', 'count(*)', '', $fname );
845 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
846 $result = $this->dbr
->select( 'watchlist',
855 while( $row = $this->dbr
->fetchObject( $result ) ) {
856 $now = $this->dbw
->timestamp();
858 'wl_user' => $row->wl_user
,
859 'wl_namespace' => Namespace::getSubject( $row->wl_namespace
),
860 'wl_title' => $this->conv( $row->wl_title
),
861 'wl_notificationtimestamp' => '0' );
862 $this->addChunk( $add );
865 'wl_user' => $row->wl_user
,
866 'wl_namespace' => Namespace::getTalk( $row->wl_namespace
),
867 'wl_title' => $this->conv( $row->wl_title
),
868 'wl_notificationtimestamp' => '0' );
869 $this->addChunk( $add );
871 $this->lastChunk( $add );
872 $this->dbr
->freeResult( $result );
874 $this->log( 'Done converting watchlist.' );
879 * Rename all our temporary tables into final place.
880 * We've left things in place so a read-only wiki can continue running
881 * on the old code during all this.
883 function upgradeCleanup() {
884 $this->renameTable( 'old', 'text' );
886 $this->swap( 'user' );
887 $this->swap( 'image' );
888 $this->swap( 'oldimage' );
889 $this->swap( 'watchlist' );
892 function renameTable( $from, $to ) {
893 $this->log( 'Renaming $from to $to...' );
895 $fromtable = $this->dbw
->tableName( $from );
896 $totable = $this->dbw
->tableName( $to );
897 $this->dbw
->query( "ALTER TABLE $fromtable RENAME TO $totable" );
900 function swap( $base ) {
901 $this->renameTable( $base, "{$base}_old" );
902 $this->renameTable( "{$base}_temp", $base );