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