3 * @addtogroup Maintenance
8 if ( !defined( 'MEDIAWIKI' ) ) {
9 echo "This file is not a valid entry point\n";
13 require_once 'convertLinks.inc';
14 require_once 'userDupes.inc';
15 require_once 'deleteDefaultMessages.php';
17 $wgRenamedTables = array(
19 # array( 'group', 'groups', 'patch-rename-group.sql' ),
23 # table patch file (in maintenance/archives)
24 array( 'hitcounter', 'patch-hitcounter.sql' ),
25 array( 'querycache', 'patch-querycache.sql' ),
26 array( 'objectcache', 'patch-objectcache.sql' ),
27 array( 'categorylinks', 'patch-categorylinks.sql' ),
28 array( 'logging', 'patch-logging.sql' ),
29 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
30 array( 'transcache', 'patch-transcache.sql' ),
31 array( 'trackbacks', 'patch-trackbacks.sql' ),
32 array( 'externallinks', 'patch-externallinks.sql' ),
33 array( 'job', 'patch-job.sql' ),
34 array( 'langlinks', 'patch-langlinks.sql' ),
35 array( 'querycache_info', 'patch-querycacheinfo.sql' ),
36 array( 'filearchive', 'patch-filearchive.sql' ),
37 array( 'querycachetwo', 'patch-querycachetwo.sql' ),
41 # table field patch file (in maintenance/archives)
42 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
43 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
44 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
45 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
46 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
47 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
48 array( 'recentchanges', 'rc_old_len', 'patch-rc_len.sql' ),
49 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
50 array( 'user', 'user_token', 'patch-user_token.sql' ),
51 array( 'user', 'user_email_token', 'patch-user_email_token.sql' ),
52 array( 'user', 'user_registration','patch-user_registration.sql' ),
53 array( 'logging', 'log_params', 'patch-log_params.sql' ),
54 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
55 array( 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
56 array( 'page', 'page_len', 'patch-page_len.sql' ),
57 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
58 array( 'image', 'img_width', 'patch-img_width.sql' ),
59 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
60 array( 'image', 'img_media_type', 'patch-img_media_type.sql' ),
61 array( 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ),
62 array( 'interwiki', 'iw_trans', 'patch-interwiki-trans.sql' ),
63 array( 'ipblocks', 'ipb_range_start', 'patch-ipb_range_start.sql' ),
64 array( 'site_stats', 'ss_images', 'patch-ss_images.sql' ),
65 array( 'ipblocks', 'ipb_anon_only', 'patch-ipb_anon_only.sql' ),
66 array( 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
67 array( 'user', 'user_newpass_time','patch-user_newpass_time.sql' ),
68 array( 'user', 'user_editcount', 'patch-user_editcount.sql' ),
71 function rename_table( $from, $to, $patch ) {
73 if ( $wgDatabase->tableExists( $from ) ) {
74 if ( $wgDatabase->tableExists( $to ) ) {
75 echo "...can't move table $from to $to, $to already exists.\n";
77 echo "Moving table $from to $to...";
78 dbsource( archive($patch), $wgDatabase );
82 // Source table does not exist
83 // Renames are done before creations, so this is typical for a new installation
88 function add_table( $name, $patch ) {
90 if ( $wgDatabase->tableExists( $name ) ) {
91 echo "...$name table already exists.\n";
93 echo "Creating $name table...";
94 dbsource( archive($patch), $wgDatabase );
99 function add_field( $table, $field, $patch ) {
101 if ( !$wgDatabase->tableExists( $table ) ) {
102 echo "...$table table does not exist, skipping new field patch\n";
103 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
104 echo "...have $field field in $table table.\n";
106 echo "Adding $field field to table $table...";
107 dbsource( archive($patch) , $wgDatabase );
112 function do_revision_updates() {
113 global $wgSoftwareRevision;
114 if ( $wgSoftwareRevision < 1001 ) {
119 function update_passwords() {
120 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
123 $fname = "Update script: update_passwords()";
124 print "\nIt appears that you need to update the user passwords in your\n" .
125 "database. If you have already done this (if you've run this update\n" .
126 "script once before, for example), doing so again will make all your\n" .
127 "user accounts inaccessible, so be sure you only do this once.\n" .
128 "Update user passwords? (yes/no)";
130 $resp = readconsole();
131 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
133 $sql = "SELECT user_id,user_password FROM user";
134 $source = $wgDatabase->query( $sql, $fname );
136 while ( $row = $wgDatabase->fetchObject( $source ) ) {
138 $oldpass = $row->user_password;
139 $newpass = md5( "{$id}-{$oldpass}" );
141 $sql = "UPDATE user SET user_password='{$newpass}' " .
142 "WHERE user_id={$id}";
143 $wgDatabase->query( $sql, $fname );
147 function do_interwiki_update() {
148 # Check that interwiki table exists; if it doesn't source it
149 global $wgDatabase, $IP;
150 if( $wgDatabase->tableExists( "interwiki" ) ) {
151 echo "...already have interwiki table\n";
154 echo "Creating interwiki table: ";
155 dbsource( archive("patch-interwiki.sql") );
157 echo "Adding default interwiki definitions: ";
158 dbsource( "$IP/maintenance/interwiki.sql" );
162 function do_index_update() {
163 # Check that proper indexes are in place
165 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
166 if( $meta->multiple_key == 0 ) {
167 echo "Updating indexes to 20031107: ";
168 dbsource( archive("patch-indexes.sql") );
172 echo "...indexes seem up to 20031107 standards\n";
176 function do_image_index_update() {
179 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
180 if( $meta->multiple_key == 0 ) {
181 echo "Updating indexes to 20050912: ";
182 dbsource( archive("patch-mimesearch-indexes.sql") );
186 echo "...indexes seem up to 20050912 standards\n";
190 function do_image_name_unique_update() {
192 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
193 echo "...image primary key already set.\n";
195 echo "Making img_name the primary key... ";
196 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
201 function do_logging_timestamp_index() {
203 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
204 echo "...timestamp key on logging already exists.\n";
206 echo "Adding timestamp key on logging table... ";
207 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
213 function do_watchlist_update() {
215 $fname = 'do_watchlist_update';
216 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
217 echo "The watchlist table is already set up for email notification.\n";
219 echo "Adding wl_notificationtimestamp field for email notification management.";
220 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
221 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
224 # Check if we need to add talk page rows to the watchlist
225 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
226 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
227 if ( $talk != $nontalk ) {
228 echo "Adding missing watchlist talk page rows... ";
231 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
233 'wl_user' => 'wl_user',
234 'wl_namespace' => 'wl_namespace | 1',
235 'wl_title' => 'wl_title',
236 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
237 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
240 echo "...watchlist talk page rows already present\n";
244 function do_copy_newtalk_to_watchlist() {
246 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
248 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
249 $wgDatabase->tableName( 'user_newtalk' ) );
250 $num_newtalks=$wgDatabase->numRows($res);
251 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
254 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
255 $wluser = $wgDatabase->fetchObject( $res );
256 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
257 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
258 $wgDatabase->replace( 'watchlist',
259 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
260 array('wl_user' => 0,
261 'wl_namespace' => NS_USER_TALK,
262 'wl_title' => $wluser->user_ip,
263 'wl_notificationtimestamp' => '19700101000000'
264 ), 'updaters.inc::do_watchlist_update2'
267 } else { # normal users ... have user_ids
268 $user->setID($wluser->user_id);
269 $wgDatabase->replace( 'watchlist',
270 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
271 array('wl_user' => $user->getID(),
272 'wl_namespace' => NS_USER_TALK,
273 'wl_title' => $user->getName(),
274 'wl_notificationtimestamp' => '19700101000000'
275 ), 'updaters.inc::do_watchlist_update3'
283 function do_user_update() {
285 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
286 echo "User table contains old email authentication field. Dropping... ";
287 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
290 echo "...user table does not contain old email authentication field.\n";
295 * 1.4 betas were missing the 'binary' marker from logging.log_title,
296 * which causes a collation mismatch error on joins in MySQL 4.1.
298 function do_logging_encoding() {
299 global $wgDatabase, $wgDBtype;
300 if ($wgDBtype != 'mysql')
302 $logging = $wgDatabase->tableName( 'logging' );
303 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
304 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
305 $wgDatabase->freeResult( $res );
307 if( in_array( 'binary', $flags ) ) {
308 echo "Logging table has correct title encoding.\n";
310 echo "Fixing title encoding on logging table... ";
311 dbsource( archive( 'patch-logging-title.sql' ), $wgDatabase );
316 function do_schema_restructuring() {
318 $fname="do_schema_restructuring";
319 if ( $wgDatabase->tableExists( 'page' ) ) {
320 echo "...page table already exists.\n";
322 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
323 echo wfTimestamp( TS_DB );
324 echo "......checking for duplicate entries.\n"; flush();
326 list ($cur, $old, $page, $revision, $text) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
328 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
329 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
331 if ( $wgDatabase->numRows( $rows ) > 0 ) {
332 echo wfTimestamp( TS_DB );
333 echo "......<b>Found duplicate entries</b>\n";
334 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
335 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
336 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
337 $duplicate[$row->cur_namespace] = array();
339 $duplicate[$row->cur_namespace][] = $row->cur_title;
340 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
342 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
344 foreach ( $duplicate as $ns => $titles ) {
350 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
352 foreach ( $titles as $t ) {
354 $sql .= $wgDatabase->addQuotes( $t );
357 $sql .= ', ' . $wgDatabase->addQuotes( $t );
362 # By sorting descending, the most recent entry will be the first in the list.
363 # All following entries will be deleted by the next while-loop.
364 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
366 $rows = $wgDatabase->query( $sql, $fname );
368 $prev_title = $prev_namespace = false;
371 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
372 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
373 $deleteId[] = $row->cur_id;
375 $prev_title = $row->cur_title;
376 $prev_namespace = $row->cur_namespace;
378 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
379 $rows = $wgDatabase->query( $sql, $fname );
380 echo wfTimestamp( TS_DB );
381 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
385 echo wfTimestamp( TS_DB );
386 echo "......Creating tables.\n";
387 $wgDatabase->query("CREATE TABLE $page (
388 page_id int(8) unsigned NOT NULL auto_increment,
389 page_namespace int NOT NULL,
390 page_title varchar(255) binary NOT NULL,
391 page_restrictions tinyblob NOT NULL,
392 page_counter bigint(20) unsigned NOT NULL default '0',
393 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
394 page_is_new tinyint(1) unsigned NOT NULL default '0',
395 page_random real unsigned NOT NULL,
396 page_touched char(14) binary NOT NULL default '',
397 page_latest int(8) unsigned NOT NULL,
398 page_len int(8) unsigned NOT NULL,
400 PRIMARY KEY page_id (page_id),
401 UNIQUE INDEX name_title (page_namespace,page_title),
404 ) TYPE=InnoDB", $fname );
405 $wgDatabase->query("CREATE TABLE $revision (
406 rev_id int(8) unsigned NOT NULL auto_increment,
407 rev_page int(8) unsigned NOT NULL,
408 rev_comment tinyblob NOT NULL,
409 rev_user int(5) unsigned NOT NULL default '0',
410 rev_user_text varchar(255) binary NOT NULL default '',
411 rev_timestamp char(14) binary NOT NULL default '',
412 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
413 rev_deleted tinyint(1) unsigned NOT NULL default '0',
415 PRIMARY KEY rev_page_id (rev_page, rev_id),
416 UNIQUE INDEX rev_id (rev_id),
417 INDEX rev_timestamp (rev_timestamp),
418 INDEX page_timestamp (rev_page,rev_timestamp),
419 INDEX user_timestamp (rev_user,rev_timestamp),
420 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
421 ) TYPE=InnoDB", $fname );
423 echo wfTimestamp( TS_DB );
424 echo "......Locking tables.\n";
425 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
427 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
428 echo wfTimestamp( TS_DB );
429 echo "......maxold is {$maxold}\n";
431 echo wfTimestamp( TS_DB );
432 global $wgLegacySchemaConversion;
433 if( $wgLegacySchemaConversion ) {
434 // Create HistoryBlobCurStub entries.
435 // Text will be pulled from the leftover 'cur' table at runtime.
436 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
437 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
438 $cur_flags = "'object'";
440 // Copy all cur text in immediately: this may take longer but avoids
441 // having to keep an extra table around.
442 echo "......Moving text from cur.\n";
443 $cur_text = 'cur_text';
446 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
447 old_timestamp, old_minor_edit, old_flags)
448 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
449 FROM $cur", $fname );
451 echo wfTimestamp( TS_DB );
452 echo "......Setting up revision table.\n";
453 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
455 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
456 old_timestamp, old_minor_edit
457 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
459 echo wfTimestamp( TS_DB );
460 echo "......Setting up page table.\n";
461 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
462 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
463 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
464 cur_random, cur_touched, rev_id, LENGTH(cur_text)
466 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
468 echo wfTimestamp( TS_DB );
469 echo "......Unlocking tables.\n";
470 $wgDatabase->query( "UNLOCK TABLES", $fname );
472 echo wfTimestamp( TS_DB );
473 echo "......Renaming old.\n";
474 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
476 echo wfTimestamp( TS_DB );
481 function do_inverse_timestamp() {
483 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
484 echo "Removing revision.inverse_timestamp and fixing indexes... ";
485 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
488 echo "revision timestamp indexes already up to 2005-03-13\n";
492 function do_text_id() {
494 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
495 echo "...rev_text_id already in place.\n";
497 echo "Adding rev_text_id field... ";
498 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
503 function do_namespace_size() {
507 'recentchanges' => 'rc',
509 'querycache' => 'qc',
512 foreach( $tables as $table => $prefix ) {
513 do_namespace_size_on( $table, $prefix );
518 function do_namespace_size_on( $table, $prefix ) {
519 global $wgDatabase, $wgDBtype;
520 if ($wgDBtype != 'mysql')
522 $field = $prefix . '_namespace';
524 $tablename = $wgDatabase->tableName( $table );
525 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
526 $info = $wgDatabase->fetchObject( $result );
527 $wgDatabase->freeResult( $result );
529 if( substr( $info->Type, 0, 3 ) == 'int' ) {
530 echo "...$field is already a full int ($info->Type).\n";
532 echo "Promoting $field from $info->Type to int... ";
534 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
535 $wgDatabase->query( $sql );
541 function do_pagelinks_update() {
543 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
544 echo "...already have pagelinks table.\n";
546 echo "Converting links and brokenlinks tables to pagelinks... ";
547 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
551 global $wgCanonicalNamespaceNames;
552 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
554 do_pagelinks_namespace( $ns );
560 function do_pagelinks_namespace( $namespace ) {
561 global $wgDatabase, $wgContLang;
563 $ns = intval( $namespace );
564 echo "Cleaning up broken links for namespace $ns... ";
566 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
567 $name = $wgContLang->getNsText( $ns );
568 $prefix = $wgDatabase->strencode( $name );
569 $likeprefix = str_replace( '_', '\\_', $prefix);
571 $sql = "UPDATE $pagelinks
572 SET pl_namespace=$ns,
573 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
575 AND pl_title LIKE '$likeprefix:%'";
577 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
581 function do_drop_img_type() {
584 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
585 echo "Dropping unused img_type field in image table... ";
586 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
589 echo "No img_type field in image table; Good.\n";
593 function do_old_links_update() {
595 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
596 echo "Already have pagelinks; skipping old links table updates.\n";
598 convertLinks(); flush();
602 function do_user_unique_update() {
604 $duper = new UserDupes( $wgDatabase );
605 if( $duper->hasUniqueIndex() ) {
606 echo "Already have unique user_name index.\n";
608 if( !$duper->clearDupes() ) {
609 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
611 echo "Adding unique index on user_name... ";
612 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
617 function do_user_groups_update() {
618 $fname = 'do_user_groups_update';
621 if( $wgDatabase->tableExists( 'user_groups' ) ) {
622 echo "...user_groups table already exists.\n";
623 return do_user_groups_reformat();
626 echo "Adding user_groups table... ";
627 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
630 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
631 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
632 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
633 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
636 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
637 echo "*** You may need to manually configure some sysops by manipulating\n";
638 echo "*** the user_groups table.\n";
643 echo "Converting user_rights table to user_groups... ";
644 $result = $wgDatabase->select( 'user_rights',
645 array( 'ur_user', 'ur_rights' ),
646 array( "ur_rights != ''" ),
649 while( $row = $wgDatabase->fetchObject( $result ) ) {
650 $groups = array_unique(
652 explode( ',', $row->ur_rights ) ) );
654 foreach( $groups as $group ) {
655 $wgDatabase->insert( 'user_groups',
657 'ug_user' => $row->ur_user,
658 'ug_group' => $group ),
662 $wgDatabase->freeResult( $result );
666 function do_user_groups_reformat() {
667 # Check for bogus formats from previous 1.5 alpha code.
669 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
671 if( $info->type == 'int' ) {
672 $oldug = $wgDatabase->tableName( 'user_groups' );
673 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
674 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
675 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
678 echo "Re-adding fresh user_groups table... ";
679 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
683 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
684 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
687 echo "...user_groups is in current format.\n";
692 function do_watchlist_null() {
693 # Make sure wl_notificationtimestamp can be NULL,
694 # and update old broken items.
696 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
698 if( $info->not_null ) {
699 echo "Making wl_notificationtimestamp nullable... ";
700 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
703 echo "...wl_notificationtimestamp is already nullable.\n";
711 function do_page_random_update() {
714 echo "Setting page_random to a random value on rows where it equals 0...";
716 $page = $wgDatabase->tableName( 'page' );
717 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
718 $rows = $wgDatabase->affectedRows();
720 echo "changed $rows rows\n";
723 function do_templatelinks_update() {
724 global $wgDatabase, $wgLoadBalancer;
725 $fname = 'do_templatelinks_update';
727 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
728 echo "...templatelinks table already exists\n";
731 echo "Creating templatelinks table...\n";
732 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
733 echo "Populating...\n";
734 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
735 // Slow, replication-friendly update
736 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
737 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
739 while ( $row = $wgDatabase->fetchObject( $res ) ) {
740 $count = ($count + 1) % 100;
742 if ( function_exists( 'wfWaitForSlaves' ) ) {
743 wfWaitForSlaves( 10 );
748 $wgDatabase->insert( 'templatelinks',
750 'tl_from' => $row->pl_from,
751 'tl_namespace' => $row->pl_namespace,
752 'tl_title' => $row->pl_title,
757 $wgDatabase->freeResult( $res );
760 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
762 'tl_from' => 'pl_from',
763 'tl_namespace' => 'pl_namespace',
764 'tl_title' => 'pl_title'
770 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
774 # Add ( rc_namespace, rc_user_text ) index [R. Church]
775 function do_rc_indices_update() {
777 echo( "Checking for additional recent changes indices...\n" );
778 # See if we can find the index we want
779 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_ns_usertext', __METHOD__ );
782 echo( "...index on ( rc_namespace, rc_user_text ) not found; creating\n" );
783 dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
785 # Index seems to exist
786 echo( "...index on ( rc_namespace, rc_user_text ) seems to be ok\n" );
789 #Add (rc_user_text, rc_timestamp) index [A. Garrett], November 2006
790 # See if we can find the index we want
791 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_user_text', __METHOD__ );
794 echo( "...index on ( rc_user_text, rc_timestamp ) not found; creating\n" );
795 dbsource( archive( 'patch-rc_user_text-index.sql' ) );
797 # Index seems to exist
798 echo( "...index on ( rc_user_text, rc_timestamp ) seems to be ok\n" );
802 function index_has_field($table, $index, $field) {
804 echo( "Checking if $table index $index includes field $field...\n" );
805 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
807 foreach($info as $row) {
808 if($row->Column_name == $field) {
809 echo( "...index $index on table $table seems to be ok\n" );
814 echo( "...index $index on table $table has no field $field; adding\n" );
818 function do_backlinking_indices_update() {
819 echo( "Checking for backlinking indices...\n" );
820 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
821 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
822 !index_has_field('imagelinks', 'il_to', 'il_from'))
824 dbsource( archive( 'patch-backlinkindexes.sql' ) );
828 function do_stats_init() {
829 // Sometimes site_stats table is not properly populated.
831 echo "Checking site_stats row...";
832 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
833 if( $row === false ) {
834 echo "data is missing! rebuilding...\n";
837 require_once "$IP/maintenance/initStats.inc";
844 function purge_cache() {
846 # We can't guarantee that the user will be able to use TRUNCATE,
847 # but we know that DELETE is available to us
848 echo( "Purging caches..." );
849 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
853 function do_all_updates( $shared = false, $purge = true ) {
854 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
856 $doUser = !$wgSharedDB || $shared;
858 if ($wgDBtype === 'postgres') {
859 do_postgres_updates();
864 foreach ( $wgRenamedTables as $tableRecord ) {
865 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
869 foreach ( $wgNewTables as $tableRecord ) {
870 add_table( $tableRecord[0], $tableRecord[1] );
875 foreach ( $wgNewFields as $fieldRecord ) {
876 if ( $fieldRecord[0] != 'user' || $doUser ) {
877 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
882 # Do schema updates which require special handling
883 do_interwiki_update(); flush();
884 do_index_update(); flush();
885 do_old_links_update(); flush();
886 do_image_name_unique_update(); flush();
887 do_watchlist_update(); flush();
889 do_user_update(); flush();
891 ###### do_copy_newtalk_to_watchlist(); flush();
892 do_logging_encoding(); flush();
894 do_schema_restructuring(); flush();
895 do_inverse_timestamp(); flush();
896 do_text_id(); flush();
897 do_namespace_size(); flush();
899 do_pagelinks_update(); flush();
900 do_templatelinks_update(); flush(); // after pagelinks
902 do_drop_img_type(); flush();
905 do_user_unique_update(); flush();
907 do_user_groups_update(); flush();
909 do_watchlist_null(); flush();
911 //do_image_index_update(); flush();
913 do_logging_timestamp_index(); flush();
915 do_page_random_update(); flush();
917 do_rc_indices_update(); flush();
919 add_table( 'redirect', 'patch-redirect.sql' );
921 do_backlinking_indices_update(); flush();
923 do_restrictions_update(); flush ();
925 echo "Deleting old default messages..."; flush();
926 deleteDefaultMessages();
927 echo "Done\n"; flush();
929 do_stats_init(); flush();
937 function archive($name) {
938 global $wgDBtype, $IP;
940 // @fixme: add mysql5 and postgres items or....?
942 return "$IP/maintenance/archives/$name";
946 function do_restrictions_update() {
947 # Adding page_restrictions table, obsoleting page.page_restrictions.
948 # Migrating old restrictions to new table
949 # -- Andrew Garrett, January 2007.
953 $name = 'page_restrictions';
954 $patch = 'patch-page_restrictions.sql';
956 if ( $wgDatabase->tableExists( $name ) ) {
957 echo "...$name table already exists.\n";
959 echo "Creating $name table...";
960 dbsource( archive($patch), $wgDatabase );
963 echo "Migrating old restrictions to new table...";
965 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array("page_restrictions!=''", "page_restrictions!='edit=:move='"), __METHOD__ );
969 while ($row = $wgDatabase->fetchObject($res) ) {
970 $count = ($count + 1) % 100;
973 if ( function_exists( 'wfWaitForSlaves' ) ) {
974 wfWaitForSlaves( 10 );
980 # Figure out what the restrictions are..
982 $flatrestrictions = explode( ':', $row->page_restrictions );
984 $restrictions = array ();
985 foreach( $flatrestrictions as $restriction ) {
986 $thisrestriction = explode( '=', $restriction, 2 );
987 if( count( $thisrestriction ) == 1 ) {
988 // Compatibility with old protections from before
989 // separate move protection was added.
990 list( $level ) = $thisrestriction;
992 $restrictions['edit'] = $level;
993 $restrictions['move'] = $level;
996 list( $type, $level ) = $thisrestriction;
998 $restrictions[$type] = $level;
1002 $wgDatabase->update( 'page', array ( 'page_restrictions' => ''), array( 'page_id' => $id ), __METHOD__ );
1006 foreach( $restrictions as $type => $level ) {
1007 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
1009 'pr_level' => $level,
1010 'pr_cascade' => 0 ),
1019 function do_postgres_updates() {
1020 global $wgDatabase, $wgVersion, $wgDBmwschema;
1022 # Just in case their LocalSetings.php does not have this:
1023 if ( !isset( $wgDBmwschema ))
1024 $wgDBmwschema = 'mediawiki';
1026 ## Default to the oldest supported version
1030 if ($wgDatabase->tableExists("mediawiki_version")) {
1032 $sql = "SELECT mw_version FROM mediawiki_version ORDER BY cdate DESC LIMIT 1";
1033 $tempversion = pg_fetch_result($wgDatabase->doQuery($sql),0,0);
1034 if (preg_match('/(\d+\.\d+)/', $tempversion, $thisver)) {
1035 $version = $thisver[1];
1039 print " Detected version: $version\n";
1041 ## Transform to a standard format
1042 if (! preg_match('/(\d+)\.(\d+)/', $version, $thisver)) {
1043 die ("Sorry, could not figure out what version \"$version\" means");
1045 $oldversion = $version;
1046 $version = sprintf("%d%03d",$thisver[1],$thisver[2]);
1047 print " Standardized version: $version\n";
1052 if ($version < 1008) {
1053 $upgrade .= <<<PGEND
1056 ALTER TABLE oldimage ALTER oi_size TYPE INTEGER;
1057 ALTER TABLE oldimage ALTER oi_width TYPE INTEGER;
1058 ALTER TABLE oldimage ALTER oi_height TYPE INTEGER;
1060 ALTER TABLE image ALTER img_size TYPE INTEGER;
1061 ALTER TABLE image ALTER img_width TYPE INTEGER;
1062 ALTER TABLE image ALTER img_height TYPE INTEGER;
1064 -- Constraint tweaking:
1065 ALTER TABLE recentchanges ALTER rc_cur_id DROP NOT NULL;
1068 ALTER TABLE ipblocks ADD ipb_anon_only CHAR NOT NULL DEFAULT '0';
1069 ALTER TABLE ipblocks ADD ipb_create_account CHAR NOT NULL DEFAULT '1';
1071 -- Index order rearrangements:
1072 DROP INDEX pagelink_unique;
1073 CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title);
1076 ALTER TABLE "user" RENAME TO mwuser;
1077 ALTER TABLE "text" RENAME to pagecontent;
1080 CREATE TABLE profiling (
1081 pf_count INTEGER NOT NULL DEFAULT 0,
1082 pf_time NUMERIC(18,10) NOT NULL DEFAULT 0,
1083 pf_name TEXT NOT NULL,
1086 CREATE UNIQUE INDEX pf_name_server ON profiling (pf_name, pf_server);
1088 CREATE TABLE mediawiki_version (
1090 mw_version TEXT NOT NULL,
1093 pg_version TEXT NULL,
1094 pg_dbname TEXT NULL,
1097 mw_schema TEXT NULL,
1098 ts2_schema TEXT NULL,
1101 sql_version TEXT NULL,
1103 cdate TIMESTAMPTZ NOT NULL DEFAULT now()
1106 -- Special modifications
1107 ALTER TABLE archive RENAME to archive2;
1108 CREATE VIEW archive AS
1110 ar_namespace, ar_title, ar_text, ar_comment, ar_user, ar_user_text,
1111 ar_minor_edit, ar_flags, ar_rev_id, ar_text_id,
1112 TO_CHAR(ar_timestamp, 'YYYYMMDDHH24MISS') AS ar_timestamp
1115 CREATE RULE archive_insert AS ON INSERT TO archive
1116 DO INSTEAD INSERT INTO archive2 VALUES (
1117 NEW.ar_namespace, NEW.ar_title, NEW.ar_text, NEW.ar_comment, NEW.ar_user, NEW.ar_user_text,
1118 TO_DATE(NEW.ar_timestamp, 'YYYYMMDDHH24MISS'),
1119 NEW.ar_minor_edit, NEW.ar_flags, NEW.ar_rev_id, NEW.ar_text_id
1122 CREATE FUNCTION page_deleted() RETURNS TRIGGER LANGUAGE plpgsql AS
1125 DELETE FROM recentchanges WHERE rc_namespace = OLD.page_namespace AND rc_title = OLD.page_title;
1130 CREATE TRIGGER page_deleted AFTER DELETE ON page
1131 FOR EACH ROW EXECUTE PROCEDURE page_deleted();
1133 -- Note this upgrade
1134 INSERT INTO mediawiki_version (type,mw_version,notes)
1135 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.8 version THISVERSION aka SVERSION');
1139 } ## end version 1.8
1142 if ($version < 1009) {
1145 -- Tighten up restrictions on the revision table so we don't lose data:
1146 ALTER TABLE revision DROP CONSTRAINT revision_rev_user_fkey;
1147 ALTER TABLE revision ADD CONSTRAINT revision_rev_user_fkey
1148 FOREIGN KEY (rev_user) REFERENCES mwuser(user_id) ON DELETE RESTRICT;
1150 -- New columns for better password tracking:
1151 ALTER TABLE mwuser ADD user_newpass_time TIMESTAMPTZ;
1152 ALTER TABLE mwuser ADD user_editcount INTEGER;
1154 -- New column for autoblocking problem users
1155 ALTER TABLE ipblocks ADD ipb_enable_autoblock CHAR NOT NULL DEFAULT '1';
1157 -- Despite it's name, ipb_address does not necessarily contain IP addresses :)
1158 ALTER TABLE ipblocks ALTER ipb_address TYPE TEXT USING ipb_address::TEXT;
1161 CREATE TABLE redirect (
1162 rd_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE,
1163 rd_namespace SMALLINT NOT NULL,
1164 rd_title TEXT NOT NULL
1166 CREATE INDEX redirect_ns_title ON redirect (rd_namespace,rd_title,rd_from);
1168 CREATE TABLE querycachetwo (
1169 qcc_type TEXT NOT NULL,
1170 qcc_value SMALLINT NOT NULL DEFAULT 0,
1171 qcc_namespace INTEGER NOT NULL DEFAULT 0,
1172 qcc_title TEXT NOT NULL DEFAULT '',
1173 qcc_namespacetwo INTEGER NOT NULL DEFAULT 0,
1174 qcc_titletwo TEXT NOT NULL DEFAULT ''
1176 CREATE INDEX querycachetwo_type_value ON querycachetwo (qcc_type, qcc_value);
1177 CREATE INDEX querycachetwo_title ON querycachetwo (qcc_type,qcc_namespace,qcc_title);
1178 CREATE INDEX querycachetwo_titletwo ON querycachetwo (qcc_type,qcc_namespacetwo,qcc_titletwo);
1180 -- New columns for fancy recentchanges display
1181 ALTER TABLE recentchanges ADD rc_old_len INT;
1182 ALTER TABLE recentchanges ADD rc_new_len INT;
1184 -- Note this upgrade
1185 INSERT INTO mediawiki_version (type,mw_version,notes)
1186 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.9 version THISVERSION aka SVERSION');
1190 } ## end version 1.9
1193 if ($version <= 1010) {
1196 CREATE TABLE page_restrictions (
1197 pr_page INTEGER NULL REFERENCES page (page_id) ON DELETE CASCADE,
1198 pr_type TEXT NOT NULL,
1199 pr_level TEXT NOT NULL,
1200 pr_cascade SMALLINT NOT NULL,
1201 pr_user INTEGER NULL,
1202 pr_expiry TIMESTAMPTZ NULL
1204 ALTER TABLE page_restrictions ADD CONSTRAINT page_restrictions_pk PRIMARY KEY (pr_page,pr_type);
1206 -- Add a new index to help with full-text searches
1207 CREATE INDEX rev_text_id_idx ON revision (rev_text_id);
1209 -- Note this upgrade
1210 INSERT INTO mediawiki_version (type,mw_version,notes)
1211 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.10 version THISVERSION aka SVERSION');
1215 } ## end version 1.10
1217 if ( !strlen($upgrade)) {
1218 print "No updates needed for this version ($oldversion)\n";
1222 $upgrade = str_replace( 'MWVERSION', $wgVersion, $upgrade );
1223 $upgrade = str_replace( 'THISVERSION', $oldversion, $upgrade );
1224 $upgrade = str_replace( 'SVERSION', $version, $upgrade );
1225 $wgDatabase->query("BEGIN;\n\n $upgrade\n\nCOMMIT;\n");