4 * @subpackage Maintenance
9 if ( !defined( 'MEDIAWIKI' ) ) {
10 echo "This file is not a valid entry point\n";
14 require_once 'convertLinks.inc';
15 require_once 'userDupes.inc';
16 require_once 'deleteDefaultMessages.php';
18 $wgRenamedTables = array(
20 # array( 'group', 'groups', 'patch-rename-group.sql' ),
24 # table patch file (in maintenance/archives)
25 array( 'hitcounter', 'patch-hitcounter.sql' ),
26 array( 'querycache', 'patch-querycache.sql' ),
27 array( 'objectcache', 'patch-objectcache.sql' ),
28 array( 'categorylinks', 'patch-categorylinks.sql' ),
29 array( 'logging', 'patch-logging.sql' ),
30 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
31 array( 'transcache', 'patch-transcache.sql' ),
32 array( 'trackbacks', 'patch-trackbacks.sql' ),
33 array( 'externallinks', 'patch-externallinks.sql' ),
34 array( 'job', 'patch-job.sql' ),
35 array( 'langlinks', 'patch-langlinks.sql' ),
36 array( 'querycache_info', 'patch-querycacheinfo.sql' ),
37 array( 'filearchive', 'patch-filearchive.sql' ),
38 array( 'redirect', 'patch-redirect.sql' ),
39 array( 'querycachetwo', 'patch-querycachetwo.sql' ),
40 # array( 'page_restrictions', 'patch-page_restrictions.sql' ),
44 # table field patch file (in maintenance/archives)
45 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
46 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
47 array( 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.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( 'user', 'user_newpass_time','patch-user_newpass_time.sql' ),
71 array( 'user', 'user_editcount', 'patch-user_editcount.sql' ),
74 function rename_table( $from, $to, $patch ) {
76 if ( $wgDatabase->tableExists( $from ) ) {
77 if ( $wgDatabase->tableExists( $to ) ) {
78 echo "...can't move table $from to $to, $to already exists.\n";
80 echo "Moving table $from to $to...";
81 dbsource( archive($patch), $wgDatabase );
85 // Source table does not exist
86 // Renames are done before creations, so this is typical for a new installation
91 function add_table( $name, $patch ) {
93 if ( $wgDatabase->tableExists( $name ) ) {
94 echo "...$name table already exists.\n";
96 echo "Creating $name table...";
97 dbsource( archive($patch), $wgDatabase );
102 function add_field( $table, $field, $patch ) {
104 if ( !$wgDatabase->tableExists( $table ) ) {
105 echo "...$table table does not exist, skipping new field patch\n";
106 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
107 echo "...have $field field in $table table.\n";
109 echo "Adding $field field to table $table...";
110 dbsource( archive($patch) , $wgDatabase );
115 function do_revision_updates() {
116 global $wgSoftwareRevision;
117 if ( $wgSoftwareRevision < 1001 ) {
122 function update_passwords() {
123 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
126 $fname = "Update script: update_passwords()";
127 print "\nIt appears that you need to update the user passwords in your\n" .
128 "database. If you have already done this (if you've run this update\n" .
129 "script once before, for example), doing so again will make all your\n" .
130 "user accounts inaccessible, so be sure you only do this once.\n" .
131 "Update user passwords? (yes/no)";
133 $resp = readconsole();
134 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
136 $sql = "SELECT user_id,user_password FROM user";
137 $source = $wgDatabase->query( $sql, $fname );
139 while ( $row = $wgDatabase->fetchObject( $source ) ) {
141 $oldpass = $row->user_password;
142 $newpass = md5( "{$id}-{$oldpass}" );
144 $sql = "UPDATE user SET user_password='{$newpass}' " .
145 "WHERE user_id={$id}";
146 $wgDatabase->query( $sql, $fname );
150 function do_interwiki_update() {
151 # Check that interwiki table exists; if it doesn't source it
152 global $wgDatabase, $IP;
153 if( $wgDatabase->tableExists( "interwiki" ) ) {
154 echo "...already have interwiki table\n";
157 echo "Creating interwiki table: ";
158 dbsource( archive("patch-interwiki.sql") );
160 echo "Adding default interwiki definitions: ";
161 dbsource( "$IP/maintenance/interwiki.sql" );
165 function do_index_update() {
166 # Check that proper indexes are in place
168 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
169 if( $meta->multiple_key == 0 ) {
170 echo "Updating indexes to 20031107: ";
171 dbsource( archive("patch-indexes.sql") );
175 echo "...indexes seem up to 20031107 standards\n";
179 function do_image_index_update() {
182 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
183 if( $meta->multiple_key == 0 ) {
184 echo "Updating indexes to 20050912: ";
185 dbsource( archive("patch-mimesearch-indexes.sql") );
189 echo "...indexes seem up to 20050912 standards\n";
193 function do_image_name_unique_update() {
195 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
196 echo "...image primary key already set.\n";
198 echo "Making img_name the primary key... ";
199 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
204 function do_logging_timestamp_index() {
206 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
207 echo "...timestamp key on logging already exists.\n";
209 echo "Adding timestamp key on logging table... ";
210 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
216 function do_watchlist_update() {
218 $fname = 'do_watchlist_update';
219 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
220 echo "The watchlist table is already set up for email notification.\n";
222 echo "Adding wl_notificationtimestamp field for email notification management.";
223 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
224 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
227 # Check if we need to add talk page rows to the watchlist
228 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
229 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
230 if ( $talk != $nontalk ) {
231 echo "Adding missing watchlist talk page rows... ";
234 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
236 'wl_user' => 'wl_user',
237 'wl_namespace' => 'wl_namespace | 1',
238 'wl_title' => 'wl_title',
239 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
240 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
243 echo "...watchlist talk page rows already present\n";
247 function do_copy_newtalk_to_watchlist() {
249 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
251 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
252 $wgDatabase->tableName( 'user_newtalk' ) );
253 $num_newtalks=$wgDatabase->numRows($res);
254 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
257 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
258 $wluser = $wgDatabase->fetchObject( $res );
259 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
260 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
261 $wgDatabase->replace( 'watchlist',
262 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
263 array('wl_user' => 0,
264 'wl_namespace' => NS_USER_TALK,
265 'wl_title' => $wluser->user_ip,
266 'wl_notificationtimestamp' => '19700101000000'
267 ), 'updaters.inc::do_watchlist_update2'
270 } else { # normal users ... have user_ids
271 $user->setID($wluser->user_id);
272 $wgDatabase->replace( 'watchlist',
273 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
274 array('wl_user' => $user->getID(),
275 'wl_namespace' => NS_USER_TALK,
276 'wl_title' => $user->getName(),
277 'wl_notificationtimestamp' => '19700101000000'
278 ), 'updaters.inc::do_watchlist_update3'
286 function do_user_update() {
288 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
289 echo "User table contains old email authentication field. Dropping... ";
290 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
293 echo "...user table does not contain old email authentication field.\n";
298 * 1.4 betas were missing the 'binary' marker from logging.log_title,
299 * which causes a collation mismatch error on joins in MySQL 4.1.
301 function do_logging_encoding() {
302 global $wgDatabase, $wgDBtype;
303 if ($wgDBtype != 'mysql')
305 $logging = $wgDatabase->tableName( 'logging' );
306 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
307 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
308 $wgDatabase->freeResult( $res );
310 if( in_array( 'binary', $flags ) ) {
311 echo "Logging table has correct title encoding.\n";
313 echo "Fixing title encoding on logging table... ";
314 dbsource( archive( 'patch-logging-title.sql' ), $wgDatabase );
319 function do_schema_restructuring() {
321 $fname="do_schema_restructuring";
322 if ( $wgDatabase->tableExists( 'page' ) ) {
323 echo "...page table already exists.\n";
325 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
327 echo "......checking for duplicate entries.\n"; flush();
329 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
331 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
332 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
334 if ( $wgDatabase->numRows( $rows ) > 0 ) {
336 echo "......<b>Found duplicate entries</b>\n";
337 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
338 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
339 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
340 $duplicate[$row->cur_namespace] = array();
342 $duplicate[$row->cur_namespace][] = $row->cur_title;
343 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
345 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
347 foreach ( $duplicate as $ns => $titles ) {
353 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
355 foreach ( $titles as $t ) {
357 $sql .= $wgDatabase->addQuotes( $t );
360 $sql .= ', ' . $wgDatabase->addQuotes( $t );
365 # By sorting descending, the most recent entry will be the first in the list.
366 # All following entries will be deleted by the next while-loop.
367 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
369 $rows = $wgDatabase->query( $sql, $fname );
371 $prev_title = $prev_namespace = false;
374 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
375 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
376 $deleteId[] = $row->cur_id;
378 $prev_title = $row->cur_title;
379 $prev_namespace = $row->cur_namespace;
381 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
382 $rows = $wgDatabase->query( $sql, $fname );
384 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
389 echo "......Creating tables.\n";
390 $wgDatabase->query("CREATE TABLE $page (
391 page_id int(8) unsigned NOT NULL auto_increment,
392 page_namespace int NOT NULL,
393 page_title varchar(255) binary NOT NULL,
394 page_restrictions tinyblob NOT NULL,
395 page_counter bigint(20) unsigned NOT NULL default '0',
396 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
397 page_is_new tinyint(1) unsigned NOT NULL default '0',
398 page_random real unsigned NOT NULL,
399 page_touched char(14) binary NOT NULL default '',
400 page_latest int(8) unsigned NOT NULL,
401 page_len int(8) unsigned NOT NULL,
403 PRIMARY KEY page_id (page_id),
404 UNIQUE INDEX name_title (page_namespace,page_title),
407 ) TYPE=InnoDB", $fname );
408 $wgDatabase->query("CREATE TABLE $revision (
409 rev_id int(8) unsigned NOT NULL auto_increment,
410 rev_page int(8) unsigned NOT NULL,
411 rev_comment tinyblob NOT NULL,
412 rev_user int(5) unsigned NOT NULL default '0',
413 rev_user_text varchar(255) binary NOT NULL default '',
414 rev_timestamp char(14) binary NOT NULL default '',
415 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
416 rev_deleted tinyint(1) unsigned NOT NULL default '0',
418 PRIMARY KEY rev_page_id (rev_page, rev_id),
419 UNIQUE INDEX rev_id (rev_id),
420 INDEX rev_timestamp (rev_timestamp),
421 INDEX page_timestamp (rev_page,rev_timestamp),
422 INDEX user_timestamp (rev_user,rev_timestamp),
423 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
424 ) TYPE=InnoDB", $fname );
427 echo "......Locking tables.\n";
428 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
430 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
432 echo "......maxold is {$maxold}\n";
435 global $wgLegacySchemaConversion;
436 if( $wgLegacySchemaConversion ) {
437 // Create HistoryBlobCurStub entries.
438 // Text will be pulled from the leftover 'cur' table at runtime.
439 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
440 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
441 $cur_flags = "'object'";
443 // Copy all cur text in immediately: this may take longer but avoids
444 // having to keep an extra table around.
445 echo "......Moving text from cur.\n";
446 $cur_text = 'cur_text';
449 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
450 old_timestamp, old_minor_edit, old_flags)
451 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
452 FROM $cur", $fname );
455 echo "......Setting up revision table.\n";
456 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
458 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
459 old_timestamp, old_minor_edit
460 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
463 echo "......Setting up page table.\n";
464 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
465 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
466 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
467 cur_random, cur_touched, rev_id, LENGTH(cur_text)
469 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
472 echo "......Unlocking tables.\n";
473 $wgDatabase->query( "UNLOCK TABLES", $fname );
476 echo "......Renaming old.\n";
477 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
484 function do_inverse_timestamp() {
486 $fname="do_schema_restructuring";
487 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
488 echo "Removing revision.inverse_timestamp and fixing indexes... ";
489 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
492 echo "revision timestamp indexes already up to 2005-03-13\n";
496 function do_text_id() {
498 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
499 echo "...rev_text_id already in place.\n";
501 echo "Adding rev_text_id field... ";
502 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
507 function do_namespace_size() {
511 'recentchanges' => 'rc',
513 'querycache' => 'qc',
516 foreach( $tables as $table => $prefix ) {
517 do_namespace_size_on( $table, $prefix );
522 function do_namespace_size_on( $table, $prefix ) {
523 global $wgDatabase, $wgDBtype;
524 if ($wgDBtype != 'mysql')
526 $field = $prefix . '_namespace';
528 $tablename = $wgDatabase->tableName( $table );
529 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
530 $info = $wgDatabase->fetchObject( $result );
531 $wgDatabase->freeResult( $result );
533 if( substr( $info->Type, 0, 3 ) == 'int' ) {
534 echo "...$field is already a full int ($info->Type).\n";
536 echo "Promoting $field from $info->Type to int... ";
538 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
539 $wgDatabase->query( $sql );
545 function do_pagelinks_update() {
547 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
548 echo "...already have pagelinks table.\n";
550 echo "Converting links and brokenlinks tables to pagelinks... ";
551 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
555 global $wgCanonicalNamespaceNames;
556 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
558 do_pagelinks_namespace( $ns );
564 function do_pagelinks_namespace( $namespace ) {
565 global $wgDatabase, $wgContLang;
567 $ns = intval( $namespace );
568 echo "Cleaning up broken links for namespace $ns... ";
570 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
571 $name = $wgContLang->getNsText( $ns );
572 $prefix = $wgDatabase->strencode( $name );
573 $likeprefix = str_replace( '_', '\\_', $prefix);
575 $sql = "UPDATE $pagelinks
576 SET pl_namespace=$ns,
577 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
579 AND pl_title LIKE '$likeprefix:%'";
581 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
585 function do_drop_img_type() {
588 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
589 echo "Dropping unused img_type field in image table... ";
590 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
593 echo "No img_type field in image table; Good.\n";
597 function do_old_links_update() {
599 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
600 echo "Already have pagelinks; skipping old links table updates.\n";
602 convertLinks(); flush();
606 function do_user_unique_update() {
608 $duper = new UserDupes( $wgDatabase );
609 if( $duper->hasUniqueIndex() ) {
610 echo "Already have unique user_name index.\n";
612 if( !$duper->clearDupes() ) {
613 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
615 echo "Adding unique index on user_name... ";
616 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
621 function do_user_groups_update() {
622 $fname = 'do_user_groups_update';
625 if( $wgDatabase->tableExists( 'user_groups' ) ) {
626 echo "...user_groups table already exists.\n";
627 return do_user_groups_reformat();
630 echo "Adding user_groups table... ";
631 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
634 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
635 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
636 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
637 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
640 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
641 echo "*** You may need to manually configure some sysops by manipulating\n";
642 echo "*** the user_groups table.\n";
647 echo "Converting user_rights table to user_groups... ";
648 $result = $wgDatabase->select( 'user_rights',
649 array( 'ur_user', 'ur_rights' ),
650 array( "ur_rights != ''" ),
653 while( $row = $wgDatabase->fetchObject( $result ) ) {
654 $groups = array_unique(
656 explode( ',', $row->ur_rights ) ) );
658 foreach( $groups as $group ) {
659 $wgDatabase->insert( 'user_groups',
661 'ug_user' => $row->ur_user,
662 'ug_group' => $group ),
666 $wgDatabase->freeResult( $result );
670 function do_user_groups_reformat() {
671 # Check for bogus formats from previous 1.5 alpha code.
673 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
675 if( $info->type == 'int' ) {
676 $oldug = $wgDatabase->tableName( 'user_groups' );
677 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
678 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
679 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
682 echo "Re-adding fresh user_groups table... ";
683 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
687 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
688 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
691 echo "...user_groups is in current format.\n";
696 function do_watchlist_null() {
697 # Make sure wl_notificationtimestamp can be NULL,
698 # and update old broken items.
700 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
702 if( $info->not_null ) {
703 echo "Making wl_notificationtimestamp nullable... ";
704 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
707 echo "...wl_notificationtimestamp is already nullable.\n";
715 function do_page_random_update() {
718 echo "Setting page_random to a random value on rows where it equals 0...";
720 $page = $wgDatabase->tableName( 'page' );
721 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
722 $rows = $wgDatabase->affectedRows();
724 echo "changed $rows rows\n";
727 function do_templatelinks_update() {
728 global $wgDatabase, $wgLoadBalancer;
729 $fname = 'do_templatelinks_update';
731 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
732 echo "...templatelinks table already exists\n";
735 echo "Creating templatelinks table...\n";
736 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
737 echo "Populating...\n";
738 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
739 // Slow, replication-friendly update
740 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
741 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
743 while ( $row = $wgDatabase->fetchObject( $res ) ) {
744 $count = ($count + 1) % 100;
746 if ( function_exists( 'wfWaitForSlaves' ) ) {
747 wfWaitForSlaves( 10 );
752 $wgDatabase->insert( 'templatelinks',
754 'tl_from' => $row->pl_from,
755 'tl_namespace' => $row->pl_namespace,
756 'tl_title' => $row->pl_title,
761 $wgDatabase->freeResult( $res );
764 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
766 'tl_from' => 'pl_from',
767 'tl_namespace' => 'pl_namespace',
768 'tl_title' => 'pl_title'
774 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
778 # Add ( rc_namespace, rc_user_text ) index [R. Church]
779 function do_rc_indices_update() {
781 echo( "Checking for additional recent changes indices...\n" );
782 # See if we can find the index we want
783 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_ns_usertext', __METHOD__ );
786 echo( "...index on ( rc_namespace, rc_user_text ) not found; creating\n" );
787 dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
789 # Index seems to exist
790 echo( "...index on ( rc_namespace, rc_user_text ) seems to be ok\n" );
793 #Add (rc_user_text, rc_timestamp) index [A. Garrett], November 2006
794 # See if we can find the index we want
795 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_user_text', __METHOD__ );
798 echo( "...index on ( rc_user_text, rc_timestamp ) not found; creating\n" );
799 dbsource( archive( 'patch-rc_user_text-index.sql' ) );
801 # Index seems to exist
802 echo( "...index on ( rc_user_text, rc_timestamp ) seems to be ok\n" );
806 function index_has_field($table, $index, $field) {
808 echo( "Checking if $table index $index includes field $field...\n" );
809 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
811 foreach($info as $row) {
812 if($row->Column_name == $field) {
813 echo( "...index $index on table $table seems to be ok\n" );
818 echo( "...index $index on table $table has no field $field; adding\n" );
822 function do_backlinking_indices_update() {
823 echo( "Checking for backlinking indices...\n" );
824 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
825 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
826 !index_has_field('imagelinks', 'il_to', 'il_from'))
828 dbsource( archive( 'patch-backlinkindexes.sql' ) );
832 function purge_cache() {
834 # We can't guarantee that the user will be able to use TRUNCATE,
835 # but we know that DELETE is available to us
836 echo( "Purging caches..." );
837 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
841 function do_all_updates( $shared = false, $purge = true ) {
842 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
844 $doUser = !$wgSharedDB || $doShared;
846 if ($wgDBtype === 'postgres') {
847 do_postgres_updates();
852 foreach ( $wgRenamedTables as $tableRecord ) {
853 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
857 foreach ( $wgNewTables as $tableRecord ) {
858 add_table( $tableRecord[0], $tableRecord[1] );
863 foreach ( $wgNewFields as $fieldRecord ) {
864 if ( $fieldRecord[0] != 'user' || $doUser ) {
865 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
870 # Do schema updates which require special handling
871 do_interwiki_update(); flush();
872 do_index_update(); flush();
873 do_old_links_update(); flush();
874 do_image_name_unique_update(); flush();
875 do_watchlist_update(); flush();
877 do_user_update(); flush();
879 ###### do_copy_newtalk_to_watchlist(); flush();
880 do_logging_encoding(); flush();
882 do_schema_restructuring(); flush();
883 do_inverse_timestamp(); flush();
884 do_text_id(); flush();
885 do_namespace_size(); flush();
887 do_pagelinks_update(); flush();
888 do_templatelinks_update(); flush(); // after pagelinks
890 do_drop_img_type(); flush();
893 do_user_unique_update(); flush();
895 do_user_groups_update(); flush();
897 do_watchlist_null(); flush();
899 //do_image_index_update(); flush();
901 do_logging_timestamp_index(); flush();
903 do_page_random_update(); flush();
905 do_rc_indices_update(); flush();
907 do_backlinking_indices_update(); flush();
909 do_restrictions_update(); flush ();
911 echo "Deleting old default messages..."; flush();
912 deleteDefaultMessages();
913 echo "Done\n"; flush();
921 function archive($name) {
922 global $wgDBtype, $IP;
925 return "$IP/maintenance/oracle/archives/$name";
927 return "$IP/maintenance/archives/$name";
931 function do_restrictions_update() {
932 # Adding page_restrictions table, obsoleting page.page_restrictions.
933 # Migrating old restrictions to new table
934 # -- Andrew Garrett, January 2007.
938 $name = 'page_restrictions';
939 $patch = 'patch-page_restrictions.sql';
941 if ( $wgDatabase->tableExists( $name ) ) {
942 echo "...$name table already exists.\n";
944 echo "Creating $name table...";
945 dbsource( archive($patch), $wgDatabase );
948 echo "Migrating old restrictions to new table...";
950 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array("page_restrictions!=''", "page_restrictions!='edit=:move='"), __METHOD__ );
954 while ($row = $wgDatabase->fetchObject($res) ) {
955 $count = ($count + 1) % 100;
958 if ( function_exists( 'wfWaitForSlaves' ) ) {
959 wfWaitForSlaves( 10 );
965 # Figure out what the restrictions are..
967 $flatterrestrictions = $row->page_restrictions;
969 $flatrestrictions = explode( ':', $flatterrestrictions );
971 $restrictions = array ();
973 foreach( $flatrestrictions as $restriction ) {
974 $thisrestriction = explode('=', $restriction);
976 $restriction_type = $thisrestriction[0];
977 $restriction_level = $thisrestriction[1];
979 $restrictions[$restriction_type] = $restriction_level;
981 if ($restriction_level != '') {
983 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
984 'pr_type' => $restriction_type,
985 'pr_level' => $restriction_level,
986 'pr_cascade' => 0 ), __METHOD );
995 function do_postgres_updates() {
996 global $wgDatabase, $wgVersion, $wgDBmwschema;
998 # Just in case their LocalSetings.php does not have this:
999 if ( !isset( $wgDBmwschema ))
1000 $wgDBmwschema = 'mediawiki';
1002 ## Default to the oldest supported version
1005 if ($wgDatabase->tableExists("mediawiki_version")) {
1007 $sql = "SELECT mw_version FROM mediawiki_version ORDER BY cdate DESC LIMIT 1";
1008 $tempversion = pg_fetch_result($wgDatabase->doQuery($sql),0,0);
1010 if (preg_match('/(\d+\.\d+)/', $tempversion, $thisver)) {
1011 $version = $thisver[1];
1015 print " Detected version: $version ";
1018 if ($version <= 1.7) {
1022 ALTER TABLE oldimage ALTER oi_size TYPE INTEGER;
1023 ALTER TABLE oldimage ALTER oi_width TYPE INTEGER;
1024 ALTER TABLE oldimage ALTER oi_height TYPE INTEGER;
1026 ALTER TABLE image ALTER img_size TYPE INTEGER;
1027 ALTER TABLE image ALTER img_width TYPE INTEGER;
1028 ALTER TABLE image ALTER img_height TYPE INTEGER;
1030 -- Constraint tweaking:
1031 ALTER TABLE recentchanges ALTER rc_cur_id DROP NOT NULL;
1034 ALTER TABLE ipblocks ADD ipb_anon_only CHAR NOT NULL DEFAULT '0';
1035 ALTER TABLE ipblocks ADD ipb_create_account CHAR NOT NULL DEFAULT '1';
1037 -- Index order rearrangements:
1038 DROP INDEX pagelink_unique;
1039 CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title);
1042 ALTER TABLE "user" RENAME TO mwuser;
1043 ALTER TABLE "text" RENAME to pagecontent;
1046 CREATE TABLE profiling (
1047 pf_count INTEGER NOT NULL DEFAULT 0,
1048 pf_time NUMERIC(18,10) NOT NULL DEFAULT 0,
1049 pf_name TEXT NOT NULL,
1052 CREATE UNIQUE INDEX pf_name_server ON profiling (pf_name, pf_server);
1054 CREATE TABLE mediawiki_version (
1056 mw_version TEXT NOT NULL,
1059 pg_version TEXT NULL,
1060 pg_dbname TEXT NULL,
1063 mw_schema TEXT NULL,
1064 ts2_schema TEXT NULL,
1067 sql_version TEXT NULL,
1069 cdate TIMESTAMPTZ NOT NULL DEFAULT now()
1072 -- Special modifications
1073 ALTER TABLE archive RENAME to archive2;
1074 CREATE VIEW archive AS
1076 ar_namespace, ar_title, ar_text, ar_comment, ar_user, ar_user_text,
1077 ar_minor_edit, ar_flags, ar_rev_id, ar_text_id,
1078 TO_CHAR(ar_timestamp, 'YYYYMMDDHH24MISS') AS ar_timestamp
1081 CREATE RULE archive_insert AS ON INSERT TO archive
1082 DO INSTEAD INSERT INTO archive2 VALUES (
1083 NEW.ar_namespace, NEW.ar_title, NEW.ar_text, NEW.ar_comment, NEW.ar_user, NEW.ar_user_text,
1084 TO_DATE(NEW.ar_timestamp, 'YYYYMMDDHH24MISS'),
1085 NEW.ar_minor_edit, NEW.ar_flags, NEW.ar_rev_id, NEW.ar_text_id
1088 CREATE FUNCTION page_deleted() RETURNS TRIGGER LANGUAGE plpgsql AS
1091 DELETE FROM recentchanges WHERE rc_namespace = OLD.page_namespace AND rc_title = OLD.page_title;
1096 CREATE TRIGGER page_deleted AFTER DELETE ON page
1097 FOR EACH ROW EXECUTE PROCEDURE page_deleted();
1101 } ## end version 1.7
1103 else if ($version <= 1.8) {
1106 -- Tighten up restrictions on the revision table so we don't lose data:
1107 ALTER TABLE revision DROP CONSTRAINT revision_rev_user_fkey;
1108 ALTER TABLE revision ADD CONSTRAINT revision_rev_user_fkey
1109 FOREIGN KEY (rev_user) REFERENCES mwuser(user_id) ON DELETE RESTRICT;
1111 -- New columns for better password tracking:
1112 ALTER TABLE mwuser ADD user_newpass_time TIMESTAMPTZ;
1113 ALTER TABLE mwuser ADD user_editcount INTEGER;
1115 -- New column for autoblocking problem users
1116 ALTER TABLE ipblocks ADD ipb_enable_autoblock CHAR NOT NULL DEFAULT '1';
1118 -- Despite it's name, ipb_address does not necessarily contain IP addresses :)
1119 ALTER TABLE ipblocks ALTER ipb_address TYPE TEXT USING ipb_address::TEXT;
1122 CREATE TABLE redirect (
1123 rd_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE,
1124 rd_namespace SMALLINT NOT NULL,
1125 rd_title TEXT NOT NULL
1127 CREATE INDEX redirect_ns_title ON redirect (rd_namespace,rd_title,rd_from);
1129 CREATE TABLE querycachetwo (
1130 qcc_type TEXT NOT NULL,
1131 qcc_value SMALLINT NOT NULL DEFAULT 0,
1132 qcc_namespace INTEGER NOT NULL DEFAULT 0,
1133 qcc_title TEXT NOT NULL DEFAULT '',
1134 qcc_namespacetwo INTEGER NOT NULL DEFAULT 0,
1135 qcc_titletwo TEXT NOT NULL DEFAULT ''
1137 CREATE INDEX querycachetwo_type_value ON querycachetwo (qcc_type, qcc_value);
1138 CREATE INDEX querycachetwo_title ON querycachetwo (qcc_type,qcc_namespace,qcc_title);
1139 CREATE INDEX querycachetwo_titletwo ON querycachetwo (qcc_type,qcc_namespacetwo,qcc_titletwo);
1141 -- New columns for fancy recentchanges display
1142 ALTER TABLE recentchanges ADD rc_old_len INT;
1143 ALTER TABLE recentchanges ADD rc_new_len INT;
1145 -- Note this upgrade
1146 INSERT INTO mediawiki_version (type,mw_version,notes)
1147 VALUES ('Upgrade','MWVERSION','Upgrade from older version THISVERSION');
1153 if ( !strlen($upgrade)) {
1154 print "No updates needed for version $version\n";
1158 $upgrade = str_replace( 'MWVERSION', $wgVersion, $upgrade );
1159 $upgrade = str_replace( 'THISVERSION', $version, $upgrade );
1160 $res = $wgDatabase->query("BEGIN;\n\n $upgrade\n\nCOMMIT;\n");