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