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' ),
69 array( 'recentchanges', 'rc_deleted', 'patch-rc_deleted.sql' ),
70 array( 'logging', 'log_id', 'patch-log_id.sql' ),
71 array( 'logging', 'log_deleted', 'patch-log_deleted.sql' ),
72 array( 'archive', 'ar_deleted', 'patch-ar_deleted.sql' ),
73 array( 'ipblocks', 'ipb_deleted', 'patch-ipb_deleted.sql' ),
74 array( 'filearchive', 'fa_deleted', 'patch-fa_deleted.sql' ),
75 array( 'revision', 'rev_len', 'patch-rev_len.sql' ),
76 array( 'archive', 'ar_len', 'patch-ar_len.sql' ),
77 array( 'revision', 'rev_parent_id', 'patch-rev_parent_id.sql' ),
78 array( 'page_restrictions', 'pr_id', 'patch-page_restrictions_sortkey.sql' ),
81 function rename_table( $from, $to, $patch ) {
83 if ( $wgDatabase->tableExists( $from ) ) {
84 if ( $wgDatabase->tableExists( $to ) ) {
85 echo "...can't move table $from to $to, $to already exists.\n";
87 echo "Moving table $from to $to...";
88 dbsource( archive($patch), $wgDatabase );
92 // Source table does not exist
93 // Renames are done before creations, so this is typical for a new installation
98 function add_table( $name, $patch ) {
100 if ( $wgDatabase->tableExists( $name ) ) {
101 echo "...$name table already exists.\n";
103 echo "Creating $name table...";
104 dbsource( archive($patch), $wgDatabase );
109 function add_field( $table, $field, $patch ) {
111 if ( !$wgDatabase->tableExists( $table ) ) {
112 echo "...$table table does not exist, skipping new field patch\n";
113 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
114 echo "...have $field field in $table table.\n";
116 echo "Adding $field field to table $table...";
117 dbsource( archive($patch) , $wgDatabase );
122 function do_revision_updates() {
123 global $wgSoftwareRevision;
124 if ( $wgSoftwareRevision < 1001 ) {
129 function update_passwords() {
130 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
133 $fname = "Update script: update_passwords()";
134 print "\nIt appears that you need to update the user passwords in your\n" .
135 "database. If you have already done this (if you've run this update\n" .
136 "script once before, for example), doing so again will make all your\n" .
137 "user accounts inaccessible, so be sure you only do this once.\n" .
138 "Update user passwords? (yes/no)";
140 $resp = readconsole();
141 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
143 $sql = "SELECT user_id,user_password FROM user";
144 $source = $wgDatabase->query( $sql, $fname );
146 while ( $row = $wgDatabase->fetchObject( $source ) ) {
148 $oldpass = $row->user_password;
149 $newpass = md5( "{$id}-{$oldpass}" );
151 $sql = "UPDATE user SET user_password='{$newpass}' " .
152 "WHERE user_id={$id}";
153 $wgDatabase->query( $sql, $fname );
157 function do_interwiki_update() {
158 # Check that interwiki table exists; if it doesn't source it
159 global $wgDatabase, $IP;
160 if( $wgDatabase->tableExists( "interwiki" ) ) {
161 echo "...already have interwiki table\n";
164 echo "Creating interwiki table: ";
165 dbsource( archive("patch-interwiki.sql") );
167 echo "Adding default interwiki definitions: ";
168 dbsource( "$IP/maintenance/interwiki.sql" );
172 function do_index_update() {
173 # Check that proper indexes are in place
175 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
176 if( !$meta->isMultipleKey() ) {
177 echo "Updating indexes to 20031107: ";
178 dbsource( archive("patch-indexes.sql") );
182 echo "...indexes seem up to 20031107 standards\n";
186 function do_image_index_update() {
189 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
190 if( !$meta->isMultipleKey() ) {
191 echo "Updating indexes to 20050912: ";
192 dbsource( archive("patch-mimesearch-indexes.sql") );
196 echo "...indexes seem up to 20050912 standards\n";
200 function do_image_name_unique_update() {
202 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
203 echo "...image primary key already set.\n";
205 echo "Making img_name the primary key... ";
206 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
211 function do_logging_timestamp_index() {
213 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
214 echo "...timestamp key on logging already exists.\n";
216 echo "Adding timestamp key on logging table... ";
217 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
223 function do_watchlist_update() {
225 $fname = 'do_watchlist_update';
226 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
227 echo "The watchlist table is already set up for email notification.\n";
229 echo "Adding wl_notificationtimestamp field for email notification management.";
230 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
231 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
234 # Check if we need to add talk page rows to the watchlist
235 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
236 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
237 if ( $talk != $nontalk ) {
238 echo "Adding missing watchlist talk page rows... ";
241 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
243 'wl_user' => 'wl_user',
244 'wl_namespace' => 'wl_namespace | 1',
245 'wl_title' => 'wl_title',
246 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
247 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
250 echo "...watchlist talk page rows already present\n";
254 function do_copy_newtalk_to_watchlist() {
256 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
258 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
259 $wgDatabase->tableName( 'user_newtalk' ) );
260 $num_newtalks=$wgDatabase->numRows($res);
261 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
264 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
265 $wluser = $wgDatabase->fetchObject( $res );
266 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
267 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
268 $wgDatabase->replace( 'watchlist',
269 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
270 array('wl_user' => 0,
271 'wl_namespace' => NS_USER_TALK,
272 'wl_title' => $wluser->user_ip,
273 'wl_notificationtimestamp' => '19700101000000'
274 ), 'updaters.inc::do_watchlist_update2'
277 } else { # normal users ... have user_ids
278 $user->setID($wluser->user_id);
279 $wgDatabase->replace( 'watchlist',
280 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
281 array('wl_user' => $user->getID(),
282 'wl_namespace' => NS_USER_TALK,
283 'wl_title' => $user->getName(),
284 'wl_notificationtimestamp' => '19700101000000'
285 ), 'updaters.inc::do_watchlist_update3'
293 function do_user_update() {
295 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
296 echo "User table contains old email authentication field. Dropping... ";
297 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
300 echo "...user table does not contain old email authentication field.\n";
305 * 1.4 betas were missing the 'binary' marker from logging.log_title,
306 * which causes a collation mismatch error on joins in MySQL 4.1.
308 function do_logging_encoding() {
309 global $wgDatabase, $wgDBtype;
310 if ($wgDBtype != 'mysql')
312 $logging = $wgDatabase->tableName( 'logging' );
313 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
314 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
315 $wgDatabase->freeResult( $res );
317 if( in_array( 'binary', $flags ) ) {
318 echo "Logging table has correct title encoding.\n";
320 echo "Fixing title encoding on logging table... ";
321 dbsource( archive( 'patch-logging-title.sql' ), $wgDatabase );
326 function do_schema_restructuring() {
328 $fname="do_schema_restructuring";
329 if ( $wgDatabase->tableExists( 'page' ) ) {
330 echo "...page table already exists.\n";
332 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
333 echo wfTimestamp( TS_DB );
334 echo "......checking for duplicate entries.\n"; flush();
336 list ($cur, $old, $page, $revision, $text) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
338 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
339 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
341 if ( $wgDatabase->numRows( $rows ) > 0 ) {
342 echo wfTimestamp( TS_DB );
343 echo "......<b>Found duplicate entries</b>\n";
344 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
345 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
346 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
347 $duplicate[$row->cur_namespace] = array();
349 $duplicate[$row->cur_namespace][] = $row->cur_title;
350 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
352 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
354 foreach ( $duplicate as $ns => $titles ) {
360 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
362 foreach ( $titles as $t ) {
364 $sql .= $wgDatabase->addQuotes( $t );
367 $sql .= ', ' . $wgDatabase->addQuotes( $t );
372 # By sorting descending, the most recent entry will be the first in the list.
373 # All following entries will be deleted by the next while-loop.
374 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
376 $rows = $wgDatabase->query( $sql, $fname );
378 $prev_title = $prev_namespace = false;
381 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
382 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
383 $deleteId[] = $row->cur_id;
385 $prev_title = $row->cur_title;
386 $prev_namespace = $row->cur_namespace;
388 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
389 $rows = $wgDatabase->query( $sql, $fname );
390 echo wfTimestamp( TS_DB );
391 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
395 echo wfTimestamp( TS_DB );
396 echo "......Creating tables.\n";
397 $wgDatabase->query("CREATE TABLE $page (
398 page_id int(8) unsigned NOT NULL auto_increment,
399 page_namespace int NOT NULL,
400 page_title varchar(255) binary NOT NULL,
401 page_restrictions tinyblob NOT NULL,
402 page_counter bigint(20) unsigned NOT NULL default '0',
403 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
404 page_is_new tinyint(1) unsigned NOT NULL default '0',
405 page_random real unsigned NOT NULL,
406 page_touched char(14) binary NOT NULL default '',
407 page_latest int(8) unsigned NOT NULL,
408 page_len int(8) unsigned NOT NULL,
410 PRIMARY KEY page_id (page_id),
411 UNIQUE INDEX name_title (page_namespace,page_title),
414 ) TYPE=InnoDB", $fname );
415 $wgDatabase->query("CREATE TABLE $revision (
416 rev_id int(8) unsigned NOT NULL auto_increment,
417 rev_page int(8) unsigned NOT NULL,
418 rev_comment tinyblob NOT NULL,
419 rev_user int(5) unsigned NOT NULL default '0',
420 rev_user_text varchar(255) binary NOT NULL default '',
421 rev_timestamp char(14) binary NOT NULL default '',
422 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
423 rev_deleted tinyint(1) unsigned NOT NULL default '0',
424 rev_parent_id int(8) unsigned default NULL,
425 PRIMARY KEY rev_page_id (rev_page, rev_id),
426 UNIQUE INDEX rev_id (rev_id),
427 INDEX rev_timestamp (rev_timestamp),
428 INDEX page_timestamp (rev_page,rev_timestamp),
429 INDEX user_timestamp (rev_user,rev_timestamp),
430 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
431 ) TYPE=InnoDB", $fname );
433 echo wfTimestamp( TS_DB );
434 echo "......Locking tables.\n";
435 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
437 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
438 echo wfTimestamp( TS_DB );
439 echo "......maxold is {$maxold}\n";
441 echo wfTimestamp( TS_DB );
442 global $wgLegacySchemaConversion;
443 if( $wgLegacySchemaConversion ) {
444 // Create HistoryBlobCurStub entries.
445 // Text will be pulled from the leftover 'cur' table at runtime.
446 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
447 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
448 $cur_flags = "'object'";
450 // Copy all cur text in immediately: this may take longer but avoids
451 // having to keep an extra table around.
452 echo "......Moving text from cur.\n";
453 $cur_text = 'cur_text';
456 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
457 old_timestamp, old_minor_edit, old_flags)
458 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
459 FROM $cur", $fname );
461 echo wfTimestamp( TS_DB );
462 echo "......Setting up revision table.\n";
463 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
465 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
466 old_timestamp, old_minor_edit
467 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
469 echo wfTimestamp( TS_DB );
470 echo "......Setting up page table.\n";
471 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
472 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
473 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
474 cur_random, cur_touched, rev_id, LENGTH(cur_text)
476 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
478 echo wfTimestamp( TS_DB );
479 echo "......Unlocking tables.\n";
480 $wgDatabase->query( "UNLOCK TABLES", $fname );
482 echo wfTimestamp( TS_DB );
483 echo "......Renaming old.\n";
484 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
486 echo wfTimestamp( TS_DB );
491 function do_inverse_timestamp() {
493 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
494 echo "Removing revision.inverse_timestamp and fixing indexes... ";
495 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
498 echo "revision timestamp indexes already up to 2005-03-13\n";
502 function do_text_id() {
504 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
505 echo "...rev_text_id already in place.\n";
507 echo "Adding rev_text_id field... ";
508 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
513 function do_namespace_size() {
517 'recentchanges' => 'rc',
519 'querycache' => 'qc',
522 foreach( $tables as $table => $prefix ) {
523 do_namespace_size_on( $table, $prefix );
528 function do_namespace_size_on( $table, $prefix ) {
529 global $wgDatabase, $wgDBtype;
530 if ($wgDBtype != 'mysql')
532 $field = $prefix . '_namespace';
534 $tablename = $wgDatabase->tableName( $table );
535 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
536 $info = $wgDatabase->fetchObject( $result );
537 $wgDatabase->freeResult( $result );
539 if( substr( $info->Type, 0, 3 ) == 'int' ) {
540 echo "...$field is already a full int ($info->Type).\n";
542 echo "Promoting $field from $info->Type to int... ";
544 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
545 $wgDatabase->query( $sql );
551 function do_pagelinks_update() {
553 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
554 echo "...already have pagelinks table.\n";
556 echo "Converting links and brokenlinks tables to pagelinks... ";
557 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
561 global $wgCanonicalNamespaceNames;
562 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
564 do_pagelinks_namespace( $ns );
570 function do_pagelinks_namespace( $namespace ) {
571 global $wgDatabase, $wgContLang;
573 $ns = intval( $namespace );
574 echo "Cleaning up broken links for namespace $ns... ";
576 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
577 $name = $wgContLang->getNsText( $ns );
578 $prefix = $wgDatabase->strencode( $name );
579 $likeprefix = str_replace( '_', '\\_', $prefix);
581 $sql = "UPDATE $pagelinks
582 SET pl_namespace=$ns,
583 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
585 AND pl_title LIKE '$likeprefix:%'";
587 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
591 function do_drop_img_type() {
594 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
595 echo "Dropping unused img_type field in image table... ";
596 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
599 echo "No img_type field in image table; Good.\n";
603 function do_old_links_update() {
605 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
606 echo "Already have pagelinks; skipping old links table updates.\n";
608 convertLinks(); flush();
612 function do_user_unique_update() {
614 $duper = new UserDupes( $wgDatabase );
615 if( $duper->hasUniqueIndex() ) {
616 echo "Already have unique user_name index.\n";
618 if( !$duper->clearDupes() ) {
619 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
621 echo "Adding unique index on user_name... ";
622 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
627 function do_user_groups_update() {
628 $fname = 'do_user_groups_update';
631 if( $wgDatabase->tableExists( 'user_groups' ) ) {
632 echo "...user_groups table already exists.\n";
633 return do_user_groups_reformat();
636 echo "Adding user_groups table... ";
637 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
640 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
641 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
642 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
643 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
646 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
647 echo "*** You may need to manually configure some sysops by manipulating\n";
648 echo "*** the user_groups table.\n";
653 echo "Converting user_rights table to user_groups... ";
654 $result = $wgDatabase->select( 'user_rights',
655 array( 'ur_user', 'ur_rights' ),
656 array( "ur_rights != ''" ),
659 while( $row = $wgDatabase->fetchObject( $result ) ) {
660 $groups = array_unique(
662 explode( ',', $row->ur_rights ) ) );
664 foreach( $groups as $group ) {
665 $wgDatabase->insert( 'user_groups',
667 'ug_user' => $row->ur_user,
668 'ug_group' => $group ),
672 $wgDatabase->freeResult( $result );
676 function do_user_groups_reformat() {
677 # Check for bogus formats from previous 1.5 alpha code.
679 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
681 if( $info->type() == 'int' ) {
682 $oldug = $wgDatabase->tableName( 'user_groups' );
683 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
684 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
685 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
688 echo "Re-adding fresh user_groups table... ";
689 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
693 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
694 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
697 echo "...user_groups is in current format.\n";
702 function do_watchlist_null() {
703 # Make sure wl_notificationtimestamp can be NULL,
704 # and update old broken items.
706 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
708 if( !$info->nullable() ) {
709 echo "Making wl_notificationtimestamp nullable... ";
710 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
713 echo "...wl_notificationtimestamp is already nullable.\n";
721 function do_page_random_update() {
724 echo "Setting page_random to a random value on rows where it equals 0...";
726 $page = $wgDatabase->tableName( 'page' );
727 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
728 $rows = $wgDatabase->affectedRows();
730 echo "changed $rows rows\n";
733 function do_templatelinks_update() {
734 global $wgDatabase, $wgLoadBalancer;
735 $fname = 'do_templatelinks_update';
737 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
738 echo "...templatelinks table already exists\n";
741 echo "Creating templatelinks table...\n";
742 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
743 echo "Populating...\n";
744 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
745 // Slow, replication-friendly update
746 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
747 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
749 while ( $row = $wgDatabase->fetchObject( $res ) ) {
750 $count = ($count + 1) % 100;
752 if ( function_exists( 'wfWaitForSlaves' ) ) {
753 wfWaitForSlaves( 10 );
758 $wgDatabase->insert( 'templatelinks',
760 'tl_from' => $row->pl_from,
761 'tl_namespace' => $row->pl_namespace,
762 'tl_title' => $row->pl_title,
767 $wgDatabase->freeResult( $res );
770 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
772 'tl_from' => 'pl_from',
773 'tl_namespace' => 'pl_namespace',
774 'tl_title' => 'pl_title'
780 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
784 # Add ( rc_namespace, rc_user_text ) index [R. Church]
785 function do_rc_indices_update() {
787 echo( "Checking for additional recent changes indices...\n" );
788 # See if we can find the index we want
789 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_ns_usertext', __METHOD__ );
792 echo( "...index on ( rc_namespace, rc_user_text ) not found; creating\n" );
793 dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
795 # Index seems to exist
796 echo( "...index on ( rc_namespace, rc_user_text ) seems to be ok\n" );
799 #Add (rc_user_text, rc_timestamp) index [A. Garrett], November 2006
800 # See if we can find the index we want
801 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_user_text', __METHOD__ );
804 echo( "...index on ( rc_user_text, rc_timestamp ) not found; creating\n" );
805 dbsource( archive( 'patch-rc_user_text-index.sql' ) );
807 # Index seems to exist
808 echo( "...index on ( rc_user_text, rc_timestamp ) seems to be ok\n" );
812 function index_has_field($table, $index, $field) {
814 echo( "Checking if $table index $index includes field $field...\n" );
815 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
817 foreach($info as $row) {
818 if($row->Column_name == $field) {
819 echo( "...index $index on table $table seems to be ok\n" );
824 echo( "...index $index on table $table has no field $field; adding\n" );
828 function do_backlinking_indices_update() {
829 echo( "Checking for backlinking indices...\n" );
830 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
831 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
832 !index_has_field('imagelinks', 'il_to', 'il_from'))
834 dbsource( archive( 'patch-backlinkindexes.sql' ) );
838 function do_stats_init() {
839 // Sometimes site_stats table is not properly populated.
841 echo "Checking site_stats row...";
842 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
843 if( $row === false ) {
844 echo "data is missing! rebuilding...\n";
847 require_once "$IP/maintenance/initStats.inc";
854 function purge_cache() {
856 # We can't guarantee that the user will be able to use TRUNCATE,
857 # but we know that DELETE is available to us
858 echo( "Purging caches..." );
859 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
863 function do_all_updates( $shared = false, $purge = true ) {
864 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
866 $doUser = !$wgSharedDB || $shared;
868 if ($wgDBtype === 'postgres') {
869 do_postgres_updates();
874 foreach ( $wgRenamedTables as $tableRecord ) {
875 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
879 foreach ( $wgNewTables as $tableRecord ) {
880 add_table( $tableRecord[0], $tableRecord[1] );
885 foreach ( $wgNewFields as $fieldRecord ) {
886 if ( $fieldRecord[0] != 'user' || $doUser ) {
887 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
892 # Do schema updates which require special handling
893 do_interwiki_update(); flush();
894 do_index_update(); flush();
895 do_old_links_update(); flush();
896 do_image_name_unique_update(); flush();
897 do_watchlist_update(); flush();
899 do_user_update(); flush();
901 ###### do_copy_newtalk_to_watchlist(); flush();
902 do_logging_encoding(); flush();
904 do_schema_restructuring(); flush();
905 do_inverse_timestamp(); flush();
906 do_text_id(); flush();
907 do_namespace_size(); flush();
909 do_pagelinks_update(); flush();
910 do_templatelinks_update(); flush(); // after pagelinks
912 do_drop_img_type(); flush();
915 do_user_unique_update(); flush();
917 do_user_groups_update(); flush();
919 do_watchlist_null(); flush();
921 //do_image_index_update(); flush();
923 do_logging_timestamp_index(); flush();
925 do_page_random_update(); flush();
927 do_rc_indices_update(); flush();
929 add_table( 'redirect', 'patch-redirect.sql' );
931 do_backlinking_indices_update(); flush();
933 do_restrictions_update(); flush ();
935 echo "Deleting old default messages (this may take a long time!)..."; flush();
936 deleteDefaultMessages();
937 echo "Done\n"; flush();
939 do_stats_init(); flush();
947 function archive($name) {
948 global $wgDBtype, $IP;
951 return "$IP/maintenance/postgres/archives/$name";
953 return "$IP/maintenance/archives/$name";
957 function do_restrictions_update() {
958 # Adding page_restrictions table, obsoleting page.page_restrictions.
959 # Migrating old restrictions to new table
960 # -- Andrew Garrett, January 2007.
964 $name = 'page_restrictions';
965 $patch = 'patch-page_restrictions.sql';
967 if ( $wgDatabase->tableExists( $name ) ) {
968 echo "...$name table already exists.\n";
970 echo "Creating $name table...";
971 dbsource( archive($patch), $wgDatabase );
974 echo "Migrating old restrictions to new table...";
976 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array("page_restrictions!=''", "page_restrictions!='edit=:move='"), __METHOD__ );
980 while ($row = $wgDatabase->fetchObject($res) ) {
981 $count = ($count + 1) % 100;
984 if ( function_exists( 'wfWaitForSlaves' ) ) {
985 wfWaitForSlaves( 10 );
991 # Figure out what the restrictions are..
993 $flatrestrictions = explode( ':', $row->page_restrictions );
995 $restrictions = array ();
996 foreach( $flatrestrictions as $restriction ) {
997 $thisrestriction = explode( '=', $restriction, 2 );
998 if( count( $thisrestriction ) == 1 ) {
999 // Compatibility with old protections from before
1000 // separate move protection was added.
1001 list( $level ) = $thisrestriction;
1003 $restrictions['edit'] = $level;
1004 $restrictions['move'] = $level;
1007 list( $type, $level ) = $thisrestriction;
1009 $restrictions[$type] = $level;
1013 $wgDatabase->update( 'page', array ( 'page_restrictions' => ''), array( 'page_id' => $id ), __METHOD__ );
1017 foreach( $restrictions as $type => $level ) {
1018 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
1020 'pr_level' => $level,
1021 'pr_cascade' => 0 ),
1031 pg_describe_table($table)
1033 global $wgDatabase, $wgDBmwschema;
1035 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
1036 WHERE pg_class.relnamespace = pg_namespace.oid
1037 AND attrelid=pg_class.oid AND attnum > 0
1038 AND relname=%s AND nspname=%s
1040 $res = $wgDatabase->query(sprintf($q,
1041 $wgDatabase->addQuotes($table),
1042 $wgDatabase->addQuotes($wgDBmwschema)));
1047 while ($r = $wgDatabase->fetchRow($res)) {
1057 pg_describe_index($idx)
1059 global $wgDatabase, $wgDBmwschema;
1061 // first fetch the key (which is a list of columns ords) and
1062 // the table the index applies to (an oid)
1064 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
1066 AND pg_class.relnamespace = pg_namespace.oid
1068 AND indexrelid=pg_class.oid
1070 $res = $wgDatabase->query(sprintf($q,
1071 $wgDatabase->addQuotes($wgDBmwschema),
1072 $wgDatabase->addQuotes($idx)));
1075 if (!($r = $wgDatabase->fetchRow($res))) {
1076 $wgDatabase->freeResult($res);
1081 $relid = intval($r[1]);
1082 $indkeys = explode(" ", $indkey);
1083 $wgDatabase->freeResult($res);
1085 $colnames = array();
1086 foreach ($indkeys as $rid) {
1088 SELECT attname FROM pg_class, pg_attribute
1089 WHERE attrelid=$relid
1091 AND attrelid=pg_class.oid
1093 $r2 = $wgDatabase->query(sprintf($query, $rid));
1096 if (!($row2 = $wgDatabase->fetchRow($r2))) {
1097 $wgDatabase->freeResult($r2);
1100 $colnames[] = $row2[0];
1101 $wgDatabase->freeResult($r2);
1108 pg_index_exists($table, $index)
1110 global $wgDatabase, $wgDBmwschema;
1111 $exists = $wgDatabase->selectField("pg_indexes", "indexname",
1112 array( "indexname" => $index,
1113 "tablename" => $table,
1114 "schemaname" => $wgDBmwschema));
1115 return $exists === $index;
1119 pg_fkey_deltype($fkey)
1121 global $wgDatabase, $wgDBmwschema;
1123 SELECT confdeltype FROM pg_constraint, pg_namespace
1124 WHERE connamespace=pg_namespace.oid
1128 $r = $wgDatabase->query(sprintf($q,
1129 $wgDatabase->addQuotes($wgDBmwschema),
1130 $wgDatabase->addQuotes($fkey)));
1131 if (!($row = $wgDatabase->fetchRow($r)))
1137 pg_rule_def($table, $rule)
1139 global $wgDatabase, $wgDBmwschema;
1141 SELECT definition FROM pg_rules
1142 WHERE schemaname = %s
1146 $r = $wgDatabase->query(sprintf($q,
1147 $wgDatabase->addQuotes($wgDBmwschema),
1148 $wgDatabase->addQuotes($table),
1149 $wgDatabase->addQuotes($rule)));
1150 $row = $wgDatabase->fetchRow($r);
1154 $wgDatabase->freeResult($r);
1158 function do_postgres_updates() {
1159 global $wgDatabase, $wgVersion, $wgDBmwschema, $wgShowExceptionDetails;
1161 $wgShowExceptionDetails = 1;
1163 # Just in case their LocalSettings.php does not have this:
1164 if ( !isset( $wgDBmwschema ))
1165 $wgDBmwschema = 'mediawiki';
1167 $newsequences = array(
1172 array("mediawiki_version", "patch-mediawiki_version.sql"),
1173 array("mwuser", "patch-mwuser.sql"),
1174 array("pagecontent", "patch-pagecontent.sql"),
1175 array("querycachetwo", "patch-querycachetwo.sql"),
1176 array("page_restrictions", "patch-page_restrictions.sql"),
1177 array("profiling", "patch-profiling.sql"),
1178 array("redirect", "patch-redirect.sql"),
1182 array("archive", "ar_len", "INTEGER"),
1183 array("ipblocks", "ipb_anon_only", "CHAR NOT NULL DEFAULT '0'"),
1184 array("ipblocks", "ipb_create_account", "CHAR NOT NULL DEFAULT '1'"),
1185 array("ipblocks", "ipb_deleted", "INTEGER NOT NULL DEFAULT 0"),
1186 array("ipblocks", "ipb_enable_autoblock", "CHAR NOT NULL DEFAULT '1'"),
1187 array("filearchive", "fa_deleted", "INTEGER NOT NULL DEFAULT 0"),
1188 array("logging", "log_deleted", "INTEGER NOT NULL DEFAULT 0"),
1189 array("logging", "log_id", "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('log_log_id_seq')"),
1190 array("logging", "log_params", "TEXT"),
1191 array("mwuser", "user_editcount", "INTEGER"),
1192 array("mwuser", "user_newpass_time", "TIMESTAMPTZ"),
1193 array("recentchanges", "rc_deleted", "INTEGER NOT NULL DEFAULT 0"),
1194 array("recentchanges", "rc_log_action", "TEXT"),
1195 array("recentchanges", "rc_log_type", "TEXT"),
1196 array("recentchanges", "rc_logid", "INTEGER NOT NULL DEFAULT 0"),
1197 array("recentchanges", "rc_new_len", "INTEGER"),
1198 array("recentchanges", "rc_old_len", "INTEGER"),
1199 array("recentchanges", "rc_params", "TEXT"),
1200 array("revision", "rev_len", "INTEGER"),
1204 # table, column, desired type, USING clause if needed
1205 $typechanges = array(
1206 array("image", "img_size", "int4", ""),
1207 array("image", "img_width", "int4", ""),
1208 array("image", "img_height", "int4", ""),
1209 array("ipblocks", "ipb_address", "text", "ipb_address::text"),
1210 array("math", "math_inputhash", "bytea", "decode(math_inputhash,'escape')"),
1211 array("math", "math_outputhash", "bytea", "decode(math_outputhash,'escape')"),
1212 array("oldimage", "oi_size", "int4", ""),
1213 array("oldimage", "oi_width", "int4", ""),
1214 array("oldimage", "oi_height", "int4", ""),
1215 array("user_newtalk", "user_ip", "text", "host(user_ip)"),
1218 $newindexes = array(
1219 array("revision", "rev_text_id_idx", "patch-rev_text_id_idx.sql")
1226 foreach ($newsequences as $ns) {
1227 if ($wgDatabase->sequenceExists($ns)) {
1228 echo "... sequence $ns already exists\n";
1232 echo "... create sequence $ns\n";
1233 $wgDatabase->query("CREATE SEQUENCE $ns");
1236 foreach ($newtables as $nt) {
1237 if ($wgDatabase->tableExists($nt[0])) {
1238 echo "... table $nt[0] already exists\n";
1242 echo "... create table $nt[0]\n";
1243 dbsource(archive($nt[1]));
1246 ## Needed before newcols
1247 if ($wgDatabase->tableExists("archive2")) {
1248 echo "... convert archive2 back to normal archive table\n";
1249 if ($wgDatabase->ruleExists("archive", "archive_insert")) {
1250 echo "... drop rule archive_insert\n";
1251 $wgDatabase->query("DROP RULE archive_insert ON archive");
1253 if ($wgDatabase->ruleExists("archive", "archive_delete")) {
1254 echo "... drop rule archive_delete\n";
1255 $wgDatabase->query("DROP RULE archive_delete ON archive");
1258 dbsource(archive("patch-remove-archive2.sql"));
1260 echo "... obsolete archive2 not present\n";
1262 foreach ($newcols as $nc) {
1263 $fi = $wgDatabase->fieldInfo($nc[0], $nc[1]);
1264 if (!is_null($fi)) {
1265 echo "... column $nc[0].$nc[1] already exists\n";
1269 echo "... add column $nc[0].$nc[1]\n";
1270 $wgDatabase->query("ALTER TABLE $nc[0] ADD $nc[1] $nc[2]");
1273 foreach ($typechanges as $tc) {
1274 $fi = $wgDatabase->fieldInfo($tc[0], $tc[1]);
1276 echo "... error: expected column $tc[0].$tc[1] to exist\n";
1280 if ($fi->type() === $tc[2])
1281 echo "... $tc[0].$tc[1] is already $tc[2]\n";
1283 echo "... change $tc[0].$tc[1] from {$fi->type()} to $tc[2]\n";
1284 $sql = "ALTER TABLE $tc[0] ALTER $tc[1] TYPE $tc[2]";
1285 if (strlen($tc[3])) {
1286 $sql .= " USING $tc[3]";
1288 $sql .= ";\nCOMMIT;\n";
1289 $wgDatabase->query($sql);
1293 foreach ($newindexes as $ni) {
1294 if (pg_index_exists($ni[0], $ni[1])) {
1295 echo "... index $ni[1] on $ni[0] already exists\n";
1298 dbsource(archive($ni[2]));
1301 foreach ($newrules as $nr) {
1302 if ($wgDatabase->ruleExists($nr[0], $nr[1])) {
1303 echo "... rule $nr[1] on $nr[0] already exists\n";
1306 dbsource(archive($nr[2]));
1309 if (!$wgDatabase->triggerExists("page", "page_deleted")) {
1310 echo "... create page_deleted trigger\n";
1311 dbsource(archive('patch-page_deleted.sql'));
1314 $fi = $wgDatabase->fieldInfo("recentchanges", "rc_cur_id");
1315 if (!$fi->nullable()) {
1316 echo "... remove NOT NULL constraint on recentchanges.rc_cur_id\n";
1317 dbsource(archive('patch-rc_cur_id-not-null.sql'));
1320 $pu = pg_describe_index("pagelink_unique");
1321 if (!is_null($pu) && ($pu[0] != "pl_from" || $pu[1] != "pl_namespace" || $pu[2] != "pl_title")) {
1322 echo "... dropping obsolete pagelink_unique index\n";
1323 $wgDatabase->query("DROP INDEX pagelink_unique");
1326 echo "... obsolete pagelink_unique index not present\n";
1329 echo "... adding new pagelink_unique index\n";
1330 $wgDatabase->query("CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)");
1332 echo "... already have current pagelink_unique index\n";
1334 if (pg_fkey_deltype("revision_rev_user_fkey") == 'r') {
1335 echo "... revision_rev_user_fkey is already ON DELETE RESTRICT\n";
1337 echo "... change revision_rev_user_fkey to ON DELETE RESTRICT\n";
1338 dbsource(archive('patch-revision_rev_user_fkey.sql'));
1341 if (is_null($wgDatabase->fieldInfo("archive", "ar_deleted"))) {
1342 echo "... add archive.ar_deleted\n";
1343 dbsource(archive("patch-archive-ar_deleted.sql"));
1345 echo "... archive.ar_deleted already exists\n";