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