* Extra newline for pretty output
[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 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
27 array( 'transcache', 'patch-transcache.sql' ),
28 array( 'trackbacks', 'patch-trackbacks.sql' ),
29 );
30
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' ),
53 );
54
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 }
70 }
71
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 }
81 }
82
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 }
94 }
95
96 function do_revision_updates() {
97 global $wgSoftwareRevision;
98 if ( $wgSoftwareRevision < 1001 ) {
99 update_passwords();
100 }
101 }
102
103 function update_passwords() {
104 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
105
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)";
113
114 $resp = readconsole();
115 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
116
117 $sql = "SELECT user_id,user_password FROM user";
118 $source = $wgDatabase->query( $sql, $fname );
119
120 while ( $row = $wgDatabase->fetchObject( $source ) ) {
121 $id = $row->user_id;
122 $oldpass = $row->user_password;
123 $newpass = md5( "{$id}-{$oldpass}" );
124
125 $sql = "UPDATE user SET user_password='{$newpass}' " .
126 "WHERE user_id={$id}";
127 $wgDatabase->query( $sql, $fname );
128 }
129 }
130
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";
144 }
145
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;
158 }
159
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;
172 }
173
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 }
183 }
184
185 function do_logging_timestamp_index() {
186 global $wgDatabase;
187 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
188 echo "...timestamp key on logging already exists.\n";
189 } else {
190 echo "Adding timestamp key on logging table... ";
191 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
192 echo "ok\n";
193 }
194 }
195
196
197 function do_watchlist_update() {
198 global $wgDatabase;
199 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
200 echo "The watchlist table is already set up for email notification.\n";
201 } else {
202 echo "Adding wl_notificationtimestamp field for email notification management.";
203 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
204 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
205 echo "ok\n";
206 }
207 }
208
209 function do_copy_newtalk_to_watchlist() {
210 global $wgDatabase;
211 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
212
213 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
214 $wgDatabase->tableName( 'user_newtalk' ) );
215 $num_newtalks=$wgDatabase->numRows($res);
216 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
217
218 $user = new User();
219 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
220 $wluser = $wgDatabase->fetchObject( $res );
221 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
222 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
223 $wgDatabase->replace( 'watchlist',
224 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
225 array('wl_user' => 0,
226 'wl_namespace' => NS_USER_TALK,
227 'wl_title' => $wluser->user_ip,
228 'wl_notificationtimestamp' => '19700101000000'
229 ), 'updaters.inc::do_watchlist_update2'
230 );
231 }
232 } else { # normal users ... have user_ids
233 $user->setID($wluser->user_id);
234 $wgDatabase->replace( 'watchlist',
235 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
236 array('wl_user' => $user->getID(),
237 'wl_namespace' => NS_USER_TALK,
238 'wl_title' => $user->getName(),
239 'wl_notificationtimestamp' => '19700101000000'
240 ), 'updaters.inc::do_watchlist_update3'
241 );
242 }
243 }
244 echo "Done.\n";
245 }
246
247
248 function do_user_update() {
249 global $wgDatabase;
250 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
251 echo "User table contains old email authentication field. Dropping... ";
252 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
253 echo "ok\n";
254 } else {
255 echo "...user table does not contain old email authentication field.\n";
256 }
257 }
258
259 /**
260 * 1.4 betas were missing the 'binary' marker from logging.log_title,
261 * which causes a collation mismatch error on joins in MySQL 4.1.
262 */
263 function do_logging_encoding() {
264 global $wgDatabase, $wgDBtype;
265 if ($wgDBtype != 'mysql')
266 return;
267 $logging = $wgDatabase->tableName( 'logging' );
268 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
269 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
270 $wgDatabase->freeResult( $res );
271
272 if( in_array( 'binary', $flags ) ) {
273 echo "Logging table has correct title encoding.\n";
274 } else {
275 echo "Fixing title encoding on logging table... ";
276 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
277 echo "ok\n";
278 }
279 }
280
281 function do_schema_restructuring() {
282 global $wgDatabase;
283 $fname="do_schema_restructuring";
284 if ( $wgDatabase->tableExists( 'page' ) ) {
285 echo "...page table already exists.\n";
286 } else {
287 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
288 echo wfTimestamp();
289 echo "......checking for duplicate entries.\n"; flush();
290
291 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
292
293 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
294 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
295
296 if ( $wgDatabase->numRows( $rows ) > 0 ) {
297 echo wfTimestamp();
298 echo "......<b>Found duplicate entries</b>\n";
299 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
300 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
301 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
302 $duplicate[$row->cur_namespace] = array();
303 }
304 $duplicate[$row->cur_namespace][] = $row->cur_title;
305 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
306 }
307 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
308 $firstCond = true;
309 foreach ( $duplicate as $ns => $titles ) {
310 if ( $firstCond ) {
311 $firstCond = false;
312 } else {
313 $sql .= ' OR ';
314 }
315 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
316 $first = true;
317 foreach ( $titles as $t ) {
318 if ( $first ) {
319 $sql .= $wgDatabase->addQuotes( $t );
320 $first = false;
321 } else {
322 $sql .= ', ' . $wgDatabase->addQuotes( $t );
323 }
324 }
325 $sql .= ") ) \n";
326 }
327 # By sorting descending, the most recent entry will be the first in the list.
328 # All following entries will be deleted by the next while-loop.
329 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
330
331 $rows = $wgDatabase->query( $sql, $fname );
332
333 $prev_title = $prev_namespace = false;
334 $deleteId = array();
335
336 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
337 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
338 $deleteId[] = $row->cur_id;
339 }
340 $prev_title = $row->cur_title;
341 $prev_namespace = $row->cur_namespace;
342 }
343 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
344 $rows = $wgDatabase->query( $sql, $fname );
345 echo wfTimestamp();
346 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
347 }
348
349
350 echo wfTimestamp();
351 echo "......Creating tables.\n";
352 $wgDatabase->query("CREATE TABLE $page (
353 page_id int(8) unsigned NOT NULL auto_increment,
354 page_namespace int NOT NULL,
355 page_title varchar(255) binary NOT NULL,
356 page_restrictions tinyblob NOT NULL default '',
357 page_counter bigint(20) unsigned NOT NULL default '0',
358 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
359 page_is_new tinyint(1) unsigned NOT NULL default '0',
360 page_random real unsigned NOT NULL,
361 page_touched char(14) binary NOT NULL default '',
362 page_latest int(8) unsigned NOT NULL,
363 page_len int(8) unsigned NOT NULL,
364
365 PRIMARY KEY page_id (page_id),
366 UNIQUE INDEX name_title (page_namespace,page_title),
367 INDEX (page_random),
368 INDEX (page_len)
369 ) TYPE=InnoDB", $fname );
370 $wgDatabase->query("CREATE TABLE $revision (
371 rev_id int(8) unsigned NOT NULL auto_increment,
372 rev_page int(8) unsigned NOT NULL,
373 rev_comment tinyblob NOT NULL default '',
374 rev_user int(5) unsigned NOT NULL default '0',
375 rev_user_text varchar(255) binary NOT NULL default '',
376 rev_timestamp char(14) binary NOT NULL default '',
377 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
378 rev_deleted tinyint(1) unsigned NOT NULL default '0',
379
380 PRIMARY KEY rev_page_id (rev_page, rev_id),
381 UNIQUE INDEX rev_id (rev_id),
382 INDEX rev_timestamp (rev_timestamp),
383 INDEX page_timestamp (rev_page,rev_timestamp),
384 INDEX user_timestamp (rev_user,rev_timestamp),
385 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
386 ) TYPE=InnoDB", $fname );
387
388 echo wfTimestamp();
389 echo "......Locking tables.\n";
390 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
391
392 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
393 echo wfTimestamp();
394 echo "......maxold is {$maxold}\n";
395
396 echo wfTimestamp();
397 global $wgLegacySchemaConversion;
398 if( $wgLegacySchemaConversion ) {
399 // Create HistoryBlobCurStub entries.
400 // Text will be pulled from the leftover 'cur' table at runtime.
401 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
402 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
403 $cur_flags = "'object'";
404 } else {
405 // Copy all cur text in immediately: this may take longer but avoids
406 // having to keep an extra table around.
407 echo "......Moving text from cur.\n";
408 $cur_text = 'cur_text';
409 $cur_flags = "''";
410 }
411 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
412 old_timestamp, old_minor_edit, old_flags)
413 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
414 FROM $cur", $fname );
415
416 echo wfTimestamp();
417 echo "......Setting up revision table.\n";
418 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
419 rev_minor_edit)
420 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
421 old_timestamp, old_minor_edit
422 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
423
424 echo wfTimestamp();
425 echo "......Setting up page table.\n";
426 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
427 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
428 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
429 cur_random, cur_touched, rev_id, LENGTH(cur_text)
430 FROM $cur,$revision
431 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
432
433 echo wfTimestamp();
434 echo "......Unlocking tables.\n";
435 $wgDatabase->query( "UNLOCK TABLES", $fname );
436
437 echo wfTimestamp();
438 echo "......Renaming old.\n";
439 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
440
441 echo wfTimestamp();
442 echo "...done.\n";
443 }
444 }
445
446 function do_inverse_timestamp() {
447 global $wgDatabase;
448 $fname="do_schema_restructuring";
449 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
450 echo "Removing revision.inverse_timestamp and fixing indexes... ";
451 dbsource( 'maintenance/archives/patch-inverse_timestamp.sql', $wgDatabase );
452 echo "ok\n";
453 } else {
454 echo "revision timestamp indexes already up to 2005-03-13\n";
455 }
456 }
457
458 function do_text_id() {
459 global $wgDatabase;
460 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
461 echo "...rev_text_id already in place.\n";
462 } else {
463 echo "Adding rev_text_id field... ";
464 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
465 echo "ok\n";
466 }
467 }
468
469 function do_namespace_size() {
470 $tables = array(
471 'page' => 'page',
472 'archive' => 'ar',
473 'recentchanges' => 'rc',
474 'watchlist' => 'wl',
475 'querycache' => 'qc',
476 'logging' => 'log',
477 );
478 foreach( $tables as $table => $prefix ) {
479 do_namespace_size_on( $table, $prefix );
480 flush();
481 }
482 }
483
484 function do_namespace_size_on( $table, $prefix ) {
485 global $wgDatabase, $wgDBtype;
486 if ($wgDBtype != 'mysql')
487 return;
488 $field = $prefix . '_namespace';
489
490 $tablename = $wgDatabase->tableName( $table );
491 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
492 $info = $wgDatabase->fetchObject( $result );
493 $wgDatabase->freeResult( $result );
494
495 if( substr( $info->Type, 0, 3 ) == 'int' ) {
496 echo "...$field is already a full int ($info->Type).\n";
497 } else {
498 echo "Promoting $field from $info->Type to int... ";
499
500 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
501 $wgDatabase->query( $sql );
502
503 echo "ok\n";
504 }
505 }
506
507 function do_pagelinks_update() {
508 global $wgDatabase;
509 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
510 echo "...already have pagelinks table.\n";
511 } else {
512 echo "Converting links and brokenlinks tables to pagelinks... ";
513 dbsource( "maintenance/archives/patch-pagelinks.sql", $wgDatabase );
514 echo "ok\n";
515 flush();
516
517 global $wgCanonicalNamespaceNames;
518 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
519 if( $ns != 0 ) {
520 do_pagelinks_namespace( $ns );
521 }
522 }
523 }
524 }
525
526 function do_pagelinks_namespace( $namespace ) {
527 global $wgDatabase, $wgContLang;
528
529 $ns = intval( $namespace );
530 echo "Cleaning up broken links for namespace $ns... ";
531
532 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
533 $name = $wgContLang->getNsText( $ns );
534 $prefix = $wgDatabase->strencode( $name );
535 $likeprefix = str_replace( '_', '\\_', $prefix);
536
537 $sql = "UPDATE $pagelinks
538 SET pl_namespace=$ns,
539 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
540 WHERE pl_namespace=0
541 AND pl_title LIKE '$likeprefix:%'";
542
543 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
544 echo "ok\n";
545 }
546
547 function do_drop_img_type() {
548 global $wgDatabase;
549
550 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
551 echo "Dropping unused img_type field in image table... ";
552 dbsource( "maintenance/archives/patch-drop_img_type.sql", $wgDatabase );
553 echo "ok\n";
554 } else {
555 echo "No img_type field in image table; Good.\n";
556 }
557 }
558
559 function do_old_links_update() {
560 global $wgDatabase;
561 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
562 echo "Already have pagelinks; skipping old links table updates.\n";
563 } else {
564 convertLinks(); flush();
565 }
566 }
567
568 function do_user_unique_update() {
569 global $wgDatabase;
570 $duper = new UserDupes( $wgDatabase );
571 if( $duper->hasUniqueIndex() ) {
572 echo "Already have unique user_name index.\n";
573 } else {
574 if( !$duper->clearDupes() ) {
575 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
576 }
577 echo "Adding unique index on user_name... ";
578 dbsource( 'maintenance/archives/patch-user_nameindex.sql', $wgDatabase );
579 echo "ok\n";
580 }
581 }
582
583 function do_user_groups_update() {
584 $fname = 'do_user_groups_update';
585 global $wgDatabase;
586
587 if( $wgDatabase->tableExists( 'user_groups' ) ) {
588 echo "...user_groups table already exists.\n";
589 return do_user_groups_reformat();
590 }
591
592 echo "Adding user_groups table... ";
593 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
594 echo "ok\n";
595
596 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
597 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
598 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
599 dbsource( 'maintenance/archives/patch-user_rights.sql', $wgDatabase );
600 echo "ok\n";
601 } else {
602 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
603 echo "*** You may need to manually configure some sysops by manipulating\n";
604 echo "*** the user_groups table.\n";
605 return;
606 }
607 }
608
609 echo "Converting user_rights table to user_groups... ";
610 $result = $wgDatabase->select( 'user_rights',
611 array( 'ur_user', 'ur_rights' ),
612 array( "ur_rights != ''" ),
613 $fname );
614
615 while( $row = $wgDatabase->fetchObject( $result ) ) {
616 $groups = array_unique(
617 array_map( 'trim',
618 explode( ',', $row->ur_rights ) ) );
619
620 foreach( $groups as $group ) {
621 $wgDatabase->insert( 'user_groups',
622 array(
623 'ug_user' => $row->ur_user,
624 'ug_group' => $group ),
625 $fname );
626 }
627 }
628 $wgDatabase->freeResult( $result );
629 echo "ok\n";
630 }
631
632 function do_user_groups_reformat() {
633 # Check for bogus formats from previous 1.5 alpha code.
634 global $wgDatabase;
635 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
636
637 if( $info->type == 'int' ) {
638 $oldug = $wgDatabase->tableName( 'user_groups' );
639 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
640 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
641 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
642 echo "ok\n";
643
644 echo "Re-adding fresh user_groups table... ";
645 dbsource( 'maintenance/archives/patch-user_groups.sql', $wgDatabase );
646 echo "ok\n";
647
648 echo "***\n";
649 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
650 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
651 echo "***\n";
652 } else {
653 echo "...user_groups is in current format.\n";
654 }
655
656 }
657
658 function do_watchlist_null() {
659 # Make sure wl_notificationtimestamp can be NULL,
660 # and update old broken items.
661 global $wgDatabase;
662 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
663
664 if( $info->not_null ) {
665 echo "Making wl_notificationtimestamp nullable... ";
666 dbsource( 'maintenance/archives/patch-watchlist-null.sql', $wgDatabase );
667 echo "ok\n";
668 } else {
669 echo "...wl_notificationtimestamp is already nullable.\n";
670 }
671
672 }
673
674 function do_all_updates() {
675 global $wgNewTables, $wgNewFields, $wgRenamedTables;
676
677 # Rename tables
678 foreach ( $wgRenamedTables as $tableRecord ) {
679 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
680 }
681
682 # Add missing tables
683 foreach ( $wgNewTables as $tableRecord ) {
684 add_table( $tableRecord[0], $tableRecord[1] );
685 flush();
686 }
687
688 # Add missing fields
689 foreach ( $wgNewFields as $fieldRecord ) {
690 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
691 flush();
692 }
693
694 # Do schema updates which require special handling
695 do_interwiki_update(); flush();
696 do_index_update(); flush();
697 do_old_links_update(); flush();
698 do_image_name_unique_update(); flush();
699 do_watchlist_update(); flush();
700 do_user_update(); flush();
701 ###### do_copy_newtalk_to_watchlist(); flush();
702 do_logging_encoding(); flush();
703
704 do_schema_restructuring(); flush();
705 do_inverse_timestamp(); flush();
706 do_text_id(); flush();
707 do_namespace_size(); flush();
708
709 do_pagelinks_update(); flush();
710
711 do_drop_img_type(); flush();
712
713 do_user_unique_update(); flush();
714 do_user_groups_update(); flush();
715
716 do_watchlist_null(); flush();
717
718 //do_image_index_update(); flush();
719
720 do_logging_timestamp_index(); flush();
721
722 initialiseMessages(); flush();
723 }
724
725 function archive($name) {
726 global $wgDBtype;
727 switch ($wgDBtype) {
728 case "oracle":
729 return "maintenance/oracle/archives/$name";
730 default:
731 return "maintenance/archives/$name";
732 }
733 }
734 ?>