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