Rework the user_groups system, again, into something that seems to actually
[lhc/web/wiklou.git] / maintenance / updaters.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 /** */
8
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11 require_once 'userDupes.inc';
12
13 $wgRenamedTables = array(
14 # from to patch file
15 # array( 'group', 'groups', 'patch-rename-group.sql' ),
16 );
17
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 );
27
28 $wgNewFields = array(
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 );
49
50 function rename_table( $from, $to, $patch ) {
51 global $wgDatabase;
52 if ( $wgDatabase->tableExists( $from ) ) {
53 if ( $wgDatabase->tableExists( $to ) ) {
54 echo "...can't move table $from to $to, $to already exists.\n";
55 } else {
56 echo "Moving table $from to $to...";
57 dbsource( "maintenance/archives/$patch", $wgDatabase );
58 echo "ok\n";
59 }
60 } else {
61 // Source table does not exist
62 // Renames are done before creations, so this is typical for a new installation
63 // Ignore silently
64 }
65 }
66
67 function add_table( $name, $patch ) {
68 global $wgDatabase;
69 if ( $wgDatabase->tableExists( $name ) ) {
70 echo "...$name table already exists.\n";
71 } else {
72 echo "Creating $name table...";
73 dbsource( "maintenance/archives/$patch", $wgDatabase );
74 echo "ok\n";
75 }
76 }
77
78 function add_field( $table, $field, $patch ) {
79 global $wgDatabase;
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";
84 } else {
85 echo "Adding $field field to table $table...";
86 dbsource( "maintenance/archives/$patch" , $wgDatabase );
87 echo "ok\n";
88 }
89 }
90
91 function do_revision_updates() {
92 global $wgSoftwareRevision;
93 if ( $wgSoftwareRevision < 1001 ) {
94 update_passwords();
95 }
96 }
97
98 function update_passwords() {
99 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
100
101 global $wgDatabase;
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)";
108
109 $resp = readconsole();
110 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
111
112 $sql = "SELECT user_id,user_password FROM user";
113 $source = $wgDatabase->query( $sql, $fname );
114
115 while ( $row = $wgDatabase->fetchObject( $source ) ) {
116 $id = $row->user_id;
117 $oldpass = $row->user_password;
118 $newpass = md5( "{$id}-{$oldpass}" );
119
120 $sql = "UPDATE user SET user_password='{$newpass}' " .
121 "WHERE user_id={$id}";
122 $wgDatabase->query( $sql, $fname );
123 }
124 }
125
126 function do_interwiki_update() {
127 # Check that interwiki table exists; if it doesn't source it
128 global $wgDatabase;
129 if( $wgDatabase->tableExists( "interwiki" ) ) {
130 echo "...already have interwiki table\n";
131 return true;
132 }
133 echo "Creating interwiki table: ";
134 dbsource( "maintenance/archives/patch-interwiki.sql" );
135 echo "ok\n";
136 echo "Adding default interwiki definitions: ";
137 dbsource( "maintenance/interwiki.sql" );
138 echo "ok\n";
139 }
140
141 function do_index_update() {
142 # Check that proper indexes are in place
143 global $wgDatabase;
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" );
148 echo "ok\n";
149 return true;
150 }
151 echo "...indexes seem up to 20031107 standards\n";
152 return false;
153 }
154
155 function do_image_name_unique_update() {
156 global $wgDatabase;
157 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
158 echo "...image primary key already set.\n";
159 } else {
160 echo "Making img_name the primary key... ";
161 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
162 echo "ok\n";
163 }
164 }
165
166 function do_watchlist_update() {
167 global $wgDatabase;
168 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
169 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
170 } else {
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 );
174 echo "ok\n";
175 }
176 }
177
178 function do_copy_newtalk_to_watchlist() {
179 global $wgDatabase;
180 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
181
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";
187
188 $user = new User();
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'
201 );
202 echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
203 }
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'
213 );
214 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
215 }
216 }
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";
220 } else {
221 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
222 }
223 }
224
225
226 function do_user_update() {
227 global $wgDatabase;
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 );
231 echo "ok\n";
232 } else {
233 echo "...user table does not contain old email authentication field.\n";
234 }
235 }
236
237 /**
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.
240 */
241 function do_logging_encoding() {
242 global $wgDatabase;
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 );
247
248 if( in_array( 'binary', $flags ) ) {
249 echo "Logging table has correct title encoding.\n";
250 } else {
251 echo "Fixing title encoding on logging table... ";
252 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
253 echo "ok\n";
254 }
255 }
256
257 function do_schema_restructuring() {
258 global $wgDatabase;
259 $fname="do_schema_restructuring";
260 if ( $wgDatabase->tableExists( 'page' ) ) {
261 echo "...page table already exists.\n";
262 } else {
263 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
264 echo wfTimestamp();
265 echo "......checking for duplicate entries.\n"; flush();
266
267 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
268
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 );
271
272 if ( $wgDatabase->numRows( $rows ) > 0 ) {
273 echo wfTimestamp();
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();
279 }
280 $duplicate[$row->cur_namespace][] = $row->cur_title;
281 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
282 }
283 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
284 $firstCond = true;
285 foreach ( $duplicate as $ns => $titles ) {
286 if ( $firstCond ) {
287 $firstCond = false;
288 } else {
289 $sql .= ' OR ';
290 }
291 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
292 $first = true;
293 foreach ( $titles as $t ) {
294 if ( $first ) {
295 $sql .= $wgDatabase->addQuotes( $t );
296 $first = false;
297 } else {
298 $sql .= ', ' . $wgDatabase->addQuotes( $t );
299 }
300 }
301 $sql .= ") ) \n";
302 }
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';
306
307 $rows = $wgDatabase->query( $sql, $fname );
308
309 $prev_title = $prev_namespace = false;
310 $deleteId = array();
311
312 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
313 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
314 $deleteId[] = $row->cur_id;
315 }
316 $prev_title = $row->cur_title;
317 $prev_namespace = $row->cur_namespace;
318 }
319 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
320 $rows = $wgDatabase->query( $sql, $fname );
321 echo wfTimestamp();
322 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
323 }
324
325
326 echo wfTimestamp();
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,
340
341 PRIMARY KEY page_id (page_id),
342 UNIQUE INDEX name_title (page_namespace,page_title),
343 INDEX (page_random),
344 INDEX (page_len)
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',
355
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 );
363
364 echo wfTimestamp();
365 echo "......Locking tables.\n";
366 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
367
368 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
369 echo wfTimestamp();
370 echo "......maxold is {$maxold}\n";
371
372 echo wfTimestamp();
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'";
380 } else {
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';
385 $cur_flags = "''";
386 }
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 );
391
392 echo wfTimestamp();
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,
395 rev_minor_edit)
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 );
399
400 echo wfTimestamp();
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)
406 FROM $cur,$revision
407 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
408
409 echo wfTimestamp();
410 echo "......Unlocking tables.\n";
411 $wgDatabase->query( "UNLOCK TABLES", $fname );
412
413 echo wfTimestamp();
414 echo "......Renaming old.\n";
415 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
416
417 echo wfTimestamp();
418 echo "...done.\n";
419 }
420 }
421
422 function do_inverse_timestamp() {
423 global $wgDatabase;
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 );
428 echo "ok\n";
429 } else {
430 echo "revision timestamp indexes already up to 2005-03-13\n";
431 }
432 }
433
434 function do_text_id() {
435 global $wgDatabase;
436 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
437 echo "...rev_text_id already in place.\n";
438 } else {
439 echo "Adding rev_text_id field... ";
440 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
441 echo "ok\n";
442 }
443 }
444
445 function do_namespace_size() {
446 $tables = array(
447 'page' => 'page',
448 'archive' => 'ar',
449 'recentchanges' => 'rc',
450 'watchlist' => 'wl',
451 'querycache' => 'qc',
452 'logging' => 'log',
453 );
454 foreach( $tables as $table => $prefix ) {
455 do_namespace_size_on( $table, $prefix );
456 flush();
457 }
458 }
459
460 function do_namespace_size_on( $table, $prefix ) {
461 global $wgDatabase;
462 $field = $prefix . '_namespace';
463
464 $tablename = $wgDatabase->tableName( $table );
465 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
466 $info = $wgDatabase->fetchObject( $result );
467 $wgDatabase->freeResult( $result );
468
469 if( substr( $info->Type, 0, 3 ) == 'int' ) {
470 echo "...$field is already a full int ($info->Type).\n";
471 } else {
472 echo "Promoting $field from $info->Type to int... ";
473
474 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
475 $wgDatabase->query( $sql );
476
477 echo "ok\n";
478 }
479 }
480
481 function do_pagelinks_update() {
482 global $wgDatabase;
483 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
484 echo "...already have pagelinks table.\n";
485 } else {
486 echo "Converting links and brokenlinks tables to pagelinks... ";
487 dbsource( "maintenance/archives/patch-pagelinks.sql", $wgDatabase );
488 echo "ok\n";
489 flush();
490
491 global $wgCanonicalNamespaceNames;
492 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
493 if( $ns != 0 ) {
494 do_pagelinks_namespace( $ns );
495 }
496 }
497 }
498 }
499
500 function do_pagelinks_namespace( $namespace ) {
501 global $wgDatabase, $wgContLang;
502
503 $ns = IntVal( $namespace );
504 echo "Cleaning up broken links for namespace $ns... ";
505
506 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
507 $name = $wgContLang->getNsText( $ns );
508 $prefix = $wgDatabase->strencode( $name );
509 $likeprefix = str_replace( '_', '\\_', $prefix);
510
511 $sql = "UPDATE $pagelinks
512 SET pl_namespace=$ns,
513 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
514 WHERE pl_namespace=0
515 AND pl_title LIKE '$likeprefix:%'";
516
517 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
518 echo "ok\n";
519 }
520
521 function do_drop_img_type() {
522 global $wgDatabase;
523
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 );
527 echo "ok\n";
528 } else {
529 echo "No img_type field in image table; Good.\n";
530 }
531 }
532
533 function do_old_links_update() {
534 global $wgDatabase;
535 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
536 echo "Already have pagelinks; skipping old links table updates.\n";
537 } else {
538 convertLinks(); flush();
539 }
540 }
541
542 function do_user_unique_update() {
543 global $wgDatabase;
544 $duper = new UserDupes( $wgDatabase );
545 if( $duper->hasUniqueIndex() ) {
546 echo "Already have unique user_name index.\n";
547 } else {
548 if( !$duper->clearDupes() ) {
549 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
550 }
551 echo "Adding unique index on user_name... ";
552 dbsource( 'maintenance/archives/patch-user_nameindex.sql', $wgDatabase );
553 echo "ok\n";
554 }
555 }
556
557 function do_user_groups_update() {
558 $fname = 'do_user_groups_update';
559 global $wgDatabase;
560
561 if( $wgDatabase->tableExists( 'user_groups' ) ) {
562 echo "...user_groups table already exists.\n";
563 return do_user_groups_reformat();
564 }
565
566 echo "Adding user_groups table... ";
567 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
568 echo "ok\n";
569
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 );
574 echo "ok\n";
575 } else {
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";
579 return;
580 }
581 }
582
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 != ''" ),
587 $fname );
588
589 while( $row = $wgDatabase->fetchObject( $result ) ) {
590 $groups = array_unique(
591 array_map( 'trim',
592 explode( ',', $row->ur_rights ) ) );
593
594 foreach( $groups as $group ) {
595 $wgDatabase->insert( 'user_groups',
596 array(
597 'ug_user' => $row->ur_user,
598 'ug_group' => $group ),
599 $fname );
600 }
601 }
602 $wgDatabase->freeResult( $result );
603 echo "ok\n";
604 }
605
606 function do_user_groups_reformat() {
607 # Check for bogus formats from previous 1.5 alpha code.
608 global $wgDatabase;
609 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
610
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" );
616 echo "ok\n";
617
618 echo "Re-adding fresh user_groups table... ";
619 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
620 echo "ok\n";
621
622 echo "***\n";
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";
625 echo "***\n";
626 } else {
627 echo "...user_groups is in current format.\n";
628 }
629
630 }
631
632 function do_all_updates() {
633 global $wgNewTables, $wgNewFields, $wgRenamedTables;
634
635 # Rename tables
636 foreach ( $wgRenamedTables as $tableRecord ) {
637 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
638 }
639
640 # Add missing tables
641 foreach ( $wgNewTables as $tableRecord ) {
642 add_table( $tableRecord[0], $tableRecord[1] );
643 flush();
644 }
645
646 # Add missing fields
647 foreach ( $wgNewFields as $fieldRecord ) {
648 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
649 flush();
650 }
651
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();
661
662 do_schema_restructuring(); flush();
663 do_inverse_timestamp(); flush();
664 do_text_id(); flush();
665 do_namespace_size(); flush();
666
667 do_pagelinks_update(); flush();
668
669 do_drop_img_type(); flush();
670
671 do_user_unique_update(); flush();
672 do_user_groups_update(); flush();
673
674 initialiseMessages(); flush();
675 }
676
677 ?>