4 * @subpackage Maintenance
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11 require_once 'userDupes.inc';
13 $wgRenamedTables = array(
15 # array( 'group', 'groups', 'patch-rename-group.sql' ),
19 # table patch file (in maintenance/archives)
20 array( 'hitcounter', 'patch-hitcounter.sql' ),
21 array( 'querycache', 'patch-querycache.sql' ),
22 array( 'objectcache', 'patch-objectcache.sql' ),
23 array( 'categorylinks', 'patch-categorylinks.sql' ),
24 array( 'logging', 'patch-logging.sql' ),
25 array( 'validate', 'patch-validate.sql' ),
26 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
30 # table field patch file (in maintenance/archives)
31 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
32 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
33 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
34 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
35 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
36 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
37 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
38 array( 'user', 'user_token', 'patch-user_token.sql' ),
39 array( 'user', 'user_email_token', 'patch-user_email_token.sql' ),
40 array( 'logging', 'log_params', 'patch-log_params.sql' ),
41 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
42 array( 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
43 array( 'page', 'page_len', 'patch-page_len.sql' ),
44 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
45 array( 'image', 'img_width', 'patch-img_width.sql' ),
46 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
47 array( 'image', 'img_media_type', 'patch-img_media_type.sql' ),
48 array( 'validate', 'val_ip', 'patch-val_ip.sql' ),
49 array( 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ),
52 function rename_table( $from, $to, $patch ) {
54 if ( $wgDatabase->tableExists( $from ) ) {
55 if ( $wgDatabase->tableExists( $to ) ) {
56 echo "...can't move table $from to $to, $to already exists.\n";
58 echo "Moving table $from to $to...";
59 dbsource( "maintenance/archives/$patch", $wgDatabase );
63 // Source table does not exist
64 // Renames are done before creations, so this is typical for a new installation
69 function add_table( $name, $patch ) {
71 if ( $wgDatabase->tableExists( $name ) ) {
72 echo "...$name table already exists.\n";
74 echo "Creating $name table...";
75 dbsource( "maintenance/archives/$patch", $wgDatabase );
80 function add_field( $table, $field, $patch ) {
82 if ( !$wgDatabase->tableExists( $table ) ) {
83 echo "...$table table does not exist, skipping new field patch\n";
84 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
85 echo "...have $field field in $table table.\n";
87 echo "Adding $field field to table $table...";
88 dbsource( "maintenance/archives/$patch" , $wgDatabase );
93 function do_revision_updates() {
94 global $wgSoftwareRevision;
95 if ( $wgSoftwareRevision < 1001 ) {
100 function update_passwords() {
101 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
104 $fname = "Update script: update_passwords()";
105 print "\nIt appears that you need to update the user passwords in your\n" .
106 "database. If you have already done this (if you've run this update\n" .
107 "script once before, for example), doing so again will make all your\n" .
108 "user accounts inaccessible, so be sure you only do this once.\n" .
109 "Update user passwords? (yes/no)";
111 $resp = readconsole();
112 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
114 $sql = "SELECT user_id,user_password FROM user";
115 $source = $wgDatabase->query( $sql, $fname );
117 while ( $row = $wgDatabase->fetchObject( $source ) ) {
119 $oldpass = $row->user_password;
120 $newpass = md5( "{$id}-{$oldpass}" );
122 $sql = "UPDATE user SET user_password='{$newpass}' " .
123 "WHERE user_id={$id}";
124 $wgDatabase->query( $sql, $fname );
128 function do_interwiki_update() {
129 # Check that interwiki table exists; if it doesn't source it
131 if( $wgDatabase->tableExists( "interwiki" ) ) {
132 echo "...already have interwiki table\n";
135 echo "Creating interwiki table: ";
136 dbsource( "maintenance/archives/patch-interwiki.sql" );
138 echo "Adding default interwiki definitions: ";
139 dbsource( "maintenance/interwiki.sql" );
143 function do_index_update() {
144 # Check that proper indexes are in place
146 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
147 if( $meta->multiple_key == 0 ) {
148 echo "Updating indexes to 20031107: ";
149 dbsource( "maintenance/archives/patch-indexes.sql" );
153 echo "...indexes seem up to 20031107 standards\n";
157 function do_image_name_unique_update() {
159 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
160 echo "...image primary key already set.\n";
162 echo "Making img_name the primary key... ";
163 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
168 function do_watchlist_update() {
170 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
171 echo "The watchlist table is already set up for email notification.\n";
173 echo "Adding wl_notificationtimestamp field for email notification management.";
174 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
175 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
180 function do_copy_newtalk_to_watchlist() {
182 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
184 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
185 $wgDatabase->tableName( 'user_newtalk' ) );
186 $num_newtalks=$wgDatabase->numRows($res);
187 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
190 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
191 $wluser = $wgDatabase->fetchObject( $res );
192 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
193 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
194 $wgDatabase->replace( 'watchlist',
195 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
196 array('wl_user' => 0,
197 'wl_namespace' => NS_USER_TALK,
198 'wl_title' => $wluser->user_ip,
199 'wl_notificationtimestamp' => '19700101000000'
200 ), 'updaters.inc::do_watchlist_update2'
203 } else { # normal users ... have user_ids
204 $user->setID($wluser->user_id);
205 $wgDatabase->replace( 'watchlist',
206 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
207 array('wl_user' => $user->getID(),
208 'wl_namespace' => NS_USER_TALK,
209 'wl_title' => $user->getName(),
210 'wl_notificationtimestamp' => '19700101000000'
211 ), 'updaters.inc::do_watchlist_update3'
219 function do_user_update() {
221 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
222 echo "User table contains old email authentication field. Dropping... ";
223 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
226 echo "...user table does not contain old email authentication field.\n";
231 * 1.4 betas were missing the 'binary' marker from logging.log_title,
232 * which causes a collation mismatch error on joins in MySQL 4.1.
234 function do_logging_encoding() {
236 $logging = $wgDatabase->tableName( 'logging' );
237 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
238 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
239 $wgDatabase->freeResult( $res );
241 if( in_array( 'binary', $flags ) ) {
242 echo "Logging table has correct title encoding.\n";
244 echo "Fixing title encoding on logging table... ";
245 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
250 function do_schema_restructuring() {
252 $fname="do_schema_restructuring";
253 if ( $wgDatabase->tableExists( 'page' ) ) {
254 echo "...page table already exists.\n";
256 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
258 echo "......checking for duplicate entries.\n"; flush();
260 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
262 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
263 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
265 if ( $wgDatabase->numRows( $rows ) > 0 ) {
267 echo "......<b>Found duplicate entries</b>\n";
268 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
269 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
270 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
271 $duplicate[$row->cur_namespace] = array();
273 $duplicate[$row->cur_namespace][] = $row->cur_title;
274 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
276 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
278 foreach ( $duplicate as $ns => $titles ) {
284 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
286 foreach ( $titles as $t ) {
288 $sql .= $wgDatabase->addQuotes( $t );
291 $sql .= ', ' . $wgDatabase->addQuotes( $t );
296 # By sorting descending, the most recent entry will be the first in the list.
297 # All following entries will be deleted by the next while-loop.
298 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
300 $rows = $wgDatabase->query( $sql, $fname );
302 $prev_title = $prev_namespace = false;
305 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
306 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
307 $deleteId[] = $row->cur_id;
309 $prev_title = $row->cur_title;
310 $prev_namespace = $row->cur_namespace;
312 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
313 $rows = $wgDatabase->query( $sql, $fname );
315 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
320 echo "......Creating tables.\n";
321 $wgDatabase->query("CREATE TABLE $page (
322 page_id int(8) unsigned NOT NULL auto_increment,
323 page_namespace int NOT NULL,
324 page_title varchar(255) binary NOT NULL,
325 page_restrictions tinyblob NOT NULL default '',
326 page_counter bigint(20) unsigned NOT NULL default '0',
327 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
328 page_is_new tinyint(1) unsigned NOT NULL default '0',
329 page_random real unsigned NOT NULL,
330 page_touched char(14) binary NOT NULL default '',
331 page_latest int(8) unsigned NOT NULL,
332 page_len int(8) unsigned NOT NULL,
334 PRIMARY KEY page_id (page_id),
335 UNIQUE INDEX name_title (page_namespace,page_title),
338 ) TYPE=InnoDB", $fname );
339 $wgDatabase->query("CREATE TABLE $revision (
340 rev_id int(8) unsigned NOT NULL auto_increment,
341 rev_page int(8) unsigned NOT NULL,
342 rev_comment tinyblob NOT NULL default '',
343 rev_user int(5) unsigned NOT NULL default '0',
344 rev_user_text varchar(255) binary NOT NULL default '',
345 rev_timestamp char(14) binary NOT NULL default '',
346 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
347 rev_deleted tinyint(1) unsigned NOT NULL default '0',
349 PRIMARY KEY rev_page_id (rev_page, rev_id),
350 UNIQUE INDEX rev_id (rev_id),
351 INDEX rev_timestamp (rev_timestamp),
352 INDEX page_timestamp (rev_page,rev_timestamp),
353 INDEX user_timestamp (rev_user,rev_timestamp),
354 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
355 ) TYPE=InnoDB", $fname );
358 echo "......Locking tables.\n";
359 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
361 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
363 echo "......maxold is {$maxold}\n";
366 global $wgLegacySchemaConversion;
367 if( $wgLegacySchemaConversion ) {
368 // Create HistoryBlobCurStub entries.
369 // Text will be pulled from the leftover 'cur' table at runtime.
370 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
371 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
372 $cur_flags = "'object'";
374 // Copy all cur text in immediately: this may take longer but avoids
375 // having to keep an extra table around.
376 echo "......Moving text from cur.\n";
377 $cur_text = 'cur_text';
380 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
381 old_timestamp, old_minor_edit, old_flags)
382 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
383 FROM $cur", $fname );
386 echo "......Setting up revision table.\n";
387 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
389 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
390 old_timestamp, old_minor_edit
391 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
394 echo "......Setting up page table.\n";
395 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
396 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
397 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
398 cur_random, cur_touched, rev_id, LENGTH(cur_text)
400 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
403 echo "......Unlocking tables.\n";
404 $wgDatabase->query( "UNLOCK TABLES", $fname );
407 echo "......Renaming old.\n";
408 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
415 function do_inverse_timestamp() {
417 $fname="do_schema_restructuring";
418 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
419 echo "Removing revision.inverse_timestamp and fixing indexes... ";
420 dbsource( 'maintenance/archives/patch-inverse_timestamp.sql', $wgDatabase );
423 echo "revision timestamp indexes already up to 2005-03-13\n";
427 function do_text_id() {
429 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
430 echo "...rev_text_id already in place.\n";
432 echo "Adding rev_text_id field... ";
433 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
438 function do_namespace_size() {
442 'recentchanges' => 'rc',
444 'querycache' => 'qc',
447 foreach( $tables as $table => $prefix ) {
448 do_namespace_size_on( $table, $prefix );
453 function do_namespace_size_on( $table, $prefix ) {
455 $field = $prefix . '_namespace';
457 $tablename = $wgDatabase->tableName( $table );
458 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
459 $info = $wgDatabase->fetchObject( $result );
460 $wgDatabase->freeResult( $result );
462 if( substr( $info->Type, 0, 3 ) == 'int' ) {
463 echo "...$field is already a full int ($info->Type).\n";
465 echo "Promoting $field from $info->Type to int... ";
467 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
468 $wgDatabase->query( $sql );
474 function do_pagelinks_update() {
476 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
477 echo "...already have pagelinks table.\n";
479 echo "Converting links and brokenlinks tables to pagelinks... ";
480 dbsource( "maintenance/archives/patch-pagelinks.sql", $wgDatabase );
484 global $wgCanonicalNamespaceNames;
485 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
487 do_pagelinks_namespace( $ns );
493 function do_pagelinks_namespace( $namespace ) {
494 global $wgDatabase, $wgContLang;
496 $ns = IntVal( $namespace );
497 echo "Cleaning up broken links for namespace $ns... ";
499 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
500 $name = $wgContLang->getNsText( $ns );
501 $prefix = $wgDatabase->strencode( $name );
502 $likeprefix = str_replace( '_', '\\_', $prefix);
504 $sql = "UPDATE $pagelinks
505 SET pl_namespace=$ns,
506 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
508 AND pl_title LIKE '$likeprefix:%'";
510 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
514 function do_drop_img_type() {
517 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
518 echo "Dropping unused img_type field in image table... ";
519 dbsource( "maintenance/archives/patch-drop_img_type.sql", $wgDatabase );
522 echo "No img_type field in image table; Good.\n";
526 function do_old_links_update() {
528 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
529 echo "Already have pagelinks; skipping old links table updates.\n";
531 convertLinks(); flush();
535 function do_user_unique_update() {
537 $duper = new UserDupes( $wgDatabase );
538 if( $duper->hasUniqueIndex() ) {
539 echo "Already have unique user_name index.\n";
541 if( !$duper->clearDupes() ) {
542 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
544 echo "Adding unique index on user_name... ";
545 dbsource( 'maintenance/archives/patch-user_nameindex.sql', $wgDatabase );
550 function do_user_groups_update() {
551 $fname = 'do_user_groups_update';
554 if( $wgDatabase->tableExists( 'user_groups' ) ) {
555 echo "...user_groups table already exists.\n";
556 return do_user_groups_reformat();
559 echo "Adding user_groups table... ";
560 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
563 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
564 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
565 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
566 dbsource( 'maintenance/archives/patch-user_rights.sql', $wgDatabase );
569 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
570 echo "*** You may need to manually configure some sysops by manipulating\n";
571 echo "*** the user_groups table.\n";
576 echo "Converting user_rights table to user_groups... ";
577 $result = $wgDatabase->select( 'user_rights',
578 array( 'ur_user', 'ur_rights' ),
579 array( "ur_rights != ''" ),
582 while( $row = $wgDatabase->fetchObject( $result ) ) {
583 $groups = array_unique(
585 explode( ',', $row->ur_rights ) ) );
587 foreach( $groups as $group ) {
588 $wgDatabase->insert( 'user_groups',
590 'ug_user' => $row->ur_user,
591 'ug_group' => $group ),
595 $wgDatabase->freeResult( $result );
599 function do_user_groups_reformat() {
600 # Check for bogus formats from previous 1.5 alpha code.
602 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
604 if( $info->type == 'int' ) {
605 $oldug = $wgDatabase->tableName( 'user_groups' );
606 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
607 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
608 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
611 echo "Re-adding fresh user_groups table... ";
612 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
616 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
617 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
620 echo "...user_groups is in current format.\n";
625 function do_all_updates() {
626 global $wgNewTables, $wgNewFields, $wgRenamedTables;
629 foreach ( $wgRenamedTables as $tableRecord ) {
630 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
634 foreach ( $wgNewTables as $tableRecord ) {
635 add_table( $tableRecord[0], $tableRecord[1] );
640 foreach ( $wgNewFields as $fieldRecord ) {
641 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
645 # Do schema updates which require special handling
646 do_interwiki_update(); flush();
647 do_index_update(); flush();
648 do_old_links_update(); flush();
649 do_image_name_unique_update(); flush();
650 do_watchlist_update(); flush();
651 do_user_update(); flush();
652 do_copy_newtalk_to_watchlist(); flush();
653 do_logging_encoding(); flush();
655 do_schema_restructuring(); flush();
656 do_inverse_timestamp(); flush();
657 do_text_id(); flush();
658 do_namespace_size(); flush();
660 do_pagelinks_update(); flush();
662 do_drop_img_type(); flush();
664 do_user_unique_update(); flush();
665 do_user_groups_update(); flush();
667 initialiseMessages(); flush();