7 if ( !defined( 'MEDIAWIKI' ) ) {
8 echo "This file is not a valid entry point\n";
12 require_once 'convertLinks.inc';
13 require_once 'userDupes.inc';
15 require_once( "$IP/includes/Hooks.php" );
18 * @deprecated. Do not use, ever.
23 # For extensions only, should be populated via hooks
24 # $wgDBtype should be checked to specifiy the proper file
25 $wgExtNewTables = array(); // table, dir
26 $wgExtNewFields = array(); // table, column, dir
27 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
28 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
29 $wgExtNewIndexes = array(); // table, index, dir
30 $wgExtModifiedFields = array(); // table, index, dir
32 # Helper function: check if the given key is present in the updatelog table.
33 # Obviously, only use this for updates that occur after the updatelog table was
35 function update_row_exists( $key ) {
36 $dbr = wfGetDB( DB_SLAVE );
37 $row = $dbr->selectRow(
40 array( 'ul_key' => $key ),
46 function modify_field( $table, $field, $patch, $fullpath = false ) {
48 if ( !$wgDatabase->tableExists( $table ) ) {
49 wfOut( "...$table table does not exist, skipping modify field patch\n" );
50 } elseif ( ! $wgDatabase->fieldExists( $table, $field ) ) {
51 wfOut( "...$field field does not exist in $table table, skipping modify field patch\n" );
53 wfOut( "Modifying $field field of table $table..." );
55 $wgDatabase->sourceFile( $patch );
57 $wgDatabase->sourceFile( archive( $patch ) );
63 function add_field( $table, $field, $patch, $fullpath = false ) {
65 if ( !$wgDatabase->tableExists( $table ) ) {
66 wfOut( "...$table table does not exist, skipping new field patch\n" );
67 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
68 wfOut( "...have $field field in $table table.\n" );
70 wfOut( "Adding $field field to table $table..." );
72 $wgDatabase->sourceFile( $patch );
74 $wgDatabase->sourceFile( archive( $patch ) );
80 function add_index( $table, $index, $patch, $fullpath = false ) {
82 if ( $wgDatabase->indexExists( $table, $index ) ) {
83 wfOut( "...$index key already set on $table table.\n" );
85 wfOut( "Adding $index key to table $table... " );
87 $wgDatabase->sourceFile( $patch );
89 $wgDatabase->sourceFile( archive( $patch ) );
95 function drop_index_if_exists( $table, $index, $patch, $fullpath = false ) {
97 if ( $wgDatabase->indexExists( $table, $index ) ) {
98 wfOut( "Dropping $index from table $table... " );
100 $wgDatabase->sourceFile( $patch );
102 $wgDatabase->sourceFile( archive( $patch ) );
106 wfOut( "...$index doesn't exist.\n" );
110 function do_interwiki_update() {
111 # Check that interwiki table exists; if it doesn't source it
112 global $wgDatabase, $IP;
113 if ( $wgDatabase->tableExists( "interwiki" ) ) {
114 wfOut( "...already have interwiki table\n" );
117 wfOut( "Creating interwiki table: " );
118 $wgDatabase->sourceFile( archive( "patch-interwiki.sql" ) );
120 wfOut( "Adding default interwiki definitions: " );
121 $wgDatabase->sourceFile( "$IP/maintenance/interwiki.sql" );
125 function do_index_update() {
126 # Check that proper indexes are in place
128 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
129 if ( !$meta->isMultipleKey() ) {
130 wfOut( "Updating indexes to 20031107: " );
131 $wgDatabase->sourceFile( archive( "patch-indexes.sql" ) );
135 wfOut( "...indexes seem up to 20031107 standards\n" );
139 function do_image_index_update() {
142 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
143 if ( !$meta->isMultipleKey() ) {
144 wfOut( "Updating indexes to 20050912: " );
145 $wgDatabase->sourceFile( archive( "patch-mimesearch-indexes.sql" ) );
149 wfOut( "...indexes seem up to 20050912 standards\n" );
153 function do_image_name_unique_update() {
155 if ( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
156 wfOut( "...image primary key already set.\n" );
158 wfOut( "Making img_name the primary key... " );
159 $wgDatabase->sourceFile( archive( "patch-image_name_primary.sql" ) );
164 function do_logging_timestamp_index() {
166 if ( $wgDatabase->indexExists( 'logging', 'times' ) ) {
167 wfOut( "...timestamp key on logging already exists.\n" );
169 wfOut( "Adding timestamp key on logging table... " );
170 $wgDatabase->sourceFile( archive( "patch-logging-times-index.sql" ) );
175 function do_archive_user_index() {
177 if ( $wgDatabase->indexExists( 'archive', 'usertext_timestamp' ) ) {
178 wfOut( "...usertext,timestamp key on archive already exists.\n" );
180 wfOut( "Adding usertext,timestamp key on archive table... " );
181 $wgDatabase->sourceFile( archive( "patch-archive-user-index.sql" ) );
186 function do_image_user_index() {
188 if ( $wgDatabase->indexExists( 'image', 'img_usertext_timestamp' ) ) {
189 wfOut( "...usertext,timestamp key on image already exists.\n" );
191 wfOut( "Adding usertext,timestamp key on image table... " );
192 $wgDatabase->sourceFile( archive( "patch-image-user-index.sql" ) );
197 function do_oldimage_user_index() {
199 if ( $wgDatabase->indexExists( 'oldimage', 'oi_usertext_timestamp' ) ) {
200 wfOut( "...usertext,timestamp key on oldimage already exists.\n" );
202 wfOut( "Adding usertext,timestamp key on oldimage table... " );
203 $wgDatabase->sourceFile( archive( "patch-oldimage-user-index.sql" ) );
208 function do_watchlist_update() {
210 $fname = 'do_watchlist_update';
211 if ( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
212 wfOut( "...the watchlist table is already set up for email notification.\n" );
214 wfOut( "Adding wl_notificationtimestamp field for email notification management." );
215 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
216 $wgDatabase->sourceFile( archive( 'patch-email-notification.sql' ) );
219 # Check if we need to add talk page rows to the watchlist
220 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
221 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
222 if ( $talk != $nontalk ) {
223 wfOut( "Adding missing watchlist talk page rows... " );
226 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
228 'wl_user' => 'wl_user',
229 'wl_namespace' => 'wl_namespace | 1',
230 'wl_title' => 'wl_title',
231 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
232 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
235 wfOut( "...watchlist talk page rows already present\n" );
239 function do_copy_newtalk_to_watchlist() {
242 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
243 $wgDatabase->tableName( 'user_newtalk' ) );
244 $num_newtalks = $wgDatabase->numRows( $res );
245 wfOut( "Now converting $num_newtalks user_newtalk entries to watchlist table entries ... \n" );
248 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
249 $wluser = $wgDatabase->fetchObject( $res );
250 if ( $wluser->user_id == 0 ) { # anonymous users ... have IP numbers as "names"
251 if ( $user->isIP( $wluser->user_ip ) ) { # do only if it really looks like an IP number (double checked)
252 $wgDatabase->replace( 'watchlist',
253 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
254 array( 'wl_user' => 0,
255 'wl_namespace' => NS_USER_TALK,
256 'wl_title' => $wluser->user_ip,
257 'wl_notificationtimestamp' => '19700101000000'
258 ), 'updaters.inc::do_watchlist_update2'
261 } else { # normal users ... have user_ids
262 $user->setID( $wluser->user_id );
263 $wgDatabase->replace( 'watchlist',
264 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
265 array( 'wl_user' => $user->getID(),
266 'wl_namespace' => NS_USER_TALK,
267 'wl_title' => $user->getName(),
268 'wl_notificationtimestamp' => '19700101000000'
269 ), 'updaters.inc::do_watchlist_update3'
276 function do_user_update() {
278 if ( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
279 wfOut( "User table contains old email authentication field. Dropping... " );
280 $wgDatabase->sourceFile( archive( 'patch-email-authentication.sql' ) );
283 wfOut( "...user table does not contain old email authentication field.\n" );
288 * 1.4 betas were missing the 'binary' marker from logging.log_title,
289 * which causes a collation mismatch error on joins in MySQL 4.1.
291 function check_bin( $table, $field, $patchFile ) {
292 global $wgDatabase, $wgDBtype;
293 if ( $wgDBtype != 'mysql' )
295 $tableName = $wgDatabase->tableName( $table );
296 $res = $wgDatabase->query( "SELECT $field FROM $tableName LIMIT 0" );
297 $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
299 if ( in_array( 'binary', $flags ) ) {
300 wfOut( "...$table table has correct $field encoding.\n" );
302 wfOut( "Fixing $field encoding on $table table... " );
303 $wgDatabase->sourceFile( archive( $patchFile ) );
308 function do_schema_restructuring() {
310 $fname = "do_schema_restructuring";
311 if ( $wgDatabase->tableExists( 'page' ) ) {
312 wfOut( "...page table already exists.\n" );
314 wfOut( "...converting from cur/old to page/revision/text DB structure.\n" );
315 wfOut( wfTimestamp( TS_DB ) );
316 wfOut( "......checking for duplicate entries.\n" );
318 list ( $cur, $old, $page, $revision, $text ) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
320 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
321 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
323 if ( $wgDatabase->numRows( $rows ) > 0 ) {
324 wfOut( wfTimestamp( TS_DB ) );
325 wfOut( "......<b>Found duplicate entries</b>\n" );
326 wfOut( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
327 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
328 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
329 $duplicate[$row->cur_namespace] = array();
331 $duplicate[$row->cur_namespace][] = $row->cur_title;
332 wfOut( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
334 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
336 foreach ( $duplicate as $ns => $titles ) {
342 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
344 foreach ( $titles as $t ) {
346 $sql .= $wgDatabase->addQuotes( $t );
349 $sql .= ', ' . $wgDatabase->addQuotes( $t );
354 # By sorting descending, the most recent entry will be the first in the list.
355 # All following entries will be deleted by the next while-loop.
356 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
358 $rows = $wgDatabase->query( $sql, $fname );
360 $prev_title = $prev_namespace = false;
363 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
364 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
365 $deleteId[] = $row->cur_id;
367 $prev_title = $row->cur_title;
368 $prev_namespace = $row->cur_namespace;
370 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
371 $rows = $wgDatabase->query( $sql, $fname );
372 wfOut( wfTimestamp( TS_DB ) );
373 wfOut( "......<b>Deleted</b> " . $wgDatabase->affectedRows() . " records.\n" );
377 wfOut( wfTimestamp( TS_DB ) );
378 wfOut( "......Creating tables.\n" );
379 $wgDatabase->query( "CREATE TABLE $page (
380 page_id int(8) unsigned NOT NULL auto_increment,
381 page_namespace int NOT NULL,
382 page_title varchar(255) binary NOT NULL,
383 page_restrictions tinyblob NOT NULL,
384 page_counter bigint(20) unsigned NOT NULL default '0',
385 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
386 page_is_new tinyint(1) unsigned NOT NULL default '0',
387 page_random real unsigned NOT NULL,
388 page_touched char(14) binary NOT NULL default '',
389 page_latest int(8) unsigned NOT NULL,
390 page_len int(8) unsigned NOT NULL,
392 PRIMARY KEY page_id (page_id),
393 UNIQUE INDEX name_title (page_namespace,page_title),
396 ) ENGINE=InnoDB", $fname );
397 $wgDatabase->query( "CREATE TABLE $revision (
398 rev_id int(8) unsigned NOT NULL auto_increment,
399 rev_page int(8) unsigned NOT NULL,
400 rev_comment tinyblob NOT NULL,
401 rev_user int(5) unsigned NOT NULL default '0',
402 rev_user_text varchar(255) binary NOT NULL default '',
403 rev_timestamp char(14) binary NOT NULL default '',
404 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
405 rev_deleted tinyint(1) unsigned NOT NULL default '0',
406 rev_len int(8) unsigned,
407 rev_parent_id int(8) unsigned default NULL,
408 PRIMARY KEY rev_page_id (rev_page, rev_id),
409 UNIQUE INDEX rev_id (rev_id),
410 INDEX rev_timestamp (rev_timestamp),
411 INDEX page_timestamp (rev_page,rev_timestamp),
412 INDEX user_timestamp (rev_user,rev_timestamp),
413 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
414 ) ENGINE=InnoDB", $fname );
416 wfOut( wfTimestamp( TS_DB ) );
417 wfOut( "......Locking tables.\n" );
418 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
420 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
421 wfOut( wfTimestamp( TS_DB ) );
422 wfOut( "......maxold is {$maxold}\n" );
424 wfOut( wfTimestamp( TS_DB ) );
425 global $wgLegacySchemaConversion;
426 if ( $wgLegacySchemaConversion ) {
427 // Create HistoryBlobCurStub entries.
428 // Text will be pulled from the leftover 'cur' table at runtime.
429 wfOut( "......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'";
433 // Copy all cur text in immediately: this may take longer but avoids
434 // having to keep an extra table around.
435 wfOut( "......Moving text from cur.\n" );
436 $cur_text = 'cur_text';
439 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
440 old_timestamp, old_minor_edit, old_flags)
441 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
442 FROM $cur", $fname );
444 wfOut( wfTimestamp( TS_DB ) );
445 wfOut( "......Setting up revision table.\n" );
446 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
448 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
449 old_timestamp, old_minor_edit
450 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
452 wfOut( wfTimestamp( TS_DB ) );
453 wfOut( "......Setting up page table.\n" );
454 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
455 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
456 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
457 cur_random, cur_touched, rev_id, LENGTH(cur_text)
459 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
461 wfOut( wfTimestamp( TS_DB ) );
462 wfOut( "......Unlocking tables.\n" );
463 $wgDatabase->query( "UNLOCK TABLES", $fname );
465 wfOut( wfTimestamp( TS_DB ) );
466 wfOut( "......Renaming old.\n" );
467 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
469 wfOut( wfTimestamp( TS_DB ) );
470 wfOut( "...done.\n" );
474 function do_inverse_timestamp() {
476 if ( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
477 wfOut( "Removing revision.inverse_timestamp and fixing indexes... " );
478 $wgDatabase->sourceFile( archive( 'patch-inverse_timestamp.sql' ) );
481 wfOut( "...revision timestamp indexes already up to 2005-03-13\n" );
485 function do_text_id() {
487 if ( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
488 wfOut( "...rev_text_id already in place.\n" );
490 wfOut( "Adding rev_text_id field... " );
491 $wgDatabase->sourceFile( archive( 'patch-rev_text_id.sql' ) );
496 function do_pagelinks_update() {
498 if ( $wgDatabase->tableExists( 'pagelinks' ) ) {
499 wfOut( "...already have pagelinks table.\n" );
501 wfOut( "Converting links and brokenlinks tables to pagelinks... " );
502 $wgDatabase->sourceFile( archive( 'patch-pagelinks.sql' ) );
506 global $wgCanonicalNamespaceNames;
507 foreach ( $wgCanonicalNamespaceNames as $ns => $name ) {
509 do_pagelinks_namespace( $ns );
515 function do_pagelinks_namespace( $namespace ) {
516 global $wgDatabase, $wgContLang;
518 $ns = intval( $namespace );
519 wfOut( "Cleaning up broken links for namespace $ns... " );
521 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
522 $name = $wgContLang->getNsText( $ns );
523 $prefix = $wgDatabase->strencode( $name );
524 $likeprefix = str_replace( '_', '\\_', $prefix );
526 $sql = "UPDATE $pagelinks
527 SET pl_namespace=$ns,
528 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
530 AND pl_title LIKE '$likeprefix:%'";
532 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
536 function do_drop_img_type() {
539 if ( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
540 wfOut( "Dropping unused img_type field in image table... " );
541 $wgDatabase->sourceFile( archive( 'patch-drop_img_type.sql' ) );
544 wfOut( "...no img_type field in image table; Good.\n" );
548 function do_old_links_update() {
550 if ( $wgDatabase->tableExists( 'pagelinks' ) ) {
551 wfOut( "...have pagelinks; skipping old links table updates.\n" );
553 convertLinks(); flush();
557 function fix_ancient_imagelinks() {
559 $info = $wgDatabase->fieldInfo( 'imagelinks', 'il_from' );
560 if ( $info && $info->type() === 'string' ) {
561 wfOut( "Fixing ancient broken imagelinks table.\n" );
562 wfOut( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
563 $wgDatabase->sourceFile( archive( 'patch-fix-il_from.sql' ) );
566 wfOut( "...il_from OK\n" );
570 function do_user_unique_update() {
572 $duper = new UserDupes( $wgDatabase );
573 if ( $duper->hasUniqueIndex() ) {
574 wfOut( "...already have unique user_name index.\n" );
576 if ( !$duper->clearDupes() ) {
577 wfOut( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
579 wfOut( "Adding unique index on user_name... " );
580 $wgDatabase->sourceFile( archive( 'patch-user_nameindex.sql' ) );
585 function do_user_groups_update() {
586 $fname = 'do_user_groups_update';
589 if ( $wgDatabase->tableExists( 'user_groups' ) ) {
590 wfOut( "...user_groups table already exists.\n" );
591 return do_user_groups_reformat();
594 wfOut( "Adding user_groups table... " );
595 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
598 if ( !$wgDatabase->tableExists( 'user_rights' ) ) {
599 if ( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
600 wfOut( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
601 $wgDatabase->sourceFile( archive( 'patch-user_rights.sql' ) );
604 wfOut( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
605 wfOut( "*** You may need to manually configure some sysops by manipulating\n" );
606 wfOut( "*** the user_groups table.\n" );
611 wfOut( "Converting user_rights table to user_groups... " );
612 $result = $wgDatabase->select( 'user_rights',
613 array( 'ur_user', 'ur_rights' ),
614 array( "ur_rights != ''" ),
617 while ( $row = $wgDatabase->fetchObject( $result ) ) {
618 $groups = array_unique(
620 explode( ',', $row->ur_rights ) ) );
622 foreach ( $groups as $group ) {
623 $wgDatabase->insert( 'user_groups',
625 'ug_user' => $row->ur_user,
626 'ug_group' => $group ),
633 function do_user_groups_reformat() {
634 # Check for bogus formats from previous 1.5 alpha code.
636 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
638 if ( $info->type() == 'int' ) {
639 $oldug = $wgDatabase->tableName( 'user_groups' );
640 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
641 wfOut( "user_groups is in bogus intermediate format. Renaming to $newug... " );
642 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
645 wfOut( "Re-adding fresh user_groups table... " );
646 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
650 wfOut( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
651 wfOut( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
654 wfOut( "...user_groups is in current format.\n" );
659 function do_watchlist_null() {
660 # Make sure wl_notificationtimestamp can be NULL,
661 # and update old broken items.
663 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
665 if ( !$info->nullable() ) {
666 wfOut( "Making wl_notificationtimestamp nullable... " );
667 $wgDatabase->sourceFile( archive( 'patch-watchlist-null.sql' ) );
670 wfOut( "...wl_notificationtimestamp is already nullable.\n" );
678 function do_page_random_update() {
681 wfOut( "Setting page_random to a random value on rows where it equals 0..." );
683 $page = $wgDatabase->tableName( 'page' );
684 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
685 $rows = $wgDatabase->affectedRows();
687 wfOut( "changed $rows rows\n" );
690 function do_templatelinks_update() {
692 $fname = 'do_templatelinks_update';
694 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
695 wfOut( "...templatelinks table already exists\n" );
698 wfOut( "Creating templatelinks table...\n" );
699 $wgDatabase->sourceFile( archive( 'patch-templatelinks.sql' ) );
700 wfOut( "Populating...\n" );
701 if ( wfGetLB()->getServerCount() > 1 ) {
702 // Slow, replication-friendly update
703 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
704 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
706 while ( $row = $wgDatabase->fetchObject( $res ) ) {
707 $count = ( $count + 1 ) % 100;
709 if ( function_exists( 'wfWaitForSlaves' ) ) {
710 wfWaitForSlaves( 10 );
715 $wgDatabase->insert( 'templatelinks',
717 'tl_from' => $row->pl_from,
718 'tl_namespace' => $row->pl_namespace,
719 'tl_title' => $row->pl_title,
726 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
728 'tl_from' => 'pl_from',
729 'tl_namespace' => 'pl_namespace',
730 'tl_title' => 'pl_title'
736 wfOut( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
739 // Add index on ( rc_namespace, rc_user_text ) [Jul. 2006]
740 // Add index on ( rc_user_text, rc_timestamp ) [Nov. 2006]
741 function do_rc_indices_update() {
743 wfOut( "Checking for additional recent changes indices...\n" );
746 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
747 'rc_user_text' => 'patch-rc_user_text-index.sql',
750 foreach ( $indexes as $index => $patch ) {
751 $info = $wgDatabase->indexInfo( 'recentchanges', $index, __METHOD__ );
753 wfOut( "...index `{$index}` not found; adding..." );
754 $wgDatabase->sourceFile( archive( $patch ) );
757 wfOut( "...index `{$index}` seems ok.\n" );
762 function index_has_field( $table, $index, $field ) {
764 wfOut( "Checking if $table index $index includes field $field...\n" );
765 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
767 foreach ( $info as $row ) {
768 if ( $row->Column_name == $field ) {
769 wfOut( "...index $index on table $table seems to be ok\n" );
774 wfOut( "...index $index on table $table has no field $field; adding\n" );
778 function do_backlinking_indices_update() {
780 wfOut( "Checking for backlinking indices...\n" );
781 if ( !index_has_field( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
782 !index_has_field( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
783 !index_has_field( 'imagelinks', 'il_to', 'il_from' ) )
785 $wgDatabase->sourceFile( archive( 'patch-backlinkindexes.sql' ) );
786 wfOut( "...backlinking indices updated\n" );
790 function do_categorylinks_indices_update() {
792 wfOut( "Checking for categorylinks indices...\n" );
793 if ( !index_has_field( 'categorylinks', 'cl_sortkey', 'cl_from' ) )
795 $wgDatabase->sourceFile( archive( 'patch-categorylinksindex.sql' ) );
796 wfOut( "...categorylinks indices updated\n" );
800 function do_filearchive_indices_update() {
802 wfOut( "Checking filearchive indices...\n" );
803 $info = $wgDatabase->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
806 $wgDatabase->sourceFile( archive( 'patch-filearchive-user-index.sql' ) );
807 wfOut( "...filearchive indices updated\n" );
811 function maybe_do_profiling_memory_update() {
813 if ( !$wgDatabase->tableExists( 'profiling' ) ) {
815 } elseif ( $wgDatabase->fieldExists( 'profiling', 'pf_memory' ) ) {
816 wfOut( "...profiling table has pf_memory field.\n" );
818 wfOut( "Adding pf_memory field to table profiling..." );
819 $wgDatabase->sourceFile( archive( 'patch-profiling-memory.sql' ) );
824 function do_stats_init() {
825 // Sometimes site_stats table is not properly populated.
827 wfOut( "\nChecking site_stats row..." );
828 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
829 if ( $row === false ) {
830 wfOut( "data is missing! rebuilding...\n" );
831 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
832 wfOut( "missing ss_total_pages, rebuilding...\n" );
837 SiteStatsInit::doAllAndCommit( false );
840 function do_active_users_init() {
842 $activeUsers = $wgDatabase->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
843 if ( $activeUsers == -1 ) {
844 $activeUsers = $wgDatabase->selectField( 'recentchanges',
845 'COUNT( DISTINCT rc_user_text )',
846 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
848 $wgDatabase->update( 'site_stats',
849 array( 'ss_active_users' => intval( $activeUsers ) ),
850 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
853 wfOut( "...ss_active_users user count set...\n" );
856 function purge_cache() {
858 # We can't guarantee that the user will be able to use TRUNCATE,
859 # but we know that DELETE is available to us
860 wfOut( "Purging caches..." );
861 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
865 function do_all_updates( $shared = false, $purge = true ) {
866 global $wgDatabase, $wgDBtype;
868 $updater = DatabaseUpdater::newForDb( $wgDatabase, $shared );
870 wfRunHooks( 'LoadExtensionSchemaUpdates', array( &$updater ) );
872 $updater->doUpdates();
874 if ( !defined( 'MW_NO_SETUP' ) ) {
875 define( 'MW_NO_SETUP', true );
878 foreach( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
879 call_user_func_array( array( new $maint, 'execute' ), array() );
882 if ( $wgDBtype === 'postgres' ) {
893 function archive( $name ) {
894 global $wgDBtype, $IP;
895 if ( file_exists( "$IP/maintenance/$wgDBtype/archives/$name" ) ) {
896 return "$IP/maintenance/$wgDBtype/archives/$name";
898 return "$IP/maintenance/archives/$name";
902 function do_restrictions_update() {
903 # Adding page_restrictions table, obsoleting page.page_restrictions.
904 # Migrating old restrictions to new table
905 # -- Andrew Garrett, January 2007.
909 $name = 'page_restrictions';
910 $patch = 'patch-page_restrictions.sql';
911 $patch2 = 'patch-page_restrictions_sortkey.sql';
913 if ( $wgDatabase->tableExists( $name ) ) {
914 wfOut( "...$name table already exists.\n" );
916 wfOut( "Creating $name table..." );
917 $wgDatabase->sourceFile( archive( $patch ) );
918 $wgDatabase->sourceFile( archive( $patch2 ) );
921 wfOut( "Migrating old restrictions to new table..." );
923 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array( "page_restrictions!=''", "page_restrictions!='edit=:move='" ), __METHOD__ );
927 while ( $row = $wgDatabase->fetchObject( $res ) ) {
928 $count = ( $count + 1 ) % 100;
931 if ( function_exists( 'wfWaitForSlaves' ) ) {
932 wfWaitForSlaves( 10 );
938 # Figure out what the restrictions are..
940 $flatrestrictions = explode( ':', $row->page_restrictions );
942 $restrictions = array ();
943 foreach ( $flatrestrictions as $restriction ) {
944 $thisrestriction = explode( '=', $restriction, 2 );
945 if ( count( $thisrestriction ) == 1 ) {
946 // Compatibility with old protections from before
947 // separate move protection was added.
948 list( $level ) = $thisrestriction;
950 $restrictions['edit'] = $level;
951 $restrictions['move'] = $level;
954 list( $type, $level ) = $thisrestriction;
956 $restrictions[$type] = $level;
960 $wgDatabase->update( 'page', array ( 'page_restrictions' => '' ), array( 'page_id' => $id ), __METHOD__ );
964 foreach ( $restrictions as $type => $level ) {
965 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
967 'pr_level' => $level,
969 'pr_expiry' => 'infinity' ),
977 function do_category_population() {
978 if ( update_row_exists( 'populate category' ) ) {
979 wfOut( "...category table already populated.\n" );
982 require_once( 'populateCategory.php' );
984 "Populating category table, printing progress markers. " .
985 "For large databases, you\n" .
986 "may want to hit Ctrl-C and do this manually with maintenance/\n" .
987 "populateCategory.php.\n"
989 $task = new PopulateCategory();
991 wfOut( "Done populating category table.\n" );
994 function do_populate_parent_id() {
995 if ( update_row_exists( 'populate rev_parent_id' ) ) {
996 wfOut( "...rev_parent_id column already populated.\n" );
999 require_once( 'populateParentId.php' );
1000 $task = new PopulateParentId();
1004 function do_populate_rev_len() {
1005 if ( update_row_exists( 'populate rev_len' ) ) {
1006 wfOut( "...rev_len column already populated.\n" );
1009 require_once( 'populateRevisionLength.php' );
1010 $task = new PopulateRevisionLength();
1014 function do_collation_update() {
1015 global $wgDatabase, $wgCollationVersion;
1016 if ( $wgDatabase->selectField(
1019 'cl_collation != ' . $wgDatabase->addQuotes( $wgCollationVersion ),
1022 wfOut( "...collations up-to-date.\n" );
1025 require_once( 'updateCollation.php' );
1026 $task = new UpdateCollation();
1030 function sqlite_initial_indexes() {
1032 // initial-indexes.sql fails if the indexes are already present, so we perform a quick check if our database is newer.
1033 if ( update_row_exists( 'initial_indexes' ) || $wgDatabase->indexExists( 'user', 'user_name' ) ) {
1034 wfOut( "...have initial indexes\n" );
1037 wfOut( "Adding initial indexes..." );
1038 $wgDatabase->sourceFile( archive( 'initial-indexes.sql' ) );
1042 function sqlite_setup_searchindex() {
1044 $module = $wgDatabase->getFulltextSearchModule();
1045 $fts3tTable = update_row_exists( 'fts3' );
1046 if ( $fts3tTable && !$module ) {
1047 wfOut( '...PHP is missing FTS3 support, downgrading tables...' );
1048 $wgDatabase->sourceFile( archive( 'searchindex-no-fts.sql' ) );
1050 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
1051 wfOut( '...adding FTS3 search capabilities...' );
1052 $wgDatabase->sourceFile( archive( 'searchindex-fts3.sql' ) );
1055 wfOut( "...fulltext search table appears to be in order.\n" );
1059 function do_unique_pl_tl_il() {
1061 $info = $wgDatabase->indexInfo( 'pagelinks', 'pl_namespace' );
1062 if ( is_array( $info ) && !$info[0]->Non_unique ) {
1063 wfOut( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
1065 wfOut( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
1066 $wgDatabase->sourceFile( archive( 'patch-pl-tl-il-unique.sql' ) );
1071 function do_log_search_population() {
1072 if ( update_row_exists( 'populate log_search' ) ) {
1073 wfOut( "...log_search table already populated.\n" );
1076 require_once( 'populateLogSearch.php' );
1078 "Populating log_search table, printing progress markers. For large\n" .
1079 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1080 "maintenance/populateLogSearch.php.\n" );
1081 $task = new PopulateLogSearch();
1083 wfOut( "Done populating log_search table.\n" );
1086 function rename_eu_wiki_id() {
1088 if ( $wgDatabase->fieldExists( 'external_user', 'eu_local_id' ) ) {
1089 wfOut( "...eu_wiki_id already renamed to eu_local_id.\n" );
1092 wfOut( "Renaming eu_wiki_id -> eu_local_id... " );
1093 $wgDatabase->sourceFile( archive( 'patch-eu_local_id.sql' ) );
1097 function do_update_transcache_field() {
1099 if ( update_row_exists( 'convert transcache field' ) ) {
1100 wfOut( "...transcache tc_time already converted.\n" );
1103 wfOut( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
1104 $wgDatabase->sourceFile( archive( 'patch-tc-timestamp.sql' ) );
1109 function do_update_mime_minor_field() {
1110 if ( update_row_exists( 'mime_minor_length' ) ) {
1111 wfOut( "...*_mime_minor fields are already long enough.\n" );
1114 wfOut( "Altering all *_mime_minor fields to 100 bytes in size ... " );
1115 $wgDatabase->sourceFile( archive( 'patch-mime_minor_length.sql' ) );
1120 /***********************************************************************
1122 * TODO: merge with above
1123 ***********************************************************************/
1125 function pg_describe_table( $table ) {
1126 global $wgDatabase, $wgDBmwschema;
1128 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
1129 WHERE pg_class.relnamespace = pg_namespace.oid
1130 AND attrelid=pg_class.oid AND attnum > 0
1131 AND relname=%s AND nspname=%s
1133 $res = $wgDatabase->query( sprintf( $q,
1134 $wgDatabase->addQuotes( $table ),
1135 $wgDatabase->addQuotes( $wgDBmwschema ) ) );
1141 while ( $r = $wgDatabase->fetchRow( $res ) ) {
1150 function pg_describe_index( $idx ) {
1151 global $wgDatabase, $wgDBmwschema;
1153 // first fetch the key (which is a list of columns ords) and
1154 // the table the index applies to (an oid)
1156 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
1158 AND pg_class.relnamespace = pg_namespace.oid
1160 AND indexrelid=pg_class.oid
1162 $res = $wgDatabase->query( sprintf( $q,
1163 $wgDatabase->addQuotes( $wgDBmwschema ),
1164 $wgDatabase->addQuotes( $idx ) ) );
1168 if ( !( $r = $wgDatabase->fetchRow( $res ) ) ) {
1173 $relid = intval( $r[1] );
1174 $indkeys = explode( " ", $indkey );
1176 $colnames = array();
1177 foreach ( $indkeys as $rid ) {
1179 SELECT attname FROM pg_class, pg_attribute
1180 WHERE attrelid=$relid
1182 AND attrelid=pg_class.oid
1184 $r2 = $wgDatabase->query( sprintf( $query, $rid ) );
1188 if ( !( $row2 = $wgDatabase->fetchRow( $r2 ) ) ) {
1191 $colnames[] = $row2[0];
1197 function pg_index_exists( $table, $index ) {
1198 global $wgDatabase, $wgDBmwschema;
1199 $exists = $wgDatabase->selectField( "pg_indexes", "indexname",
1200 array( "indexname" => $index,
1201 "tablename" => $table,
1202 "schemaname" => $wgDBmwschema ) );
1203 return $exists === $index;
1206 function pg_fkey_deltype( $fkey ) {
1207 global $wgDatabase, $wgDBmwschema;
1209 SELECT confdeltype FROM pg_constraint, pg_namespace
1210 WHERE connamespace=pg_namespace.oid
1214 $r = $wgDatabase->query( sprintf( $q,
1215 $wgDatabase->addQuotes( $wgDBmwschema ),
1216 $wgDatabase->addQuotes( $fkey ) ) );
1217 if ( !( $row = $wgDatabase->fetchRow( $r ) ) ) {
1223 function pg_rule_def( $table, $rule ) {
1224 global $wgDatabase, $wgDBmwschema;
1226 SELECT definition FROM pg_rules
1227 WHERE schemaname = %s
1231 $r = $wgDatabase->query( sprintf( $q,
1232 $wgDatabase->addQuotes( $wgDBmwschema ),
1233 $wgDatabase->addQuotes( $table ),
1234 $wgDatabase->addQuotes( $rule ) ) );
1235 $row = $wgDatabase->fetchRow( $r );
1243 function do_postgres_updates() {
1244 global $wgDatabase, $wgDBmwschema, $wgDBts2schema, $wgShowExceptionDetails, $wgDBuser;
1246 # # Gather version numbers in case we need them
1247 $version = $wgDatabase->getServerVersion(); # # long string
1248 $numver = $wgDatabase->numeric_version; # # X.Y e.g. 8.3
1250 # Just in case their LocalSettings.php does not have this:
1251 if ( !isset( $wgDBmwschema ) ) {
1252 $wgDBmwschema = 'mediawiki';
1255 # Verify that this user is configured correctly
1256 $safeuser = $wgDatabase->addQuotes( $wgDBuser );
1257 $SQL = "SELECT array_to_string(useconfig,'*') FROM pg_catalog.pg_user WHERE usename = $safeuser";
1258 $config = pg_fetch_result( $wgDatabase->doQuery( $SQL ), 0, 0 );
1260 foreach ( explode( '*', $config ) as $c ) {
1261 list( $x, $y ) = explode( '=', $c );
1264 if ( !array_key_exists( 'search_path', $conf ) ) {
1267 $search_path = $conf['search_path'];
1269 if ( strpos( $search_path, $wgDBmwschema ) === false ) {
1270 wfOut( "Adding in schema \"$wgDBmwschema\" to search_path for user \"$wgDBuser\"\n" );
1271 $search_path = "$wgDBmwschema, $search_path";
1273 if ( strpos( $search_path, $wgDBts2schema ) === false ) {
1274 wfOut( "Adding in schema \"$wgDBts2schema\" to search_path for user \"$wgDBuser\"\n" );
1275 $search_path = "$search_path, $wgDBts2schema";
1277 $search_path = str_replace( ', ,', ',', $search_path );
1278 if ( array_key_exists( 'search_path', $conf ) === false || $search_path != $conf['search_path'] ) {
1279 $wgDatabase->doQuery( "ALTER USER $wgDBuser SET search_path = $search_path" );
1280 $wgDatabase->doQuery( "SET search_path = $search_path" );
1282 $path = $conf['search_path'];
1283 wfOut( "... search_path for user \"$wgDBuser\" looks correct ($path)\n" );
1286 'client_min_messages' => 'error',
1287 'DateStyle' => 'ISO, YMD',
1290 foreach ( array_keys( $goodconf ) AS $key ) {
1291 $value = $goodconf[$key];
1292 if ( !array_key_exists( $key, $conf ) or $conf[$key] !== $value ) {
1293 wfOut( "Setting $key to '$value' for user \"$wgDBuser\"\n" );
1294 $wgDatabase->doQuery( "ALTER USER $wgDBuser SET $key = '$value'" );
1295 $wgDatabase->doQuery( "SET $key = '$value'" );
1297 wfOut( "... default value of \"$key\" is correctly set to \"$value\" for user \"$wgDBuser\"\n" );
1301 $newsequences = array(
1302 "logging_log_id_seq",
1303 "page_restrictions_pr_id_seq",
1306 $renamed_sequences = array(
1307 array('ipblocks_ipb_id_val', 'ipblocks_ipb_id_seq'),
1308 array('rev_rev_id_val', 'revision_rev_id_seq'),
1309 array('text_old_id_val', 'text_old_id_seq'),
1310 array('category_id_seq', 'category_cat_id_seq'),
1311 array('rc_rc_id_seq', 'recentchanges_rc_id_seq'),
1312 array('log_log_id_seq', 'logging_log_id_seq'),
1313 array('pr_id_val', 'page_restrictions_pr_id_seq'),
1317 array( "category", "patch-category.sql" ),
1318 array( "mwuser", "patch-mwuser.sql" ),
1319 array( "pagecontent", "patch-pagecontent.sql" ),
1320 array( "querycachetwo", "patch-querycachetwo.sql" ),
1321 array( "page_props", "patch-page_props.sql" ),
1322 array( "page_restrictions", "patch-page_restrictions.sql" ),
1323 array( "profiling", "patch-profiling.sql" ),
1324 array( "protected_titles", "patch-protected_titles.sql" ),
1325 array( "redirect", "patch-redirect.sql" ),
1326 array( "updatelog", "patch-updatelog.sql" ),
1327 array( 'change_tag', 'patch-change_tag.sql' ),
1328 array( 'tag_summary', 'patch-change_tag.sql' ),
1329 array( 'valid_tag', 'patch-change_tag.sql' ),
1330 array( 'user_properties', 'patch-user_properties.sql' ),
1331 array( 'log_search', 'patch-log_search.sql' ),
1332 array( 'l10n_cache', 'patch-l10n_cache.sql' ),
1333 array( 'iwlinks', 'patch-iwlinks.sql' ),
1337 array( "archive", "ar_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1338 array( "archive", "ar_len", "INTEGER" ),
1339 array( "archive", "ar_page_id", "INTEGER" ),
1340 array( "archive", "ar_parent_id", "INTEGER" ),
1341 array( "image", "img_sha1", "TEXT NOT NULL DEFAULT ''" ),
1342 array( "ipblocks", "ipb_allow_usertalk", "SMALLINT NOT NULL DEFAULT 0" ),
1343 array( "ipblocks", "ipb_anon_only", "SMALLINT NOT NULL DEFAULT 0" ),
1344 array( "ipblocks", "ipb_by_text", "TEXT NOT NULL DEFAULT ''" ),
1345 array( "ipblocks", "ipb_block_email", "SMALLINT NOT NULL DEFAULT 0" ),
1346 array( "ipblocks", "ipb_create_account", "SMALLINT NOT NULL DEFAULT 1" ),
1347 array( "ipblocks", "ipb_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1348 array( "ipblocks", "ipb_enable_autoblock", "SMALLINT NOT NULL DEFAULT 1" ),
1349 array( "filearchive", "fa_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1350 array( "logging", "log_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1351 array( "logging", "log_id", "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq')" ),
1352 array( "logging", "log_params", "TEXT" ),
1353 array( "mwuser", "user_editcount", "INTEGER" ),
1354 array( "mwuser", "user_hidden", "SMALLINT NOT NULL DEFAULT 0" ),
1355 array( "mwuser", "user_newpass_time", "TIMESTAMPTZ" ),
1356 array( "oldimage", "oi_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1357 array( "oldimage", "oi_major_mime", "TEXT NOT NULL DEFAULT 'unknown'" ),
1358 array( "oldimage", "oi_media_type", "TEXT" ),
1359 array( "oldimage", "oi_metadata", "BYTEA NOT NULL DEFAULT ''" ),
1360 array( "oldimage", "oi_minor_mime", "TEXT NOT NULL DEFAULT 'unknown'" ),
1361 array( "oldimage", "oi_sha1", "TEXT NOT NULL DEFAULT ''" ),
1362 array( "page_restrictions", "pr_id", "INTEGER NOT NULL UNIQUE DEFAULT nextval('page_restrictions_pr_id_val')" ),
1363 array( "profiling", "pf_memory", "NUMERIC(18,10) NOT NULL DEFAULT 0" ),
1364 array( "recentchanges", "rc_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1365 array( "recentchanges", "rc_log_action", "TEXT" ),
1366 array( "recentchanges", "rc_log_type", "TEXT" ),
1367 array( "recentchanges", "rc_logid", "INTEGER NOT NULL DEFAULT 0" ),
1368 array( "recentchanges", "rc_new_len", "INTEGER" ),
1369 array( "recentchanges", "rc_old_len", "INTEGER" ),
1370 array( "recentchanges", "rc_params", "TEXT" ),
1371 array( "redirect", "rd_interwiki", "TEXT NULL" ),
1372 array( "redirect", "rd_fragment", "TEXT NULL" ),
1373 array( "revision", "rev_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1374 array( "revision", "rev_len", "INTEGER" ),
1375 array( "revision", "rev_parent_id", "INTEGER DEFAULT NULL" ),
1376 array( "site_stats", "ss_active_users", "INTEGER DEFAULT '-1'" ),
1377 array( "user_newtalk", "user_last_timestamp", "TIMESTAMPTZ" ),
1378 array( "logging", "log_user_text", "TEXT NOT NULL DEFAULT ''" ),
1379 array( "logging", "log_page", "INTEGER" ),
1380 array( "interwiki", "iw_api", "TEXT NOT NULL DEFAULT ''"),
1381 array( "interwiki", "iw_wikiid", "TEXT NOT NULL DEFAULT ''"),
1385 # table, column, desired type, USING clause if needed (with new default if needed)
1386 $typechanges = array(
1387 array( "archive", "ar_deleted", "smallint", "" ),
1388 array( "archive", "ar_minor_edit", "smallint", "ar_minor_edit::smallint DEFAULT 0" ),
1389 array( "filearchive", "fa_deleted", "smallint", "" ),
1390 array( "filearchive", "fa_height", "integer", "" ),
1391 array( "filearchive", "fa_metadata", "bytea", "decode(fa_metadata,'escape')" ),
1392 array( "filearchive", "fa_size", "integer", "" ),
1393 array( "filearchive", "fa_width", "integer", "" ),
1394 array( "filearchive", "fa_storage_group", "text", "" ),
1395 array( "filearchive", "fa_storage_key", "text", "" ),
1396 array( "image", "img_metadata", "bytea", "decode(img_metadata,'escape')" ),
1397 array( "image", "img_size", "integer", "" ),
1398 array( "image", "img_width", "integer", "" ),
1399 array( "image", "img_height", "integer", "" ),
1400 array( "interwiki", "iw_local", "smallint", "iw_local::smallint DEFAULT 0" ),
1401 array( "interwiki", "iw_trans", "smallint", "iw_trans::smallint DEFAULT 0" ),
1402 array( "ipblocks", "ipb_auto", "smallint", "ipb_auto::smallint DEFAULT 0" ),
1403 array( "ipblocks", "ipb_anon_only", "smallint", "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ),
1404 array( "ipblocks", "ipb_create_account", "smallint", "CASE WHEN ipb_create_account=' ' THEN 0 ELSE ipb_create_account::smallint END DEFAULT 1" ),
1405 array( "ipblocks", "ipb_enable_autoblock", "smallint", "CASE WHEN ipb_enable_autoblock=' ' THEN 0 ELSE ipb_enable_autoblock::smallint END DEFAULT 1" ),
1406 array( "ipblocks", "ipb_block_email", "smallint", "CASE WHEN ipb_block_email=' ' THEN 0 ELSE ipb_block_email::smallint END DEFAULT 0" ),
1407 array( "ipblocks", "ipb_address", "text", "ipb_address::text" ),
1408 array( "ipblocks", "ipb_deleted", "smallint", "ipb_deleted::smallint DEFAULT 0" ),
1409 array( "math", "math_inputhash", "bytea", "decode(math_inputhash,'escape')" ),
1410 array( "math", "math_outputhash", "bytea", "decode(math_outputhash,'escape')" ),
1411 array( "mwuser", "user_token", "text", "" ),
1412 array( "mwuser", "user_email_token", "text", "" ),
1413 array( "objectcache", "keyname", "text", "" ),
1414 array( "oldimage", "oi_height", "integer", "" ),
1415 array( "oldimage", "oi_metadata", "bytea", "decode(img_metadata,'escape')" ),
1416 array( "oldimage", "oi_size", "integer", "" ),
1417 array( "oldimage", "oi_width", "integer", "" ),
1418 array( "page", "page_is_redirect", "smallint", "page_is_redirect::smallint DEFAULT 0" ),
1419 array( "page", "page_is_new", "smallint", "page_is_new::smallint DEFAULT 0" ),
1420 array( "querycache", "qc_value", "integer", "" ),
1421 array( "querycachetwo", "qcc_value", "integer", "" ),
1422 array( "recentchanges", "rc_bot", "smallint", "rc_bot::smallint DEFAULT 0" ),
1423 array( "recentchanges", "rc_deleted", "smallint", "" ),
1424 array( "recentchanges", "rc_minor", "smallint", "rc_minor::smallint DEFAULT 0" ),
1425 array( "recentchanges", "rc_new", "smallint", "rc_new::smallint DEFAULT 0" ),
1426 array( "recentchanges", "rc_type", "smallint", "rc_type::smallint DEFAULT 0" ),
1427 array( "recentchanges", "rc_patrolled", "smallint", "rc_patrolled::smallint DEFAULT 0" ),
1428 array( "revision", "rev_deleted", "smallint", "rev_deleted::smallint DEFAULT 0" ),
1429 array( "revision", "rev_minor_edit", "smallint", "rev_minor_edit::smallint DEFAULT 0" ),
1430 array( "templatelinks", "tl_namespace", "smallint", "tl_namespace::smallint" ),
1431 array( "user_newtalk", "user_ip", "text", "host(user_ip)" ),
1434 # table, column, nullability
1435 $nullchanges = array(
1436 array( "oldimage", "oi_bits", "NULL" ),
1437 array( "oldimage", "oi_timestamp", "NULL" ),
1438 array( "oldimage", "oi_major_mime", "NULL" ),
1439 array( "oldimage", "oi_minor_mime", "NULL" ),
1442 $newindexes = array(
1443 array( "archive", "archive_user_text", "(ar_user_text)" ),
1444 array( "image", "img_sha1", "(img_sha1)" ),
1445 array( "oldimage", "oi_sha1", "(oi_sha1)" ),
1446 array( "page", "page_mediawiki_title", "(page_title) WHERE page_namespace = 8" ),
1447 array( "revision", "rev_text_id_idx", "(rev_text_id)" ),
1448 array( "recentchanges", "rc_timestamp_bot", "(rc_timestamp) WHERE rc_bot = 0" ),
1449 array( "templatelinks", "templatelinks_from", "(tl_from)" ),
1450 array( "watchlist", "wl_user", "(wl_user)" ),
1451 array( "logging", "logging_user_type_time", "(log_user, log_type, log_timestamp)" ),
1452 array( "logging", "logging_page_id_time", "(log_page,log_timestamp)" ),
1453 array( "iwlinks", "iwl_prefix_title_from", "(iwl_prefix, iwl_title, iwl_from)" ),
1459 # # All FK columns should be deferred
1460 $deferredcols = array(
1461 array( "archive", "ar_user", "mwuser(user_id) ON DELETE SET NULL" ),
1462 array( "categorylinks", "cl_from", "page(page_id) ON DELETE CASCADE" ),
1463 array( "externallinks", "el_from", "page(page_id) ON DELETE CASCADE" ),
1464 array( "filearchive", "fa_deleted_user", "mwuser(user_id) ON DELETE SET NULL" ),
1465 array( "filearchive", "fa_user", "mwuser(user_id) ON DELETE SET NULL" ),
1466 array( "image", "img_user", "mwuser(user_id) ON DELETE SET NULL" ),
1467 array( "imagelinks", "il_from", "page(page_id) ON DELETE CASCADE" ),
1468 array( "ipblocks", "ipb_by", "mwuser(user_id) ON DELETE CASCADE" ),
1469 array( "ipblocks", "ipb_user", "mwuser(user_id) ON DELETE SET NULL" ),
1470 array( "langlinks", "ll_from", "page(page_id) ON DELETE CASCADE" ),
1471 array( "logging", "log_user", "mwuser(user_id) ON DELETE SET NULL" ),
1472 array( "oldimage", "oi_name", "image(img_name) ON DELETE CASCADE ON UPDATE CASCADE" ),
1473 array( "oldimage", "oi_user", "mwuser(user_id) ON DELETE SET NULL" ),
1474 array( "pagelinks", "pl_from", "page(page_id) ON DELETE CASCADE" ),
1475 array( "page_props", "pp_page", "page (page_id) ON DELETE CASCADE" ),
1476 array( "page_restrictions", "pr_page", "page(page_id) ON DELETE CASCADE" ),
1477 array( "protected_titles", "pt_user", "mwuser(user_id) ON DELETE SET NULL" ),
1478 array( "recentchanges", "rc_cur_id", "page(page_id) ON DELETE SET NULL" ),
1479 array( "recentchanges", "rc_user", "mwuser(user_id) ON DELETE SET NULL" ),
1480 array( "redirect", "rd_from", "page(page_id) ON DELETE CASCADE" ),
1481 array( "revision", "rev_page", "page (page_id) ON DELETE CASCADE" ),
1482 array( "revision", "rev_user", "mwuser(user_id) ON DELETE RESTRICT" ),
1483 array( "templatelinks", "tl_from", "page(page_id) ON DELETE CASCADE" ),
1484 array( "trackbacks", "tb_page", "page(page_id) ON DELETE CASCADE" ),
1485 array( "user_groups", "ug_user", "mwuser(user_id) ON DELETE CASCADE" ),
1486 array( "user_newtalk", "user_id", "mwuser(user_id) ON DELETE CASCADE" ),
1487 array( "user_properties", "up_user", "mwuser(user_id) ON DELETE CASCADE" ),
1488 array( "watchlist", "wl_user", "mwuser(user_id) ON DELETE CASCADE" ),
1491 # Create new sequences
1492 foreach ( $newsequences as $ns ) {
1493 if ( ! $wgDatabase->sequenceExists( $ns ) ) {
1494 wfOut( "Creating sequence $ns\n" );
1495 $wgDatabase->query( "CREATE SEQUENCE $ns" );
1500 foreach ( $renamed_sequences as $ren ) {
1501 if ( $wgDatabase->sequenceExists( $ren[0] ) ) {
1502 wfOut( "Renaming sequence $ren[0] to $ren[1]\n" );
1503 $wgDatabase->query( "ALTER SEQUENCE $ren[0] RENAME TO $ren[1]" );
1508 foreach ( $newtables as $nt ) {
1509 if ( $wgDatabase->tableExists( $nt[0] ) ) {
1510 wfOut( "... table \"$nt[0]\" already exists\n" );
1514 wfOut( "Creating table \"$nt[0]\"\n" );
1515 $wgDatabase->sourceFile( archive( $nt[1] ) );
1518 # Needed before newcols
1519 if ( $wgDatabase->tableExists( "archive2" ) ) {
1520 wfOut( "Converting \"archive2\" back to normal archive table\n" );
1521 if ( $wgDatabase->ruleExists( "archive", "archive_insert" ) ) {
1522 wfOut( "Dropping rule \"archive_insert\"\n" );
1523 $wgDatabase->query( "DROP RULE archive_insert ON archive" );
1525 if ( $wgDatabase->ruleExists( "archive", "archive_delete" ) ) {
1526 wfOut( "Dropping rule \"archive_delete\"\n" );
1527 $wgDatabase->query( "DROP RULE archive_delete ON archive" );
1529 $wgDatabase->sourceFile( archive( "patch-remove-archive2.sql" ) );
1532 wfOut( "... obsolete table \"archive2\" does not exist\n" );
1534 foreach ( $newcols as $nc ) {
1535 $fi = $wgDatabase->fieldInfo( $nc[0], $nc[1] );
1536 if ( !is_null( $fi ) ) {
1537 wfOut( "... column \"$nc[0].$nc[1]\" already exists\n" );
1541 wfOut( "Adding column \"$nc[0].$nc[1]\"\n" );
1542 $wgDatabase->query( "ALTER TABLE $nc[0] ADD $nc[1] $nc[2]" );
1545 foreach ( $typechanges as $tc ) {
1546 $fi = $wgDatabase->fieldInfo( $tc[0], $tc[1] );
1547 if ( is_null( $fi ) ) {
1548 wfOut( "... error: expected column $tc[0].$tc[1] to exist\n" );
1552 if ( $fi->type() === $tc[2] )
1553 wfOut( "... column \"$tc[0].$tc[1]\" is already of type \"$tc[2]\"\n" );
1555 wfOut( "Changing column type of \"$tc[0].$tc[1]\" from \"{$fi->type()}\" to \"$tc[2]\"\n" );
1556 $sql = "ALTER TABLE $tc[0] ALTER $tc[1] TYPE $tc[2]";
1557 if ( strlen( $tc[3] ) ) {
1559 if ( preg_match( '/DEFAULT (.+)/', $tc[3], $default ) ) {
1560 $sqldef = "ALTER TABLE $tc[0] ALTER $tc[1] SET DEFAULT $default[1]";
1561 $wgDatabase->query( $sqldef );
1562 $tc[3] = preg_replace( '/\s*DEFAULT .+/', '', $tc[3] );
1564 $sql .= " USING $tc[3]";
1566 $sql .= ";\nCOMMIT;\n";
1567 $wgDatabase->query( $sql );
1571 foreach ( $nullchanges as $nc ) {
1572 $fi = $wgDatabase->fieldInfo( $nc[0], $nc[1] );
1573 if ( is_null( $fi ) ) {
1574 wfOut( "... error: expected column $nc[0].$nc[1] to exist\n" );
1577 if ( $fi->nullable() ) {
1578 # # It's NULL - does it need to be NOT NULL?
1579 if ( 'NOT NULL' === $nc[2] ) {
1580 wfOut( "Changing \"$nc[0].$nc[1]\" to not allow NULLs\n" );
1581 $wgDatabase->query( "ALTER TABLE $nc[0] ALTER $nc[1] SET NOT NULL" );
1583 wfOut( "... column \"$nc[0].$nc[1]\" is already set as NULL\n" );
1586 # # It's NOT NULL - does it need to be NULL?
1587 if ( 'NULL' === $nc[2] ) {
1588 wfOut( "Changing \"$nc[0].$nc[1]\" to allow NULLs\n" );
1589 $wgDatabase->query( "ALTER TABLE $nc[0] ALTER $nc[1] DROP NOT NULL" );
1592 wfOut( "... column \"$nc[0].$nc[1]\" is already set as NOT NULL\n" );
1597 if ( $wgDatabase->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) {
1598 wfOut( "Changing \"oldimage.oi_deleted\" to type \"smallint\"\n" );
1599 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
1600 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
1601 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
1603 wfOut( "... column \"oldimage.oi_deleted\" is already of type \"smallint\"\n" );
1606 foreach ( $newindexes as $ni ) {
1607 if ( pg_index_exists( $ni[0], $ni[1] ) ) {
1608 wfOut( "... index \"$ni[1]\" on table \"$ni[0]\" already exists\n" );
1611 wfOut( "Creating index \"$ni[1]\" on table \"$ni[0]\" $ni[2]\n" );
1612 $wgDatabase->query( "CREATE INDEX $ni[1] ON $ni[0] $ni[2]" );
1615 foreach ( $newrules as $nr ) {
1616 if ( $wgDatabase->ruleExists( $nr[0], $nr[1] ) ) {
1617 wfOut( "... rule \"$nr[1]\" on table \"$nr[0]\" already exists\n" );
1620 wfOut( "Adding rule \"$nr[1]\" to table \"$nr[0]\"\n" );
1621 $wgDatabase->sourceFile( archive( $nr[2] ) );
1624 if ( $wgDatabase->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) {
1625 wfOut( "... table \"oldimage\" has correct cascading delete/update foreign key to image\n" );
1627 if ( $wgDatabase->hasConstraint( "oldimage_oi_name_fkey" ) ) {
1628 $wgDatabase->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
1630 if ( $wgDatabase->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) {
1631 $wgDatabase->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" );
1633 wfOut( "Making foreign key on table \"oldimage\" (to image) a cascade delete/update\n" );
1634 $wgDatabase->query( "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " .
1635 "FOREIGN KEY (oi_name) REFERENCES image(img_name) ON DELETE CASCADE ON UPDATE CASCADE" );
1638 if ( !$wgDatabase->triggerExists( "page", "page_deleted" ) ) {
1639 wfOut( "Adding function and trigger \"page_deleted\" to table \"page\"\n" );
1640 $wgDatabase->sourceFile( archive( 'patch-page_deleted.sql' ) );
1642 wfOut( "... table \"page\" has \"page_deleted\" trigger\n" );
1645 $fi = $wgDatabase->fieldInfo( "recentchanges", "rc_cur_id" );
1646 if ( !$fi->nullable() ) {
1647 wfOut( "Removing NOT NULL constraint from \"recentchanges.rc_cur_id\"\n" );
1648 $wgDatabase->sourceFile( archive( 'patch-rc_cur_id-not-null.sql' ) );
1650 wfOut( "... column \"recentchanges.rc_cur_id\" has a NOT NULL constraint\n" );
1653 $pu = pg_describe_index( "pagelink_unique" );
1654 if ( !is_null( $pu ) && ( $pu[0] != "pl_from" || $pu[1] != "pl_namespace" || $pu[2] != "pl_title" ) ) {
1655 wfOut( "Dropping obsolete version of index \"pagelink_unique index\"\n" );
1656 $wgDatabase->query( "DROP INDEX pagelink_unique" );
1659 wfOut( "... obsolete version of index \"pagelink_unique index\" does not exist\n" );
1662 if ( is_null( $pu ) ) {
1663 wfOut( "Creating index \"pagelink_unique index\"\n" );
1664 $wgDatabase->query( "CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)" );
1666 wfOut( "... index \"pagelink_unique_index\" already exists\n" );
1669 if ( pg_fkey_deltype( "revision_rev_user_fkey" ) == 'r' ) {
1670 wfOut( "... constraint \"revision_rev_user_fkey\" is ON DELETE RESTRICT\n" );
1672 wfOut( "Changing constraint \"revision_rev_user_fkey\" to ON DELETE RESTRICT\n" );
1673 $wgDatabase->sourceFile( archive( 'patch-revision_rev_user_fkey.sql' ) );
1676 # Fix ipb_address index
1677 if ( pg_index_exists( 'ipblocks', 'ipb_address' ) ) {
1678 wfOut( "Removing deprecated index 'ipb_address'...\n" );
1679 $wgDatabase->query( 'DROP INDEX ipb_address' );
1681 if ( pg_index_exists( 'ipblocks', 'ipb_address_unique' ) ) {
1682 wfOut( "... have ipb_address_unique\n" );
1684 wfOut( "Adding ipb_address_unique index\n" );
1685 $wgDatabase->sourceFile( archive( 'patch-ipb_address_unique.sql' ) );
1689 if ( pg_index_exists( 'iwlinks', 'iwl_prefix' ) ) {
1690 wfOut( "Replacing index 'iwl_prefix' with 'iwl_prefix_from_title'...\n" );
1691 $wgDatabase->sourceFile( archive( 'patch-rename-iwl_prefix.sql' ) );
1694 global $wgExtNewTables, $wgExtPGNewFields, $wgExtPGAlteredFields, $wgExtNewIndexes;
1695 # Add missing extension tables
1696 foreach ( $wgExtNewTables as $nt ) {
1697 if ( $wgDatabase->tableExists( $nt[0] ) ) {
1698 wfOut( "... table \"$nt[0]\" already exists\n" );
1701 wfOut( "Creating table \"$nt[0]\"\n" );
1702 $wgDatabase->sourceFile( $nt[1] );
1704 # Add missing extension fields
1705 foreach ( $wgExtPGNewFields as $nc ) {
1706 $fi = $wgDatabase->fieldInfo( $nc[0], $nc[1] );
1707 if ( !is_null( $fi ) ) {
1708 wfOut( "... column \"$nc[0].$nc[1]\" already exists\n" );
1711 wfOut( "Adding column \"$nc[0].$nc[1]\"\n" );
1712 $wgDatabase->query( "ALTER TABLE $nc[0] ADD $nc[1] $nc[2]" );
1714 # Change altered columns
1715 foreach ( $wgExtPGAlteredFields as $nc ) {
1716 $fi = $wgDatabase->fieldInfo( $nc[0], $nc[1] );
1717 if ( is_null( $fi ) ) {
1718 wfOut( "WARNING! Column \"$nc[0].$nc[1]\" does not exist but had an alter request! Please report this.\n" );
1721 $oldtype = $fi->type();
1722 $newtype = strtolower( $nc[2] );
1723 if ( $oldtype === $newtype ) {
1724 wfOut( "... column \"$nc[0].$nc[1]\" has correct type of \"$newtype\"\n" );
1727 $command = "ALTER TABLE $nc[0] ALTER $nc[1] TYPE $nc[2]";
1728 if ( isset( $nc[3] ) ) {
1729 $command .= " USING $nc[3]";
1731 wfOut( "Altering column \"$nc[0].$nc[1]\" from type \"$oldtype\" to \"$newtype\"\n" );
1732 $wgDatabase->query( $command );
1734 # Add missing extension indexes
1735 foreach ( $wgExtNewIndexes as $ni ) {
1736 if ( pg_index_exists( $ni[0], $ni[1] ) ) {
1737 wfOut( "... index \"$ni[1]\" on table \"$ni[0]\" already exists\n" );
1740 wfOut( "Creating index \"$ni[1]\" on table \"$ni[0]\"\n" );
1741 if ( preg_match( '/^\(/', $ni[2] ) ) {
1742 $wgDatabase->query( "CREATE INDEX $ni[1] ON $ni[0] $ni[2]" );
1745 $wgDatabase->sourceFile( $ni[2] );
1749 foreach ( $deferredcols AS $dc ) {
1750 $fi = $wgDatabase->fieldInfo( $dc[0], $dc[1] );
1751 if ( is_null( $fi ) ) {
1752 wfOut( "WARNING! Column \"$dc[0].$dc[1]\" does not exist but it should! Please report this.\n" );
1755 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
1758 wfOut( "Altering column \"$dc[0].$dc[1]\" to be DEFERRABLE INITIALLY DEFERRED\n" );
1759 $conname = $fi->conname();
1761 $command = "ALTER TABLE $dc[0] DROP CONSTRAINT $conname";
1762 $wgDatabase->query( $command );
1763 $command = "ALTER TABLE $dc[0] ADD CONSTRAINT $conname FOREIGN KEY ($dc[1]) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
1764 $wgDatabase->query( $command );
1767 # Tweak the page_title tsearch2 trigger to filter out slashes
1768 # This is create or replace, so harmless to call if not needed
1769 $wgDatabase->sourceFile( archive( 'patch-ts2pagetitle.sql' ) );
1771 # # If the server is 8.3 or higher, rewrite the tsearch2 triggers
1772 # # in case they have the old 'default' versions
1773 if ( $numver >= 8.3 ) {
1774 $wgDatabase->sourceFile( archive( 'patch-tsearch2funcs.sql' ) );