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