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 require_once( 'commandLine.inc' );
16 require_once( 'cleanupDupes.inc' );
17 require_once( 'userDupes.inc' );
18 require_once( 'updaters.inc' );
20 $upgrade = new FiveUpgrade();
24 function FiveUpgrade() {
26 $this->conversionTables
= $this->prepareWindows1252();
27 $this->dbw
=& $this->newConnection();
28 $this->dbr
=& $this->newConnection();
29 $this->dbr
->bufferResults( false );
34 $this->upgradeLinks();
36 $this->upgradeImage();
37 $this->upgradeOldImage();
39 $this->upgradeCleanup();
44 * Open a second connection to the master server, with buffering off.
45 * This will let us stream large datasets in and write in chunks on the
50 function &newConnection() {
51 global $wgDBadminuser, $wgDBadminpassword;
52 global $wgDBserver, $wgDBname;
53 $db =& new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
58 * Prepare a conversion array for converting Windows Code Page 1252 to
59 * UTF-8. This should provide proper conversion of text that was miscoded
60 * as Windows-1252 by naughty user-agents, and doesn't rely on an outside
66 function prepareWindows1252() {
68 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
69 static $cp1252 = array(
70 0x80 => 0x20AC, #EURO SIGN
71 0x81 => UNICODE_REPLACEMENT
,
72 0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
73 0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
74 0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
75 0x85 => 0x2026, #HORIZONTAL ELLIPSIS
76 0x86 => 0x2020, #DAGGER
77 0x87 => 0x2021, #DOUBLE DAGGER
78 0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
79 0x89 => 0x2030, #PER MILLE SIGN
80 0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
81 0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
82 0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
83 0x8D => UNICODE_REPLACEMENT
,
84 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
85 0x8F => UNICODE_REPLACEMENT
,
86 0x90 => UNICODE_REPLACEMENT
,
87 0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
88 0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
89 0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
90 0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
91 0x95 => 0x2022, #BULLET
92 0x96 => 0x2013, #EN DASH
93 0x97 => 0x2014, #EM DASH
94 0x98 => 0x02DC, #SMALL TILDE
95 0x99 => 0x2122, #TRADE MARK SIGN
96 0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
97 0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
98 0x9C => 0x0153, #LATIN SMALL LIGATURE OE
99 0x9D => UNICODE_REPLACEMENT
,
100 0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
101 0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
104 for( $i = 0; $i < 0x100; $i++
) {
105 $unicode = isset( $cp1252[$i] ) ?
$cp1252[$i] : $i;
106 $pairs[chr( $i )] = codepointToUtf8( $unicode );
112 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
113 * @param string $text
117 function conv( $text ) {
120 return strtr( $text, $this->conversionTables
);
127 * Dump timestamp and message to output
128 * @param string $message
131 function log( $message ) {
132 echo wfTimestamp( TS_DB
) . ': ' . $message . "\n";
137 * Initialize the chunked-insert system.
138 * Rows will be inserted in chunks of the given number, rather
139 * than in a giant INSERT...SELECT query, to keep the serialized
140 * MySQL database replication from getting hung up. This way other
141 * things can be going on during conversion without waiting for
142 * slaves to catch up as badly.
144 * @param int $chunksize Number of rows to insert at once
145 * @param int $final Total expected number of rows / id of last row,
146 * used for progress reports.
147 * @param string $table to insert on
148 * @param string $fname function name to report in SQL
151 function setChunkScale( $chunksize, $final, $table, $fname ) {
152 $this->chunkSize
= $chunksize;
153 $this->chunkFinal
= $final;
154 $this->chunkCount
= 0;
155 $this->chunkStartTime
= wfTime();
156 $this->chunkOptions
= array();
157 $this->chunkTable
= $table;
158 $this->chunkFunction
= $fname;
162 * Chunked inserts: perform an insert if we've reached the chunk limit.
163 * Prints a progress report with estimated completion time.
164 * @param array &$chunk -- This will be emptied if an insert is done.
165 * @param int $key A key identifier to use in progress estimation in
166 * place of the number of rows inserted. Use this if
167 * you provided a max key number instead of a count
168 * as the final chunk number in setChunkScale()
171 function addChunk( &$chunk, $key = null ) {
172 if( count( $chunk ) >= $this->chunkSize
) {
173 $this->insertChunk( $chunk );
175 $this->chunkCount +
= count( $chunk );
177 $delta = $now - $this->chunkStartTime
;
178 $rate = $this->chunkCount
/ $delta;
180 if( is_null( $key ) ) {
181 $completed = $this->chunkCount
;
185 $portion = $completed / $this->chunkFinal
;
187 $estimatedTotalTime = $delta / $portion;
188 $eta = $this->chunkStartTime +
$estimatedTotalTime;
190 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
191 wfTimestamp( TS_DB
, intval( $now ) ),
194 wfTimestamp( TS_DB
, intval( $eta ) ),
205 * Chunked inserts: perform an insert unconditionally, at the end, and log.
206 * @param array &$chunk -- This will be emptied if an insert is done.
209 function lastChunk( &$chunk ) {
210 $n = count( $chunk );
212 $this->insertChunk( $chunk );
214 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
218 * Chunked inserts: perform an insert.
219 * @param array &$chunk -- This will be emptied if an insert is done.
222 function insertChunk( &$chunk ) {
223 $this->dbw
->insert( $this->chunkTable
, $chunk, $this->chunkFunction
, $this->chunkOptions
);
227 function upgradePage() {
228 $fname = "FiveUpgrade::upgradePage";
232 $this->log( "Checking cur table for unique title index and applying if necessary" );
235 $this->log( "...converting from cur/old to page/revision/text DB structure." );
237 extract( $this->dbw
->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
239 $this->log( "Creating page and revision tables..." );
240 $this->dbw
->query("CREATE TABLE $page (
241 page_id int(8) unsigned NOT NULL auto_increment,
242 page_namespace int NOT NULL,
243 page_title varchar(255) binary NOT NULL,
244 page_restrictions tinyblob NOT NULL default '',
245 page_counter bigint(20) unsigned NOT NULL default '0',
246 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
247 page_is_new tinyint(1) unsigned NOT NULL default '0',
248 page_random real unsigned NOT NULL,
249 page_touched char(14) binary NOT NULL default '',
250 page_latest int(8) unsigned NOT NULL,
251 page_len int(8) unsigned NOT NULL,
253 PRIMARY KEY page_id (page_id),
254 UNIQUE INDEX name_title (page_namespace,page_title),
257 ) TYPE=InnoDB", $fname );
258 $this->dbw
->query("CREATE TABLE $revision (
259 rev_id int(8) unsigned NOT NULL auto_increment,
260 rev_page int(8) unsigned NOT NULL,
261 rev_comment tinyblob NOT NULL default '',
262 rev_user int(5) unsigned NOT NULL default '0',
263 rev_user_text varchar(255) binary NOT NULL default '',
264 rev_timestamp char(14) binary NOT NULL default '',
265 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
266 rev_deleted tinyint(1) unsigned NOT NULL default '0',
268 PRIMARY KEY rev_page_id (rev_page, rev_id),
269 UNIQUE INDEX rev_id (rev_id),
270 INDEX rev_timestamp (rev_timestamp),
271 INDEX page_timestamp (rev_page,rev_timestamp),
272 INDEX user_timestamp (rev_user,rev_timestamp),
273 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
274 ) TYPE=InnoDB", $fname );
276 $maxold = $this->dbw
->selectField( 'old', 'max(old_id)', '', $fname );
277 $this->log( "Last old record is {$maxold}" );
279 global $wgLegacySchemaConversion;
280 if( $wgLegacySchemaConversion ) {
281 // Create HistoryBlobCurStub entries.
282 // Text will be pulled from the leftover 'cur' table at runtime.
283 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
284 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
285 $cur_flags = "'object'";
287 // Copy all cur text in immediately: this may take longer but avoids
288 // having to keep an extra table around.
289 echo "......Moving text from cur.\n";
290 $cur_text = 'cur_text';
294 $maxcur = $this->dbw
->selectField( 'cur', 'max(cur_id)', '', $fname );
295 $this->log( "Last cur entry is $maxcur" );
298 * Copy placeholder records for each page's current version into old
299 * Don't do any conversion here; text records are converted at runtime
300 * based on the flags (and may be originally binary!) while the meta
301 * fields will be converted in the old -> rev and cur -> page steps.
303 $this->setChunkScale( $chunksize, $maxcur, 'old', $fname );
304 $result = $this->dbr
->query(
305 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
306 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
308 ORDER BY cur_id", $fname );
310 while( $row = $this->dbr
->fetchObject( $result ) ) {
312 'old_namespace' => $row->cur_namespace
,
313 'old_title' => $row->cur_title
,
314 'old_text' => $row->text
,
315 'old_comment' => $row->cur_comment
,
316 'old_user' => $row->cur_user
,
317 'old_user_text' => $row->cur_user_text
,
318 'old_timestamp' => $row->cur_timestamp
,
319 'old_minor_edit' => $row->cur_minor_edit
,
320 'old_flags' => $row->flags
);
321 $this->addChunk( $add, $row->cur_id
);
323 $this->lastChunk( $add );
324 $this->dbr
->freeResult( $result );
327 * Copy revision metadata from old into revision.
328 * We'll also do UTF-8 conversion of usernames and comments.
330 #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
331 #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
332 $countold = $this->dbw
->selectField( 'old', 'count(old_id)', '', $fname );
333 $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
335 $this->log( "......Setting up revision table." );
336 $result = $this->dbr
->query(
337 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
338 old_timestamp, old_minor_edit
339 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
343 while( $row = $this->dbr
->fetchObject( $result ) ) {
345 'rev_id' => $row->old_id
,
346 'rev_page' => $row->cur_id
,
347 'rev_comment' => $this->conv( $row->old_comment
),
348 'rev_user' => $row->old_user
,
349 'rev_user_text' => $this->conv( $row->old_user_text
),
350 'rev_timestamp' => $row->old_timestamp
,
351 'rev_minor_edit' => $row->old_minor_edit
);
352 $this->addChunk( $add );
354 $this->lastChunk( $add );
355 $this->dbr
->freeResult( $result );
359 * Copy page metadata from cur into page.
360 * We'll also do UTF-8 conversion of titles.
362 $this->log( "......Setting up page table." );
363 $this->setChunkScale( $chunksize, $maxcur, 'page', $fname );
364 $result = $this->dbr
->query( "
365 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
366 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
368 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
369 ORDER BY cur_id", $fname );
371 while( $row = $this->dbr
->fetchObject( $result ) ) {
373 'page_id' => $row->cur_id
,
374 'page_namespace' => $row->cur_namespace
,
375 'page_title' => $this->conv( $row->cur_title
),
376 'page_restrictions' => $row->cur_restrictions
,
377 'page_counter' => $row->cur_counter
,
378 'page_is_redirect' => $row->cur_is_redirect
,
379 'page_is_new' => $row->cur_is_new
,
380 'page_random' => $row->cur_random
,
381 'page_touched' => $this->dbw
->timestamp(),
382 'page_latest' => $row->rev_id
,
383 'page_len' => $row->len
);
384 $this->addChunk( $add, $row->cur_id
);
386 $this->lastChunk( $add );
387 $this->dbr
->freeResult( $result );
389 $this->log( "...done with cur/old -> page/revision." );
392 function upgradeLinks() {
393 $fname = 'FiveUpgrade::upgradeLinks';
395 extract( $this->dbw
->tableNames( 'links', 'brokenlinks', 'pagelinks', 'page' ) );
397 $this->log( 'Creating pagelinks table...' );
399 CREATE TABLE $pagelinks (
400 -- Key to the page_id of the page containing the link.
401 pl_from int(8) unsigned NOT NULL default '0',
403 -- Key to page_namespace/page_title of the target page.
404 -- The target page may or may not exist, and due to renames
405 -- and deletions may refer to different page records as time
407 pl_namespace int NOT NULL default '0',
408 pl_title varchar(255) binary NOT NULL default '',
410 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
411 KEY (pl_namespace,pl_title)
415 $this->log( 'Importing live links -> pagelinks' );
416 $nlinks = $this->dbw
->selectField( 'links', 'count(*)', '', $fname );
418 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
419 $result = $this->dbr
->query( "
420 SELECT l_from,page_namespace,page_title
422 WHERE l_to=page_id", $fname );
424 while( $row = $this->dbr
->fetchObject( $result ) ) {
426 'pl_from' => $row->l_from
,
427 'pl_namespace' => $row->page_namespace
,
428 'pl_title' => $row->page_title
);
429 $this->addChunk( $add );
431 $this->lastChunk( $add );
433 $this->log( 'no links!' );
436 $this->log( 'Importing brokenlinks -> pagelinks' );
437 $nbrokenlinks = $this->dbw
->selectField( 'brokenlinks', 'count(*)', '', $fname );
438 if( $nbrokenlinks ) {
439 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
440 $this->chunkOptions
= array( 'IGNORE' );
441 $result = $this->dbr
->query(
442 "SELECT bl_from, bl_to FROM $brokenlinks",
445 while( $row = $this->dbr
->fetchObject( $result ) ) {
446 $pagename = $this->conv( $row->bl_to
);
447 $title = Title
::newFromText( $pagename );
448 if( is_null( $title ) ) {
449 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
452 'pl_from' => $row->bl_from
,
453 'pl_namespace' => $title->getNamespace(),
454 'pl_title' => $title->getDBkey() );
455 $this->addChunk( $add );
458 $this->lastChunk( $add );
460 $this->log( 'no brokenlinks!' );
463 $this->log( 'Done with links.' );
466 function upgradeUser() {
467 $fname = 'FiveUpgrade::upgradeUser';
471 // Apply unique index, if necessary:
472 $duper = new UserDupes( $this->dbw
);
473 if( $duper->hasUniqueIndex() ) {
474 $this->log( "Already have unique user_name index." );
476 $this->log( "Clearing user duplicates..." );
477 if( !$duper->clearDupes() ) {
478 $this->log( "WARNING: Duplicate user accounts, may explode!" );
482 /** Convert encoding on options, etc */
483 extract( $this->dbw
->tableNames( 'user', 'user_temp', 'user_old' ) );
485 $this->log( 'Migrating user table to user_temp...' );
486 $this->dbw
->query( "CREATE TABLE $user_temp (
487 user_id int(5) unsigned NOT NULL auto_increment,
488 user_name varchar(255) binary NOT NULL default '',
489 user_real_name varchar(255) binary NOT NULL default '',
490 user_password tinyblob NOT NULL default '',
491 user_newpassword tinyblob NOT NULL default '',
492 user_email tinytext NOT NULL default '',
493 user_options blob NOT NULL default '',
494 user_touched char(14) binary NOT NULL default '',
495 user_token char(32) binary NOT NULL default '',
496 user_email_authenticated CHAR(14) BINARY,
497 user_email_token CHAR(32) BINARY,
498 user_email_token_expires CHAR(14) BINARY,
500 PRIMARY KEY user_id (user_id),
501 UNIQUE INDEX user_name (user_name),
502 INDEX (user_email_token)
504 ) TYPE=InnoDB", $fname );
506 // Fix encoding for Latin-1 upgrades, and add some fields.
507 $numusers = $this->dbw
->selectField( 'user', 'count(*)', '', $fname );
508 $this->setChunkScale( $chunksize, $numusers, 'user_temp', $fname );
509 $result = $this->dbr
->select( 'user',
523 while( $row = $this->dbr
->fetchObject( $result ) ) {
524 $now = $this->dbw
->timestamp();
526 'user_id' => $row->user_id
,
527 'user_name' => $this->conv( $row->user_name
),
528 'user_real_name' => $this->conv( $row->user_real_name
),
529 'user_password' => $row->user_password
,
530 'user_newpassword' => $row->user_newpassword
,
531 'user_email' => $this->conv( $row->user_email
),
532 'user_options' => $this->conv( $row->user_options
),
533 'user_touched' => $now,
534 'user_token' => $row->user_token
,
535 'user_email_authenticated' => $preauth ?
$now : null,
536 'user_email_token' => null,
537 'user_email_token_expires' => null );
538 $this->addChunk( $add );
540 $this->lastChunk( $add );
541 $this->dbr
->freeResult( $result );
544 function upgradeImage() {
545 $fname = 'FiveUpgrade::upgradeImage';
548 extract( $this->dbw
->tableNames( 'image', 'image_temp', 'image_old' ) );
549 $this->log( 'Creating temporary image_temp to merge into...' );
550 $this->dbw
->query( <<<END
551 CREATE TABLE $image_temp (
552 img_name varchar(255) binary NOT NULL default '',
553 img_size int(8) unsigned NOT NULL default '0',
554 img_width int(5) NOT NULL default '0',
555 img_height int(5) NOT NULL default '0',
556 img_metadata mediumblob NOT NULL,
557 img_bits int(3) NOT NULL default '0',
558 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
559 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
560 img_minor_mime varchar(32) NOT NULL default "unknown",
561 img_description tinyblob NOT NULL default '',
562 img_user int(5) unsigned NOT NULL default '0',
563 img_user_text varchar(255) binary NOT NULL default '',
564 img_timestamp char(14) binary NOT NULL default '',
566 PRIMARY KEY img_name (img_name),
567 INDEX img_size (img_size),
568 INDEX img_timestamp (img_timestamp)
573 $numimages = $this->dbw
->selectField( 'image', 'count(*)', '', $fname );
574 $result = $this->dbr
->select( 'image',
585 $this->setChunkScale( $chunksize, $numimages, 'image_temp', $fname );
586 while( $row = $this->dbr
->fetchObject( $result ) ) {
587 // Fill in the new image info fields
588 $info = $this->imageInfo( $row->img_name
);
590 // Update and convert encoding
592 'img_name' => $this->conv( $row->img_name
),
593 'img_size' => $row->img_size
,
594 'img_width' => $info['width'],
595 'img_height' => $info['height'],
596 'img_metadata' => "", // loaded on-demand
597 'img_bits' => $info['bits'],
598 'img_media_type' => $info['media'],
599 'img_major_mime' => $info['major'],
600 'img_minor_mime' => $info['minor'],
601 'img_description' => $this->conv( $row->img_description
),
602 'img_user' => $row->img_user
,
603 'img_user_text' => $this->conv( $row->img_user_text
),
604 'img_timestamp' => $row->img_timestamp
);
606 // If doing UTF8 conversion the file must be renamed
607 $this->renameFile( $row->img_name
, 'wfImageDir' );
609 $this->lastChunk( $add );
611 $this->log( 'done with image table.' );
614 function imageInfo( $name, $subdirCallback='wfImageDir', $basename = null ) {
615 if( is_null( $basename ) ) $basename = $name;
616 $dir = call_user_func( $subdirCallback, $basename );
617 $filename = $dir . '/' . $name;
626 $magic =& wfGetMimeMagic();
627 $mime = $magic->guessMimeType( $filename, true );
628 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
630 $info['media'] = $magic->getMediaType( $filename, $mime );
634 if( $mime == 'image/svg' ) {
635 $gis = wfGetSVGsize( $this->imagePath
);
636 } elseif( $magic->isPHPImageType( $mime ) ) {
637 $gis = getimagesize( $filename );
639 $this->log( "Surprising mime type: $mime" );
642 $info['width' ] = $gis[0];
643 $info['height'] = $gis[1];
645 if( isset( $gis['bits'] ) ) {
646 $info['bits'] = $gis['bits'];
655 * @param string $table The table name to be truncated
657 function clearTable( $table ) {
658 print "Clearing $table...\n";
659 $tableName = $this->db
->tableName( $table );
660 $this->db
->query( 'TRUNCATE $tableName' );
664 * Rename a given image or archived image file to the converted filename,
665 * leaving a symlink for URL compatibility.
667 * @param string $oldname pre-conversion filename
668 * @param string $basename pre-conversion base filename for dir hashing, if an archive
671 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
672 $newname = $this->conv( $oldname );
673 if( $newname == $oldname ) {
674 // No need to rename; another field triggered this row.
678 if( is_null( $basename ) ) $basename = $oldname;
679 $ubasename = $this->conv( $basename );
680 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
681 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
683 $this->log( "$oldpath -> $newpath" );
684 if( rename( $oldpath, $newpath ) ) {
685 $relpath = $this->relativize( $newpath, dirname( $oldpath ) );
686 if( !symlink( $relpath, $oldpath ) ) {
687 $this->log( "... symlink failed!" );
690 $this->log( "... rename failed!" );
695 * Generate a relative path name to the given file.
696 * Assumes Unix-style paths, separators, and semantics.
698 * @param string $path Absolute destination path including target filename
699 * @param string $from Absolute source path, directory only
704 function relativize( $path, $from ) {
705 $pieces = explode( '/', dirname( $path ) );
706 $against = explode( '/', $from );
708 // Trim off common prefix
709 while( count( $pieces ) && count( $against )
710 && $pieces[0] == $against[0] ) {
711 array_shift( $pieces );
712 array_shift( $against );
715 // relative dots to bump us to the parent
716 while( count( $against ) ) {
717 array_unshift( $pieces, '..' );
718 array_shift( $against );
721 array_push( $pieces, basename( $path ) );
723 return implode( '/', $pieces );
726 function upgradeOldImage() {
727 $fname = 'FiveUpgrade::upgradeOldImage';
730 extract( $this->dbw
->tableNames( 'oldimage', 'oldimage_temp', 'oldimage_old' ) );
731 $this->log( 'Creating temporary oldimage_temp to merge into...' );
732 $this->dbw
->query( <<<END
733 CREATE TABLE $oldimage_temp (
734 -- Base filename: key to image.img_name
735 oi_name varchar(255) binary NOT NULL default '',
737 -- Filename of the archived file.
738 -- This is generally a timestamp and '!' prepended to the base name.
739 oi_archive_name varchar(255) binary NOT NULL default '',
741 -- Other fields as in image...
742 oi_size int(8) unsigned NOT NULL default 0,
743 oi_width int(5) NOT NULL default 0,
744 oi_height int(5) NOT NULL default 0,
745 oi_bits int(3) NOT NULL default 0,
746 oi_description tinyblob NOT NULL default '',
747 oi_user int(5) unsigned NOT NULL default '0',
748 oi_user_text varchar(255) binary NOT NULL default '',
749 oi_timestamp char(14) binary NOT NULL default '',
751 INDEX oi_name (oi_name(10))
757 $numimages = $this->dbw
->selectField( 'oldimage', 'count(*)', '', $fname );
758 $result = $this->dbr
->select( 'oldimage',
770 $this->setChunkScale( $chunksize, $numimages, 'oldimage_temp', $fname );
771 while( $row = $this->dbr
->fetchObject( $result ) ) {
772 // Fill in the new image info fields
773 $info = $this->imageInfo( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
775 // Update and convert encoding
777 'oi_name' => $this->conv( $row->oi_name
),
778 'oi_archive_name' => $this->conv( $row->oi_archive_name
),
779 'oi_size' => $row->oi_size
,
780 'oi_width' => $info['width'],
781 'oi_height' => $info['height'],
782 'oi_bits' => $info['bits'],
783 'oi_description' => $this->conv( $row->oi_description
),
784 'oi_user' => $row->oi_user
,
785 'oi_user_text' => $this->conv( $row->oi_user_text
),
786 'oi_timestamp' => $row->oi_timestamp
);
788 // If doing UTF8 conversion the file must be renamed
789 $this->renameFile( $row->oi_archive_name
, 'wfImageArchiveDir', $row->oi_name
);
791 $this->lastChunk( $add );
793 $this->log( 'done with oldimage table.' );
797 * Rename all our temporary tables into final place.
798 * We've left things in place so a read-only wiki can continue running
799 * on the old code during all this.
801 function upgradeCleanup() {
802 $this->log( "Renaming old to text..." );
803 $this->dbw
->query( "ALTER TABLE $old RENAME TO $text", $fname );
805 $this->log( 'Renaming user to user_old and user_temp to user...' );
806 $this->dbw
->query( "ALTER TABLE $user RENAME TO $user_old" );
807 $this->dbw
->query( "ALTER TABLE $user_temp RENAME TO $user" );
809 $this->log( 'Renaming image to image_old and image_temp to image...' );
810 $this->dbw
->query( "ALTER TABLE $image RENAME TO $image_old" );
811 $this->dbw
->query( "ALTER TABLE $image_temp RENAME TO $image" );
813 $this->log( 'Renaming oldimage to oldimage_old and oldimage_temp to oldimage...' );
814 $this->dbw
->query( "ALTER TABLE $oldimage RENAME TO $oldimage_old" );
815 $this->dbw
->query( "ALTER TABLE $oldimage_temp RENAME TO $oldimage" );