* watchlist conversion (utf8 and namespace doubling)
[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 $options = array( 'step' );
16
17 require_once( 'commandLine.inc' );
18 require_once( 'cleanupDupes.inc' );
19 require_once( 'userDupes.inc' );
20 require_once( 'updaters.inc' );
21
22 $upgrade = new FiveUpgrade();
23 $step = isset( $options['step'] ) ? $options['step'] : null;
24 $upgrade->upgrade( $step );
25
26 class FiveUpgrade {
27 function FiveUpgrade() {
28 global $wgDatabase;
29 $this->conversionTables = $this->prepareWindows1252();
30 $this->dbw =& $this->newConnection();
31 $this->dbr =& $this->newConnection();
32 $this->dbr->bufferResults( false );
33 }
34
35 function doing( $step ) {
36 return is_null( $this->step ) || $step == $this->step;
37 }
38
39 function upgrade( $step ) {
40 $this->step = $step;
41 if( $this->doing( 'page' ) )
42 $this->upgradePage();
43 if( $this->doing( 'links' ) )
44 $this->upgradeLinks();
45 if( $this->doing( 'user' ) )
46 $this->upgradeUser();
47 if( $this->doing( 'image' ) )
48 $this->upgradeImage();
49 if( $this->doing( 'oldimage' ) )
50 $this->upgradeOldImage();
51 if( $this->doing( 'watchlist' ) )
52 $this->upgradeWatchlist();
53
54 if( $this->doing( 'cleanup' ) )
55 $this->upgradeCleanup();
56 }
57
58
59 /**
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
62 * other end.
63 * @return Database
64 * @access private
65 */
66 function &newConnection() {
67 global $wgDBadminuser, $wgDBadminpassword;
68 global $wgDBserver, $wgDBname;
69 $db =& new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
70 return $db;
71 }
72
73 /**
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
77 * iconv library.
78 *
79 * @return array
80 * @access private
81 */
82 function prepareWindows1252() {
83 # Mappings from:
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
118 );
119 $pairs = array();
120 for( $i = 0; $i < 0x100; $i++ ) {
121 $unicode = isset( $cp1252[$i] ) ? $cp1252[$i] : $i;
122 $pairs[chr( $i )] = codepointToUtf8( $unicode );
123 }
124 return $pairs;
125 }
126
127 /**
128 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
129 * @param string $text
130 * @return string
131 * @access private
132 */
133 function conv( $text ) {
134 global $wgUseLatin1;
135 if( $wgUseLatin1 ) {
136 return strtr( $text, $this->conversionTables );
137 } else {
138 return $text;
139 }
140 }
141
142 /**
143 * Dump timestamp and message to output
144 * @param string $message
145 * @access private
146 */
147 function log( $message ) {
148 echo wfTimestamp( TS_DB ) . ': ' . $message . "\n";
149 flush();
150 }
151
152 /**
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.
159 *
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
165 * @access private
166 */
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;
175 }
176
177 /**
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()
185 * @access private
186 */
187 function addChunk( &$chunk, $key = null ) {
188 if( count( $chunk ) >= $this->chunkSize ) {
189 $this->insertChunk( $chunk );
190
191 $this->chunkCount += count( $chunk );
192 $now = wfTime();
193 $delta = $now - $this->chunkStartTime;
194 $rate = $this->chunkCount / $delta;
195
196 if( is_null( $key ) ) {
197 $completed = $this->chunkCount;
198 } else {
199 $completed = $key;
200 }
201 $portion = $completed / $this->chunkFinal;
202
203 $estimatedTotalTime = $delta / $portion;
204 $eta = $this->chunkStartTime + $estimatedTotalTime;
205
206 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
207 wfTimestamp( TS_DB, intval( $now ) ),
208 $portion * 100.0,
209 $this->chunkTable,
210 wfTimestamp( TS_DB, intval( $eta ) ),
211 $completed,
212 $this->chunkFinal,
213 $rate );
214 flush();
215
216 $chunk = array();
217 }
218 }
219
220 /**
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.
223 * @access private
224 */
225 function lastChunk( &$chunk ) {
226 $n = count( $chunk );
227 if( $n > 0 ) {
228 $this->insertChunk( $chunk );
229 }
230 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
231 }
232
233 /**
234 * Chunked inserts: perform an insert.
235 * @param array &$chunk -- This will be emptied if an insert is done.
236 * @access private
237 */
238 function insertChunk( &$chunk ) {
239 $this->dbw->insert( $this->chunkTable, $chunk, $this->chunkFunction, $this->chunkOptions );
240 }
241
242
243 function upgradePage() {
244 $fname = "FiveUpgrade::upgradePage";
245 $chunksize = 100;
246
247
248 $this->log( "Checking cur table for unique title index and applying if necessary" );
249 checkDupes( true );
250
251 $this->log( "...converting from cur/old to page/revision/text DB structure." );
252
253 extract( $this->dbw->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
254
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,
268
269 PRIMARY KEY page_id (page_id),
270 UNIQUE INDEX name_title (page_namespace,page_title),
271 INDEX (page_random),
272 INDEX (page_len)
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',
283
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 );
291
292 $maxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
293 $this->log( "Last old record is {$maxold}" );
294
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'";
302 } else {
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';
307 $cur_flags = "''";
308 }
309
310 $maxcur = $this->dbw->selectField( 'cur', 'max(cur_id)', '', $fname );
311 $this->log( "Last cur entry is $maxcur" );
312
313 /**
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.
318 */
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
323 FROM $cur
324 ORDER BY cur_id", $fname );
325 $add = array();
326 while( $row = $this->dbr->fetchObject( $result ) ) {
327 $add[] = array(
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 );
338 }
339 $this->lastChunk( $add );
340 $this->dbr->freeResult( $result );
341
342 /**
343 * Copy revision metadata from old into revision.
344 * We'll also do UTF-8 conversion of usernames and comments.
345 */
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 );
350
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",
356 $fname );
357
358 $add = array();
359 while( $row = $this->dbr->fetchObject( $result ) ) {
360 $add[] = array(
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 );
369 }
370 $this->lastChunk( $add );
371 $this->dbr->freeResult( $result );
372
373
374 /**
375 * Copy page metadata from cur into page.
376 * We'll also do UTF-8 conversion of titles.
377 */
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
383 FROM $cur,$revision
384 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
385 ORDER BY cur_id", $fname );
386 $add = array();
387 while( $row = $this->dbr->fetchObject( $result ) ) {
388 $add[] = array(
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 );
401 }
402 $this->lastChunk( $add );
403 $this->dbr->freeResult( $result );
404
405 $this->log( "...done with cur/old -> page/revision." );
406 }
407
408 function upgradeLinks() {
409 $fname = 'FiveUpgrade::upgradeLinks';
410 $chunksize = 200;
411 extract( $this->dbw->tableNames( 'links', 'brokenlinks', 'pagelinks', 'page' ) );
412
413 $this->log( 'Creating pagelinks table...' );
414 $this->dbw->query( "
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',
418
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
422 -- goes by.
423 pl_namespace int NOT NULL default '0',
424 pl_title varchar(255) binary NOT NULL default '',
425
426 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
427 KEY (pl_namespace,pl_title)
428
429 ) TYPE=InnoDB" );
430
431 $this->log( 'Importing live links -> pagelinks' );
432 $nlinks = $this->dbw->selectField( 'links', 'count(*)', '', $fname );
433 if( $nlinks ) {
434 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
435 $result = $this->dbr->query( "
436 SELECT l_from,page_namespace,page_title
437 FROM $links, $page
438 WHERE l_to=page_id", $fname );
439 $add = array();
440 while( $row = $this->dbr->fetchObject( $result ) ) {
441 $add[] = array(
442 'pl_from' => $row->l_from,
443 'pl_namespace' => $row->page_namespace,
444 'pl_title' => $row->page_title );
445 $this->addChunk( $add );
446 }
447 $this->lastChunk( $add );
448 } else {
449 $this->log( 'no links!' );
450 }
451
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",
459 $fname );
460 $add = array();
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')" );
466 } else {
467 $add[] = array(
468 'pl_from' => $row->bl_from,
469 'pl_namespace' => $title->getNamespace(),
470 'pl_title' => $title->getDBkey() );
471 $this->addChunk( $add );
472 }
473 }
474 $this->lastChunk( $add );
475 } else {
476 $this->log( 'no brokenlinks!' );
477 }
478
479 $this->log( 'Done with links.' );
480 }
481
482 function upgradeUser() {
483 $fname = 'FiveUpgrade::upgradeUser';
484 $chunksize = 100;
485 $preauth = 0;
486
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." );
491 } else {
492 $this->log( "Clearing user duplicates..." );
493 if( !$duper->clearDupes() ) {
494 $this->log( "WARNING: Duplicate user accounts, may explode!" );
495 }
496 }
497
498 /** Convert encoding on options, etc */
499 extract( $this->dbw->tableNames( 'user', 'user_temp', 'user_old' ) );
500
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,
515
516 PRIMARY KEY user_id (user_id),
517 UNIQUE INDEX user_name (user_name),
518 INDEX (user_email_token)
519
520 ) TYPE=InnoDB", $fname );
521
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',
526 array(
527 'user_id',
528 'user_name',
529 'user_real_name',
530 'user_password',
531 'user_newpassword',
532 'user_email',
533 'user_options',
534 'user_touched',
535 'user_token' ),
536 '',
537 $fname );
538 $add = array();
539 while( $row = $this->dbr->fetchObject( $result ) ) {
540 $now = $this->dbw->timestamp();
541 $add[] = array(
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 );
555 }
556 $this->lastChunk( $add );
557 $this->dbr->freeResult( $result );
558 }
559
560 function upgradeImage() {
561 $fname = 'FiveUpgrade::upgradeImage';
562 $chunksize = 100;
563
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 '',
581
582 PRIMARY KEY img_name (img_name),
583 INDEX img_size (img_size),
584 INDEX img_timestamp (img_timestamp)
585 ) TYPE=InnoDB
586 END
587 , $fname);
588
589 $numimages = $this->dbw->selectField( 'image', 'count(*)', '', $fname );
590 $result = $this->dbr->select( 'image',
591 array(
592 'img_name',
593 'img_size',
594 'img_description',
595 'img_user',
596 'img_user_text',
597 'img_timestamp' ),
598 '',
599 $fname );
600 $add = array();
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 );
605
606 // Update and convert encoding
607 $add[] = array(
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 );
621
622 // If doing UTF8 conversion the file must be renamed
623 $this->renameFile( $row->img_name, 'wfImageDir' );
624 }
625 $this->lastChunk( $add );
626
627 $this->log( 'done with image table.' );
628 }
629
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;
634 $info = array(
635 'width' => 0,
636 'height' => 0,
637 'bits' => 0,
638 'media' => '',
639 'major' => '',
640 'minor' => '' );
641
642 $magic =& wfGetMimeMagic();
643 $mime = $magic->guessMimeType( $filename, true );
644 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
645
646 $info['media'] = $magic->getMediaType( $filename, $mime );
647
648 # Height and width
649 $gis = false;
650 if( $mime == 'image/svg' ) {
651 $gis = wfGetSVGsize( $this->imagePath );
652 } elseif( $magic->isPHPImageType( $mime ) ) {
653 $gis = getimagesize( $filename );
654 } else {
655 $this->log( "Surprising mime type: $mime" );
656 }
657 if( $gis ) {
658 $info['width' ] = $gis[0];
659 $info['height'] = $gis[1];
660 }
661 if( isset( $gis['bits'] ) ) {
662 $info['bits'] = $gis['bits'];
663 }
664
665 return $info;
666 }
667
668
669 /**
670 * Truncate a table.
671 * @param string $table The table name to be truncated
672 */
673 function clearTable( $table ) {
674 print "Clearing $table...\n";
675 $tableName = $this->db->tableName( $table );
676 $this->db->query( 'TRUNCATE $tableName' );
677 }
678
679 /**
680 * Rename a given image or archived image file to the converted filename,
681 * leaving a symlink for URL compatibility.
682 *
683 * @param string $oldname pre-conversion filename
684 * @param string $basename pre-conversion base filename for dir hashing, if an archive
685 * @access private
686 */
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.
691 return;
692 }
693
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;
698
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!" );
704 }
705 } else {
706 $this->log( "... rename failed!" );
707 }
708 }
709
710 /**
711 * Generate a relative path name to the given file.
712 * Assumes Unix-style paths, separators, and semantics.
713 *
714 * @param string $path Absolute destination path including target filename
715 * @param string $from Absolute source path, directory only
716 * @return string
717 * @access private
718 * @static
719 */
720 function relativize( $path, $from ) {
721 $pieces = explode( '/', dirname( $path ) );
722 $against = explode( '/', $from );
723
724 // Trim off common prefix
725 while( count( $pieces ) && count( $against )
726 && $pieces[0] == $against[0] ) {
727 array_shift( $pieces );
728 array_shift( $against );
729 }
730
731 // relative dots to bump us to the parent
732 while( count( $against ) ) {
733 array_unshift( $pieces, '..' );
734 array_shift( $against );
735 }
736
737 array_push( $pieces, basename( $path ) );
738
739 return implode( '/', $pieces );
740 }
741
742 function upgradeOldImage() {
743 $fname = 'FiveUpgrade::upgradeOldImage';
744 $chunksize = 100;
745
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 '',
752
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 '',
756
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 '',
766
767 INDEX oi_name (oi_name(10))
768
769 ) TYPE=InnoDB;
770 END
771 , $fname);
772
773 $numimages = $this->dbw->selectField( 'oldimage', 'count(*)', '', $fname );
774 $result = $this->dbr->select( 'oldimage',
775 array(
776 'oi_name',
777 'oi_archive_name',
778 'oi_size',
779 'oi_description',
780 'oi_user',
781 'oi_user_text',
782 'oi_timestamp' ),
783 '',
784 $fname );
785 $add = array();
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 );
790
791 // Update and convert encoding
792 $add[] = array(
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 );
803
804 // If doing UTF8 conversion the file must be renamed
805 $this->renameFile( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
806 }
807 $this->lastChunk( $add );
808
809 $this->log( 'done with oldimage table.' );
810 }
811
812
813 function upgradeWatchlist() {
814 $fname = 'FiveUpgrade::upgradeWatchlist';
815 $chunksize = 100;
816
817 extract( $this->dbw->tableNames( 'watchlist', 'watchlist_temp' ) );
818
819 $this->log( 'Migrating watchlist table to watchlist_temp...' );
820 $this->dbw->query(
821 "CREATE TABLE $watchlist_temp (
822 -- Key to user_id
823 wl_user int(5) unsigned NOT NULL,
824
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 '',
830
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',
835
836 UNIQUE KEY (wl_user, wl_namespace, wl_title),
837 KEY namespace_title (wl_namespace,wl_title)
838
839 ) TYPE=InnoDB;", $fname );
840
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 );
844
845 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
846 $result = $this->dbr->select( 'watchlist',
847 array(
848 'wl_user',
849 'wl_namespace',
850 'wl_title' ),
851 '',
852 $fname );
853
854 $add = array();
855 while( $row = $this->dbr->fetchObject( $result ) ) {
856 $now = $this->dbw->timestamp();
857 $add[] = array(
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 );
863
864 $add[] = array(
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 );
870 }
871 $this->lastChunk( $add );
872 $this->dbr->freeResult( $result );
873
874 $this->log( 'Done converting watchlist.' );
875 }
876
877
878 /**
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.
882 */
883 function upgradeCleanup() {
884 $this->renameTable( 'old', 'text' );
885
886 $this->swap( 'user' );
887 $this->swap( 'image' );
888 $this->swap( 'oldimage' );
889 $this->swap( 'watchlist' );
890 }
891
892 function renameTable( $from, $to ) {
893 $this->log( 'Renaming $from to $to...' );
894
895 $fromtable = $this->dbw->tableName( $from );
896 $totable = $this->dbw->tableName( $to );
897 $this->dbw->query( "ALTER TABLE $fromtable RENAME TO $totable" );
898 }
899
900 function swap( $base ) {
901 $this->renameTable( $base, "{$base}_old" );
902 $this->renameTable( "{$base}_temp", $base );
903 }
904
905 }
906
907 ?>