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' ),
43 # table field patch file (in maintenance/archives)
44 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
45 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
46 array( 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
47 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
48 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
49 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
50 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
51 array( 'recentchanges', 'rc_old_len', 'patch-rc_len.sql' ),
52 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
53 array( 'user', 'user_token', 'patch-user_token.sql' ),
54 array( 'user', 'user_email_token', 'patch-user_email_token.sql' ),
55 array( 'user', 'user_registration','patch-user_registration.sql' ),
56 array( 'logging', 'log_params', 'patch-log_params.sql' ),
57 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
58 array( 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
59 array( 'page', 'page_len', 'patch-page_len.sql' ),
60 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
61 array( 'image', 'img_width', 'patch-img_width.sql' ),
62 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
63 array( 'image', 'img_media_type', 'patch-img_media_type.sql' ),
64 array( 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ),
65 array( 'interwiki', 'iw_trans', 'patch-interwiki-trans.sql' ),
66 array( 'ipblocks', 'ipb_range_start', 'patch-ipb_range_start.sql' ),
67 array( 'site_stats', 'ss_images', 'patch-ss_images.sql' ),
68 array( 'ipblocks', 'ipb_anon_only', 'patch-ipb_anon_only.sql' ),
69 array( 'user', 'user_newpass_time','patch-user_newpass_time.sql' ),
70 array( 'user', 'user_editcount', 'patch-user_editcount.sql' ),
73 function rename_table( $from, $to, $patch ) {
75 if ( $wgDatabase->tableExists( $from ) ) {
76 if ( $wgDatabase->tableExists( $to ) ) {
77 echo "...can't move table $from to $to, $to already exists.\n";
79 echo "Moving table $from to $to...";
80 dbsource( archive($patch), $wgDatabase );
84 // Source table does not exist
85 // Renames are done before creations, so this is typical for a new installation
90 function add_table( $name, $patch ) {
92 if ( $wgDatabase->tableExists( $name ) ) {
93 echo "...$name table already exists.\n";
95 echo "Creating $name table...";
96 dbsource( archive($patch), $wgDatabase );
101 function add_field( $table, $field, $patch ) {
103 if ( !$wgDatabase->tableExists( $table ) ) {
104 echo "...$table table does not exist, skipping new field patch\n";
105 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
106 echo "...have $field field in $table table.\n";
108 echo "Adding $field field to table $table...";
109 dbsource( archive($patch) , $wgDatabase );
114 function do_revision_updates() {
115 global $wgSoftwareRevision;
116 if ( $wgSoftwareRevision < 1001 ) {
121 function update_passwords() {
122 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
125 $fname = "Update script: update_passwords()";
126 print "\nIt appears that you need to update the user passwords in your\n" .
127 "database. If you have already done this (if you've run this update\n" .
128 "script once before, for example), doing so again will make all your\n" .
129 "user accounts inaccessible, so be sure you only do this once.\n" .
130 "Update user passwords? (yes/no)";
132 $resp = readconsole();
133 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
135 $sql = "SELECT user_id,user_password FROM user";
136 $source = $wgDatabase->query( $sql, $fname );
138 while ( $row = $wgDatabase->fetchObject( $source ) ) {
140 $oldpass = $row->user_password;
141 $newpass = md5( "{$id}-{$oldpass}" );
143 $sql = "UPDATE user SET user_password='{$newpass}' " .
144 "WHERE user_id={$id}";
145 $wgDatabase->query( $sql, $fname );
149 function do_interwiki_update() {
150 # Check that interwiki table exists; if it doesn't source it
151 global $wgDatabase, $IP;
152 if( $wgDatabase->tableExists( "interwiki" ) ) {
153 echo "...already have interwiki table\n";
156 echo "Creating interwiki table: ";
157 dbsource( archive("patch-interwiki.sql") );
159 echo "Adding default interwiki definitions: ";
160 dbsource( "$IP/maintenance/interwiki.sql" );
164 function do_index_update() {
165 # Check that proper indexes are in place
167 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
168 if( $meta->multiple_key == 0 ) {
169 echo "Updating indexes to 20031107: ";
170 dbsource( archive("patch-indexes.sql") );
174 echo "...indexes seem up to 20031107 standards\n";
178 function do_image_index_update() {
181 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
182 if( $meta->multiple_key == 0 ) {
183 echo "Updating indexes to 20050912: ";
184 dbsource( archive("patch-mimesearch-indexes.sql") );
188 echo "...indexes seem up to 20050912 standards\n";
192 function do_image_name_unique_update() {
194 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
195 echo "...image primary key already set.\n";
197 echo "Making img_name the primary key... ";
198 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
203 function do_logging_timestamp_index() {
205 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
206 echo "...timestamp key on logging already exists.\n";
208 echo "Adding timestamp key on logging table... ";
209 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
215 function do_watchlist_update() {
217 $fname = 'do_watchlist_update';
218 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
219 echo "The watchlist table is already set up for email notification.\n";
221 echo "Adding wl_notificationtimestamp field for email notification management.";
222 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
223 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
226 # Check if we need to add talk page rows to the watchlist
227 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
228 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
229 if ( $talk != $nontalk ) {
230 echo "Adding missing watchlist talk page rows... ";
233 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
235 'wl_user' => 'wl_user',
236 'wl_namespace' => 'wl_namespace | 1',
237 'wl_title' => 'wl_title',
238 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
239 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
242 echo "...watchlist talk page rows already present\n";
246 function do_copy_newtalk_to_watchlist() {
248 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
250 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
251 $wgDatabase->tableName( 'user_newtalk' ) );
252 $num_newtalks=$wgDatabase->numRows($res);
253 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
256 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
257 $wluser = $wgDatabase->fetchObject( $res );
258 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
259 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
260 $wgDatabase->replace( 'watchlist',
261 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
262 array('wl_user' => 0,
263 'wl_namespace' => NS_USER_TALK,
264 'wl_title' => $wluser->user_ip,
265 'wl_notificationtimestamp' => '19700101000000'
266 ), 'updaters.inc::do_watchlist_update2'
269 } else { # normal users ... have user_ids
270 $user->setID($wluser->user_id);
271 $wgDatabase->replace( 'watchlist',
272 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
273 array('wl_user' => $user->getID(),
274 'wl_namespace' => NS_USER_TALK,
275 'wl_title' => $user->getName(),
276 'wl_notificationtimestamp' => '19700101000000'
277 ), 'updaters.inc::do_watchlist_update3'
285 function do_user_update() {
287 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
288 echo "User table contains old email authentication field. Dropping... ";
289 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
292 echo "...user table does not contain old email authentication field.\n";
297 * 1.4 betas were missing the 'binary' marker from logging.log_title,
298 * which causes a collation mismatch error on joins in MySQL 4.1.
300 function do_logging_encoding() {
301 global $wgDatabase, $wgDBtype;
302 if ($wgDBtype != 'mysql')
304 $logging = $wgDatabase->tableName( 'logging' );
305 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
306 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
307 $wgDatabase->freeResult( $res );
309 if( in_array( 'binary', $flags ) ) {
310 echo "Logging table has correct title encoding.\n";
312 echo "Fixing title encoding on logging table... ";
313 dbsource( archive( 'patch-logging-title.sql' ), $wgDatabase );
318 function do_schema_restructuring() {
320 $fname="do_schema_restructuring";
321 if ( $wgDatabase->tableExists( 'page' ) ) {
322 echo "...page table already exists.\n";
324 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
326 echo "......checking for duplicate entries.\n"; flush();
328 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
330 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
331 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
333 if ( $wgDatabase->numRows( $rows ) > 0 ) {
335 echo "......<b>Found duplicate entries</b>\n";
336 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
337 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
338 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
339 $duplicate[$row->cur_namespace] = array();
341 $duplicate[$row->cur_namespace][] = $row->cur_title;
342 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
344 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
346 foreach ( $duplicate as $ns => $titles ) {
352 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
354 foreach ( $titles as $t ) {
356 $sql .= $wgDatabase->addQuotes( $t );
359 $sql .= ', ' . $wgDatabase->addQuotes( $t );
364 # By sorting descending, the most recent entry will be the first in the list.
365 # All following entries will be deleted by the next while-loop.
366 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
368 $rows = $wgDatabase->query( $sql, $fname );
370 $prev_title = $prev_namespace = false;
373 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
374 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
375 $deleteId[] = $row->cur_id;
377 $prev_title = $row->cur_title;
378 $prev_namespace = $row->cur_namespace;
380 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
381 $rows = $wgDatabase->query( $sql, $fname );
383 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
388 echo "......Creating tables.\n";
389 $wgDatabase->query("CREATE TABLE $page (
390 page_id int(8) unsigned NOT NULL auto_increment,
391 page_namespace int NOT NULL,
392 page_title varchar(255) binary NOT NULL,
393 page_restrictions tinyblob NOT NULL,
394 page_counter bigint(20) unsigned NOT NULL default '0',
395 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
396 page_is_new tinyint(1) unsigned NOT NULL default '0',
397 page_random real unsigned NOT NULL,
398 page_touched char(14) binary NOT NULL default '',
399 page_latest int(8) unsigned NOT NULL,
400 page_len int(8) unsigned NOT NULL,
402 PRIMARY KEY page_id (page_id),
403 UNIQUE INDEX name_title (page_namespace,page_title),
406 ) TYPE=InnoDB", $fname );
407 $wgDatabase->query("CREATE TABLE $revision (
408 rev_id int(8) unsigned NOT NULL auto_increment,
409 rev_page int(8) unsigned NOT NULL,
410 rev_comment tinyblob NOT NULL,
411 rev_user int(5) unsigned NOT NULL default '0',
412 rev_user_text varchar(255) binary NOT NULL default '',
413 rev_timestamp char(14) binary NOT NULL default '',
414 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
415 rev_deleted tinyint(1) unsigned NOT NULL default '0',
417 PRIMARY KEY rev_page_id (rev_page, rev_id),
418 UNIQUE INDEX rev_id (rev_id),
419 INDEX rev_timestamp (rev_timestamp),
420 INDEX page_timestamp (rev_page,rev_timestamp),
421 INDEX user_timestamp (rev_user,rev_timestamp),
422 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
423 ) TYPE=InnoDB", $fname );
426 echo "......Locking tables.\n";
427 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
429 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
431 echo "......maxold is {$maxold}\n";
434 global $wgLegacySchemaConversion;
435 if( $wgLegacySchemaConversion ) {
436 // Create HistoryBlobCurStub entries.
437 // Text will be pulled from the leftover 'cur' table at runtime.
438 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
439 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
440 $cur_flags = "'object'";
442 // Copy all cur text in immediately: this may take longer but avoids
443 // having to keep an extra table around.
444 echo "......Moving text from cur.\n";
445 $cur_text = 'cur_text';
448 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
449 old_timestamp, old_minor_edit, old_flags)
450 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
451 FROM $cur", $fname );
454 echo "......Setting up revision table.\n";
455 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
457 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
458 old_timestamp, old_minor_edit
459 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
462 echo "......Setting up page table.\n";
463 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
464 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
465 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
466 cur_random, cur_touched, rev_id, LENGTH(cur_text)
468 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
471 echo "......Unlocking tables.\n";
472 $wgDatabase->query( "UNLOCK TABLES", $fname );
475 echo "......Renaming old.\n";
476 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
483 function do_inverse_timestamp() {
485 $fname="do_schema_restructuring";
486 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
487 echo "Removing revision.inverse_timestamp and fixing indexes... ";
488 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
491 echo "revision timestamp indexes already up to 2005-03-13\n";
495 function do_text_id() {
497 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
498 echo "...rev_text_id already in place.\n";
500 echo "Adding rev_text_id field... ";
501 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
506 function do_namespace_size() {
510 'recentchanges' => 'rc',
512 'querycache' => 'qc',
515 foreach( $tables as $table => $prefix ) {
516 do_namespace_size_on( $table, $prefix );
521 function do_namespace_size_on( $table, $prefix ) {
522 global $wgDatabase, $wgDBtype;
523 if ($wgDBtype != 'mysql')
525 $field = $prefix . '_namespace';
527 $tablename = $wgDatabase->tableName( $table );
528 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
529 $info = $wgDatabase->fetchObject( $result );
530 $wgDatabase->freeResult( $result );
532 if( substr( $info->Type, 0, 3 ) == 'int' ) {
533 echo "...$field is already a full int ($info->Type).\n";
535 echo "Promoting $field from $info->Type to int... ";
537 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
538 $wgDatabase->query( $sql );
544 function do_pagelinks_update() {
546 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
547 echo "...already have pagelinks table.\n";
549 echo "Converting links and brokenlinks tables to pagelinks... ";
550 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
554 global $wgCanonicalNamespaceNames;
555 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
557 do_pagelinks_namespace( $ns );
563 function do_pagelinks_namespace( $namespace ) {
564 global $wgDatabase, $wgContLang;
566 $ns = intval( $namespace );
567 echo "Cleaning up broken links for namespace $ns... ";
569 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
570 $name = $wgContLang->getNsText( $ns );
571 $prefix = $wgDatabase->strencode( $name );
572 $likeprefix = str_replace( '_', '\\_', $prefix);
574 $sql = "UPDATE $pagelinks
575 SET pl_namespace=$ns,
576 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
578 AND pl_title LIKE '$likeprefix:%'";
580 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
584 function do_drop_img_type() {
587 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
588 echo "Dropping unused img_type field in image table... ";
589 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
592 echo "No img_type field in image table; Good.\n";
596 function do_old_links_update() {
598 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
599 echo "Already have pagelinks; skipping old links table updates.\n";
601 convertLinks(); flush();
605 function do_user_unique_update() {
607 $duper = new UserDupes( $wgDatabase );
608 if( $duper->hasUniqueIndex() ) {
609 echo "Already have unique user_name index.\n";
611 if( !$duper->clearDupes() ) {
612 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
614 echo "Adding unique index on user_name... ";
615 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
620 function do_user_groups_update() {
621 $fname = 'do_user_groups_update';
624 if( $wgDatabase->tableExists( 'user_groups' ) ) {
625 echo "...user_groups table already exists.\n";
626 return do_user_groups_reformat();
629 echo "Adding user_groups table... ";
630 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
633 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
634 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
635 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
636 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
639 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
640 echo "*** You may need to manually configure some sysops by manipulating\n";
641 echo "*** the user_groups table.\n";
646 echo "Converting user_rights table to user_groups... ";
647 $result = $wgDatabase->select( 'user_rights',
648 array( 'ur_user', 'ur_rights' ),
649 array( "ur_rights != ''" ),
652 while( $row = $wgDatabase->fetchObject( $result ) ) {
653 $groups = array_unique(
655 explode( ',', $row->ur_rights ) ) );
657 foreach( $groups as $group ) {
658 $wgDatabase->insert( 'user_groups',
660 'ug_user' => $row->ur_user,
661 'ug_group' => $group ),
665 $wgDatabase->freeResult( $result );
669 function do_user_groups_reformat() {
670 # Check for bogus formats from previous 1.5 alpha code.
672 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
674 if( $info->type == 'int' ) {
675 $oldug = $wgDatabase->tableName( 'user_groups' );
676 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
677 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
678 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
681 echo "Re-adding fresh user_groups table... ";
682 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
686 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
687 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
690 echo "...user_groups is in current format.\n";
695 function do_watchlist_null() {
696 # Make sure wl_notificationtimestamp can be NULL,
697 # and update old broken items.
699 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
701 if( $info->not_null ) {
702 echo "Making wl_notificationtimestamp nullable... ";
703 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
706 echo "...wl_notificationtimestamp is already nullable.\n";
714 function do_page_random_update() {
717 echo "Setting page_random to a random value on rows where it equals 0...";
719 $page = $wgDatabase->tableName( 'page' );
720 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
721 $rows = $wgDatabase->affectedRows();
723 echo "changed $rows rows\n";
726 function do_templatelinks_update() {
727 global $wgDatabase, $wgLoadBalancer;
728 $fname = 'do_templatelinks_update';
730 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
731 echo "...templatelinks table already exists\n";
734 echo "Creating templatelinks table...\n";
735 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
736 echo "Populating...\n";
737 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
738 // Slow, replication-friendly update
739 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
740 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
742 while ( $row = $wgDatabase->fetchObject( $res ) ) {
743 $count = ($count + 1) % 100;
745 if ( function_exists( 'wfWaitForSlaves' ) ) {
746 wfWaitForSlaves( 10 );
751 $wgDatabase->insert( 'templatelinks',
753 'tl_from' => $row->pl_from,
754 'tl_namespace' => $row->pl_namespace,
755 'tl_title' => $row->pl_title,
760 $wgDatabase->freeResult( $res );
763 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
765 'tl_from' => 'pl_from',
766 'tl_namespace' => 'pl_namespace',
767 'tl_title' => 'pl_title'
773 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
777 # Add ( rc_namespace, rc_user_text ) index [R. Church]
778 function do_rc_indices_update() {
780 echo( "Checking for additional recent changes indices...\n" );
781 # See if we can find the index we want
782 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_ns_usertext', __METHOD__ );
785 echo( "...index on ( rc_namespace, rc_user_text ) not found; creating\n" );
786 dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
788 # Index seems to exist
789 echo( "...index on ( rc_namespace, rc_user_text ) seems to be ok\n" );
792 #Add (rc_user_text, rc_timestamp) index [A. Garrett], November 2006
793 # See if we can find the index we want
794 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_user_text', __METHOD__ );
797 echo( "...index on ( rc_user_text, rc_timestamp ) not found; creating\n" );
798 dbsource( archive( 'patch-rc_user_text-index.sql' ) );
800 # Index seems to exist
801 echo( "...index on ( rc_user_text, rc_timestamp ) seems to be ok\n" );
805 function index_has_field($table, $index, $field) {
807 echo( "Checking if $table index $index includes field $field...\n" );
808 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
810 foreach($info as $row) {
811 if($row->Column_name == $field) {
812 echo( "...index $index on table $table seems to be ok\n" );
817 echo( "...index $index on table $table has no field $field; adding\n" );
821 function do_backlinking_indices_update() {
822 echo( "Checking for backlinking indices...\n" );
823 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
824 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
825 !index_has_field('imagelinks', 'il_to', 'il_from'))
827 dbsource( archive( 'patch-backlinkindexes.sql' ) );
831 function purge_cache() {
833 # We can't guarantee that the user will be able to use TRUNCATE,
834 # but we know that DELETE is available to us
835 echo( "Purging caches..." );
836 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
840 function do_all_updates( $shared = false, $purge = true ) {
841 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
843 $doUser = !$wgSharedDB || $doShared;
845 if ($wgDBtype === 'postgres') {
846 do_postgres_updates();
851 foreach ( $wgRenamedTables as $tableRecord ) {
852 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
856 foreach ( $wgNewTables as $tableRecord ) {
857 add_table( $tableRecord[0], $tableRecord[1] );
862 foreach ( $wgNewFields as $fieldRecord ) {
863 if ( $fieldRecord[0] != 'user' || $doUser ) {
864 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
869 # Do schema updates which require special handling
870 do_interwiki_update(); flush();
871 do_index_update(); flush();
872 do_old_links_update(); flush();
873 do_image_name_unique_update(); flush();
874 do_watchlist_update(); flush();
876 do_user_update(); flush();
878 ###### do_copy_newtalk_to_watchlist(); flush();
879 do_logging_encoding(); flush();
881 do_schema_restructuring(); flush();
882 do_inverse_timestamp(); flush();
883 do_text_id(); flush();
884 do_namespace_size(); flush();
886 do_pagelinks_update(); flush();
887 do_templatelinks_update(); flush(); // after pagelinks
889 do_drop_img_type(); flush();
892 do_user_unique_update(); flush();
894 do_user_groups_update(); flush();
896 do_watchlist_null(); flush();
898 //do_image_index_update(); flush();
900 do_logging_timestamp_index(); flush();
902 do_page_random_update(); flush();
904 do_rc_indices_update(); flush();
906 do_backlinking_indices_update(); flush();
908 echo "Deleting old default messages..."; flush();
909 deleteDefaultMessages();
910 echo "Done\n"; flush();
918 function archive($name) {
919 global $wgDBtype, $IP;
922 return "$IP/maintenance/oracle/archives/$name";
924 return "$IP/maintenance/archives/$name";
928 function do_postgres_updates() {
929 global $wgDatabase, $wgVersion, $wgDBmwschema;
931 # Just in case their LocalSetings.php does not have this:
932 if ( !isset( $wgDBmwschema ))
933 $wgDBmwschema = 'mediawiki';
935 ## Default to the oldest supported version
938 if ($wgDatabase->tableExists("mediawiki_version")) {
940 $sql = "SELECT mw_version FROM mediawiki_version ORDER BY cdate DESC LIMIT 1";
941 $tempversion = pg_fetch_result($wgDatabase->doQuery($sql),0,0);
943 if (preg_match('/(\d+\.\d+)/', $tempversion, $thisver)) {
944 $version = $thisver[1];
948 print " Detected version: $version ";
951 if ($version <= 1.7) {
955 ALTER TABLE oldimage ALTER oi_size TYPE INTEGER;
956 ALTER TABLE oldimage ALTER oi_width TYPE INTEGER;
957 ALTER TABLE oldimage ALTER oi_height TYPE INTEGER;
959 ALTER TABLE image ALTER img_size TYPE INTEGER;
960 ALTER TABLE image ALTER img_width TYPE INTEGER;
961 ALTER TABLE image ALTER img_height TYPE INTEGER;
963 -- Constraint tweaking:
964 ALTER TABLE recentchanges ALTER rc_cur_id DROP NOT NULL;
967 ALTER TABLE ipblocks ADD ipb_anon_only CHAR NOT NULL DEFAULT '0';
968 ALTER TABLE ipblocks ADD ipb_create_account CHAR NOT NULL DEFAULT '1';
970 -- Index order rearrangements:
971 DROP INDEX pagelink_unique;
972 CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title);
975 ALTER TABLE "user" RENAME TO mwuser;
976 ALTER TABLE "text" RENAME to pagecontent;
979 CREATE TABLE profiling (
980 pf_count INTEGER NOT NULL DEFAULT 0,
981 pf_time NUMERIC(18,10) NOT NULL DEFAULT 0,
982 pf_name TEXT NOT NULL,
985 CREATE UNIQUE INDEX pf_name_server ON profiling (pf_name, pf_server);
987 CREATE TABLE mediawiki_version (
989 mw_version TEXT NOT NULL,
992 pg_version TEXT NULL,
997 ts2_schema TEXT NULL,
1000 sql_version TEXT NULL,
1002 cdate TIMESTAMPTZ NOT NULL DEFAULT now()
1005 -- Special modifications
1006 ALTER TABLE archive RENAME to archive2;
1007 CREATE VIEW archive AS
1009 ar_namespace, ar_title, ar_text, ar_comment, ar_user, ar_user_text,
1010 ar_minor_edit, ar_flags, ar_rev_id, ar_text_id,
1011 TO_CHAR(ar_timestamp, 'YYYYMMDDHH24MISS') AS ar_timestamp
1014 CREATE RULE archive_insert AS ON INSERT TO archive
1015 DO INSTEAD INSERT INTO archive2 VALUES (
1016 NEW.ar_namespace, NEW.ar_title, NEW.ar_text, NEW.ar_comment, NEW.ar_user, NEW.ar_user_text,
1017 TO_DATE(NEW.ar_timestamp, 'YYYYMMDDHH24MISS'),
1018 NEW.ar_minor_edit, NEW.ar_flags, NEW.ar_rev_id, NEW.ar_text_id
1021 CREATE FUNCTION page_deleted() RETURNS TRIGGER LANGUAGE plpgsql AS
1024 DELETE FROM recentchanges WHERE rc_namespace = OLD.page_namespace AND rc_title = OLD.page_title;
1029 CREATE TRIGGER page_deleted AFTER DELETE ON page
1030 FOR EACH ROW EXECUTE PROCEDURE page_deleted();
1034 } ## end version 1.7
1036 else if ($version <= 1.8) {
1039 -- Tighten up restrictions on the revision table so we don't lose data:
1040 ALTER TABLE revision DROP CONSTRAINT revision_rev_user_fkey;
1041 ALTER TABLE revision ADD CONSTRAINT revision_rev_user_fkey
1042 FOREIGN KEY (rev_user) REFERENCES mwuser(user_id) ON DELETE RESTRICT;
1044 -- New columns for better password tracking:
1045 ALTER TABLE mwuser ADD user_newpass_time TIMESTAMPTZ;
1046 ALTER TABLE mwuser ADD user_editcount INTEGER;
1048 -- New column for autoblocking problem users
1049 ALTER TABLE ipblocks ADD ipb_enable_autoblock CHAR NOT NULL DEFAULT '1';
1051 -- Despite it's name, ipb_address does not necessarily contain IP addresses :)
1052 ALTER TABLE ipblocks ALTER ipb_address TYPE TEXT USING ipb_address::TEXT;
1055 CREATE TABLE redirect (
1056 rd_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE,
1057 rd_namespace SMALLINT NOT NULL,
1058 rd_title TEXT NOT NULL
1060 CREATE INDEX redirect_ns_title ON redirect (rd_namespace,rd_title,rd_from);
1062 CREATE TABLE querycachetwo (
1063 qcc_type TEXT NOT NULL,
1064 qcc_value SMALLINT NOT NULL DEFAULT 0,
1065 qcc_namespace INTEGER NOT NULL DEFAULT 0,
1066 qcc_title TEXT NOT NULL DEFAULT '',
1067 qcc_namespacetwo INTEGER NOT NULL DEFAULT 0,
1068 qcc_titletwo TEXT NOT NULL DEFAULT ''
1070 CREATE INDEX querycachetwo_type_value ON querycachetwo (qcc_type, qcc_value);
1071 CREATE INDEX querycachetwo_title ON querycachetwo (qcc_type,qcc_namespace,qcc_title);
1072 CREATE INDEX querycachetwo_titletwo ON querycachetwo (qcc_type,qcc_namespacetwo,qcc_titletwo);
1074 -- New columns for fancy recentchanges display
1075 ALTER TABLE recentchanges ADD rc_old_len INT;
1076 ALTER TABLE recentchanges ADD rc_new_len INT;
1078 -- Note this upgrade
1079 INSERT INTO mediawiki_version (type,mw_version,notes)
1080 VALUES ('Upgrade','MWVERSION','Upgrade from older version THISVERSION');
1086 if ( !strlen($upgrade)) {
1087 print "No updates needed for version $version\n";
1091 $upgrade = str_replace( 'MWVERSION', $wgVersion, $upgrade );
1092 $upgrade = str_replace( 'THISVERSION', $version, $upgrade );
1093 $res = $wgDatabase->query("BEGIN;\n\n $upgrade\n\nCOMMIT;\n");