3 * @addtogroup Maintenance
8 if ( !defined( 'MEDIAWIKI' ) ) {
9 echo "This file is not a valid entry point\n";
13 require_once 'convertLinks.inc';
14 require_once 'userDupes.inc';
15 require_once 'deleteDefaultMessages.php';
17 require_once( "$IP/includes/Hooks.php" );
19 $wgRenamedTables = array(
21 # array( 'group', 'groups', 'patch-rename-group.sql' ),
25 # table patch file (in maintenance/archives)
26 array( 'hitcounter', 'patch-hitcounter.sql' ),
27 array( 'querycache', 'patch-querycache.sql' ),
28 array( 'objectcache', 'patch-objectcache.sql' ),
29 array( 'categorylinks', 'patch-categorylinks.sql' ),
30 array( 'logging', 'patch-logging.sql' ),
31 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
32 array( 'transcache', 'patch-transcache.sql' ),
33 array( 'trackbacks', 'patch-trackbacks.sql' ),
34 array( 'externallinks', 'patch-externallinks.sql' ),
35 array( 'job', 'patch-job.sql' ),
36 array( 'langlinks', 'patch-langlinks.sql' ),
37 array( 'querycache_info', 'patch-querycacheinfo.sql' ),
38 array( 'filearchive', 'patch-filearchive.sql' ),
39 array( 'querycachetwo', 'patch-querycachetwo.sql' ),
40 array( 'redirect', 'patch-redirect.sql' ),
41 array( 'protected_titles', 'patch-protected_titles.sql' ),
45 # table field patch file (in maintenance/archives)
46 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
47 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
48 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
49 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
50 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
51 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
52 array( 'recentchanges', 'rc_old_len', 'patch-rc_len.sql' ),
53 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
54 array( 'user', 'user_token', 'patch-user_token.sql' ),
55 array( 'user', 'user_email_token', 'patch-user_email_token.sql' ),
56 array( 'user', 'user_registration','patch-user_registration.sql' ),
57 array( 'logging', 'log_params', 'patch-log_params.sql' ),
58 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
59 array( 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
60 array( 'page', 'page_len', 'patch-page_len.sql' ),
61 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
62 array( 'image', 'img_width', 'patch-img_width.sql' ),
63 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
64 array( 'image', 'img_media_type', 'patch-img_media_type.sql' ),
65 array( 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ),
66 array( 'interwiki', 'iw_trans', 'patch-interwiki-trans.sql' ),
67 array( 'ipblocks', 'ipb_range_start', 'patch-ipb_range_start.sql' ),
68 array( 'site_stats', 'ss_images', 'patch-ss_images.sql' ),
69 array( 'ipblocks', 'ipb_anon_only', 'patch-ipb_anon_only.sql' ),
70 array( 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
71 array( 'user', 'user_newpass_time','patch-user_newpass_time.sql' ),
72 array( 'user', 'user_editcount', 'patch-user_editcount.sql' ),
73 array( 'recentchanges', 'rc_deleted', 'patch-rc_deleted.sql' ),
74 array( 'logging', 'log_id', 'patch-log_id.sql' ),
75 array( 'logging', 'log_deleted', 'patch-log_deleted.sql' ),
76 array( 'archive', 'ar_deleted', 'patch-ar_deleted.sql' ),
77 array( 'ipblocks', 'ipb_deleted', 'patch-ipb_deleted.sql' ),
78 array( 'filearchive', 'fa_deleted', 'patch-fa_deleted.sql' ),
79 array( 'revision', 'rev_len', 'patch-rev_len.sql' ),
80 array( 'archive', 'ar_len', 'patch-ar_len.sql' ),
81 array( 'revision', 'rev_parent_id', 'patch-rev_parent_id.sql' ),
82 array( 'page_restrictions', 'pr_id', 'patch-page_restrictions_sortkey.sql' ),
83 array( 'ipblocks', 'ipb_block_email', 'patch-ipb_emailban.sql' ),
84 array( 'oldimage', 'oi_metadata', 'patch-oi_metadata.sql'),
85 array( 'archive', 'ar_page_id', 'patch-archive-page_id.sql'),
86 array( 'image', 'img_sha1', 'patch-img_sha1.sql' ),
87 array( 'ipblocks', 'ipb_by_text', 'patch-ipb_by_text.sql' ),
90 # For extensions only, should be populated via hooks
91 # $wgDBtype should be checked to specifiy the proper file
92 $wgExtNewTables = array(); // table, dir
93 $wgExtNewFields = array(); // table, column, dir
94 $wgExtPGNewFields = array(); // table, column attributes; for PostgreSQL
95 $wgExtNewIndexes = array(); // table, index, dir
97 function rename_table( $from, $to, $patch ) {
99 if ( $wgDatabase->tableExists( $from ) ) {
100 if ( $wgDatabase->tableExists( $to ) ) {
101 echo "...can't move table $from to $to, $to already exists.\n";
103 echo "Moving table $from to $to...";
104 dbsource( archive($patch), $wgDatabase );
108 // Source table does not exist
109 // Renames are done before creations, so this is typical for a new installation
114 function add_table( $name, $patch, $fullpath=false ) {
116 if ( $wgDatabase->tableExists( $name ) ) {
117 echo "...$name table already exists.\n";
119 echo "Creating $name table...";
121 dbsource( $patch, $wgDatabase );
123 dbsource( archive($patch), $wgDatabase );
129 function add_field( $table, $field, $patch, $fullpath=false ) {
131 if ( !$wgDatabase->tableExists( $table ) ) {
132 echo "...$table table does not exist, skipping new field patch\n";
133 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
134 echo "...have $field field in $table table.\n";
136 echo "Adding $field field to table $table...";
138 dbsource( $patch, $wgDatabase );
140 dbsource( archive($patch), $wgDatabase );
146 function add_index( $table, $index, $patch, $fullpath=false ) {
148 if( $wgDatabase->indexExists( $table, $index ) ) {
149 echo "...$index key already set on $table table.\n";
151 echo "Adding $index key to table $table... ";
153 dbsource( $patch, $wgDatabase );
155 dbsource( archive($patch), $wgDatabase );
161 function do_revision_updates() {
162 global $wgSoftwareRevision;
163 if ( $wgSoftwareRevision < 1001 ) {
168 function update_passwords() {
169 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
172 $fname = "Update script: update_passwords()";
173 print "\nIt appears that you need to update the user passwords in your\n" .
174 "database. If you have already done this (if you've run this update\n" .
175 "script once before, for example), doing so again will make all your\n" .
176 "user accounts inaccessible, so be sure you only do this once.\n" .
177 "Update user passwords? (yes/no)";
179 $resp = readconsole();
180 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
182 $sql = "SELECT user_id,user_password FROM user";
183 $source = $wgDatabase->query( $sql, $fname );
185 while ( $row = $wgDatabase->fetchObject( $source ) ) {
187 $oldpass = $row->user_password;
188 $newpass = md5( "{$id}-{$oldpass}" );
190 $sql = "UPDATE user SET user_password='{$newpass}' " .
191 "WHERE user_id={$id}";
192 $wgDatabase->query( $sql, $fname );
196 function do_interwiki_update() {
197 # Check that interwiki table exists; if it doesn't source it
198 global $wgDatabase, $IP;
199 if( $wgDatabase->tableExists( "interwiki" ) ) {
200 echo "...already have interwiki table\n";
203 echo "Creating interwiki table: ";
204 dbsource( archive("patch-interwiki.sql") );
206 echo "Adding default interwiki definitions: ";
207 dbsource( "$IP/maintenance/interwiki.sql" );
211 function do_index_update() {
212 # Check that proper indexes are in place
214 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
215 if( !$meta->isMultipleKey() ) {
216 echo "Updating indexes to 20031107: ";
217 dbsource( archive("patch-indexes.sql") );
221 echo "...indexes seem up to 20031107 standards\n";
225 function do_image_index_update() {
228 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
229 if( !$meta->isMultipleKey() ) {
230 echo "Updating indexes to 20050912: ";
231 dbsource( archive("patch-mimesearch-indexes.sql") );
235 echo "...indexes seem up to 20050912 standards\n";
239 function do_image_name_unique_update() {
241 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
242 echo "...image primary key already set.\n";
244 echo "Making img_name the primary key... ";
245 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
250 function do_logging_timestamp_index() {
252 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
253 echo "...timestamp key on logging already exists.\n";
255 echo "Adding timestamp key on logging table... ";
256 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
261 function do_archive_user_index() {
263 if( $wgDatabase->indexExists( 'archive', 'usertext_timestamp' ) ) {
264 echo "...usertext,timestamp key on archive already exists.\n";
266 echo "Adding usertext,timestamp key on archive table... ";
267 dbsource( archive("patch-archive-user-index.sql"), $wgDatabase );
272 function do_image_user_index() {
274 if( $wgDatabase->indexExists( 'image', 'img_usertext_timestamp' ) ) {
275 echo "...usertext,timestamp key on image already exists.\n";
277 echo "Adding usertext,timestamp key on image table... ";
278 dbsource( archive("patch-image-user-index.sql"), $wgDatabase );
283 function do_oldimage_user_index() {
285 if( $wgDatabase->indexExists( 'oldimage', 'oi_usertext_timestamp' ) ) {
286 echo "...usertext,timestamp key on oldimage already exists.\n";
288 echo "Adding usertext,timestamp key on oldimage table... ";
289 dbsource( archive("patch-oldimage-user-index.sql"), $wgDatabase );
294 function do_watchlist_update() {
296 $fname = 'do_watchlist_update';
297 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
298 echo "The watchlist table is already set up for email notification.\n";
300 echo "Adding wl_notificationtimestamp field for email notification management.";
301 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
302 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
305 # Check if we need to add talk page rows to the watchlist
306 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
307 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
308 if ( $talk != $nontalk ) {
309 echo "Adding missing watchlist talk page rows... ";
312 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
314 'wl_user' => 'wl_user',
315 'wl_namespace' => 'wl_namespace | 1',
316 'wl_title' => 'wl_title',
317 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
318 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
321 echo "...watchlist talk page rows already present\n";
325 function do_copy_newtalk_to_watchlist() {
327 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
329 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
330 $wgDatabase->tableName( 'user_newtalk' ) );
331 $num_newtalks=$wgDatabase->numRows($res);
332 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
335 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
336 $wluser = $wgDatabase->fetchObject( $res );
337 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
338 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
339 $wgDatabase->replace( 'watchlist',
340 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
341 array('wl_user' => 0,
342 'wl_namespace' => NS_USER_TALK,
343 'wl_title' => $wluser->user_ip,
344 'wl_notificationtimestamp' => '19700101000000'
345 ), 'updaters.inc::do_watchlist_update2'
348 } else { # normal users ... have user_ids
349 $user->setID($wluser->user_id);
350 $wgDatabase->replace( 'watchlist',
351 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
352 array('wl_user' => $user->getID(),
353 'wl_namespace' => NS_USER_TALK,
354 'wl_title' => $user->getName(),
355 'wl_notificationtimestamp' => '19700101000000'
356 ), 'updaters.inc::do_watchlist_update3'
364 function do_user_update() {
366 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
367 echo "User table contains old email authentication field. Dropping... ";
368 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
371 echo "...user table does not contain old email authentication field.\n";
376 * 1.4 betas were missing the 'binary' marker from logging.log_title,
377 * which causes a collation mismatch error on joins in MySQL 4.1.
379 function do_logging_encoding() {
380 global $wgDatabase, $wgDBtype;
381 if ($wgDBtype != 'mysql')
383 $logging = $wgDatabase->tableName( 'logging' );
384 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
385 $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
386 $wgDatabase->freeResult( $res );
388 if( in_array( 'binary', $flags ) ) {
389 echo "Logging table has correct title encoding.\n";
391 echo "Fixing title encoding on logging table... ";
392 dbsource( archive( 'patch-logging-title.sql' ), $wgDatabase );
397 function do_schema_restructuring() {
399 $fname="do_schema_restructuring";
400 if ( $wgDatabase->tableExists( 'page' ) ) {
401 echo "...page table already exists.\n";
403 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
404 echo wfTimestamp( TS_DB );
405 echo "......checking for duplicate entries.\n"; flush();
407 list ($cur, $old, $page, $revision, $text) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
409 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
410 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
412 if ( $wgDatabase->numRows( $rows ) > 0 ) {
413 echo wfTimestamp( TS_DB );
414 echo "......<b>Found duplicate entries</b>\n";
415 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
416 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
417 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
418 $duplicate[$row->cur_namespace] = array();
420 $duplicate[$row->cur_namespace][] = $row->cur_title;
421 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
423 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
425 foreach ( $duplicate as $ns => $titles ) {
431 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
433 foreach ( $titles as $t ) {
435 $sql .= $wgDatabase->addQuotes( $t );
438 $sql .= ', ' . $wgDatabase->addQuotes( $t );
443 # By sorting descending, the most recent entry will be the first in the list.
444 # All following entries will be deleted by the next while-loop.
445 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
447 $rows = $wgDatabase->query( $sql, $fname );
449 $prev_title = $prev_namespace = false;
452 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
453 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
454 $deleteId[] = $row->cur_id;
456 $prev_title = $row->cur_title;
457 $prev_namespace = $row->cur_namespace;
459 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
460 $rows = $wgDatabase->query( $sql, $fname );
461 echo wfTimestamp( TS_DB );
462 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
466 echo wfTimestamp( TS_DB );
467 echo "......Creating tables.\n";
468 $wgDatabase->query("CREATE TABLE $page (
469 page_id int(8) unsigned NOT NULL auto_increment,
470 page_namespace int NOT NULL,
471 page_title varchar(255) binary NOT NULL,
472 page_restrictions tinyblob NOT NULL,
473 page_counter bigint(20) unsigned NOT NULL default '0',
474 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
475 page_is_new tinyint(1) unsigned NOT NULL default '0',
476 page_random real unsigned NOT NULL,
477 page_touched char(14) binary NOT NULL default '',
478 page_latest int(8) unsigned NOT NULL,
479 page_len int(8) unsigned NOT NULL,
481 PRIMARY KEY page_id (page_id),
482 UNIQUE INDEX name_title (page_namespace,page_title),
485 ) TYPE=InnoDB", $fname );
486 $wgDatabase->query("CREATE TABLE $revision (
487 rev_id int(8) unsigned NOT NULL auto_increment,
488 rev_page int(8) unsigned NOT NULL,
489 rev_comment tinyblob NOT NULL,
490 rev_user int(5) unsigned NOT NULL default '0',
491 rev_user_text varchar(255) binary NOT NULL default '',
492 rev_timestamp char(14) binary NOT NULL default '',
493 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
494 rev_deleted tinyint(1) unsigned NOT NULL default '0',
495 rev_len int(8) unsigned,
496 rev_parent_id int(8) unsigned default NULL,
497 PRIMARY KEY rev_page_id (rev_page, rev_id),
498 UNIQUE INDEX rev_id (rev_id),
499 INDEX rev_timestamp (rev_timestamp),
500 INDEX page_timestamp (rev_page,rev_timestamp),
501 INDEX user_timestamp (rev_user,rev_timestamp),
502 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
503 ) TYPE=InnoDB", $fname );
505 echo wfTimestamp( TS_DB );
506 echo "......Locking tables.\n";
507 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
509 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
510 echo wfTimestamp( TS_DB );
511 echo "......maxold is {$maxold}\n";
513 echo wfTimestamp( TS_DB );
514 global $wgLegacySchemaConversion;
515 if( $wgLegacySchemaConversion ) {
516 // Create HistoryBlobCurStub entries.
517 // Text will be pulled from the leftover 'cur' table at runtime.
518 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
519 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
520 $cur_flags = "'object'";
522 // Copy all cur text in immediately: this may take longer but avoids
523 // having to keep an extra table around.
524 echo "......Moving text from cur.\n";
525 $cur_text = 'cur_text';
528 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
529 old_timestamp, old_minor_edit, old_flags)
530 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
531 FROM $cur", $fname );
533 echo wfTimestamp( TS_DB );
534 echo "......Setting up revision table.\n";
535 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
537 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
538 old_timestamp, old_minor_edit
539 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
541 echo wfTimestamp( TS_DB );
542 echo "......Setting up page table.\n";
543 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
544 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
545 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
546 cur_random, cur_touched, rev_id, LENGTH(cur_text)
548 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
550 echo wfTimestamp( TS_DB );
551 echo "......Unlocking tables.\n";
552 $wgDatabase->query( "UNLOCK TABLES", $fname );
554 echo wfTimestamp( TS_DB );
555 echo "......Renaming old.\n";
556 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
558 echo wfTimestamp( TS_DB );
563 function do_inverse_timestamp() {
565 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
566 echo "Removing revision.inverse_timestamp and fixing indexes... ";
567 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
570 echo "revision timestamp indexes already up to 2005-03-13\n";
574 function do_text_id() {
576 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
577 echo "...rev_text_id already in place.\n";
579 echo "Adding rev_text_id field... ";
580 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
585 function do_namespace_size() {
589 'recentchanges' => 'rc',
591 'querycache' => 'qc',
594 foreach( $tables as $table => $prefix ) {
595 do_namespace_size_on( $table, $prefix );
600 function do_namespace_size_on( $table, $prefix ) {
601 global $wgDatabase, $wgDBtype;
602 if ($wgDBtype != 'mysql')
604 $field = $prefix . '_namespace';
606 $tablename = $wgDatabase->tableName( $table );
607 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
608 $info = $wgDatabase->fetchObject( $result );
609 $wgDatabase->freeResult( $result );
611 if( substr( $info->Type, 0, 3 ) == 'int' ) {
612 echo "...$field is already a full int ($info->Type).\n";
614 echo "Promoting $field from $info->Type to int... ";
616 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
617 $wgDatabase->query( $sql );
623 function do_pagelinks_update() {
625 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
626 echo "...already have pagelinks table.\n";
628 echo "Converting links and brokenlinks tables to pagelinks... ";
629 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
633 global $wgCanonicalNamespaceNames;
634 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
636 do_pagelinks_namespace( $ns );
642 function do_pagelinks_namespace( $namespace ) {
643 global $wgDatabase, $wgContLang;
645 $ns = intval( $namespace );
646 echo "Cleaning up broken links for namespace $ns... ";
648 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
649 $name = $wgContLang->getNsText( $ns );
650 $prefix = $wgDatabase->strencode( $name );
651 $likeprefix = str_replace( '_', '\\_', $prefix);
653 $sql = "UPDATE $pagelinks
654 SET pl_namespace=$ns,
655 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
657 AND pl_title LIKE '$likeprefix:%'";
659 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
663 function do_drop_img_type() {
666 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
667 echo "Dropping unused img_type field in image table... ";
668 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
671 echo "No img_type field in image table; Good.\n";
675 function do_old_links_update() {
677 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
678 echo "Already have pagelinks; skipping old links table updates.\n";
680 convertLinks(); flush();
684 function do_user_unique_update() {
686 $duper = new UserDupes( $wgDatabase );
687 if( $duper->hasUniqueIndex() ) {
688 echo "Already have unique user_name index.\n";
690 if( !$duper->clearDupes() ) {
691 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
693 echo "Adding unique index on user_name... ";
694 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
699 function do_user_groups_update() {
700 $fname = 'do_user_groups_update';
703 if( $wgDatabase->tableExists( 'user_groups' ) ) {
704 echo "...user_groups table already exists.\n";
705 return do_user_groups_reformat();
708 echo "Adding user_groups table... ";
709 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
712 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
713 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
714 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
715 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
718 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
719 echo "*** You may need to manually configure some sysops by manipulating\n";
720 echo "*** the user_groups table.\n";
725 echo "Converting user_rights table to user_groups... ";
726 $result = $wgDatabase->select( 'user_rights',
727 array( 'ur_user', 'ur_rights' ),
728 array( "ur_rights != ''" ),
731 while( $row = $wgDatabase->fetchObject( $result ) ) {
732 $groups = array_unique(
734 explode( ',', $row->ur_rights ) ) );
736 foreach( $groups as $group ) {
737 $wgDatabase->insert( 'user_groups',
739 'ug_user' => $row->ur_user,
740 'ug_group' => $group ),
744 $wgDatabase->freeResult( $result );
748 function do_user_groups_reformat() {
749 # Check for bogus formats from previous 1.5 alpha code.
751 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
753 if( $info->type() == 'int' ) {
754 $oldug = $wgDatabase->tableName( 'user_groups' );
755 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
756 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
757 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
760 echo "Re-adding fresh user_groups table... ";
761 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
765 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
766 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
769 echo "...user_groups is in current format.\n";
774 function do_watchlist_null() {
775 # Make sure wl_notificationtimestamp can be NULL,
776 # and update old broken items.
778 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
780 if( !$info->nullable() ) {
781 echo "Making wl_notificationtimestamp nullable... ";
782 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
785 echo "...wl_notificationtimestamp is already nullable.\n";
793 function do_page_random_update() {
796 echo "Setting page_random to a random value on rows where it equals 0...";
798 $page = $wgDatabase->tableName( 'page' );
799 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
800 $rows = $wgDatabase->affectedRows();
802 echo "changed $rows rows\n";
805 function do_templatelinks_update() {
806 global $wgDatabase, $wgLoadBalancer;
807 $fname = 'do_templatelinks_update';
809 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
810 echo "...templatelinks table already exists\n";
813 echo "Creating templatelinks table...\n";
814 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
815 echo "Populating...\n";
816 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
817 // Slow, replication-friendly update
818 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
819 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
821 while ( $row = $wgDatabase->fetchObject( $res ) ) {
822 $count = ($count + 1) % 100;
824 if ( function_exists( 'wfWaitForSlaves' ) ) {
825 wfWaitForSlaves( 10 );
830 $wgDatabase->insert( 'templatelinks',
832 'tl_from' => $row->pl_from,
833 'tl_namespace' => $row->pl_namespace,
834 'tl_title' => $row->pl_title,
839 $wgDatabase->freeResult( $res );
842 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
844 'tl_from' => 'pl_from',
845 'tl_namespace' => 'pl_namespace',
846 'tl_title' => 'pl_title'
852 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
855 // Add index on ( rc_namespace, rc_user_text ) [Jul. 2006]
856 // Add index on ( rc_user_text, rc_timestamp ) [Nov. 2006]
857 function do_rc_indices_update() {
859 echo( "Checking for additional recent changes indices...\n" );
862 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
863 'rc_user_text' => 'patch-rc_user_text-index.sql',
866 foreach( $indexes as $index => $patch ) {
867 $info = $wgDatabase->indexInfo( 'recentchanges', $index, __METHOD__ );
869 echo( "...index `{$index}` not found; adding..." );
870 dbsource( archive( $patch ) );
873 echo( "...index `{$index}` seems ok.\n" );
878 function index_has_field($table, $index, $field) {
880 echo( "Checking if $table index $index includes field $field...\n" );
881 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
883 foreach($info as $row) {
884 if($row->Column_name == $field) {
885 echo( "...index $index on table $table seems to be ok\n" );
890 echo( "...index $index on table $table has no field $field; adding\n" );
894 function do_backlinking_indices_update() {
895 echo( "Checking for backlinking indices...\n" );
896 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
897 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
898 !index_has_field('imagelinks', 'il_to', 'il_from'))
900 dbsource( archive( 'patch-backlinkindexes.sql' ) );
904 function do_categorylinks_indices_update() {
905 echo( "Checking for categorylinks indices...\n" );
906 if (!index_has_field('categorylinks', 'cl_sortkey', 'cl_from'))
908 dbsource( archive( 'patch-categorylinksindex.sql' ) );
912 function do_stats_init() {
913 // Sometimes site_stats table is not properly populated.
915 echo "Checking site_stats row...";
916 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
917 if( $row === false ) {
918 echo "data is missing! rebuilding...\n";
921 require_once "$IP/maintenance/initStats.inc";
928 function purge_cache() {
930 # We can't guarantee that the user will be able to use TRUNCATE,
931 # but we know that DELETE is available to us
932 echo( "Purging caches..." );
933 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
937 function do_all_updates( $shared = false, $purge = true ) {
938 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
940 wfRunHooks('LoadExtensionSchemaUpdates');
942 $doUser = !$wgSharedDB || $shared;
944 if ($wgDBtype === 'postgres') {
945 do_postgres_updates();
950 foreach ( $wgRenamedTables as $tableRecord ) {
951 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
955 foreach ( $wgNewTables as $tableRecord ) {
956 add_table( $tableRecord[0], $tableRecord[1] );
961 foreach ( $wgNewFields as $fieldRecord ) {
962 if ( $fieldRecord[0] != 'user' || $doUser ) {
963 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
968 global $wgExtNewTables, $wgExtNewFields, $wgExtNewIndexes;
969 # Add missing extension tables
970 foreach ( $wgExtNewTables as $tableRecord ) {
971 add_table( $tableRecord[0], $tableRecord[1], true );
974 # Add missing extension fields
975 foreach ( $wgExtNewFields as $fieldRecord ) {
976 if ( $fieldRecord[0] != 'user' || $doUser ) {
977 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2], true );
981 # Add missing extension indexes
982 foreach ( $wgExtNewIndexes as $fieldRecord ) {
983 add_index( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2], true );
987 # Do schema updates which require special handling
988 do_interwiki_update(); flush();
989 do_index_update(); flush();
990 do_old_links_update(); flush();
991 do_image_name_unique_update(); flush();
992 do_watchlist_update(); flush();
994 do_user_update(); flush();
996 ###### do_copy_newtalk_to_watchlist(); flush();
997 do_logging_encoding(); flush();
999 do_schema_restructuring(); flush();
1000 do_inverse_timestamp(); flush();
1001 do_text_id(); flush();
1002 do_namespace_size(); flush();
1004 do_pagelinks_update(); flush();
1005 do_templatelinks_update(); flush(); // after pagelinks
1007 do_drop_img_type(); flush();
1010 do_user_unique_update(); flush();
1012 do_user_groups_update(); flush();
1014 do_watchlist_null(); flush();
1016 //do_image_index_update(); flush();
1018 do_logging_timestamp_index(); flush();
1020 do_page_random_update(); flush();
1022 do_rc_indices_update(); flush();
1024 do_backlinking_indices_update(); flush();
1026 do_categorylinks_indices_update(); flush();
1028 do_restrictions_update(); flush ();
1030 do_archive_user_index(); flush ();
1032 do_image_user_index(); flush ();
1034 do_oldimage_user_index(); flush ();
1036 echo "Deleting old default messages (this may take a long time!)..."; flush();
1037 deleteDefaultMessages();
1038 echo "Done\n"; flush();
1040 do_stats_init(); flush();
1048 function archive($name) {
1049 global $wgDBtype, $IP;
1050 switch ($wgDBtype) {
1052 return "$IP/maintenance/postgres/archives/$name";
1054 return "$IP/maintenance/archives/$name";
1058 function do_restrictions_update() {
1059 # Adding page_restrictions table, obsoleting page.page_restrictions.
1060 # Migrating old restrictions to new table
1061 # -- Andrew Garrett, January 2007.
1065 $name = 'page_restrictions';
1066 $patch = 'patch-page_restrictions.sql';
1067 $patch2 = 'patch-page_restrictions_sortkey.sql';
1069 if ( $wgDatabase->tableExists( $name ) ) {
1070 echo "...$name table already exists.\n";
1072 echo "Creating $name table...";
1073 dbsource( archive($patch), $wgDatabase );
1074 dbsource( archive($patch2), $wgDatabase );
1077 echo "Migrating old restrictions to new table...";
1079 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array("page_restrictions!=''", "page_restrictions!='edit=:move='"), __METHOD__ );
1083 while ($row = $wgDatabase->fetchObject($res) ) {
1084 $count = ($count + 1) % 100;
1087 if ( function_exists( 'wfWaitForSlaves' ) ) {
1088 wfWaitForSlaves( 10 );
1094 # Figure out what the restrictions are..
1095 $id = $row->page_id;
1096 $flatrestrictions = explode( ':', $row->page_restrictions );
1098 $restrictions = array ();
1099 foreach( $flatrestrictions as $restriction ) {
1100 $thisrestriction = explode( '=', $restriction, 2 );
1101 if( count( $thisrestriction ) == 1 ) {
1102 // Compatibility with old protections from before
1103 // separate move protection was added.
1104 list( $level ) = $thisrestriction;
1106 $restrictions['edit'] = $level;
1107 $restrictions['move'] = $level;
1110 list( $type, $level ) = $thisrestriction;
1112 $restrictions[$type] = $level;
1116 $wgDatabase->update( 'page', array ( 'page_restrictions' => ''), array( 'page_id' => $id ), __METHOD__ );
1120 foreach( $restrictions as $type => $level ) {
1121 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
1123 'pr_level' => $level,
1125 'pr_expiry' => 'infinity' ),
1135 pg_describe_table($table)
1137 global $wgDatabase, $wgDBmwschema;
1139 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
1140 WHERE pg_class.relnamespace = pg_namespace.oid
1141 AND attrelid=pg_class.oid AND attnum > 0
1142 AND relname=%s AND nspname=%s
1144 $res = $wgDatabase->query(sprintf($q,
1145 $wgDatabase->addQuotes($table),
1146 $wgDatabase->addQuotes($wgDBmwschema)));
1151 while ($r = $wgDatabase->fetchRow($res)) {
1161 pg_describe_index($idx)
1163 global $wgDatabase, $wgDBmwschema;
1165 // first fetch the key (which is a list of columns ords) and
1166 // the table the index applies to (an oid)
1168 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
1170 AND pg_class.relnamespace = pg_namespace.oid
1172 AND indexrelid=pg_class.oid
1174 $res = $wgDatabase->query(sprintf($q,
1175 $wgDatabase->addQuotes($wgDBmwschema),
1176 $wgDatabase->addQuotes($idx)));
1179 if (!($r = $wgDatabase->fetchRow($res))) {
1180 $wgDatabase->freeResult($res);
1185 $relid = intval($r[1]);
1186 $indkeys = explode(" ", $indkey);
1187 $wgDatabase->freeResult($res);
1189 $colnames = array();
1190 foreach ($indkeys as $rid) {
1192 SELECT attname FROM pg_class, pg_attribute
1193 WHERE attrelid=$relid
1195 AND attrelid=pg_class.oid
1197 $r2 = $wgDatabase->query(sprintf($query, $rid));
1200 if (!($row2 = $wgDatabase->fetchRow($r2))) {
1201 $wgDatabase->freeResult($r2);
1204 $colnames[] = $row2[0];
1205 $wgDatabase->freeResult($r2);
1212 pg_index_exists($table, $index)
1214 global $wgDatabase, $wgDBmwschema;
1215 $exists = $wgDatabase->selectField("pg_indexes", "indexname",
1216 array( "indexname" => $index,
1217 "tablename" => $table,
1218 "schemaname" => $wgDBmwschema));
1219 return $exists === $index;
1223 pg_fkey_deltype($fkey)
1225 global $wgDatabase, $wgDBmwschema;
1227 SELECT confdeltype FROM pg_constraint, pg_namespace
1228 WHERE connamespace=pg_namespace.oid
1232 $r = $wgDatabase->query(sprintf($q,
1233 $wgDatabase->addQuotes($wgDBmwschema),
1234 $wgDatabase->addQuotes($fkey)));
1235 if (!($row = $wgDatabase->fetchRow($r)))
1241 pg_rule_def($table, $rule)
1243 global $wgDatabase, $wgDBmwschema;
1245 SELECT definition FROM pg_rules
1246 WHERE schemaname = %s
1250 $r = $wgDatabase->query(sprintf($q,
1251 $wgDatabase->addQuotes($wgDBmwschema),
1252 $wgDatabase->addQuotes($table),
1253 $wgDatabase->addQuotes($rule)));
1254 $row = $wgDatabase->fetchRow($r);
1258 $wgDatabase->freeResult($r);
1262 function do_postgres_updates() {
1263 global $wgDatabase, $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgShowExceptionDetails, $wgDBuser;
1265 ## Gather version numbers in case we need them
1266 $version = $wgDatabase->getServerVersion(); ## long string
1267 $numver = $wgDatabase->numeric_version; ## X.Y e.g. 8.3
1269 $wgShowExceptionDetails = 1;
1271 # Just in case their LocalSettings.php does not have this:
1272 if ( !isset( $wgDBmwschema ))
1273 $wgDBmwschema = 'mediawiki';
1275 # Verify that this user is configured correctly
1276 $safeuser = $wgDatabase->addQuotes($wgDBuser);
1277 $SQL = "SELECT array_to_string(useconfig,'*') FROM pg_catalog.pg_user WHERE usename = $safeuser";
1278 $config = pg_fetch_result( $wgDatabase->doQuery( $SQL ), 0, 0 );
1280 foreach( explode( '*', $config ) as $c ) {
1281 list( $x,$y ) = explode( '=', $c );
1285 if( $wgDBmwschema === 'mediawiki' ) {
1286 if (!array_key_exists( 'search_path', $conf ) or strpos( $conf['search_path'],$wgDBmwschema ) === false ) {
1287 echo "Adding in schema \"$wgDBmwschema\" to search_path for user \"$wgDBuser\"\n";
1288 $newpath[$wgDBmwschema] = 1;
1291 if( !array_key_exists( 'search_path', $conf ) or strpos( $conf['search_path'],$wgDBts2schema ) === false ) {
1292 echo "Adding in schema \"$wgDBts2schema\" to search_path for user \"$wgDBuser\"\n";
1293 $newpath[$wgDBts2schema] = 1;
1295 $searchpath = implode( ',', array_keys( $newpath ) );
1296 if( strlen( $searchpath ) ) {
1297 $wgDatabase->doQuery( "ALTER USER $wgDBuser SET search_path = $searchpath" );
1298 $wgDatabase->doQuery( "SET search_path = $searchpath" );
1301 $path = $conf['search_path'];
1302 echo "... search_path for user \"$wgDBuser\" looks correct ($path)\n";
1305 'client_min_messages' => 'error',
1306 'DateStyle' => 'ISO, YMD',
1309 foreach( array_keys( $goodconf ) AS $key ) {
1310 $value = $goodconf[$key];
1311 if( !array_key_exists( $key, $conf ) or $conf[$key] !== $value ) {
1312 echo "Setting $key to '$value' for user \"$wgDBuser\"\n";
1313 $wgDatabase->doQuery( "ALTER USER $wgDBuser SET $key = '$value'" );
1314 $wgDatabase->doQuery( "SET $key = '$value'" );
1317 echo "... default value of \"$key\" is correctly set to \"$value\" for user \"$wgDBuser\"\n";
1321 $newsequences = array(
1327 array("mediawiki_version", "patch-mediawiki_version.sql"),
1328 array("mwuser", "patch-mwuser.sql"),
1329 array("pagecontent", "patch-pagecontent.sql"),
1330 array("querycachetwo", "patch-querycachetwo.sql"),
1331 array("page_restrictions", "patch-page_restrictions.sql"),
1332 array("profiling", "patch-profiling.sql"),
1333 array("protected_titles", "patch-protected_titles.sql"),
1334 array("redirect", "patch-redirect.sql"),
1338 array("archive", "ar_deleted", "SMALLINT NOT NULL DEFAULT 0"),
1339 array("archive", "ar_len", "INTEGER"),
1340 array("archive", "ar_page_id", "INTEGER"),
1341 array("image", "img_sha1", "TEXT NOT NULL DEFAULT ''"),
1342 array("ipblocks", "ipb_anon_only", "CHAR NOT NULL DEFAULT '0'"),
1343 array("ipblocks", "ipb_by_text", "TEXT NOT NULL DEFAULT ''"),
1344 array("ipblocks", "ipb_block_email", "CHAR NOT NULL DEFAULT '0'"),
1345 array("ipblocks", "ipb_create_account", "CHAR NOT NULL DEFAULT '1'"),
1346 array("ipblocks", "ipb_deleted", "SMALLINT NOT NULL DEFAULT 0"),
1347 array("ipblocks", "ipb_enable_autoblock", "CHAR NOT NULL DEFAULT '1'"),
1348 array("filearchive", "fa_deleted", "SMALLINT NOT NULL DEFAULT 0"),
1349 array("logging", "log_deleted", "SMALLINT NOT NULL DEFAULT 0"),
1350 array("logging", "log_id", "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('log_log_id_seq')"),
1351 array("logging", "log_params", "TEXT"),
1352 array("mwuser", "user_editcount", "INTEGER"),
1353 array("mwuser", "user_newpass_time", "TIMESTAMPTZ"),
1354 array("oldimage", "oi_deleted", "SMALLINT NOT NULL DEFAULT 0"),
1355 array("oldimage", "oi_metadata", "BYTEA NOT NULL DEFAULT ''"),
1356 array("oldimage", "oi_media_type", "TEXT"),
1357 array("oldimage", "oi_major_mime", "TEXT NOT NULL DEFAULT 'unknown'"),
1358 array("oldimage", "oi_minor_mime", "TEXT NOT NULL DEFAULT 'unknown'"),
1359 array("oldimage", "oi_sha1", "TEXT NOT NULL DEFAULT ''"),
1360 array("page_restrictions", "pr_id", "INTEGER NOT NULL UNIQUE DEFAULT nextval('pr_id_val')"),
1361 array("recentchanges", "rc_deleted", "SMALLINT NOT NULL DEFAULT 0"),
1362 array("recentchanges", "rc_log_action", "TEXT"),
1363 array("recentchanges", "rc_log_type", "TEXT"),
1364 array("recentchanges", "rc_logid", "INTEGER NOT NULL DEFAULT 0"),
1365 array("recentchanges", "rc_new_len", "INTEGER"),
1366 array("recentchanges", "rc_old_len", "INTEGER"),
1367 array("recentchanges", "rc_params", "TEXT"),
1368 array("revision", "rev_len", "INTEGER"),
1369 array("revision", "rev_deleted", "SMALLINT NOT NULL DEFAULT 0"),
1373 # table, column, desired type, USING clause if needed (with new default if needed)
1374 $typechanges = array(
1375 array("archive", "ar_deleted", "smallint", ""),
1376 array("archive", "ar_minor_edit", "smallint", "ar_minor_edit::smallint DEFAULT 0"),
1377 array("filearchive", "fa_deleted", "smallint", ""),
1378 array("filearchive", "fa_height", "integer", ""),
1379 array("filearchive", "fa_metadata", "bytea", "decode(fa_metadata,'escape')"),
1380 array("filearchive", "fa_size", "integer", ""),
1381 array("filearchive", "fa_width", "integer", ""),
1382 array("filearchive", "fa_storage_group","text", ""),
1383 array("filearchive", "fa_storage_key", "text", ""),
1384 array("image", "img_metadata", "bytea", "decode(img_metadata,'escape')"),
1385 array("image", "img_size", "integer", ""),
1386 array("image", "img_width", "integer", ""),
1387 array("image", "img_height", "integer", ""),
1388 array("interwiki", "iw_local", "smallint", "iw_local::smallint DEFAULT 0"),
1389 array("interwiki", "iw_trans", "smallint", "iw_trans::smallint DEFAULT 0"),
1390 array("ipblocks", "ipb_auto", "smallint", "ipb_auto::smallint DEFAULT 0"),
1391 array("ipblocks", "ipb_anon_only", "smallint", "ipb_anon_only::smallint DEFAULT 0"),
1392 array("ipblocks", "ipb_create_account", "smallint", "ipb_create_account::smallint DEFAULT 1"),
1393 array("ipblocks", "ipb_enable_autoblock", "smallint", "ipb_enable_autoblock::smallint DEFAULT 1"),
1394 array("ipblocks", "ipb_block_email", "smallint", "ipb_block_email::smallint DEFAULT 0"),
1395 array("ipblocks", "ipb_address", "text", "ipb_address::text"),
1396 array("ipblocks", "ipb_deleted", "smallint", ""),
1397 array("math", "math_inputhash", "bytea", "decode(math_inputhash,'escape')"),
1398 array("math", "math_outputhash", "bytea", "decode(math_outputhash,'escape')"),
1399 array("mwuser", "user_token", "text", ""),
1400 array("mwuser", "user_email_token","text", ""),
1401 array("objectcache", "keyname", "text", ""),
1402 array("oldimage", "oi_height", "integer", ""),
1403 array("oldimage", "oi_size", "integer", ""),
1404 array("oldimage", "oi_width", "integer", ""),
1405 array("page", "page_is_redirect","smallint", "page_is_redirect::smallint DEFAULT 0"),
1406 array("page", "page_is_new", "smallint", "page_is_new::smallint DEFAULT 0"),
1407 array("querycache", "qc_value", "integer", ""),
1408 array("querycachetwo","qcc_value", "integer", ""),
1409 array("recentchanges","rc_bot", "smallint", "rc_bot::smallint DEFAULT 0"),
1410 array("recentchanges","rc_deleted", "smallint", ""),
1411 array("recentchanges","rc_minor", "smallint", "rc_minor::smallint DEFAULT 0"),
1412 array("recentchanges","rc_new", "smallint", "rc_new::smallint DEFAULT 0"),
1413 array("recentchanges","rc_type", "smallint", "rc_type::smallint DEFAULT 0"),
1414 array("recentchanges","rc_patrolled", "smallint", "rc_patrolled::smallint DEFAULT 0"),
1415 array("revision", "rev_minor_edit", "smallint", "rev_minor_edit::smallint DEFAULT 0"),
1416 array("templatelinks","tl_namespace", "smallint", "tl_namespace::smallint"),
1417 array("user_newtalk", "user_ip", "text", "host(user_ip)"),
1420 $newindexes = array(
1421 array("archive", "archive_user_text", "(ar_user_text)"),
1422 array("image", "img_sha1", "(img_sha1)"),
1423 array("oldimage", "oi_sha1", "(oi_sha1)"),
1424 array("revision", "rev_text_id_idx", "(rev_text_id)"),
1430 foreach ($newsequences as $ns) {
1431 if ($wgDatabase->sequenceExists($ns)) {
1432 echo "... sequence \"$ns\" already exists\n";
1436 echo "Creating sequence \"$ns\"\n";
1437 $wgDatabase->query("CREATE SEQUENCE $ns");
1440 foreach ($newtables as $nt) {
1441 if ($wgDatabase->tableExists($nt[0])) {
1442 echo "... table \"$nt[0]\" already exists\n";
1446 echo "Creating table \"$nt[0]\"\n";
1447 dbsource(archive($nt[1]));
1450 ## Needed before newcols
1451 if ($wgDatabase->tableExists("archive2")) {
1452 echo "Converting \"archive2\" back to normal archive table\n";
1453 if ($wgDatabase->ruleExists("archive", "archive_insert")) {
1454 echo "Dropping rule \"archive_insert\"\n";
1455 $wgDatabase->query("DROP RULE archive_insert ON archive");
1457 if ($wgDatabase->ruleExists("archive", "archive_delete")) {
1458 echo "Dropping rule \"archive_delete\"\n";
1459 $wgDatabase->query("DROP RULE archive_delete ON archive");
1461 dbsource(archive("patch-remove-archive2.sql"));
1464 echo "... obsolete table \"archive2\" does not exist\n";
1466 foreach ($newcols as $nc) {
1467 $fi = $wgDatabase->fieldInfo($nc[0], $nc[1]);
1468 if (!is_null($fi)) {
1469 echo "... column \"$nc[0].$nc[1]\" already exists\n";
1473 echo "Adding column \"$nc[0].$nc[1]\"\n";
1474 $wgDatabase->query("ALTER TABLE $nc[0] ADD $nc[1] $nc[2]");
1477 foreach ($typechanges as $tc) {
1478 $fi = $wgDatabase->fieldInfo($tc[0], $tc[1]);
1480 echo "... error: expected column $tc[0].$tc[1] to exist\n";
1484 if ($fi->type() === $tc[2])
1485 echo "... column \"$tc[0].$tc[1]\" is already of type \"$tc[2]\"\n";
1487 echo "Changing column type of \"$tc[0].$tc[1]\" from \"{$fi->type()}\" to \"$tc[2]\"\n";
1488 $sql = "ALTER TABLE $tc[0] ALTER $tc[1] TYPE $tc[2]";
1489 if (strlen($tc[3])) {
1491 if (preg_match( '/DEFAULT (.+)/', $tc[3], $default)) {
1492 $sqldef = "ALTER TABLE $tc[0] ALTER $tc[1] SET DEFAULT $default[1]";
1493 $wgDatabase->query($sqldef);
1494 $tc[3] = preg_replace( '/\s*DEFAULT .+/', '', $tc[3]);
1496 $sql .= " USING $tc[3]";
1498 $sql .= ";\nCOMMIT;\n";
1499 $wgDatabase->query($sql);
1503 if ($wgDatabase->fieldInfo('oldimage','oi_deleted')->type() !== 'smallint') {
1504 echo "Changing \"oldimage.oi_deleted\" to type \"smallint\"\n";
1505 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
1506 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
1507 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
1510 echo "... column \"oldimage.oi_deleted\" is already of type \"smallint\"\n";
1513 foreach ($newindexes as $ni) {
1514 if (pg_index_exists($ni[0], $ni[1])) {
1515 echo "... index \"$ni[1]\" on table \"$ni[0]\" already exists\n";
1518 echo "Creating index \"$ni[1]\" on table \"$ni[0]\" $ni[2]\n";
1519 $wgDatabase->query( "CREATE INDEX $ni[1] ON $ni[0] $ni[2]" );
1522 foreach ($newrules as $nr) {
1523 if ($wgDatabase->ruleExists($nr[0], $nr[1])) {
1524 echo "... rule \"$nr[1]\" on table \"$nr[0]\" already exists\n";
1527 echo "Adding rule \"$nr[1]\" to table \"$nr[0]\"\n";
1528 dbsource(archive($nr[2]));
1531 if ($wgDatabase->hasConstraint("oldimage_oi_name_fkey")) {
1532 echo "Making foriegn key on table \"oldimage\" (to image) a cascade delete\n";
1533 $wgDatabase->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
1534 $wgDatabase->query( "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascade ".
1535 "FOREIGN KEY (oi_name) REFERENCES image(img_name) ON DELETE CASCADE" );
1538 echo "... table \"oldimage\" has correct cascade delete foreign key to image\n";
1540 if (!$wgDatabase->triggerExists("page", "page_deleted")) {
1541 echo "Adding function and trigger \"page_deleted\" to table \"page\"\n";
1542 dbsource(archive('patch-page_deleted.sql'));
1545 echo "... table \"page\" has \"page_deleted\" trigger\n";
1547 $fi = $wgDatabase->fieldInfo("recentchanges", "rc_cur_id");
1548 if (!$fi->nullable()) {
1549 echo "Removing NOT NULL constraint from \"recentchanges.rc_cur_id\"\n";
1550 dbsource(archive('patch-rc_cur_id-not-null.sql'));
1553 echo "... column \"recentchanges.rc_cur_id\" has a NOT NULL constraint\n";
1555 $pu = pg_describe_index("pagelink_unique");
1556 if (!is_null($pu) && ($pu[0] != "pl_from" || $pu[1] != "pl_namespace" || $pu[2] != "pl_title")) {
1557 echo "Dropping obsolete version of index \"pagelink_unique index\"\n";
1558 $wgDatabase->query("DROP INDEX pagelink_unique");
1562 echo "... obsolete version of index \"pagelink_unique index\" does not exist\n";
1565 echo "Creating index \"pagelink_unique index\"\n";
1566 $wgDatabase->query("CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)");
1569 echo "... index \"pagelink_unique_index\" aready exists\n";
1571 if (pg_fkey_deltype("revision_rev_user_fkey") == 'r') {
1572 echo "... constraint \"revision_rev_user_fkey\" is ON DELETE RESTRICT\n";
1575 echo "Changing constraint \"revision_rev_user_fkey\" to ON DELETE RESTRICT\n";
1576 dbsource(archive('patch-revision_rev_user_fkey.sql'));
1579 global $wgExtNewTables, $wgExtPGNewFields, $wgExtNewIndexes;
1580 # Add missing extension tables
1581 foreach ( $wgExtNewTables as $nt ) {
1582 if ($wgDatabase->tableExists($nt[0])) {
1583 echo "... table \"$nt[0]\" already exists\n";
1586 echo "Creating table \"$nt[0]\"\n";
1589 # Add missing extension fields
1590 foreach ( $wgExtPGNewFields as $nc ) {
1591 $fi = $wgDatabase->fieldInfo($nc[0], $nc[1]);
1592 if (!is_null($fi)) {
1593 echo "... column \"$nc[0].$nc[1]\" already exists\n";
1596 echo "Adding column \"$nc[0].$nc[1]\"\n";
1597 $wgDatabase->query( "ALTER TABLE $nc[0] ADD $nc[1] $nc[2]" );
1599 # Add missing extension indexes
1600 foreach ( $wgExtNewIndexes as $ni ) {
1601 if (pg_index_exists($ni[0], $ni[1])) {
1602 echo "... index \"$ni[1]\" on table \"$ni[0]\" already exists\n";
1605 echo "Creating index \"$ni[1]\" on table \"$ni[0]\"\n";
1609 # Tweak the page_title tsearch2 trigger to filter out slashes
1610 # This is create or replace, so harmless to call if not needed
1611 dbsource(archive('patch-ts2pagetitle.sql'));
1613 ## If the server is 8.3 or higher, rewrite teh tsearch2 triggers
1614 ## in case they have the old 'default' versions
1615 if ( $numver >= 8.3 )
1616 dbsource(archive('patch-tsearch2funcs.sql'));