oldimage table:
[lhc/web/wiklou.git] / maintenance / upgrade1_5.php
1 <?php
2
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
8 // other services.
9 //
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.
14
15 require_once( 'commandLine.inc' );
16 require_once( 'cleanupDupes.inc' );
17 require_once( 'userDupes.inc' );
18 require_once( 'updaters.inc' );
19
20 $upgrade = new FiveUpgrade();
21 $upgrade->upgrade();
22
23 class FiveUpgrade {
24 function FiveUpgrade() {
25 global $wgDatabase;
26 $this->conversionTables = $this->prepareWindows1252();
27 $this->dbw =& $this->newConnection();
28 $this->dbr =& $this->newConnection();
29 $this->dbr->bufferResults( false );
30 }
31
32 function upgrade() {
33 $this->upgradePage();
34 $this->upgradeLinks();
35 $this->upgradeUser();
36 $this->upgradeImage();
37 $this->upgradeOldImage();
38
39 $this->upgradeCleanup();
40 }
41
42
43 /**
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
46 * other end.
47 * @return Database
48 * @access private
49 */
50 function &newConnection() {
51 global $wgDBadminuser, $wgDBadminpassword;
52 global $wgDBserver, $wgDBname;
53 $db =& new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
54 return $db;
55 }
56
57 /**
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
61 * iconv library.
62 *
63 * @return array
64 * @access private
65 */
66 function prepareWindows1252() {
67 # Mappings from:
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
102 );
103 $pairs = array();
104 for( $i = 0; $i < 0x100; $i++ ) {
105 $unicode = isset( $cp1252[$i] ) ? $cp1252[$i] : $i;
106 $pairs[chr( $i )] = codepointToUtf8( $unicode );
107 }
108 return $pairs;
109 }
110
111 /**
112 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
113 * @param string $text
114 * @return string
115 * @access private
116 */
117 function conv( $text ) {
118 global $wgUseLatin1;
119 if( $wgUseLatin1 ) {
120 return strtr( $text, $this->conversionTables );
121 } else {
122 return $text;
123 }
124 }
125
126 /**
127 * Dump timestamp and message to output
128 * @param string $message
129 * @access private
130 */
131 function log( $message ) {
132 echo wfTimestamp( TS_DB ) . ': ' . $message . "\n";
133 flush();
134 }
135
136 /**
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.
143 *
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
149 * @access private
150 */
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;
159 }
160
161 /**
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()
169 * @access private
170 */
171 function addChunk( &$chunk, $key = null ) {
172 if( count( $chunk ) >= $this->chunkSize ) {
173 $this->insertChunk( $chunk );
174
175 $this->chunkCount += count( $chunk );
176 $now = wfTime();
177 $delta = $now - $this->chunkStartTime;
178 $rate = $this->chunkCount / $delta;
179
180 if( is_null( $key ) ) {
181 $completed = $this->chunkCount;
182 } else {
183 $completed = $key;
184 }
185 $portion = $completed / $this->chunkFinal;
186
187 $estimatedTotalTime = $delta / $portion;
188 $eta = $this->chunkStartTime + $estimatedTotalTime;
189
190 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
191 wfTimestamp( TS_DB, intval( $now ) ),
192 $portion * 100.0,
193 $this->chunkTable,
194 wfTimestamp( TS_DB, intval( $eta ) ),
195 $completed,
196 $this->chunkFinal,
197 $rate );
198 flush();
199
200 $chunk = array();
201 }
202 }
203
204 /**
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.
207 * @access private
208 */
209 function lastChunk( &$chunk ) {
210 $n = count( $chunk );
211 if( $n > 0 ) {
212 $this->insertChunk( $chunk );
213 }
214 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
215 }
216
217 /**
218 * Chunked inserts: perform an insert.
219 * @param array &$chunk -- This will be emptied if an insert is done.
220 * @access private
221 */
222 function insertChunk( &$chunk ) {
223 $this->dbw->insert( $this->chunkTable, $chunk, $this->chunkFunction, $this->chunkOptions );
224 }
225
226
227 function upgradePage() {
228 $fname = "FiveUpgrade::upgradePage";
229 $chunksize = 100;
230
231
232 $this->log( "Checking cur table for unique title index and applying if necessary" );
233 checkDupes( true );
234
235 $this->log( "...converting from cur/old to page/revision/text DB structure." );
236
237 extract( $this->dbw->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
238
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,
252
253 PRIMARY KEY page_id (page_id),
254 UNIQUE INDEX name_title (page_namespace,page_title),
255 INDEX (page_random),
256 INDEX (page_len)
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',
267
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 );
275
276 $maxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
277 $this->log( "Last old record is {$maxold}" );
278
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'";
286 } else {
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';
291 $cur_flags = "''";
292 }
293
294 $maxcur = $this->dbw->selectField( 'cur', 'max(cur_id)', '', $fname );
295 $this->log( "Last cur entry is $maxcur" );
296
297 /**
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.
302 */
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
307 FROM $cur
308 ORDER BY cur_id", $fname );
309 $add = array();
310 while( $row = $this->dbr->fetchObject( $result ) ) {
311 $add[] = array(
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 );
322 }
323 $this->lastChunk( $add );
324 $this->dbr->freeResult( $result );
325
326 /**
327 * Copy revision metadata from old into revision.
328 * We'll also do UTF-8 conversion of usernames and comments.
329 */
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 );
334
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",
340 $fname );
341
342 $add = array();
343 while( $row = $this->dbr->fetchObject( $result ) ) {
344 $add[] = array(
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 );
353 }
354 $this->lastChunk( $add );
355 $this->dbr->freeResult( $result );
356
357
358 /**
359 * Copy page metadata from cur into page.
360 * We'll also do UTF-8 conversion of titles.
361 */
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
367 FROM $cur,$revision
368 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
369 ORDER BY cur_id", $fname );
370 $add = array();
371 while( $row = $this->dbr->fetchObject( $result ) ) {
372 $add[] = array(
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 );
385 }
386 $this->lastChunk( $add );
387 $this->dbr->freeResult( $result );
388
389 $this->log( "...done with cur/old -> page/revision." );
390 }
391
392 function upgradeLinks() {
393 $fname = 'FiveUpgrade::upgradeLinks';
394 $chunksize = 200;
395 extract( $this->dbw->tableNames( 'links', 'brokenlinks', 'pagelinks', 'page' ) );
396
397 $this->log( 'Creating pagelinks table...' );
398 $this->dbw->query( "
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',
402
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
406 -- goes by.
407 pl_namespace int NOT NULL default '0',
408 pl_title varchar(255) binary NOT NULL default '',
409
410 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
411 KEY (pl_namespace,pl_title)
412
413 ) TYPE=InnoDB" );
414
415 $this->log( 'Importing live links -> pagelinks' );
416 $nlinks = $this->dbw->selectField( 'links', 'count(*)', '', $fname );
417 if( $nlinks ) {
418 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
419 $result = $this->dbr->query( "
420 SELECT l_from,page_namespace,page_title
421 FROM $links, $page
422 WHERE l_to=page_id", $fname );
423 $add = array();
424 while( $row = $this->dbr->fetchObject( $result ) ) {
425 $add[] = array(
426 'pl_from' => $row->l_from,
427 'pl_namespace' => $row->page_namespace,
428 'pl_title' => $row->page_title );
429 $this->addChunk( $add );
430 }
431 $this->lastChunk( $add );
432 } else {
433 $this->log( 'no links!' );
434 }
435
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",
443 $fname );
444 $add = array();
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')" );
450 } else {
451 $add[] = array(
452 'pl_from' => $row->bl_from,
453 'pl_namespace' => $title->getNamespace(),
454 'pl_title' => $title->getDBkey() );
455 $this->addChunk( $add );
456 }
457 }
458 $this->lastChunk( $add );
459 } else {
460 $this->log( 'no brokenlinks!' );
461 }
462
463 $this->log( 'Done with links.' );
464 }
465
466 function upgradeUser() {
467 $fname = 'FiveUpgrade::upgradeUser';
468 $chunksize = 100;
469 $preauth = 0;
470
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." );
475 } else {
476 $this->log( "Clearing user duplicates..." );
477 if( !$duper->clearDupes() ) {
478 $this->log( "WARNING: Duplicate user accounts, may explode!" );
479 }
480 }
481
482 /** Convert encoding on options, etc */
483 extract( $this->dbw->tableNames( 'user', 'user_temp', 'user_old' ) );
484
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,
499
500 PRIMARY KEY user_id (user_id),
501 UNIQUE INDEX user_name (user_name),
502 INDEX (user_email_token)
503
504 ) TYPE=InnoDB", $fname );
505
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',
510 array(
511 'user_id',
512 'user_name',
513 'user_real_name',
514 'user_password',
515 'user_newpassword',
516 'user_email',
517 'user_options',
518 'user_touched',
519 'user_token' ),
520 '',
521 $fname );
522 $add = array();
523 while( $row = $this->dbr->fetchObject( $result ) ) {
524 $now = $this->dbw->timestamp();
525 $add[] = array(
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 );
539 }
540 $this->lastChunk( $add );
541 $this->dbr->freeResult( $result );
542 }
543
544 function upgradeImage() {
545 $fname = 'FiveUpgrade::upgradeImage';
546 $chunksize = 100;
547
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 '',
565
566 PRIMARY KEY img_name (img_name),
567 INDEX img_size (img_size),
568 INDEX img_timestamp (img_timestamp)
569 ) TYPE=InnoDB
570 END
571 , $fname);
572
573 $numimages = $this->dbw->selectField( 'image', 'count(*)', '', $fname );
574 $result = $this->dbr->select( 'image',
575 array(
576 'img_name',
577 'img_size',
578 'img_description',
579 'img_user',
580 'img_user_text',
581 'img_timestamp' ),
582 '',
583 $fname );
584 $add = array();
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 );
589
590 // Update and convert encoding
591 $add[] = array(
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 );
605
606 // If doing UTF8 conversion the file must be renamed
607 $this->renameFile( $row->img_name, 'wfImageDir' );
608 }
609 $this->lastChunk( $add );
610
611 $this->log( 'done with image table.' );
612 }
613
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;
618 $info = array(
619 'width' => 0,
620 'height' => 0,
621 'bits' => 0,
622 'media' => '',
623 'major' => '',
624 'minor' => '' );
625
626 $magic =& wfGetMimeMagic();
627 $mime = $magic->guessMimeType( $filename, true );
628 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
629
630 $info['media'] = $magic->getMediaType( $filename, $mime );
631
632 # Height and width
633 $gis = false;
634 if( $mime == 'image/svg' ) {
635 $gis = wfGetSVGsize( $this->imagePath );
636 } elseif( $magic->isPHPImageType( $mime ) ) {
637 $gis = getimagesize( $filename );
638 } else {
639 $this->log( "Surprising mime type: $mime" );
640 }
641 if( $gis ) {
642 $info['width' ] = $gis[0];
643 $info['height'] = $gis[1];
644 }
645 if( isset( $gis['bits'] ) ) {
646 $info['bits'] = $gis['bits'];
647 }
648
649 return $info;
650 }
651
652
653 /**
654 * Truncate a table.
655 * @param string $table The table name to be truncated
656 */
657 function clearTable( $table ) {
658 print "Clearing $table...\n";
659 $tableName = $this->db->tableName( $table );
660 $this->db->query( 'TRUNCATE $tableName' );
661 }
662
663 /**
664 * Rename a given image or archived image file to the converted filename,
665 * leaving a symlink for URL compatibility.
666 *
667 * @param string $oldname pre-conversion filename
668 * @param string $basename pre-conversion base filename for dir hashing, if an archive
669 * @access private
670 */
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.
675 return;
676 }
677
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;
682
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!" );
688 }
689 } else {
690 $this->log( "... rename failed!" );
691 }
692 }
693
694 /**
695 * Generate a relative path name to the given file.
696 * Assumes Unix-style paths, separators, and semantics.
697 *
698 * @param string $path Absolute destination path including target filename
699 * @param string $from Absolute source path, directory only
700 * @return string
701 * @access private
702 * @static
703 */
704 function relativize( $path, $from ) {
705 $pieces = explode( '/', dirname( $path ) );
706 $against = explode( '/', $from );
707
708 // Trim off common prefix
709 while( count( $pieces ) && count( $against )
710 && $pieces[0] == $against[0] ) {
711 array_shift( $pieces );
712 array_shift( $against );
713 }
714
715 // relative dots to bump us to the parent
716 while( count( $against ) ) {
717 array_unshift( $pieces, '..' );
718 array_shift( $against );
719 }
720
721 array_push( $pieces, basename( $path ) );
722
723 return implode( '/', $pieces );
724 }
725
726 function upgradeOldImage() {
727 $fname = 'FiveUpgrade::upgradeOldImage';
728 $chunksize = 100;
729
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 '',
736
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 '',
740
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 '',
750
751 INDEX oi_name (oi_name(10))
752
753 ) TYPE=InnoDB;
754 END
755 , $fname);
756
757 $numimages = $this->dbw->selectField( 'oldimage', 'count(*)', '', $fname );
758 $result = $this->dbr->select( 'oldimage',
759 array(
760 'oi_name',
761 'oi_archive_name',
762 'oi_size',
763 'oi_description',
764 'oi_user',
765 'oi_user_text',
766 'oi_timestamp' ),
767 '',
768 $fname );
769 $add = array();
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 );
774
775 // Update and convert encoding
776 $add[] = array(
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 );
787
788 // If doing UTF8 conversion the file must be renamed
789 $this->renameFile( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
790 }
791 $this->lastChunk( $add );
792
793 $this->log( 'done with oldimage table.' );
794 }
795
796 /**
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.
800 */
801 function upgradeCleanup() {
802 $this->log( "Renaming old to text..." );
803 $this->dbw->query( "ALTER TABLE $old RENAME TO $text", $fname );
804
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" );
808
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" );
812
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" );
816 }
817
818 }
819
820 ?>