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' ),
29 # table field patch file (in maintenance/archives)
30 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
31 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
32 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
33 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
34 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
35 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
36 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
37 array( 'user', 'user_token', 'patch-user_token.sql' ),
38 array( 'user', 'user_email_token', 'patch-user_email_token.sql' ),
39 array( 'logging', 'log_params', 'patch-log_params.sql' ),
40 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
41 array( 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
42 array( 'page', 'page_len', 'patch-page_len.sql' ),
43 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
44 array( 'image', 'img_width', 'patch-img_width.sql' ),
45 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
46 array( 'image', 'img_media_type', 'patch-img_media_type.sql' ),
47 array( 'validate', 'val_ip', 'patch-val_ip.sql' ),
50 function rename_table( $from, $to, $patch ) {
52 if ( $wgDatabase->tableExists( $from ) ) {
53 if ( $wgDatabase->tableExists( $to ) ) {
54 echo "...can't move table $from to $to, $to already exists.\n";
56 echo "Moving table $from to $to...";
57 dbsource( "maintenance/archives/$patch", $wgDatabase );
61 // Source table does not exist
62 // Renames are done before creations, so this is typical for a new installation
67 function add_table( $name, $patch ) {
69 if ( $wgDatabase->tableExists( $name ) ) {
70 echo "...$name table already exists.\n";
72 echo "Creating $name table...";
73 dbsource( "maintenance/archives/$patch", $wgDatabase );
78 function add_field( $table, $field, $patch ) {
80 if ( !$wgDatabase->tableExists( $table ) ) {
81 echo "...$table table does not exist, skipping new field patch\n";
82 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
83 echo "...have $field field in $table table.\n";
85 echo "Adding $field field to table $table...";
86 dbsource( "maintenance/archives/$patch" , $wgDatabase );
91 function do_revision_updates() {
92 global $wgSoftwareRevision;
93 if ( $wgSoftwareRevision < 1001 ) {
98 function update_passwords() {
99 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
102 $fname = "Update script: update_passwords()";
103 print "\nIt appears that you need to update the user passwords in your\n" .
104 "database. If you have already done this (if you've run this update\n" .
105 "script once before, for example), doing so again will make all your\n" .
106 "user accounts inaccessible, so be sure you only do this once.\n" .
107 "Update user passwords? (yes/no)";
109 $resp = readconsole();
110 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
112 $sql = "SELECT user_id,user_password FROM user";
113 $source = $wgDatabase->query( $sql, $fname );
115 while ( $row = $wgDatabase->fetchObject( $source ) ) {
117 $oldpass = $row->user_password;
118 $newpass = md5( "{$id}-{$oldpass}" );
120 $sql = "UPDATE user SET user_password='{$newpass}' " .
121 "WHERE user_id={$id}";
122 $wgDatabase->query( $sql, $fname );
126 function do_interwiki_update() {
127 # Check that interwiki table exists; if it doesn't source it
129 if( $wgDatabase->tableExists( "interwiki" ) ) {
130 echo "...already have interwiki table\n";
133 echo "Creating interwiki table: ";
134 dbsource( "maintenance/archives/patch-interwiki.sql" );
136 echo "Adding default interwiki definitions: ";
137 dbsource( "maintenance/interwiki.sql" );
141 function do_index_update() {
142 # Check that proper indexes are in place
144 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
145 if( $meta->multiple_key == 0 ) {
146 echo "Updating indexes to 20031107: ";
147 dbsource( "maintenance/archives/patch-indexes.sql" );
151 echo "...indexes seem up to 20031107 standards\n";
155 function do_image_name_unique_update() {
157 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
158 echo "...image primary key already set.\n";
160 echo "Making img_name the primary key... ";
161 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
166 function do_watchlist_update() {
168 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
169 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
171 echo "ENOTIF: Adding wl_notificationtimestamp field for email notification management.";
172 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
173 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
178 function do_copy_newtalk_to_watchlist() {
180 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
182 if ( $wgDatabase->tableExists( 'user_newtalk' ) ) {
183 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
184 $wgDatabase->tableName( 'user_newtalk' ) );
185 $num_newtalks=$wgDatabase->numRows($res);
186 echo "ENOTIF: Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
189 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
190 $wluser = $wgDatabase->fetchObject( $res );
191 echo 'ENOTIF: <= user_newtalk: user_id='.$wluser->user_id.' user_ip='.$wluser->user_ip."\n";
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'
202 echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
204 } else { # normal users ... have user_ids
205 $user->setID($wluser->user_id);
206 $wgDatabase->replace( 'watchlist',
207 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
208 array('wl_user' => $user->getID(),
209 'wl_namespace' => NS_USER_TALK,
210 'wl_title' => $user->getName(),
211 'wl_notificationtimestamp' => '19700101000000'
212 ), 'updaters.inc::do_watchlist_update3'
214 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
217 echo "ENOTIF: The watchlist table has got the former user_newtalk entries.\n";
218 dbsource( "maintenance/archives/patch-drop-user_newtalk.sql", $wgDatabase );
219 echo "ENOTIF: Deleting the user_newtalk table as its entries are now in the watchlist table.\n";
221 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
226 function do_user_update() {
228 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
229 echo "User table contains old email authentication field. Dropping... ";
230 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
233 echo "...user table does not contain old email authentication field.\n";
238 * 1.4 betas were missing the 'binary' marker from logging.log_title,
239 * which causes a collation mismatch error on joins in MySQL 4.1.
241 function do_logging_encoding() {
243 $logging = $wgDatabase->tableName( 'logging' );
244 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
245 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
246 $wgDatabase->freeResult( $res );
248 if( in_array( 'binary', $flags ) ) {
249 echo "Logging table has correct title encoding.\n";
251 echo "Fixing title encoding on logging table... ";
252 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
257 function do_schema_restructuring() {
259 $fname="do_schema_restructuring";
260 if ( $wgDatabase->tableExists( 'page' ) ) {
261 echo "...page table already exists.\n";
263 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
265 echo "......checking for duplicate entries.\n"; flush();
267 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
269 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
270 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
272 if ( $wgDatabase->numRows( $rows ) > 0 ) {
274 echo "......<b>Found duplicate entries</b>\n";
275 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
276 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
277 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
278 $duplicate[$row->cur_namespace] = array();
280 $duplicate[$row->cur_namespace][] = $row->cur_title;
281 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
283 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
285 foreach ( $duplicate as $ns => $titles ) {
291 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
293 foreach ( $titles as $t ) {
295 $sql .= $wgDatabase->addQuotes( $t );
298 $sql .= ', ' . $wgDatabase->addQuotes( $t );
303 # By sorting descending, the most recent entry will be the first in the list.
304 # All following entries will be deleted by the next while-loop.
305 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
307 $rows = $wgDatabase->query( $sql, $fname );
309 $prev_title = $prev_namespace = false;
312 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
313 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
314 $deleteId[] = $row->cur_id;
316 $prev_title = $row->cur_title;
317 $prev_namespace = $row->cur_namespace;
319 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
320 $rows = $wgDatabase->query( $sql, $fname );
322 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
327 echo "......Creating tables.\n";
328 $wgDatabase->query("CREATE TABLE $page (
329 page_id int(8) unsigned NOT NULL auto_increment,
330 page_namespace int NOT NULL,
331 page_title varchar(255) binary NOT NULL,
332 page_restrictions tinyblob NOT NULL default '',
333 page_counter bigint(20) unsigned NOT NULL default '0',
334 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
335 page_is_new tinyint(1) unsigned NOT NULL default '0',
336 page_random real unsigned NOT NULL,
337 page_touched char(14) binary NOT NULL default '',
338 page_latest int(8) unsigned NOT NULL,
339 page_len int(8) unsigned NOT NULL,
341 PRIMARY KEY page_id (page_id),
342 UNIQUE INDEX name_title (page_namespace,page_title),
345 ) TYPE=InnoDB", $fname );
346 $wgDatabase->query("CREATE TABLE $revision (
347 rev_id int(8) unsigned NOT NULL auto_increment,
348 rev_page int(8) unsigned NOT NULL,
349 rev_comment tinyblob NOT NULL default '',
350 rev_user int(5) unsigned NOT NULL default '0',
351 rev_user_text varchar(255) binary NOT NULL default '',
352 rev_timestamp char(14) binary NOT NULL default '',
353 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
354 rev_deleted tinyint(1) unsigned NOT NULL default '0',
356 PRIMARY KEY rev_page_id (rev_page, rev_id),
357 UNIQUE INDEX rev_id (rev_id),
358 INDEX rev_timestamp (rev_timestamp),
359 INDEX page_timestamp (rev_page,rev_timestamp),
360 INDEX user_timestamp (rev_user,rev_timestamp),
361 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
362 ) TYPE=InnoDB", $fname );
365 echo "......Locking tables.\n";
366 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
368 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
370 echo "......maxold is {$maxold}\n";
373 global $wgLegacySchemaConversion;
374 if( $wgLegacySchemaConversion ) {
375 // Create HistoryBlobCurStub entries.
376 // Text will be pulled from the leftover 'cur' table at runtime.
377 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
378 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
379 $cur_flags = "'object'";
381 // Copy all cur text in immediately: this may take longer but avoids
382 // having to keep an extra table around.
383 echo "......Moving text from cur.\n";
384 $cur_text = 'cur_text';
387 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
388 old_timestamp, old_minor_edit, old_flags)
389 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
390 FROM $cur", $fname );
393 echo "......Setting up revision table.\n";
394 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
396 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
397 old_timestamp, old_minor_edit
398 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
401 echo "......Setting up page table.\n";
402 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
403 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
404 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
405 cur_random, cur_touched, rev_id, LENGTH(cur_text)
407 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
410 echo "......Unlocking tables.\n";
411 $wgDatabase->query( "UNLOCK TABLES", $fname );
414 echo "......Renaming old.\n";
415 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
422 function do_inverse_timestamp() {
424 $fname="do_schema_restructuring";
425 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
426 echo "Removing revision.inverse_timestamp and fixing indexes... ";
427 dbsource( 'maintenance/archives/patch-inverse_timestamp.sql', $wgDatabase );
430 echo "revision timestamp indexes already up to 2005-03-13\n";
434 function do_text_id() {
436 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
437 echo "...rev_text_id already in place.\n";
439 echo "Adding rev_text_id field... ";
440 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
445 function do_namespace_size() {
449 'recentchanges' => 'rc',
451 'querycache' => 'qc',
454 foreach( $tables as $table => $prefix ) {
455 do_namespace_size_on( $table, $prefix );
460 function do_namespace_size_on( $table, $prefix ) {
462 $field = $prefix . '_namespace';
464 $tablename = $wgDatabase->tableName( $table );
465 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
466 $info = $wgDatabase->fetchObject( $result );
467 $wgDatabase->freeResult( $result );
469 if( substr( $info->Type, 0, 3 ) == 'int' ) {
470 echo "...$field is already a full int ($info->Type).\n";
472 echo "Promoting $field from $info->Type to int... ";
474 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
475 $wgDatabase->query( $sql );
481 function do_pagelinks_update() {
483 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
484 echo "...already have pagelinks table.\n";
486 echo "Converting links and brokenlinks tables to pagelinks... ";
487 dbsource( "maintenance/archives/patch-pagelinks.sql", $wgDatabase );
491 global $wgCanonicalNamespaceNames;
492 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
494 do_pagelinks_namespace( $ns );
500 function do_pagelinks_namespace( $namespace ) {
501 global $wgDatabase, $wgContLang;
503 $ns = IntVal( $namespace );
504 echo "Cleaning up broken links for namespace $ns... ";
506 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
507 $name = $wgContLang->getNsText( $ns );
508 $prefix = $wgDatabase->strencode( $name );
509 $likeprefix = str_replace( '_', '\\_', $prefix);
511 $sql = "UPDATE $pagelinks
512 SET pl_namespace=$ns,
513 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
515 AND pl_title LIKE '$likeprefix:%'";
517 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
521 function do_drop_img_type() {
524 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
525 echo "Dropping unused img_type field in image table... ";
526 dbsource( "maintenance/archives/patch-drop_img_type.sql", $wgDatabase );
529 echo "No img_type field in image table; Good.\n";
533 function do_old_links_update() {
535 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
536 echo "Already have pagelinks; skipping old links table updates.\n";
538 convertLinks(); flush();
542 function do_user_unique_update() {
544 $duper = new UserDupes( $wgDatabase );
545 if( $duper->hasUniqueIndex() ) {
546 echo "Already have unique user_name index.\n";
548 if( !$duper->clearDupes() ) {
549 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
551 echo "Adding unique index on user_name... ";
552 dbsource( 'maintenance/archives/patch-user_nameindex.sql', $wgDatabase );
557 function do_user_groups_update() {
558 $fname = 'do_user_groups_update';
561 if( $wgDatabase->tableExists( 'user_groups' ) ) {
562 echo "...user_groups table already exists.\n";
563 return do_user_groups_reformat();
566 echo "Adding user_groups table... ";
567 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
570 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
571 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
572 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
573 dbsource( 'maintenance/archives/patch-user_rights.sql', $wgDatabase );
576 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
577 echo "*** You may need to manually configure some sysops by manipulating\n";
578 echo "*** the user_groups table.\n";
583 echo "Converting user_rights table to user_groups... ";
584 $result = $wgDatabase->select( 'user_rights',
585 array( 'ur_user', 'ur_rights' ),
586 array( "ur_rights != ''" ),
589 while( $row = $wgDatabase->fetchObject( $result ) ) {
590 $groups = array_unique(
592 explode( ',', $row->ur_rights ) ) );
594 foreach( $groups as $group ) {
595 $wgDatabase->insert( 'user_groups',
597 'ug_user' => $row->ur_user,
598 'ug_group' => $group ),
602 $wgDatabase->freeResult( $result );
606 function do_user_groups_reformat() {
607 # Check for bogus formats from previous 1.5 alpha code.
609 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
611 if( $info->type == 'int' ) {
612 $oldug = $wgDatabase->tableName( 'user_groups' );
613 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
614 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
615 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
618 echo "Re-adding fresh user_groups table... ";
619 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
623 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
624 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
627 echo "...user_groups is in current format.\n";
632 function do_all_updates() {
633 global $wgNewTables, $wgNewFields, $wgRenamedTables;
636 foreach ( $wgRenamedTables as $tableRecord ) {
637 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
641 foreach ( $wgNewTables as $tableRecord ) {
642 add_table( $tableRecord[0], $tableRecord[1] );
647 foreach ( $wgNewFields as $fieldRecord ) {
648 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
652 # Do schema updates which require special handling
653 do_interwiki_update(); flush();
654 do_index_update(); flush();
655 do_old_links_update(); flush();
656 do_image_name_unique_update(); flush();
657 do_watchlist_update(); flush();
658 do_user_update(); flush();
659 do_copy_newtalk_to_watchlist(); flush();
660 do_logging_encoding(); flush();
662 do_schema_restructuring(); flush();
663 do_inverse_timestamp(); flush();
664 do_text_id(); flush();
665 do_namespace_size(); flush();
667 do_pagelinks_update(); flush();
669 do_drop_img_type(); flush();
671 do_user_unique_update(); flush();
672 do_user_groups_update(); flush();
674 initialiseMessages(); flush();