Use null in wl_notificationtimestamp! The database provides this for a reason.
[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_name_unique_update() {
161 global $wgDatabase;
162 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
163 echo "...image primary key already set.\n";
164 } else {
165 echo "Making img_name the primary key... ";
166 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
167 echo "ok\n";
168 }
169 }
170
171 function do_watchlist_update() {
172 global $wgDatabase;
173 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
174 echo "The watchlist table is already set up for email notification.\n";
175 } else {
176 echo "Adding wl_notificationtimestamp field for email notification management.";
177 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
178 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
179 echo "ok\n";
180 }
181 }
182
183 function do_copy_newtalk_to_watchlist() {
184 global $wgDatabase;
185 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
186
187 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
188 $wgDatabase->tableName( 'user_newtalk' ) );
189 $num_newtalks=$wgDatabase->numRows($res);
190 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
191
192 $user = new User();
193 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
194 $wluser = $wgDatabase->fetchObject( $res );
195 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
196 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
197 $wgDatabase->replace( 'watchlist',
198 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
199 array('wl_user' => 0,
200 'wl_namespace' => NS_USER_TALK,
201 'wl_title' => $wluser->user_ip,
202 'wl_notificationtimestamp' => '19700101000000'
203 ), 'updaters.inc::do_watchlist_update2'
204 );
205 }
206 } else { # normal users ... have user_ids
207 $user->setID($wluser->user_id);
208 $wgDatabase->replace( 'watchlist',
209 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
210 array('wl_user' => $user->getID(),
211 'wl_namespace' => NS_USER_TALK,
212 'wl_title' => $user->getName(),
213 'wl_notificationtimestamp' => '19700101000000'
214 ), 'updaters.inc::do_watchlist_update3'
215 );
216 }
217 }
218 echo "Done.\n";
219 }
220
221
222 function do_user_update() {
223 global $wgDatabase;
224 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
225 echo "User table contains old email authentication field. Dropping... ";
226 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
227 echo "ok\n";
228 } else {
229 echo "...user table does not contain old email authentication field.\n";
230 }
231 }
232
233 /**
234 * 1.4 betas were missing the 'binary' marker from logging.log_title,
235 * which causes a collation mismatch error on joins in MySQL 4.1.
236 */
237 function do_logging_encoding() {
238 global $wgDatabase, $wgDBtype;
239 if ($wgDBtype != 'mysql')
240 return;
241 $logging = $wgDatabase->tableName( 'logging' );
242 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
243 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
244 $wgDatabase->freeResult( $res );
245
246 if( in_array( 'binary', $flags ) ) {
247 echo "Logging table has correct title encoding.\n";
248 } else {
249 echo "Fixing title encoding on logging table... ";
250 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
251 echo "ok\n";
252 }
253 }
254
255 function do_schema_restructuring() {
256 global $wgDatabase;
257 $fname="do_schema_restructuring";
258 if ( $wgDatabase->tableExists( 'page' ) ) {
259 echo "...page table already exists.\n";
260 } else {
261 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
262 echo wfTimestamp();
263 echo "......checking for duplicate entries.\n"; flush();
264
265 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
266
267 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
268 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
269
270 if ( $wgDatabase->numRows( $rows ) > 0 ) {
271 echo wfTimestamp();
272 echo "......<b>Found duplicate entries</b>\n";
273 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
274 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
275 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
276 $duplicate[$row->cur_namespace] = array();
277 }
278 $duplicate[$row->cur_namespace][] = $row->cur_title;
279 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
280 }
281 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
282 $firstCond = true;
283 foreach ( $duplicate as $ns => $titles ) {
284 if ( $firstCond ) {
285 $firstCond = false;
286 } else {
287 $sql .= ' OR ';
288 }
289 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
290 $first = true;
291 foreach ( $titles as $t ) {
292 if ( $first ) {
293 $sql .= $wgDatabase->addQuotes( $t );
294 $first = false;
295 } else {
296 $sql .= ', ' . $wgDatabase->addQuotes( $t );
297 }
298 }
299 $sql .= ") ) \n";
300 }
301 # By sorting descending, the most recent entry will be the first in the list.
302 # All following entries will be deleted by the next while-loop.
303 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
304
305 $rows = $wgDatabase->query( $sql, $fname );
306
307 $prev_title = $prev_namespace = false;
308 $deleteId = array();
309
310 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
311 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
312 $deleteId[] = $row->cur_id;
313 }
314 $prev_title = $row->cur_title;
315 $prev_namespace = $row->cur_namespace;
316 }
317 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
318 $rows = $wgDatabase->query( $sql, $fname );
319 echo wfTimestamp();
320 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
321 }
322
323
324 echo wfTimestamp();
325 echo "......Creating tables.\n";
326 $wgDatabase->query("CREATE TABLE $page (
327 page_id int(8) unsigned NOT NULL auto_increment,
328 page_namespace int NOT NULL,
329 page_title varchar(255) binary NOT NULL,
330 page_restrictions tinyblob NOT NULL default '',
331 page_counter bigint(20) unsigned NOT NULL default '0',
332 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
333 page_is_new tinyint(1) unsigned NOT NULL default '0',
334 page_random real unsigned NOT NULL,
335 page_touched char(14) binary NOT NULL default '',
336 page_latest int(8) unsigned NOT NULL,
337 page_len int(8) unsigned NOT NULL,
338
339 PRIMARY KEY page_id (page_id),
340 UNIQUE INDEX name_title (page_namespace,page_title),
341 INDEX (page_random),
342 INDEX (page_len)
343 ) TYPE=InnoDB", $fname );
344 $wgDatabase->query("CREATE TABLE $revision (
345 rev_id int(8) unsigned NOT NULL auto_increment,
346 rev_page int(8) unsigned NOT NULL,
347 rev_comment tinyblob NOT NULL default '',
348 rev_user int(5) unsigned NOT NULL default '0',
349 rev_user_text varchar(255) binary NOT NULL default '',
350 rev_timestamp char(14) binary NOT NULL default '',
351 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
352 rev_deleted tinyint(1) unsigned NOT NULL default '0',
353
354 PRIMARY KEY rev_page_id (rev_page, rev_id),
355 UNIQUE INDEX rev_id (rev_id),
356 INDEX rev_timestamp (rev_timestamp),
357 INDEX page_timestamp (rev_page,rev_timestamp),
358 INDEX user_timestamp (rev_user,rev_timestamp),
359 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
360 ) TYPE=InnoDB", $fname );
361
362 echo wfTimestamp();
363 echo "......Locking tables.\n";
364 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
365
366 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
367 echo wfTimestamp();
368 echo "......maxold is {$maxold}\n";
369
370 echo wfTimestamp();
371 global $wgLegacySchemaConversion;
372 if( $wgLegacySchemaConversion ) {
373 // Create HistoryBlobCurStub entries.
374 // Text will be pulled from the leftover 'cur' table at runtime.
375 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
376 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
377 $cur_flags = "'object'";
378 } else {
379 // Copy all cur text in immediately: this may take longer but avoids
380 // having to keep an extra table around.
381 echo "......Moving text from cur.\n";
382 $cur_text = 'cur_text';
383 $cur_flags = "''";
384 }
385 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
386 old_timestamp, old_minor_edit, old_flags)
387 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
388 FROM $cur", $fname );
389
390 echo wfTimestamp();
391 echo "......Setting up revision table.\n";
392 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
393 rev_minor_edit)
394 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
395 old_timestamp, old_minor_edit
396 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
397
398 echo wfTimestamp();
399 echo "......Setting up page table.\n";
400 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
401 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
402 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
403 cur_random, cur_touched, rev_id, LENGTH(cur_text)
404 FROM $cur,$revision
405 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
406
407 echo wfTimestamp();
408 echo "......Unlocking tables.\n";
409 $wgDatabase->query( "UNLOCK TABLES", $fname );
410
411 echo wfTimestamp();
412 echo "......Renaming old.\n";
413 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
414
415 echo wfTimestamp();
416 echo "...done.\n";
417 }
418 }
419
420 function do_inverse_timestamp() {
421 global $wgDatabase;
422 $fname="do_schema_restructuring";
423 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
424 echo "Removing revision.inverse_timestamp and fixing indexes... ";
425 dbsource( 'maintenance/archives/patch-inverse_timestamp.sql', $wgDatabase );
426 echo "ok\n";
427 } else {
428 echo "revision timestamp indexes already up to 2005-03-13\n";
429 }
430 }
431
432 function do_text_id() {
433 global $wgDatabase;
434 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
435 echo "...rev_text_id already in place.\n";
436 } else {
437 echo "Adding rev_text_id field... ";
438 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
439 echo "ok\n";
440 }
441 }
442
443 function do_namespace_size() {
444 $tables = array(
445 'page' => 'page',
446 'archive' => 'ar',
447 'recentchanges' => 'rc',
448 'watchlist' => 'wl',
449 'querycache' => 'qc',
450 'logging' => 'log',
451 );
452 foreach( $tables as $table => $prefix ) {
453 do_namespace_size_on( $table, $prefix );
454 flush();
455 }
456 }
457
458 function do_namespace_size_on( $table, $prefix ) {
459 global $wgDatabase, $wgDBtype;
460 if ($wgDBtype != 'mysql')
461 return;
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_watchlist_null() {
633 # Make sure wl_notificationtimestamp can be NULL,
634 # and update old broken items.
635 global $wgDatabase;
636 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
637
638 if( $info->not_null ) {
639 echo "Making wl_notificationtimestamp nullable... ";
640 dbsource( 'maintenance/archives/patch-watchlist-null.sql', $wgDatabase );
641 echo "ok\n";
642 } else {
643 echo "...wl_notificationtimestamp is already nullable.\n";
644 }
645
646 }
647
648 function do_all_updates() {
649 global $wgNewTables, $wgNewFields, $wgRenamedTables;
650
651 # Rename tables
652 foreach ( $wgRenamedTables as $tableRecord ) {
653 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
654 }
655
656 # Add missing tables
657 foreach ( $wgNewTables as $tableRecord ) {
658 add_table( $tableRecord[0], $tableRecord[1] );
659 flush();
660 }
661
662 # Add missing fields
663 foreach ( $wgNewFields as $fieldRecord ) {
664 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
665 flush();
666 }
667
668 # Do schema updates which require special handling
669 do_interwiki_update(); flush();
670 do_index_update(); flush();
671 do_old_links_update(); flush();
672 do_image_name_unique_update(); flush();
673 do_watchlist_update(); flush();
674 do_user_update(); flush();
675 ###### do_copy_newtalk_to_watchlist(); flush();
676 do_logging_encoding(); flush();
677
678 do_schema_restructuring(); flush();
679 do_inverse_timestamp(); flush();
680 do_text_id(); flush();
681 do_namespace_size(); flush();
682
683 do_pagelinks_update(); flush();
684
685 do_drop_img_type(); flush();
686
687 do_user_unique_update(); flush();
688 do_user_groups_update(); flush();
689
690 do_watchlist_null(); flush();
691
692 initialiseMessages(); flush();
693 }
694
695 function archive($name) {
696 global $wgDBtype;
697 switch ($wgDBtype) {
698 case "oracle":
699 return "maintenance/oracle/archives/$name";
700 default:
701 return "maintenance/archives/$name";
702 }
703 }
704 ?>