* (bug 12145) Mark 'tog-nolangconversion', 'yourvariant' as optional. Messages need...
[lhc/web/wiklou.git] / maintenance / FiveUpgrade.inc
1 <?php
2
3 require_once( 'cleanupDupes.inc' );
4 require_once( 'userDupes.inc' );
5 require_once( 'updaters.inc' );
6
7 define( 'MW_UPGRADE_COPY', false );
8 define( 'MW_UPGRADE_ENCODE', true );
9 define( 'MW_UPGRADE_NULL', null );
10 define( 'MW_UPGRADE_CALLBACK', null ); // for self-documentation only
11
12 class FiveUpgrade {
13 function FiveUpgrade() {
14 $this->conversionTables = $this->prepareWindows1252();
15
16 $this->dbw =& $this->newConnection();
17 $this->dbr =& $this->streamConnection();
18
19 $this->cleanupSwaps = array();
20 $this->emailAuth = false; # don't preauthenticate emails
21 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
22 }
23
24 function doing( $step ) {
25 return is_null( $this->step ) || $step == $this->step;
26 }
27
28 function upgrade( $step ) {
29 $this->step = $step;
30
31 $tables = array(
32 'page',
33 'links',
34 'user',
35 'image',
36 'oldimage',
37 'watchlist',
38 'logging',
39 'archive',
40 'imagelinks',
41 'categorylinks',
42 'ipblocks',
43 'recentchanges',
44 'querycache' );
45 foreach( $tables as $table ) {
46 if( $this->doing( $table ) ) {
47 $method = 'upgrade' . ucfirst( $table );
48 $this->$method();
49 }
50 }
51
52 if( $this->doing( 'cleanup' ) ) {
53 $this->upgradeCleanup();
54 }
55 }
56
57
58 /**
59 * Open a connection to the master server with the admin rights.
60 * @return Database
61 * @access private
62 */
63 function &newConnection() {
64 global $wgDBadminuser, $wgDBadminpassword;
65 global $wgDBserver, $wgDBname;
66 $db = new Database( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
67 return $db;
68 }
69
70 /**
71 * Open a second connection to the master server, with buffering off.
72 * This will let us stream large datasets in and write in chunks on the
73 * other end.
74 * @return Database
75 * @access private
76 */
77 function &streamConnection() {
78 $timeout = 3600 * 24;
79 $db =& $this->newConnection();
80 $db->bufferResults( false );
81 $db->query( "SET net_read_timeout=$timeout" );
82 $db->query( "SET net_write_timeout=$timeout" );
83 return $db;
84 }
85
86 /**
87 * Prepare a conversion array for converting Windows Code Page 1252 to
88 * UTF-8. This should provide proper conversion of text that was miscoded
89 * as Windows-1252 by naughty user-agents, and doesn't rely on an outside
90 * iconv library.
91 *
92 * @return array
93 * @access private
94 */
95 function prepareWindows1252() {
96 # Mappings from:
97 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
98 static $cp1252 = array(
99 0x80 => 0x20AC, #EURO SIGN
100 0x81 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
101 0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
102 0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
103 0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
104 0x85 => 0x2026, #HORIZONTAL ELLIPSIS
105 0x86 => 0x2020, #DAGGER
106 0x87 => 0x2021, #DOUBLE DAGGER
107 0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
108 0x89 => 0x2030, #PER MILLE SIGN
109 0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
110 0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
111 0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
112 0x8D => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
113 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
114 0x8F => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
115 0x90 => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
116 0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
117 0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
118 0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
119 0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
120 0x95 => 0x2022, #BULLET
121 0x96 => 0x2013, #EN DASH
122 0x97 => 0x2014, #EM DASH
123 0x98 => 0x02DC, #SMALL TILDE
124 0x99 => 0x2122, #TRADE MARK SIGN
125 0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
126 0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
127 0x9C => 0x0153, #LATIN SMALL LIGATURE OE
128 0x9D => 0xFFFD, #REPLACEMENT CHARACTER (no mapping)
129 0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
130 0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
131 );
132 $pairs = array();
133 for( $i = 0; $i < 0x100; $i++ ) {
134 $unicode = isset( $cp1252[$i] ) ? $cp1252[$i] : $i;
135 $pairs[chr( $i )] = codepointToUtf8( $unicode );
136 }
137 return $pairs;
138 }
139
140 /**
141 * Convert from 8-bit Windows-1252 to UTF-8 if necessary.
142 * @param string $text
143 * @return string
144 * @access private
145 */
146 function conv( $text ) {
147 global $wgUseLatin1;
148 return is_null( $text )
149 ? null
150 : ( $wgUseLatin1
151 ? strtr( $text, $this->conversionTables )
152 : $text );
153 }
154
155 /**
156 * Dump timestamp and message to output
157 * @param string $message
158 * @access private
159 */
160 function log( $message ) {
161 echo wfWikiID() . ' ' . wfTimestamp( TS_DB ) . ': ' . $message . "\n";
162 flush();
163 }
164
165 /**
166 * Initialize the chunked-insert system.
167 * Rows will be inserted in chunks of the given number, rather
168 * than in a giant INSERT...SELECT query, to keep the serialized
169 * MySQL database replication from getting hung up. This way other
170 * things can be going on during conversion without waiting for
171 * slaves to catch up as badly.
172 *
173 * @param int $chunksize Number of rows to insert at once
174 * @param int $final Total expected number of rows / id of last row,
175 * used for progress reports.
176 * @param string $table to insert on
177 * @param string $fname function name to report in SQL
178 * @access private
179 */
180 function setChunkScale( $chunksize, $final, $table, $fname ) {
181 $this->chunkSize = $chunksize;
182 $this->chunkFinal = $final;
183 $this->chunkCount = 0;
184 $this->chunkStartTime = wfTime();
185 $this->chunkOptions = array( 'IGNORE' );
186 $this->chunkTable = $table;
187 $this->chunkFunction = $fname;
188 }
189
190 /**
191 * Chunked inserts: perform an insert if we've reached the chunk limit.
192 * Prints a progress report with estimated completion time.
193 * @param array &$chunk -- This will be emptied if an insert is done.
194 * @param int $key A key identifier to use in progress estimation in
195 * place of the number of rows inserted. Use this if
196 * you provided a max key number instead of a count
197 * as the final chunk number in setChunkScale()
198 * @access private
199 */
200 function addChunk( &$chunk, $key = null ) {
201 if( count( $chunk ) >= $this->chunkSize ) {
202 $this->insertChunk( $chunk );
203
204 $this->chunkCount += count( $chunk );
205 $now = wfTime();
206 $delta = $now - $this->chunkStartTime;
207 $rate = $this->chunkCount / $delta;
208
209 if( is_null( $key ) ) {
210 $completed = $this->chunkCount;
211 } else {
212 $completed = $key;
213 }
214 $portion = $completed / $this->chunkFinal;
215
216 $estimatedTotalTime = $delta / $portion;
217 $eta = $this->chunkStartTime + $estimatedTotalTime;
218
219 printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec\n",
220 wfTimestamp( TS_DB, intval( $now ) ),
221 $portion * 100.0,
222 $this->chunkTable,
223 wfTimestamp( TS_DB, intval( $eta ) ),
224 $completed,
225 $this->chunkFinal,
226 $rate );
227 flush();
228
229 $chunk = array();
230 }
231 }
232
233 /**
234 * Chunked inserts: perform an insert unconditionally, at the end, and log.
235 * @param array &$chunk -- This will be emptied if an insert is done.
236 * @access private
237 */
238 function lastChunk( &$chunk ) {
239 $n = count( $chunk );
240 if( $n > 0 ) {
241 $this->insertChunk( $chunk );
242 }
243 $this->log( "100.00% done on $this->chunkTable (last chunk $n rows)." );
244 }
245
246 /**
247 * Chunked inserts: perform an insert.
248 * @param array &$chunk -- This will be emptied if an insert is done.
249 * @access private
250 */
251 function insertChunk( &$chunk ) {
252 // Give slaves a chance to catch up
253 wfWaitForSlaves( $this->maxLag );
254 $this->dbw->insert( $this->chunkTable, $chunk, $this->chunkFunction, $this->chunkOptions );
255 }
256
257
258 /**
259 * Copy and transcode a table to table_temp.
260 * @param string $name Base name of the source table
261 * @param string $tabledef CREATE TABLE definition, w/ $1 for the name
262 * @param array $fields set of destination fields to these constants:
263 * MW_UPGRADE_COPY - straight copy
264 * MW_UPGRADE_ENCODE - for old Latin1 wikis, conv to UTF-8
265 * MW_UPGRADE_NULL - just put NULL
266 * @param callable $callback An optional callback to modify the data
267 * or perform other processing. Func should be
268 * ( object $row, array $copy ) and return $copy
269 * @access private
270 */
271 function copyTable( $name, $tabledef, $fields, $callback = null ) {
272 $fname = 'FiveUpgrade::copyTable';
273
274 $name_temp = $name . '_temp';
275 $this->log( "Migrating $name table to $name_temp..." );
276
277 $table_temp = $this->dbw->tableName( $name_temp );
278
279 // Create temporary table; we're going to copy everything in there,
280 // then at the end rename the final tables into place.
281 $def = str_replace( '$1', $table_temp, $tabledef );
282 $this->dbw->query( $def, $fname );
283
284 $numRecords = $this->dbw->selectField( $name, 'COUNT(*)', '', $fname );
285 $this->setChunkScale( 100, $numRecords, $name_temp, $fname );
286
287 // Pull all records from the second, streaming database connection.
288 $sourceFields = array_keys( array_filter( $fields,
289 create_function( '$x', 'return $x !== MW_UPGRADE_NULL;' ) ) );
290 $result = $this->dbr->select( $name,
291 $sourceFields,
292 '',
293 $fname );
294
295 $add = array();
296 while( $row = $this->dbr->fetchObject( $result ) ) {
297 $copy = array();
298 foreach( $fields as $field => $source ) {
299 if( $source === MW_UPGRADE_COPY ) {
300 $copy[$field] = $row->$field;
301 } elseif( $source === MW_UPGRADE_ENCODE ) {
302 $copy[$field] = $this->conv( $row->$field );
303 } elseif( $source === MW_UPGRADE_NULL ) {
304 $copy[$field] = null;
305 } else {
306 $this->log( "Unknown field copy type: $field => $source" );
307 }
308 }
309 if( is_callable( $callback ) ) {
310 $copy = call_user_func( $callback, $row, $copy );
311 }
312 $add[] = $copy;
313 $this->addChunk( $add );
314 }
315 $this->lastChunk( $add );
316 $this->dbr->freeResult( $result );
317
318 $this->log( "Done converting $name." );
319 $this->cleanupSwaps[] = $name;
320 }
321
322 function upgradePage() {
323 $fname = "FiveUpgrade::upgradePage";
324 $chunksize = 100;
325
326 if( $this->dbw->tableExists( 'page' ) ) {
327 $this->log( 'Page table already exists; aborting.' );
328 die( -1 );
329 }
330
331 $this->log( "Checking cur table for unique title index and applying if necessary" );
332 checkDupes( true );
333
334 $this->log( "...converting from cur/old to page/revision/text DB structure." );
335
336 list ($cur, $old, $page, $revision, $text) = $this->dbw->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
337
338 $this->log( "Creating page and revision tables..." );
339 $this->dbw->query("CREATE TABLE $page (
340 page_id int(8) unsigned NOT NULL auto_increment,
341 page_namespace int NOT NULL,
342 page_title varchar(255) binary NOT NULL,
343 page_restrictions tinyblob NOT NULL default '',
344 page_counter bigint(20) unsigned NOT NULL default '0',
345 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
346 page_is_new tinyint(1) unsigned NOT NULL default '0',
347 page_random real unsigned NOT NULL,
348 page_touched char(14) binary NOT NULL default '',
349 page_latest int(8) unsigned NOT NULL,
350 page_len int(8) unsigned NOT NULL,
351
352 PRIMARY KEY page_id (page_id),
353 UNIQUE INDEX name_title (page_namespace,page_title),
354 INDEX (page_random),
355 INDEX (page_len)
356 ) TYPE=InnoDB", $fname );
357 $this->dbw->query("CREATE TABLE $revision (
358 rev_id int(8) unsigned NOT NULL auto_increment,
359 rev_page int(8) unsigned NOT NULL,
360 rev_text_id int(8) unsigned NOT NULL,
361 rev_comment tinyblob NOT NULL default '',
362 rev_user int(5) unsigned NOT NULL default '0',
363 rev_user_text varchar(255) binary NOT NULL default '',
364 rev_timestamp char(14) binary NOT NULL default '',
365 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
366 rev_deleted tinyint(1) unsigned NOT NULL default '0',
367
368 PRIMARY KEY rev_page_id (rev_page, rev_id),
369 UNIQUE INDEX rev_id (rev_id),
370 INDEX rev_timestamp (rev_timestamp),
371 INDEX page_timestamp (rev_page,rev_timestamp),
372 INDEX user_timestamp (rev_user,rev_timestamp),
373 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
374 ) TYPE=InnoDB", $fname );
375
376 $maxold = intval( $this->dbw->selectField( 'old', 'max(old_id)', '', $fname ) );
377 $this->log( "Last old record is {$maxold}" );
378
379 global $wgLegacySchemaConversion;
380 if( $wgLegacySchemaConversion ) {
381 // Create HistoryBlobCurStub entries.
382 // Text will be pulled from the leftover 'cur' table at runtime.
383 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
384 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
385 $cur_flags = "'object'";
386 } else {
387 // Copy all cur text in immediately: this may take longer but avoids
388 // having to keep an extra table around.
389 echo "......Moving text from cur.\n";
390 $cur_text = 'cur_text';
391 $cur_flags = "''";
392 }
393
394 $maxcur = $this->dbw->selectField( 'cur', 'max(cur_id)', '', $fname );
395 $this->log( "Last cur entry is $maxcur" );
396
397 /**
398 * Copy placeholder records for each page's current version into old
399 * Don't do any conversion here; text records are converted at runtime
400 * based on the flags (and may be originally binary!) while the meta
401 * fields will be converted in the old -> rev and cur -> page steps.
402 */
403 $this->setChunkScale( $chunksize, $maxcur, 'old', $fname );
404 $result = $this->dbr->query(
405 "SELECT cur_id, cur_namespace, cur_title, $cur_text AS text, cur_comment,
406 cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags AS flags
407 FROM $cur
408 ORDER BY cur_id", $fname );
409 $add = array();
410 while( $row = $this->dbr->fetchObject( $result ) ) {
411 $add[] = array(
412 'old_namespace' => $row->cur_namespace,
413 'old_title' => $row->cur_title,
414 'old_text' => $row->text,
415 'old_comment' => $row->cur_comment,
416 'old_user' => $row->cur_user,
417 'old_user_text' => $row->cur_user_text,
418 'old_timestamp' => $row->cur_timestamp,
419 'old_minor_edit' => $row->cur_minor_edit,
420 'old_flags' => $row->flags );
421 $this->addChunk( $add, $row->cur_id );
422 }
423 $this->lastChunk( $add );
424 $this->dbr->freeResult( $result );
425
426 /**
427 * Copy revision metadata from old into revision.
428 * We'll also do UTF-8 conversion of usernames and comments.
429 */
430 #$newmaxold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
431 #$this->setChunkScale( $chunksize, $newmaxold, 'revision', $fname );
432 #$countold = $this->dbw->selectField( 'old', 'count(old_id)', '', $fname );
433 $countold = $this->dbw->selectField( 'old', 'max(old_id)', '', $fname );
434 $this->setChunkScale( $chunksize, $countold, 'revision', $fname );
435
436 $this->log( "......Setting up revision table." );
437 $result = $this->dbr->query(
438 "SELECT old_id, cur_id, old_comment, old_user, old_user_text,
439 old_timestamp, old_minor_edit
440 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title",
441 $fname );
442
443 $add = array();
444 while( $row = $this->dbr->fetchObject( $result ) ) {
445 $add[] = array(
446 'rev_id' => $row->old_id,
447 'rev_page' => $row->cur_id,
448 'rev_text_id' => $row->old_id,
449 'rev_comment' => $this->conv( $row->old_comment ),
450 'rev_user' => $row->old_user,
451 'rev_user_text' => $this->conv( $row->old_user_text ),
452 'rev_timestamp' => $row->old_timestamp,
453 'rev_minor_edit' => $row->old_minor_edit );
454 $this->addChunk( $add );
455 }
456 $this->lastChunk( $add );
457 $this->dbr->freeResult( $result );
458
459
460 /**
461 * Copy page metadata from cur into page.
462 * We'll also do UTF-8 conversion of titles.
463 */
464 $this->log( "......Setting up page table." );
465 $this->setChunkScale( $chunksize, $maxcur, 'page', $fname );
466 $result = $this->dbr->query( "
467 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
468 cur_random, cur_touched, rev_id, LENGTH(cur_text) AS len
469 FROM $cur,$revision
470 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}
471 ORDER BY cur_id", $fname );
472 $add = array();
473 while( $row = $this->dbr->fetchObject( $result ) ) {
474 $add[] = array(
475 'page_id' => $row->cur_id,
476 'page_namespace' => $row->cur_namespace,
477 'page_title' => $this->conv( $row->cur_title ),
478 'page_restrictions' => $row->cur_restrictions,
479 'page_counter' => $row->cur_counter,
480 'page_is_redirect' => $row->cur_is_redirect,
481 'page_is_new' => $row->cur_is_new,
482 'page_random' => $row->cur_random,
483 'page_touched' => $this->dbw->timestamp(),
484 'page_latest' => $row->rev_id,
485 'page_len' => $row->len );
486 #$this->addChunk( $add, $row->cur_id );
487 $this->addChunk( $add );
488 }
489 $this->lastChunk( $add );
490 $this->dbr->freeResult( $result );
491
492 $this->log( "...done with cur/old -> page/revision." );
493 }
494
495 function upgradeLinks() {
496 $fname = 'FiveUpgrade::upgradeLinks';
497 $chunksize = 200;
498 list ($links, $brokenlinks, $pagelinks, $cur) = $this->dbw->tableNamesN( 'links', 'brokenlinks', 'pagelinks', 'cur' );
499
500 $this->log( 'Checking for interwiki table change in case of bogus items...' );
501 if( $this->dbw->fieldExists( 'interwiki', 'iw_trans' ) ) {
502 $this->log( 'interwiki has iw_trans.' );
503 } else {
504 global $IP;
505 $this->log( 'adding iw_trans...' );
506 dbsource( $IP . '/maintenance/archives/patch-interwiki-trans.sql', $this->dbw );
507 $this->log( 'added iw_trans.' );
508 }
509
510 $this->log( 'Creating pagelinks table...' );
511 $this->dbw->query( "
512 CREATE TABLE $pagelinks (
513 -- Key to the page_id of the page containing the link.
514 pl_from int(8) unsigned NOT NULL default '0',
515
516 -- Key to page_namespace/page_title of the target page.
517 -- The target page may or may not exist, and due to renames
518 -- and deletions may refer to different page records as time
519 -- goes by.
520 pl_namespace int NOT NULL default '0',
521 pl_title varchar(255) binary NOT NULL default '',
522
523 UNIQUE KEY pl_from(pl_from,pl_namespace,pl_title),
524 KEY (pl_namespace,pl_title)
525
526 ) TYPE=InnoDB" );
527
528 $this->log( 'Importing live links -> pagelinks' );
529 $nlinks = $this->dbw->selectField( 'links', 'count(*)', '', $fname );
530 if( $nlinks ) {
531 $this->setChunkScale( $chunksize, $nlinks, 'pagelinks', $fname );
532 $result = $this->dbr->query( "
533 SELECT l_from,cur_namespace,cur_title
534 FROM $links, $cur
535 WHERE l_to=cur_id", $fname );
536 $add = array();
537 while( $row = $this->dbr->fetchObject( $result ) ) {
538 $add[] = array(
539 'pl_from' => $row->l_from,
540 'pl_namespace' => $row->cur_namespace,
541 'pl_title' => $this->conv( $row->cur_title ) );
542 $this->addChunk( $add );
543 }
544 $this->lastChunk( $add );
545 } else {
546 $this->log( 'no links!' );
547 }
548
549 $this->log( 'Importing brokenlinks -> pagelinks' );
550 $nbrokenlinks = $this->dbw->selectField( 'brokenlinks', 'count(*)', '', $fname );
551 if( $nbrokenlinks ) {
552 $this->setChunkScale( $chunksize, $nbrokenlinks, 'pagelinks', $fname );
553 $result = $this->dbr->query(
554 "SELECT bl_from, bl_to FROM $brokenlinks",
555 $fname );
556 $add = array();
557 while( $row = $this->dbr->fetchObject( $result ) ) {
558 $pagename = $this->conv( $row->bl_to );
559 $title = Title::newFromText( $pagename );
560 if( is_null( $title ) ) {
561 $this->log( "** invalid brokenlink: $row->bl_from -> '$pagename' (converted from '$row->bl_to')" );
562 } else {
563 $add[] = array(
564 'pl_from' => $row->bl_from,
565 'pl_namespace' => $title->getNamespace(),
566 'pl_title' => $title->getDBkey() );
567 $this->addChunk( $add );
568 }
569 }
570 $this->lastChunk( $add );
571 } else {
572 $this->log( 'no brokenlinks!' );
573 }
574
575 $this->log( 'Done with links.' );
576 }
577
578 function upgradeUser() {
579 // Apply unique index, if necessary:
580 $duper = new UserDupes( $this->dbw );
581 if( $duper->hasUniqueIndex() ) {
582 $this->log( "Already have unique user_name index." );
583 } else {
584 $this->log( "Clearing user duplicates..." );
585 if( !$duper->clearDupes() ) {
586 $this->log( "WARNING: Duplicate user accounts, may explode!" );
587 }
588 }
589
590 $tabledef = <<<END
591 CREATE TABLE $1 (
592 user_id int(5) unsigned NOT NULL auto_increment,
593 user_name varchar(255) binary NOT NULL default '',
594 user_real_name varchar(255) binary NOT NULL default '',
595 user_password tinyblob NOT NULL default '',
596 user_newpassword tinyblob NOT NULL default '',
597 user_email tinytext NOT NULL default '',
598 user_options blob NOT NULL default '',
599 user_touched char(14) binary NOT NULL default '',
600 user_token char(32) binary NOT NULL default '',
601 user_email_authenticated CHAR(14) BINARY,
602 user_email_token CHAR(32) BINARY,
603 user_email_token_expires CHAR(14) BINARY,
604
605 PRIMARY KEY user_id (user_id),
606 UNIQUE INDEX user_name (user_name),
607 INDEX (user_email_token)
608
609 ) TYPE=InnoDB
610 END;
611 $fields = array(
612 'user_id' => MW_UPGRADE_COPY,
613 'user_name' => MW_UPGRADE_ENCODE,
614 'user_real_name' => MW_UPGRADE_ENCODE,
615 'user_password' => MW_UPGRADE_COPY,
616 'user_newpassword' => MW_UPGRADE_COPY,
617 'user_email' => MW_UPGRADE_ENCODE,
618 'user_options' => MW_UPGRADE_ENCODE,
619 'user_touched' => MW_UPGRADE_CALLBACK,
620 'user_token' => MW_UPGRADE_COPY,
621 'user_email_authenticated' => MW_UPGRADE_CALLBACK,
622 'user_email_token' => MW_UPGRADE_NULL,
623 'user_email_token_expires' => MW_UPGRADE_NULL );
624 $this->copyTable( 'user', $tabledef, $fields,
625 array( &$this, 'userCallback' ) );
626 }
627
628 function userCallback( $row, $copy ) {
629 $now = $this->dbw->timestamp();
630 $copy['user_touched'] = $now;
631 $copy['user_email_authenticated'] = $this->emailAuth ? $now : null;
632 return $copy;
633 }
634
635 function upgradeImage() {
636 $tabledef = <<<END
637 CREATE TABLE $1 (
638 img_name varchar(255) binary NOT NULL default '',
639 img_size int(8) unsigned NOT NULL default '0',
640 img_width int(5) NOT NULL default '0',
641 img_height int(5) NOT NULL default '0',
642 img_metadata mediumblob NOT NULL,
643 img_bits int(3) NOT NULL default '0',
644 img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO", "MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
645 img_major_mime ENUM("unknown", "application", "audio", "image", "text", "video", "message", "model", "multipart") NOT NULL default "unknown",
646 img_minor_mime varchar(32) NOT NULL default "unknown",
647 img_description tinyblob NOT NULL default '',
648 img_user int(5) unsigned NOT NULL default '0',
649 img_user_text varchar(255) binary NOT NULL default '',
650 img_timestamp char(14) binary NOT NULL default '',
651
652 PRIMARY KEY img_name (img_name),
653 INDEX img_size (img_size),
654 INDEX img_timestamp (img_timestamp)
655 ) TYPE=InnoDB
656 END;
657 $fields = array(
658 'img_name' => MW_UPGRADE_ENCODE,
659 'img_size' => MW_UPGRADE_COPY,
660 'img_width' => MW_UPGRADE_CALLBACK,
661 'img_height' => MW_UPGRADE_CALLBACK,
662 'img_metadata' => MW_UPGRADE_CALLBACK,
663 'img_bits' => MW_UPGRADE_CALLBACK,
664 'img_media_type' => MW_UPGRADE_CALLBACK,
665 'img_major_mime' => MW_UPGRADE_CALLBACK,
666 'img_minor_mime' => MW_UPGRADE_CALLBACK,
667 'img_description' => MW_UPGRADE_ENCODE,
668 'img_user' => MW_UPGRADE_COPY,
669 'img_user_text' => MW_UPGRADE_ENCODE,
670 'img_timestamp' => MW_UPGRADE_COPY );
671 $this->copyTable( 'image', $tabledef, $fields,
672 array( &$this, 'imageCallback' ) );
673 }
674
675 function imageCallback( $row, $copy ) {
676 global $options;
677 if( !isset( $options['noimage'] ) ) {
678 // Fill in the new image info fields
679 $info = $this->imageInfo( $row->img_name );
680
681 $copy['img_width' ] = $info['width'];
682 $copy['img_height' ] = $info['height'];
683 $copy['img_metadata' ] = ""; // loaded on-demand
684 $copy['img_bits' ] = $info['bits'];
685 $copy['img_media_type'] = $info['media'];
686 $copy['img_major_mime'] = $info['major'];
687 $copy['img_minor_mime'] = $info['minor'];
688 }
689
690 // If doing UTF8 conversion the file must be renamed
691 $this->renameFile( $row->img_name, 'wfImageDir' );
692
693 return $copy;
694 }
695
696 function imageInfo( $filename ) {
697 $info = array(
698 'width' => 0,
699 'height' => 0,
700 'bits' => 0,
701 'media' => '',
702 'major' => '',
703 'minor' => '' );
704
705 $magic =& wfGetMimeMagic();
706 $mime = $magic->guessMimeType( $filename, true );
707 list( $info['major'], $info['minor'] ) = explode( '/', $mime );
708
709 $info['media'] = $magic->getMediaType( $filename, $mime );
710
711 $image = UnregisteredLocalFile::newFromPath( $filename, $mime );
712
713 $info['width'] = $image->getWidth();
714 $info['height'] = $image->getHeight();
715
716 $gis = $image->getImageSize();
717 if ( isset( $gis['bits'] ) ) {
718 $info['bits'] = $gis['bits'];
719 }
720
721 return $info;
722 }
723
724
725 /**
726 * Truncate a table.
727 * @param string $table The table name to be truncated
728 */
729 function clearTable( $table ) {
730 print "Clearing $table...\n";
731 $tableName = $this->db->tableName( $table );
732 $this->db->query( "TRUNCATE $tableName" );
733 }
734
735 /**
736 * Rename a given image or archived image file to the converted filename,
737 * leaving a symlink for URL compatibility.
738 *
739 * @param string $oldname pre-conversion filename
740 * @param string $basename pre-conversion base filename for dir hashing, if an archive
741 * @access private
742 */
743 function renameFile( $oldname, $subdirCallback='wfImageDir', $basename=null ) {
744 $newname = $this->conv( $oldname );
745 if( $newname == $oldname ) {
746 // No need to rename; another field triggered this row.
747 return false;
748 }
749
750 if( is_null( $basename ) ) $basename = $oldname;
751 $ubasename = $this->conv( $basename );
752 $oldpath = call_user_func( $subdirCallback, $basename ) . '/' . $oldname;
753 $newpath = call_user_func( $subdirCallback, $ubasename ) . '/' . $newname;
754
755 $this->log( "$oldpath -> $newpath" );
756 if( rename( $oldpath, $newpath ) ) {
757 $relpath = wfRelativePath( $newpath, dirname( $oldpath ) );
758 if( !symlink( $relpath, $oldpath ) ) {
759 $this->log( "... symlink failed!" );
760 }
761 return $newname;
762 } else {
763 $this->log( "... rename failed!" );
764 return false;
765 }
766 }
767
768 function upgradeOldImage() {
769 $tabledef = <<<END
770 CREATE TABLE $1 (
771 -- Base filename: key to image.img_name
772 oi_name varchar(255) binary NOT NULL default '',
773
774 -- Filename of the archived file.
775 -- This is generally a timestamp and '!' prepended to the base name.
776 oi_archive_name varchar(255) binary NOT NULL default '',
777
778 -- Other fields as in image...
779 oi_size int(8) unsigned NOT NULL default 0,
780 oi_width int(5) NOT NULL default 0,
781 oi_height int(5) NOT NULL default 0,
782 oi_bits int(3) NOT NULL default 0,
783 oi_description tinyblob NOT NULL default '',
784 oi_user int(5) unsigned NOT NULL default '0',
785 oi_user_text varchar(255) binary NOT NULL default '',
786 oi_timestamp char(14) binary NOT NULL default '',
787
788 INDEX oi_name (oi_name(10))
789
790 ) TYPE=InnoDB;
791 END;
792 $fields = array(
793 'oi_name' => MW_UPGRADE_ENCODE,
794 'oi_archive_name' => MW_UPGRADE_ENCODE,
795 'oi_size' => MW_UPGRADE_COPY,
796 'oi_width' => MW_UPGRADE_CALLBACK,
797 'oi_height' => MW_UPGRADE_CALLBACK,
798 'oi_bits' => MW_UPGRADE_CALLBACK,
799 'oi_description' => MW_UPGRADE_ENCODE,
800 'oi_user' => MW_UPGRADE_COPY,
801 'oi_user_text' => MW_UPGRADE_ENCODE,
802 'oi_timestamp' => MW_UPGRADE_COPY );
803 $this->copyTable( 'oldimage', $tabledef, $fields,
804 array( &$this, 'oldimageCallback' ) );
805 }
806
807 function oldimageCallback( $row, $copy ) {
808 global $options;
809 if( !isset( $options['noimage'] ) ) {
810 // Fill in the new image info fields
811 $info = $this->imageInfo( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
812 $copy['oi_width' ] = $info['width' ];
813 $copy['oi_height'] = $info['height'];
814 $copy['oi_bits' ] = $info['bits' ];
815 }
816
817 // If doing UTF8 conversion the file must be renamed
818 $this->renameFile( $row->oi_archive_name, 'wfImageArchiveDir', $row->oi_name );
819
820 return $copy;
821 }
822
823
824 function upgradeWatchlist() {
825 $fname = 'FiveUpgrade::upgradeWatchlist';
826 $chunksize = 100;
827
828 list ($watchlist, $watchlist_temp) = $this->dbw->tableNamesN( 'watchlist', 'watchlist_temp' );
829
830 $this->log( 'Migrating watchlist table to watchlist_temp...' );
831 $this->dbw->query(
832 "CREATE TABLE $watchlist_temp (
833 -- Key to user_id
834 wl_user int(5) unsigned NOT NULL,
835
836 -- Key to page_namespace/page_title
837 -- Note that users may watch patches which do not exist yet,
838 -- or existed in the past but have been deleted.
839 wl_namespace int NOT NULL default '0',
840 wl_title varchar(255) binary NOT NULL default '',
841
842 -- Timestamp when user was last sent a notification e-mail;
843 -- cleared when the user visits the page.
844 -- FIXME: add proper null support etc
845 wl_notificationtimestamp varchar(14) binary NOT NULL default '0',
846
847 UNIQUE KEY (wl_user, wl_namespace, wl_title),
848 KEY namespace_title (wl_namespace,wl_title)
849
850 ) TYPE=InnoDB;", $fname );
851
852 // Fix encoding for Latin-1 upgrades, add some fields,
853 // and double article to article+talk pairs
854 $numwatched = $this->dbw->selectField( 'watchlist', 'count(*)', '', $fname );
855
856 $this->setChunkScale( $chunksize, $numwatched * 2, 'watchlist_temp', $fname );
857 $result = $this->dbr->select( 'watchlist',
858 array(
859 'wl_user',
860 'wl_namespace',
861 'wl_title' ),
862 '',
863 $fname );
864
865 $add = array();
866 while( $row = $this->dbr->fetchObject( $result ) ) {
867 $add[] = array(
868 'wl_user' => $row->wl_user,
869 'wl_namespace' => Namespace::getSubject( $row->wl_namespace ),
870 'wl_title' => $this->conv( $row->wl_title ),
871 'wl_notificationtimestamp' => '0' );
872 $this->addChunk( $add );
873
874 $add[] = array(
875 'wl_user' => $row->wl_user,
876 'wl_namespace' => Namespace::getTalk( $row->wl_namespace ),
877 'wl_title' => $this->conv( $row->wl_title ),
878 'wl_notificationtimestamp' => '0' );
879 $this->addChunk( $add );
880 }
881 $this->lastChunk( $add );
882 $this->dbr->freeResult( $result );
883
884 $this->log( 'Done converting watchlist.' );
885 $this->cleanupSwaps[] = 'watchlist';
886 }
887
888 function upgradeLogging() {
889 $tabledef = <<<ENDS
890 CREATE TABLE $1 (
891 -- Symbolic keys for the general log type and the action type
892 -- within the log. The output format will be controlled by the
893 -- action field, but only the type controls categorization.
894 log_type char(10) NOT NULL default '',
895 log_action char(10) NOT NULL default '',
896
897 -- Timestamp. Duh.
898 log_timestamp char(14) NOT NULL default '19700101000000',
899
900 -- The user who performed this action; key to user_id
901 log_user int unsigned NOT NULL default 0,
902
903 -- Key to the page affected. Where a user is the target,
904 -- this will point to the user page.
905 log_namespace int NOT NULL default 0,
906 log_title varchar(255) binary NOT NULL default '',
907
908 -- Freeform text. Interpreted as edit history comments.
909 log_comment varchar(255) NOT NULL default '',
910
911 -- LF separated list of miscellaneous parameters
912 log_params blob NOT NULL default '',
913
914 KEY type_time (log_type, log_timestamp),
915 KEY user_time (log_user, log_timestamp),
916 KEY page_time (log_namespace, log_title, log_timestamp)
917
918 ) TYPE=InnoDB
919 ENDS;
920 $fields = array(
921 'log_type' => MW_UPGRADE_COPY,
922 'log_action' => MW_UPGRADE_COPY,
923 'log_timestamp' => MW_UPGRADE_COPY,
924 'log_user' => MW_UPGRADE_COPY,
925 'log_namespace' => MW_UPGRADE_COPY,
926 'log_title' => MW_UPGRADE_ENCODE,
927 'log_comment' => MW_UPGRADE_ENCODE,
928 'log_params' => MW_UPGRADE_ENCODE );
929 $this->copyTable( 'logging', $tabledef, $fields );
930 }
931
932 function upgradeArchive() {
933 $tabledef = <<<ENDS
934 CREATE TABLE $1 (
935 ar_namespace int NOT NULL default '0',
936 ar_title varchar(255) binary NOT NULL default '',
937 ar_text mediumblob NOT NULL default '',
938
939 ar_comment tinyblob NOT NULL default '',
940 ar_user int(5) unsigned NOT NULL default '0',
941 ar_user_text varchar(255) binary NOT NULL,
942 ar_timestamp char(14) binary NOT NULL default '',
943 ar_minor_edit tinyint(1) NOT NULL default '0',
944
945 ar_flags tinyblob NOT NULL default '',
946
947 ar_rev_id int(8) unsigned,
948 ar_text_id int(8) unsigned,
949
950 KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp)
951
952 ) TYPE=InnoDB
953 ENDS;
954 $fields = array(
955 'ar_namespace' => MW_UPGRADE_COPY,
956 'ar_title' => MW_UPGRADE_ENCODE,
957 'ar_text' => MW_UPGRADE_COPY,
958 'ar_comment' => MW_UPGRADE_ENCODE,
959 'ar_user' => MW_UPGRADE_COPY,
960 'ar_user_text' => MW_UPGRADE_ENCODE,
961 'ar_timestamp' => MW_UPGRADE_COPY,
962 'ar_minor_edit' => MW_UPGRADE_COPY,
963 'ar_flags' => MW_UPGRADE_COPY,
964 'ar_rev_id' => MW_UPGRADE_NULL,
965 'ar_text_id' => MW_UPGRADE_NULL );
966 $this->copyTable( 'archive', $tabledef, $fields );
967 }
968
969 function upgradeImagelinks() {
970 global $wgUseLatin1;
971 if( $wgUseLatin1 ) {
972 $tabledef = <<<ENDS
973 CREATE TABLE $1 (
974 -- Key to page_id of the page containing the image / media link.
975 il_from int(8) unsigned NOT NULL default '0',
976
977 -- Filename of target image.
978 -- This is also the page_title of the file's description page;
979 -- all such pages are in namespace 6 (NS_IMAGE).
980 il_to varchar(255) binary NOT NULL default '',
981
982 UNIQUE KEY il_from(il_from,il_to),
983 KEY (il_to)
984
985 ) TYPE=InnoDB
986 ENDS;
987 $fields = array(
988 'il_from' => MW_UPGRADE_COPY,
989 'il_to' => MW_UPGRADE_ENCODE );
990 $this->copyTable( 'imagelinks', $tabledef, $fields );
991 }
992 }
993
994 function upgradeCategorylinks() {
995 global $wgUseLatin1;
996 if( $wgUseLatin1 ) {
997 $tabledef = <<<ENDS
998 CREATE TABLE $1 (
999 cl_from int(8) unsigned NOT NULL default '0',
1000 cl_to varchar(255) binary NOT NULL default '',
1001 cl_sortkey varchar(86) binary NOT NULL default '',
1002 cl_timestamp timestamp NOT NULL,
1003
1004 UNIQUE KEY cl_from(cl_from,cl_to),
1005 KEY cl_sortkey(cl_to,cl_sortkey),
1006 KEY cl_timestamp(cl_to,cl_timestamp)
1007 ) TYPE=InnoDB
1008 ENDS;
1009 $fields = array(
1010 'cl_from' => MW_UPGRADE_COPY,
1011 'cl_to' => MW_UPGRADE_ENCODE,
1012 'cl_sortkey' => MW_UPGRADE_ENCODE,
1013 'cl_timestamp' => MW_UPGRADE_COPY );
1014 $this->copyTable( 'categorylinks', $tabledef, $fields );
1015 }
1016 }
1017
1018 function upgradeIpblocks() {
1019 global $wgUseLatin1;
1020 if( $wgUseLatin1 ) {
1021 $tabledef = <<<ENDS
1022 CREATE TABLE $1 (
1023 ipb_id int(8) NOT NULL auto_increment,
1024 ipb_address varchar(40) binary NOT NULL default '',
1025 ipb_user int(8) unsigned NOT NULL default '0',
1026 ipb_by int(8) unsigned NOT NULL default '0',
1027 ipb_reason tinyblob NOT NULL default '',
1028 ipb_timestamp char(14) binary NOT NULL default '',
1029 ipb_auto tinyint(1) NOT NULL default '0',
1030 ipb_expiry char(14) binary NOT NULL default '',
1031
1032 PRIMARY KEY ipb_id (ipb_id),
1033 INDEX ipb_address (ipb_address),
1034 INDEX ipb_user (ipb_user)
1035
1036 ) TYPE=InnoDB
1037 ENDS;
1038 $fields = array(
1039 'ipb_id' => MW_UPGRADE_COPY,
1040 'ipb_address' => MW_UPGRADE_COPY,
1041 'ipb_user' => MW_UPGRADE_COPY,
1042 'ipb_by' => MW_UPGRADE_COPY,
1043 'ipb_reason' => MW_UPGRADE_ENCODE,
1044 'ipb_timestamp' => MW_UPGRADE_COPY,
1045 'ipb_auto' => MW_UPGRADE_COPY,
1046 'ipb_expiry' => MW_UPGRADE_COPY );
1047 $this->copyTable( 'ipblocks', $tabledef, $fields );
1048 }
1049 }
1050
1051 function upgradeRecentchanges() {
1052 // There's a format change in the namespace field
1053 $tabledef = <<<ENDS
1054 CREATE TABLE $1 (
1055 rc_id int(8) NOT NULL auto_increment,
1056 rc_timestamp varchar(14) binary NOT NULL default '',
1057 rc_cur_time varchar(14) binary NOT NULL default '',
1058
1059 rc_user int(10) unsigned NOT NULL default '0',
1060 rc_user_text varchar(255) binary NOT NULL default '',
1061
1062 rc_namespace int NOT NULL default '0',
1063 rc_title varchar(255) binary NOT NULL default '',
1064
1065 rc_comment varchar(255) binary NOT NULL default '',
1066 rc_minor tinyint(3) unsigned NOT NULL default '0',
1067
1068 rc_bot tinyint(3) unsigned NOT NULL default '0',
1069 rc_new tinyint(3) unsigned NOT NULL default '0',
1070
1071 rc_cur_id int(10) unsigned NOT NULL default '0',
1072 rc_this_oldid int(10) unsigned NOT NULL default '0',
1073 rc_last_oldid int(10) unsigned NOT NULL default '0',
1074
1075 rc_type tinyint(3) unsigned NOT NULL default '0',
1076 rc_moved_to_ns tinyint(3) unsigned NOT NULL default '0',
1077 rc_moved_to_title varchar(255) binary NOT NULL default '',
1078
1079 rc_patrolled tinyint(3) unsigned NOT NULL default '0',
1080
1081 rc_ip char(15) NOT NULL default '',
1082
1083 PRIMARY KEY rc_id (rc_id),
1084 INDEX rc_timestamp (rc_timestamp),
1085 INDEX rc_namespace_title (rc_namespace, rc_title),
1086 INDEX rc_cur_id (rc_cur_id),
1087 INDEX new_name_timestamp(rc_new,rc_namespace,rc_timestamp),
1088 INDEX rc_ip (rc_ip)
1089
1090 ) TYPE=InnoDB
1091 ENDS;
1092 $fields = array(
1093 'rc_id' => MW_UPGRADE_COPY,
1094 'rc_timestamp' => MW_UPGRADE_COPY,
1095 'rc_cur_time' => MW_UPGRADE_COPY,
1096 'rc_user' => MW_UPGRADE_COPY,
1097 'rc_user_text' => MW_UPGRADE_ENCODE,
1098 'rc_namespace' => MW_UPGRADE_COPY,
1099 'rc_title' => MW_UPGRADE_ENCODE,
1100 'rc_comment' => MW_UPGRADE_ENCODE,
1101 'rc_minor' => MW_UPGRADE_COPY,
1102 'rc_bot' => MW_UPGRADE_COPY,
1103 'rc_new' => MW_UPGRADE_COPY,
1104 'rc_cur_id' => MW_UPGRADE_COPY,
1105 'rc_this_oldid' => MW_UPGRADE_COPY,
1106 'rc_last_oldid' => MW_UPGRADE_COPY,
1107 'rc_type' => MW_UPGRADE_COPY,
1108 'rc_moved_to_ns' => MW_UPGRADE_COPY,
1109 'rc_moved_to_title' => MW_UPGRADE_ENCODE,
1110 'rc_patrolled' => MW_UPGRADE_COPY,
1111 'rc_ip' => MW_UPGRADE_COPY );
1112 $this->copyTable( 'recentchanges', $tabledef, $fields );
1113 }
1114
1115 function upgradeQuerycache() {
1116 // There's a format change in the namespace field
1117 $tabledef = <<<ENDS
1118 CREATE TABLE $1 (
1119 -- A key name, generally the base name of of the special page.
1120 qc_type char(32) NOT NULL,
1121
1122 -- Some sort of stored value. Sizes, counts...
1123 qc_value int(5) unsigned NOT NULL default '0',
1124
1125 -- Target namespace+title
1126 qc_namespace int NOT NULL default '0',
1127 qc_title char(255) binary NOT NULL default '',
1128
1129 KEY (qc_type,qc_value)
1130
1131 ) TYPE=InnoDB
1132 ENDS;
1133 $fields = array(
1134 'qc_type' => MW_UPGRADE_COPY,
1135 'qc_value' => MW_UPGRADE_COPY,
1136 'qc_namespace' => MW_UPGRADE_COPY,
1137 'qc_title' => MW_UPGRADE_ENCODE );
1138 $this->copyTable( 'querycache', $tabledef, $fields );
1139 }
1140
1141 /**
1142 * Rename all our temporary tables into final place.
1143 * We've left things in place so a read-only wiki can continue running
1144 * on the old code during all this.
1145 */
1146 function upgradeCleanup() {
1147 $this->renameTable( 'old', 'text' );
1148
1149 foreach( $this->cleanupSwaps as $table ) {
1150 $this->swap( $table );
1151 }
1152 }
1153
1154 function renameTable( $from, $to ) {
1155 $this->log( "Renaming $from to $to..." );
1156
1157 $fromtable = $this->dbw->tableName( $from );
1158 $totable = $this->dbw->tableName( $to );
1159 $this->dbw->query( "ALTER TABLE $fromtable RENAME TO $totable" );
1160 }
1161
1162 function swap( $base ) {
1163 $this->renameTable( $base, "{$base}_old" );
1164 $this->renameTable( "{$base}_temp", $base );
1165 }
1166
1167 }
1168
1169 ?>