* Adding name for ilo (Ilokano)
[mediawiki.git] / maintenance / updaters.inc
blob757f38d57821720538ca50dd5a7130924c992172
1 <?php
2 /**
3  * @package MediaWiki
4  * @subpackage Maintenance
5  */
7  /** */
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11 require_once 'userDupes.inc';
13 $wgRenamedTables = array(
14 #           from             to                  patch file
15 #       array( 'group',         'groups',           'patch-rename-group.sql' ),
18 $wgNewTables = array(
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' ),
27         array( 'transcache',    'patch-transcache.sql' ),
28         array( 'trackbacks',    'patch-trackbacks.sql' ),
31 $wgNewFields = array(
32 #           table            field             patch file (in maintenance/archives)
33         array( 'ipblocks',      'ipb_id',           'patch-ipblocks.sql' ),
34         array( 'ipblocks',      'ipb_expiry',       'patch-ipb_expiry.sql' ),
35         array( 'recentchanges', 'rc_type',          'patch-rc_type.sql' ),
36         array( 'recentchanges', 'rc_ip',            'patch-rc_ip.sql' ),
37         array( 'recentchanges', 'rc_id',            'patch-rc_id.sql' ),
38         array( 'recentchanges', 'rc_patrolled',     'patch-rc-patrol.sql' ),
39         array( 'user',          'user_real_name',   'patch-user-realname.sql' ),
40         array( 'user',          'user_token',       'patch-user_token.sql' ),
41         array( 'user',          'user_email_token', 'patch-user_email_token.sql' ),
42         array( 'logging',       'log_params',       'patch-log_params.sql' ),
43         array( 'archive',       'ar_rev_id',        'patch-archive-rev_id.sql' ),
44         array( 'archive',       'ar_text_id',       'patch-archive-text_id.sql' ),
45         array( 'page',          'page_len',         'patch-page_len.sql' ),
46         array( 'revision',      'rev_deleted',      'patch-rev_deleted.sql' ),
47         array( 'image',         'img_width',        'patch-img_width.sql' ),
48         array( 'image',         'img_metadata',     'patch-img_metadata.sql' ),
49         array( 'image',         'img_media_type',   'patch-img_media_type.sql' ),
50         array( 'validate',      'val_ip',           'patch-val_ip.sql' ),
51         array( 'site_stats',    'ss_total_pages',   'patch-ss_total_articles.sql' ),
52         array( 'interwiki',     'iw_trans',         'patch-interwiki-trans.sql' ),
55 function rename_table( $from, $to, $patch ) {
56         global $wgDatabase;
57         if ( $wgDatabase->tableExists( $from ) ) {
58                 if ( $wgDatabase->tableExists( $to ) ) {
59                         echo "...can't move table $from to $to, $to already exists.\n";
60                 } else {
61                         echo "Moving table $from to $to...";
62                         dbsource( archive($patch), $wgDatabase );
63                         echo "ok\n";
64                 }
65         } else {
66                 // Source table does not exist
67                 // Renames are done before creations, so this is typical for a new installation
68                 // Ignore silently
69         }
72 function add_table( $name, $patch ) {
73         global $wgDatabase;
74         if ( $wgDatabase->tableExists( $name ) ) {
75                 echo "...$name table already exists.\n";
76         } else {
77                 echo "Creating $name table...";
78                 dbsource( archive($patch), $wgDatabase );
79                 echo "ok\n";
80         }
83 function add_field( $table, $field, $patch ) {
84         global $wgDatabase;
85         if ( !$wgDatabase->tableExists( $table ) ) {
86                 echo "...$table table does not exist, skipping new field patch\n";
87         } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
88                 echo "...have $field field in $table table.\n";
89         } else {
90                 echo "Adding $field field to table $table...";
91                 dbsource( archive($patch) , $wgDatabase );
92                 echo "ok\n";
93         }
96 function do_revision_updates() {
97         global $wgSoftwareRevision;
98         if ( $wgSoftwareRevision < 1001 ) {
99                 update_passwords();
100         }
103 function update_passwords() {
104         wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
106         global $wgDatabase;
107         $fname = "Update script: update_passwords()";
108         print "\nIt appears that you need to update the user passwords in your\n" .
109           "database. If you have already done this (if you've run this update\n" .
110           "script once before, for example), doing so again will make all your\n" .
111           "user accounts inaccessible, so be sure you only do this once.\n" .
112           "Update user passwords? (yes/no)";
114         $resp = readconsole();
115     if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
117         $sql = "SELECT user_id,user_password FROM user";
118         $source = $wgDatabase->query( $sql, $fname );
120         while ( $row = $wgDatabase->fetchObject( $source ) ) {
121                 $id = $row->user_id;
122                 $oldpass = $row->user_password;
123                 $newpass = md5( "{$id}-{$oldpass}" );
125                 $sql = "UPDATE user SET user_password='{$newpass}' " .
126                   "WHERE user_id={$id}";
127                 $wgDatabase->query( $sql, $fname );
128         }
131 function do_interwiki_update() {
132         # Check that interwiki table exists; if it doesn't source it
133         global $wgDatabase;
134         if( $wgDatabase->tableExists( "interwiki" ) ) {
135                 echo "...already have interwiki table\n";
136                 return true;
137         }
138         echo "Creating interwiki table: ";
139         dbsource( archive("patch-interwiki.sql") );
140         echo "ok\n";
141         echo "Adding default interwiki definitions: ";
142         dbsource( "maintenance/interwiki.sql" );
143         echo "ok\n";
146 function do_index_update() {
147         # Check that proper indexes are in place
148         global $wgDatabase;
149         $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
150         if( $meta->multiple_key == 0 ) {
151                 echo "Updating indexes to 20031107: ";
152                 dbsource( archive("patch-indexes.sql") );
153                 echo "ok\n";
154                 return true;
155         }
156         echo "...indexes seem up to 20031107 standards\n";
157         return false;
160 function do_image_index_update() {
161         global $wgDatabase;
162         
163         $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
164         if( $meta->multiple_key == 0 ) {
165                 echo "Updating indexes to 20050912: ";
166                 dbsource( archive("patch-mimesearch-indexes.sql") );
167                 echo "ok\n";
168                 return true;
169         }
170         echo "...indexes seem up to 20050912 standards\n";
171         return false;
174 function do_image_name_unique_update() {
175         global $wgDatabase;
176         if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
177                 echo "...image primary key already set.\n";
178         } else {
179                 echo "Making img_name the primary key... ";
180                 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
181                 echo "ok\n";
182         }
185 function do_watchlist_update() {
186         global $wgDatabase;
187         if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
188                 echo "The watchlist table is already set up for email notification.\n";
189         } else {
190                 echo "Adding wl_notificationtimestamp field for email notification management.";
191                 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
192                 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
193                 echo "ok\n";
194         }
197 function do_copy_newtalk_to_watchlist() {
198         global $wgDatabase;
199         global $wgCommandLineMode;      # this needs to be saved while getID() and getName() are called
201         $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
202                 $wgDatabase->tableName( 'user_newtalk' ) );
203         $num_newtalks=$wgDatabase->numRows($res);
204         echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
206         $user = new User();
207         for ( $i = 1; $i <= $num_newtalks; $i++ ) {
208                 $wluser = $wgDatabase->fetchObject( $res );
209                 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
210                         if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
211                                 $wgDatabase->replace( 'watchlist',
212                                         array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
213                                           array('wl_user'                       => 0,
214                                                 'wl_namespace'                  => NS_USER_TALK,
215                                                 'wl_title'                      => $wluser->user_ip,
216                                                 'wl_notificationtimestamp'      => '19700101000000'
217                                                 ), 'updaters.inc::do_watchlist_update2'
218                                         );
219                         }
220                 } else { # normal users ... have user_ids
221                         $user->setID($wluser->user_id);
222                         $wgDatabase->replace( 'watchlist',
223                                 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
224                                   array('wl_user'                       => $user->getID(),
225                                         'wl_namespace'                  => NS_USER_TALK,
226                                         'wl_title'                      => $user->getName(),
227                                         'wl_notificationtimestamp'      => '19700101000000'
228                                         ), 'updaters.inc::do_watchlist_update3'
229                                 );
230                 }
231         }
232         echo "Done.\n";
236 function do_user_update() {
237         global $wgDatabase;
238         if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
239                 echo "User table contains old email authentication field. Dropping... ";
240                 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
241                 echo "ok\n";
242         } else {
243                 echo "...user table does not contain old email authentication field.\n";
244         }
248  * 1.4 betas were missing the 'binary' marker from logging.log_title,
249  * which causes a collation mismatch error on joins in MySQL 4.1.
250  */
251 function do_logging_encoding() {
252         global $wgDatabase, $wgDBtype;
253         if ($wgDBtype != 'mysql')
254                 return;
255         $logging = $wgDatabase->tableName( 'logging' );
256         $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
257         $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
258         $wgDatabase->freeResult( $res );
260         if( in_array( 'binary', $flags ) ) {
261                 echo "Logging table has correct title encoding.\n";
262         } else {
263                 echo "Fixing title encoding on logging table... ";
264                 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
265                 echo "ok\n";
266         }
269 function do_schema_restructuring() {
270         global $wgDatabase;
271         $fname="do_schema_restructuring";
272         if ( $wgDatabase->tableExists( 'page' ) ) {
273                 echo "...page table already exists.\n";
274         } else {
275                 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
276                 echo wfTimestamp();
277                 echo "......checking for duplicate entries.\n"; flush();
279                 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
281                 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
282                                 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
284                 if ( $wgDatabase->numRows( $rows ) > 0 ) {
285                         echo wfTimestamp();
286                         echo "......<b>Found duplicate entries</b>\n";
287                         echo ( sprintf( "<b>      %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
288                         while ( $row = $wgDatabase->fetchObject( $rows ) ) {
289                                 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
290                                         $duplicate[$row->cur_namespace] = array();
291                                 }
292                                 $duplicate[$row->cur_namespace][] = $row->cur_title;
293                                 echo ( sprintf( "      %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
294                         }
295                         $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
296                         $firstCond = true;
297                         foreach ( $duplicate as $ns => $titles ) {
298                                 if ( $firstCond ) {
299                                         $firstCond = false;
300                                 } else {
301                                         $sql .= ' OR ';
302                                 }
303                                 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
304                                 $first = true;
305                                 foreach ( $titles as $t ) {
306                                         if ( $first ) {
307                                                 $sql .= $wgDatabase->addQuotes( $t );
308                                                 $first = false;
309                                         } else {
310                                                 $sql .= ', ' . $wgDatabase->addQuotes( $t );
311                                         }
312                                 }
313                                 $sql .= ") ) \n";
314                         }
315                         # By sorting descending, the most recent entry will be the first in the list.
316                         # All following entries will be deleted by the next while-loop.
317                         $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
319                         $rows = $wgDatabase->query( $sql, $fname );
321                         $prev_title = $prev_namespace = false;
322                         $deleteId = array();
324                         while ( $row = $wgDatabase->fetchObject( $rows ) ) {
325                                 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
326                                         $deleteId[] = $row->cur_id;
327                                 }
328                                 $prev_title     = $row->cur_title;
329                                 $prev_namespace = $row->cur_namespace;
330                         }
331                         $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
332                         $rows = $wgDatabase->query( $sql, $fname );
333                         echo wfTimestamp();
334                         echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
335                 }
338                 echo wfTimestamp();
339                 echo "......Creating tables.\n";
340                 $wgDatabase->query("CREATE TABLE $page (
341                         page_id int(8) unsigned NOT NULL auto_increment,
342                         page_namespace int NOT NULL,
343                         page_title varchar(255) binary NOT NULL,
344                         page_restrictions tinyblob NOT NULL default '',
345                         page_counter bigint(20) unsigned NOT NULL default '0',
346                         page_is_redirect tinyint(1) unsigned NOT NULL default '0',
347                         page_is_new tinyint(1) unsigned NOT NULL default '0',
348                         page_random real unsigned NOT NULL,
349                         page_touched char(14) binary NOT NULL default '',
350                         page_latest int(8) unsigned NOT NULL,
351                         page_len int(8) unsigned NOT NULL,
353                         PRIMARY KEY page_id (page_id),
354                         UNIQUE INDEX name_title (page_namespace,page_title),
355                         INDEX (page_random),
356                         INDEX (page_len)
357                         ) TYPE=InnoDB", $fname );
358                 $wgDatabase->query("CREATE TABLE $revision (
359                         rev_id int(8) unsigned NOT NULL auto_increment,
360                         rev_page int(8) unsigned NOT NULL,
361                         rev_comment tinyblob NOT NULL default '',
362                         rev_user int(5) unsigned NOT NULL default '0',
363                         rev_user_text varchar(255) binary NOT NULL default '',
364                         rev_timestamp char(14) binary NOT NULL default '',
365                         rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
366                         rev_deleted tinyint(1) unsigned NOT NULL default '0',
368                         PRIMARY KEY rev_page_id (rev_page, rev_id),
369                         UNIQUE INDEX rev_id (rev_id),
370                         INDEX rev_timestamp (rev_timestamp),
371                         INDEX page_timestamp (rev_page,rev_timestamp),
372                         INDEX user_timestamp (rev_user,rev_timestamp),
373                         INDEX usertext_timestamp (rev_user_text,rev_timestamp)
374                         ) TYPE=InnoDB", $fname );
376                 echo wfTimestamp();
377                 echo "......Locking tables.\n";
378                 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
380                 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
381                 echo wfTimestamp();
382                 echo "......maxold is {$maxold}\n";
384                 echo wfTimestamp();
385                 global $wgLegacySchemaConversion;
386                 if( $wgLegacySchemaConversion ) {
387                         // Create HistoryBlobCurStub entries.
388                         // Text will be pulled from the leftover 'cur' table at runtime.
389                         echo "......Moving metadata from cur; using blob references to text in cur table.\n";
390                         $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
391                         $cur_flags = "'object'";
392                 } else {
393                         // Copy all cur text in immediately: this may take longer but avoids
394                         // having to keep an extra table around.
395                         echo "......Moving text from cur.\n";
396                         $cur_text = 'cur_text';
397                         $cur_flags = "''";
398                 }
399                 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
400                                 old_timestamp, old_minor_edit, old_flags)
401                         SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
402                         FROM $cur", $fname );
404                 echo wfTimestamp();
405                 echo "......Setting up revision table.\n";
406                 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
407                                 rev_minor_edit)
408                         SELECT old_id, cur_id, old_comment, old_user, old_user_text,
409                                 old_timestamp, old_minor_edit
410                         FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
412                 echo wfTimestamp();
413                 echo "......Setting up page table.\n";
414                 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
415                                 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
416                         SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
417                                 cur_random, cur_touched, rev_id, LENGTH(cur_text)
418                         FROM $cur,$revision
419                         WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
421                 echo wfTimestamp();
422                 echo "......Unlocking tables.\n";
423                 $wgDatabase->query( "UNLOCK TABLES", $fname );
425                 echo wfTimestamp();
426                 echo "......Renaming old.\n";
427                 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
429                 echo wfTimestamp();
430                 echo "...done.\n";
431         }
434 function do_inverse_timestamp() {
435         global $wgDatabase;
436         $fname="do_schema_restructuring";
437         if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
438                 echo "Removing revision.inverse_timestamp and fixing indexes... ";
439                 dbsource( 'maintenance/archives/patch-inverse_timestamp.sql', $wgDatabase );
440                 echo "ok\n";
441         } else {
442                 echo "revision timestamp indexes already up to 2005-03-13\n";
443         }
446 function do_text_id() {
447         global $wgDatabase;
448         if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
449                 echo "...rev_text_id already in place.\n";
450         } else {
451                 echo "Adding rev_text_id field... ";
452                 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
453                 echo "ok\n";
454         }
457 function do_namespace_size() {
458         $tables = array(
459                 'page'          => 'page',
460                 'archive'       => 'ar',
461                 'recentchanges' => 'rc',
462                 'watchlist'     => 'wl',
463                 'querycache'    => 'qc',
464                 'logging'       => 'log',
465         );
466         foreach( $tables as $table => $prefix ) {
467                 do_namespace_size_on( $table, $prefix );
468                 flush();
469         }
472 function do_namespace_size_on( $table, $prefix ) {
473         global $wgDatabase, $wgDBtype;
474         if ($wgDBtype != 'mysql')
475                 return;
476         $field = $prefix . '_namespace';
478         $tablename = $wgDatabase->tableName( $table );
479         $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
480         $info = $wgDatabase->fetchObject( $result );
481         $wgDatabase->freeResult( $result );
483         if( substr( $info->Type, 0, 3 ) == 'int' ) {
484                 echo "...$field is already a full int ($info->Type).\n";
485         } else {
486                 echo "Promoting $field from $info->Type to int... ";
488                 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
489                 $wgDatabase->query( $sql );
491                 echo "ok\n";
492         }
495 function do_pagelinks_update() {
496         global $wgDatabase;
497         if( $wgDatabase->tableExists( 'pagelinks' ) ) {
498                 echo "...already have pagelinks table.\n";
499         } else {
500                 echo "Converting links and brokenlinks tables to pagelinks... ";
501                 dbsource( "maintenance/archives/patch-pagelinks.sql", $wgDatabase );
502                 echo "ok\n";
503                 flush();
505                 global $wgCanonicalNamespaceNames;
506                 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
507                         if( $ns != 0 ) {
508                                 do_pagelinks_namespace( $ns );
509                         }
510                 }
511         }
514 function do_pagelinks_namespace( $namespace ) {
515         global $wgDatabase, $wgContLang;
517         $ns = intval( $namespace );
518         echo "Cleaning up broken links for namespace $ns... ";
520         $pagelinks = $wgDatabase->tableName( 'pagelinks' );
521         $name = $wgContLang->getNsText( $ns );
522         $prefix = $wgDatabase->strencode( $name );
523         $likeprefix = str_replace( '_', '\\_', $prefix);
525         $sql = "UPDATE $pagelinks
526                    SET pl_namespace=$ns,
527                        pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
528                  WHERE pl_namespace=0
529                    AND pl_title LIKE '$likeprefix:%'";
531         $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
532         echo "ok\n";
535 function do_drop_img_type() {
536         global $wgDatabase;
538         if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
539                 echo "Dropping unused img_type field in image table... ";
540                 dbsource( "maintenance/archives/patch-drop_img_type.sql", $wgDatabase );
541                 echo "ok\n";
542         } else {
543                 echo "No img_type field in image table; Good.\n";
544         }
547 function do_old_links_update() {
548         global $wgDatabase;
549         if( $wgDatabase->tableExists( 'pagelinks' ) ) {
550                 echo "Already have pagelinks; skipping old links table updates.\n";
551         } else {
552                 convertLinks(); flush();
553         }
556 function do_user_unique_update() {
557         global $wgDatabase;
558         $duper = new UserDupes( $wgDatabase );
559         if( $duper->hasUniqueIndex() ) {
560                 echo "Already have unique user_name index.\n";
561         } else {
562                 if( !$duper->clearDupes() ) {
563                         echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
564                 }
565                 echo "Adding unique index on user_name... ";
566                 dbsource( 'maintenance/archives/patch-user_nameindex.sql', $wgDatabase );
567                 echo "ok\n";
568         }
571 function do_user_groups_update() {
572         $fname = 'do_user_groups_update';
573         global $wgDatabase;
575         if( $wgDatabase->tableExists( 'user_groups' ) ) {
576                 echo "...user_groups table already exists.\n";
577                 return do_user_groups_reformat();
578         }
580         echo "Adding user_groups table... ";
581         dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
582         echo "ok\n";
584         if( !$wgDatabase->tableExists( 'user_rights' ) ) {
585                 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
586                         echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
587                         dbsource( 'maintenance/archives/patch-user_rights.sql', $wgDatabase );
588                         echo "ok\n";
589                 } else {
590                         echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
591                         echo "*** You may need to manually configure some sysops by manipulating\n";
592                         echo "*** the user_groups table.\n";
593                         return;
594                 }
595         }
597         echo "Converting user_rights table to user_groups... ";
598         $result = $wgDatabase->select( 'user_rights',
599                 array( 'ur_user', 'ur_rights' ),
600                 array( "ur_rights != ''" ),
601                 $fname );
603         while( $row = $wgDatabase->fetchObject( $result ) ) {
604                 $groups = array_unique(
605                         array_map( 'trim',
606                                 explode( ',', $row->ur_rights ) ) );
608                 foreach( $groups as $group ) {
609                         $wgDatabase->insert( 'user_groups',
610                                 array(
611                                         'ug_user'  => $row->ur_user,
612                                         'ug_group' => $group ),
613                                 $fname );
614                 }
615         }
616         $wgDatabase->freeResult( $result );
617         echo "ok\n";
620 function do_user_groups_reformat() {
621         # Check for bogus formats from previous 1.5 alpha code.
622         global $wgDatabase;
623         $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
625         if( $info->type == 'int' ) {
626                 $oldug = $wgDatabase->tableName( 'user_groups' );
627                 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
628                 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
629                 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
630                 echo "ok\n";
632                 echo "Re-adding fresh user_groups table... ";
633                 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
634                 echo "ok\n";
636                 echo "***\n";
637                 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
638                 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
639                 echo "***\n";
640         } else {
641                 echo "...user_groups is in current format.\n";
642         }
646 function do_watchlist_null() {
647         # Make sure wl_notificationtimestamp can be NULL,
648         # and update old broken items.
649         global $wgDatabase;
650         $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
651         
652         if( $info->not_null ) {
653                 echo "Making wl_notificationtimestamp nullable... ";
654                 dbsource( 'maintenance/archives/patch-watchlist-null.sql', $wgDatabase );
655                 echo "ok\n";
656         } else {
657                 echo "...wl_notificationtimestamp is already nullable.\n";
658         }
662 function do_all_updates() {
663         global $wgNewTables, $wgNewFields, $wgRenamedTables;
665         # Rename tables
666         foreach ( $wgRenamedTables as $tableRecord ) {
667                 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
668         }
670         # Add missing tables
671         foreach ( $wgNewTables as $tableRecord ) {
672                 add_table( $tableRecord[0], $tableRecord[1] );
673                 flush();
674         }
676         # Add missing fields
677         foreach ( $wgNewFields as $fieldRecord ) {
678                 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
679                 flush();
680         }
682         # Do schema updates which require special handling
683         do_interwiki_update(); flush();
684         do_index_update(); flush();
685         do_old_links_update(); flush();
686         do_image_name_unique_update(); flush();
687         do_watchlist_update(); flush();
688         do_user_update(); flush();
689 ######  do_copy_newtalk_to_watchlist(); flush();
690         do_logging_encoding(); flush();
692         do_schema_restructuring(); flush();
693         do_inverse_timestamp(); flush();
694         do_text_id(); flush();
695         do_namespace_size(); flush();
697         do_pagelinks_update(); flush();
699         do_drop_img_type(); flush();
701         do_user_unique_update(); flush();
702         do_user_groups_update(); flush();
703         
704         do_watchlist_null(); flush();
706         do_image_index_update(); flush();
708         initialiseMessages(); flush();
711 function archive($name) {
712         global $wgDBtype;
713         switch ($wgDBtype) {
714         case "oracle":
715                 return "maintenance/oracle/archives/$name";
716         default:
717                 return "maintenance/archives/$name";
718         }