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' ),
48 array( 'site_stats', 'ss_total_articles', 'patch-ss_total_articles.sql' ),
51 function rename_table( $from, $to, $patch ) {
53 if ( $wgDatabase->tableExists( $from ) ) {
54 if ( $wgDatabase->tableExists( $to ) ) {
55 echo "...can't move table $from to $to, $to already exists.\n";
57 echo "Moving table $from to $to...";
58 dbsource( "maintenance/archives/$patch", $wgDatabase );
62 // Source table does not exist
63 // Renames are done before creations, so this is typical for a new installation
68 function add_table( $name, $patch ) {
70 if ( $wgDatabase->tableExists( $name ) ) {
71 echo "...$name table already exists.\n";
73 echo "Creating $name table...";
74 dbsource( "maintenance/archives/$patch", $wgDatabase );
79 function add_field( $table, $field, $patch ) {
81 if ( !$wgDatabase->tableExists( $table ) ) {
82 echo "...$table table does not exist, skipping new field patch\n";
83 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
84 echo "...have $field field in $table table.\n";
86 echo "Adding $field field to table $table...";
87 dbsource( "maintenance/archives/$patch" , $wgDatabase );
92 function do_revision_updates() {
93 global $wgSoftwareRevision;
94 if ( $wgSoftwareRevision < 1001 ) {
99 function update_passwords() {
100 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
103 $fname = "Update script: update_passwords()";
104 print "\nIt appears that you need to update the user passwords in your\n" .
105 "database. If you have already done this (if you've run this update\n" .
106 "script once before, for example), doing so again will make all your\n" .
107 "user accounts inaccessible, so be sure you only do this once.\n" .
108 "Update user passwords? (yes/no)";
110 $resp = readconsole();
111 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
113 $sql = "SELECT user_id,user_password FROM user";
114 $source = $wgDatabase->query( $sql, $fname );
116 while ( $row = $wgDatabase->fetchObject( $source ) ) {
118 $oldpass = $row->user_password;
119 $newpass = md5( "{$id}-{$oldpass}" );
121 $sql = "UPDATE user SET user_password='{$newpass}' " .
122 "WHERE user_id={$id}";
123 $wgDatabase->query( $sql, $fname );
127 function do_interwiki_update() {
128 # Check that interwiki table exists; if it doesn't source it
130 if( $wgDatabase->tableExists( "interwiki" ) ) {
131 echo "...already have interwiki table\n";
134 echo "Creating interwiki table: ";
135 dbsource( "maintenance/archives/patch-interwiki.sql" );
137 echo "Adding default interwiki definitions: ";
138 dbsource( "maintenance/interwiki.sql" );
142 function do_index_update() {
143 # Check that proper indexes are in place
145 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
146 if( $meta->multiple_key == 0 ) {
147 echo "Updating indexes to 20031107: ";
148 dbsource( "maintenance/archives/patch-indexes.sql" );
152 echo "...indexes seem up to 20031107 standards\n";
156 function do_image_name_unique_update() {
158 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
159 echo "...image primary key already set.\n";
161 echo "Making img_name the primary key... ";
162 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
167 function do_watchlist_update() {
169 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
170 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
172 echo "ENOTIF: Adding wl_notificationtimestamp field for email notification management.";
173 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
174 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
179 function do_copy_newtalk_to_watchlist() {
181 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
183 if ( $wgDatabase->tableExists( 'user_newtalk' ) ) {
184 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
185 $wgDatabase->tableName( 'user_newtalk' ) );
186 $num_newtalks=$wgDatabase->numRows($res);
187 echo "ENOTIF: 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 echo 'ENOTIF: <= user_newtalk: user_id='.$wluser->user_id.' user_ip='.$wluser->user_ip."\n";
193 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
194 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
195 $wgDatabase->replace( 'watchlist',
196 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
197 array('wl_user' => 0,
198 'wl_namespace' => NS_USER_TALK,
199 'wl_title' => $wluser->user_ip,
200 'wl_notificationtimestamp' => '19700101000000'
201 ), 'updaters.inc::do_watchlist_update2'
203 echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
205 } else { # normal users ... have user_ids
206 $user->setID($wluser->user_id);
207 $wgDatabase->replace( 'watchlist',
208 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
209 array('wl_user' => $user->getID(),
210 'wl_namespace' => NS_USER_TALK,
211 'wl_title' => $user->getName(),
212 'wl_notificationtimestamp' => '19700101000000'
213 ), 'updaters.inc::do_watchlist_update3'
215 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
218 echo "ENOTIF: The watchlist table has got the former user_newtalk entries.\n";
219 dbsource( "maintenance/archives/patch-drop-user_newtalk.sql", $wgDatabase );
220 echo "ENOTIF: Deleting the user_newtalk table as its entries are now in the watchlist table.\n";
222 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
227 function do_user_update() {
229 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
230 echo "User table contains old email authentication field. Dropping... ";
231 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
234 echo "...user table does not contain old email authentication field.\n";
239 * 1.4 betas were missing the 'binary' marker from logging.log_title,
240 * which causes a collation mismatch error on joins in MySQL 4.1.
242 function do_logging_encoding() {
244 $logging = $wgDatabase->tableName( 'logging' );
245 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
246 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
247 $wgDatabase->freeResult( $res );
249 if( in_array( 'binary', $flags ) ) {
250 echo "Logging table has correct title encoding.\n";
252 echo "Fixing title encoding on logging table... ";
253 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
258 function do_schema_restructuring() {
260 $fname="do_schema_restructuring";
261 if ( $wgDatabase->tableExists( 'page' ) ) {
262 echo "...page table already exists.\n";
264 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
266 echo "......checking for duplicate entries.\n"; flush();
268 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
270 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
271 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
273 if ( $wgDatabase->numRows( $rows ) > 0 ) {
275 echo "......<b>Found duplicate entries</b>\n";
276 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
277 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
278 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
279 $duplicate[$row->cur_namespace] = array();
281 $duplicate[$row->cur_namespace][] = $row->cur_title;
282 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
284 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
286 foreach ( $duplicate as $ns => $titles ) {
292 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
294 foreach ( $titles as $t ) {
296 $sql .= $wgDatabase->addQuotes( $t );
299 $sql .= ', ' . $wgDatabase->addQuotes( $t );
304 # By sorting descending, the most recent entry will be the first in the list.
305 # All following entries will be deleted by the next while-loop.
306 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
308 $rows = $wgDatabase->query( $sql, $fname );
310 $prev_title = $prev_namespace = false;
313 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
314 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
315 $deleteId[] = $row->cur_id;
317 $prev_title = $row->cur_title;
318 $prev_namespace = $row->cur_namespace;
320 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
321 $rows = $wgDatabase->query( $sql, $fname );
323 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
328 echo "......Creating tables.\n";
329 $wgDatabase->query("CREATE TABLE $page (
330 page_id int(8) unsigned NOT NULL auto_increment,
331 page_namespace int NOT NULL,
332 page_title varchar(255) binary NOT NULL,
333 page_restrictions tinyblob NOT NULL default '',
334 page_counter bigint(20) unsigned NOT NULL default '0',
335 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
336 page_is_new tinyint(1) unsigned NOT NULL default '0',
337 page_random real unsigned NOT NULL,
338 page_touched char(14) binary NOT NULL default '',
339 page_latest int(8) unsigned NOT NULL,
340 page_len int(8) unsigned NOT NULL,
342 PRIMARY KEY page_id (page_id),
343 UNIQUE INDEX name_title (page_namespace,page_title),
346 ) TYPE=InnoDB", $fname );
347 $wgDatabase->query("CREATE TABLE $revision (
348 rev_id int(8) unsigned NOT NULL auto_increment,
349 rev_page int(8) unsigned NOT NULL,
350 rev_comment tinyblob NOT NULL default '',
351 rev_user int(5) unsigned NOT NULL default '0',
352 rev_user_text varchar(255) binary NOT NULL default '',
353 rev_timestamp char(14) binary NOT NULL default '',
354 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
355 rev_deleted tinyint(1) unsigned NOT NULL default '0',
357 PRIMARY KEY rev_page_id (rev_page, rev_id),
358 UNIQUE INDEX rev_id (rev_id),
359 INDEX rev_timestamp (rev_timestamp),
360 INDEX page_timestamp (rev_page,rev_timestamp),
361 INDEX user_timestamp (rev_user,rev_timestamp),
362 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
363 ) TYPE=InnoDB", $fname );
366 echo "......Locking tables.\n";
367 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
369 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
371 echo "......maxold is {$maxold}\n";
374 global $wgLegacySchemaConversion;
375 if( $wgLegacySchemaConversion ) {
376 // Create HistoryBlobCurStub entries.
377 // Text will be pulled from the leftover 'cur' table at runtime.
378 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
379 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
380 $cur_flags = "'object'";
382 // Copy all cur text in immediately: this may take longer but avoids
383 // having to keep an extra table around.
384 echo "......Moving text from cur.\n";
385 $cur_text = 'cur_text';
388 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
389 old_timestamp, old_minor_edit, old_flags)
390 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
391 FROM $cur", $fname );
394 echo "......Setting up revision table.\n";
395 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
397 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
398 old_timestamp, old_minor_edit
399 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
402 echo "......Setting up page table.\n";
403 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
404 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
405 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
406 cur_random, cur_touched, rev_id, LENGTH(cur_text)
408 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
411 echo "......Unlocking tables.\n";
412 $wgDatabase->query( "UNLOCK TABLES", $fname );
415 echo "......Renaming old.\n";
416 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
423 function do_inverse_timestamp() {
425 $fname="do_schema_restructuring";
426 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
427 echo "Removing revision.inverse_timestamp and fixing indexes... ";
428 dbsource( 'maintenance/archives/patch-inverse_timestamp.sql', $wgDatabase );
431 echo "revision timestamp indexes already up to 2005-03-13\n";
435 function do_text_id() {
437 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
438 echo "...rev_text_id already in place.\n";
440 echo "Adding rev_text_id field... ";
441 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
446 function do_namespace_size() {
450 'recentchanges' => 'rc',
452 'querycache' => 'qc',
455 foreach( $tables as $table => $prefix ) {
456 do_namespace_size_on( $table, $prefix );
461 function do_namespace_size_on( $table, $prefix ) {
463 $field = $prefix . '_namespace';
465 $tablename = $wgDatabase->tableName( $table );
466 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
467 $info = $wgDatabase->fetchObject( $result );
468 $wgDatabase->freeResult( $result );
470 if( substr( $info->Type, 0, 3 ) == 'int' ) {
471 echo "...$field is already a full int ($info->Type).\n";
473 echo "Promoting $field from $info->Type to int... ";
475 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
476 $wgDatabase->query( $sql );
482 function do_pagelinks_update() {
484 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
485 echo "...already have pagelinks table.\n";
487 echo "Converting links and brokenlinks tables to pagelinks... ";
488 dbsource( "maintenance/archives/patch-pagelinks.sql", $wgDatabase );
492 global $wgCanonicalNamespaceNames;
493 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
495 do_pagelinks_namespace( $ns );
501 function do_pagelinks_namespace( $namespace ) {
502 global $wgDatabase, $wgContLang;
504 $ns = IntVal( $namespace );
505 echo "Cleaning up broken links for namespace $ns... ";
507 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
508 $name = $wgContLang->getNsText( $ns );
509 $prefix = $wgDatabase->strencode( $name );
510 $likeprefix = str_replace( '_', '\\_', $prefix);
512 $sql = "UPDATE $pagelinks
513 SET pl_namespace=$ns,
514 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
516 AND pl_title LIKE '$likeprefix:%'";
518 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
522 function do_drop_img_type() {
525 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
526 echo "Dropping unused img_type field in image table... ";
527 dbsource( "maintenance/archives/patch-drop_img_type.sql", $wgDatabase );
530 echo "No img_type field in image table; Good.\n";
534 function do_old_links_update() {
536 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
537 echo "Already have pagelinks; skipping old links table updates.\n";
539 convertLinks(); flush();
543 function do_user_unique_update() {
545 $duper = new UserDupes( $wgDatabase );
546 if( $duper->hasUniqueIndex() ) {
547 echo "Already have unique user_name index.\n";
549 if( !$duper->clearDupes() ) {
550 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
552 echo "Adding unique index on user_name... ";
553 dbsource( 'maintenance/archives/patch-user_nameindex.sql', $wgDatabase );
558 function do_user_groups_update() {
559 $fname = 'do_user_groups_update';
562 if( $wgDatabase->tableExists( 'user_groups' ) ) {
563 echo "...user_groups table already exists.\n";
564 return do_user_groups_reformat();
567 echo "Adding user_groups table... ";
568 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
571 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
572 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
573 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
574 dbsource( 'maintenance/archives/patch-user_rights.sql', $wgDatabase );
577 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
578 echo "*** You may need to manually configure some sysops by manipulating\n";
579 echo "*** the user_groups table.\n";
584 echo "Converting user_rights table to user_groups... ";
585 $result = $wgDatabase->select( 'user_rights',
586 array( 'ur_user', 'ur_rights' ),
587 array( "ur_rights != ''" ),
590 while( $row = $wgDatabase->fetchObject( $result ) ) {
591 $groups = array_unique(
593 explode( ',', $row->ur_rights ) ) );
595 foreach( $groups as $group ) {
596 $wgDatabase->insert( 'user_groups',
598 'ug_user' => $row->ur_user,
599 'ug_group' => $group ),
603 $wgDatabase->freeResult( $result );
607 function do_user_groups_reformat() {
608 # Check for bogus formats from previous 1.5 alpha code.
610 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
612 if( $info->type == 'int' ) {
613 $oldug = $wgDatabase->tableName( 'user_groups' );
614 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
615 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
616 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
619 echo "Re-adding fresh user_groups table... ";
620 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
624 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
625 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
628 echo "...user_groups is in current format.\n";
633 function do_all_updates() {
634 global $wgNewTables, $wgNewFields, $wgRenamedTables;
637 foreach ( $wgRenamedTables as $tableRecord ) {
638 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
642 foreach ( $wgNewTables as $tableRecord ) {
643 add_table( $tableRecord[0], $tableRecord[1] );
648 foreach ( $wgNewFields as $fieldRecord ) {
649 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
653 # Do schema updates which require special handling
654 do_interwiki_update(); flush();
655 do_index_update(); flush();
656 do_old_links_update(); flush();
657 do_image_name_unique_update(); flush();
658 do_watchlist_update(); flush();
659 do_user_update(); flush();
660 do_copy_newtalk_to_watchlist(); flush();
661 do_logging_encoding(); flush();
663 do_schema_restructuring(); flush();
664 do_inverse_timestamp(); flush();
665 do_text_id(); flush();
666 do_namespace_size(); flush();
668 do_pagelinks_update(); flush();
670 do_drop_img_type(); flush();
672 do_user_unique_update(); flush();
673 do_user_groups_update(); flush();
675 initialiseMessages(); flush();