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