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