* (bug 9222) PostgreSQL updater should not be version-specific
[lhc/web/wiklou.git] / maintenance / updaters.inc
1 <?php
2 /**
3 * @addtogroup Maintenance
4 */
5
6 /** */
7
8 if ( !defined( 'MEDIAWIKI' ) ) {
9 echo "This file is not a valid entry point\n";
10 exit( 1 );
11 }
12
13 require_once 'convertLinks.inc';
14 require_once 'userDupes.inc';
15 require_once 'deleteDefaultMessages.php';
16
17 $wgRenamedTables = array(
18 # from to patch file
19 # array( 'group', 'groups', 'patch-rename-group.sql' ),
20 );
21
22 $wgNewTables = array(
23 # table patch file (in maintenance/archives)
24 array( 'hitcounter', 'patch-hitcounter.sql' ),
25 array( 'querycache', 'patch-querycache.sql' ),
26 array( 'objectcache', 'patch-objectcache.sql' ),
27 array( 'categorylinks', 'patch-categorylinks.sql' ),
28 array( 'logging', 'patch-logging.sql' ),
29 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
30 array( 'transcache', 'patch-transcache.sql' ),
31 array( 'trackbacks', 'patch-trackbacks.sql' ),
32 array( 'externallinks', 'patch-externallinks.sql' ),
33 array( 'job', 'patch-job.sql' ),
34 array( 'langlinks', 'patch-langlinks.sql' ),
35 array( 'querycache_info', 'patch-querycacheinfo.sql' ),
36 array( 'filearchive', 'patch-filearchive.sql' ),
37 array( 'querycachetwo', 'patch-querycachetwo.sql' ),
38 );
39
40 $wgNewFields = array(
41 # table field patch file (in maintenance/archives)
42 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
43 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
44 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
45 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
46 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
47 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
48 array( 'recentchanges', 'rc_old_len', 'patch-rc_len.sql' ),
49 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
50 array( 'user', 'user_token', 'patch-user_token.sql' ),
51 array( 'user', 'user_email_token', 'patch-user_email_token.sql' ),
52 array( 'user', 'user_registration','patch-user_registration.sql' ),
53 array( 'logging', 'log_params', 'patch-log_params.sql' ),
54 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
55 array( 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
56 array( 'page', 'page_len', 'patch-page_len.sql' ),
57 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
58 array( 'image', 'img_width', 'patch-img_width.sql' ),
59 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
60 array( 'image', 'img_media_type', 'patch-img_media_type.sql' ),
61 array( 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ),
62 array( 'interwiki', 'iw_trans', 'patch-interwiki-trans.sql' ),
63 array( 'ipblocks', 'ipb_range_start', 'patch-ipb_range_start.sql' ),
64 array( 'site_stats', 'ss_images', 'patch-ss_images.sql' ),
65 array( 'ipblocks', 'ipb_anon_only', 'patch-ipb_anon_only.sql' ),
66 array( 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
67 array( 'user', 'user_newpass_time','patch-user_newpass_time.sql' ),
68 array( 'user', 'user_editcount', 'patch-user_editcount.sql' ),
69 array( 'logging', 'log_id', 'patch-log_id.sql' ),
70 array( 'page_restrictions', 'pr_id', 'patch-page_restrictions_sortkey.sql' ),
71 );
72
73 function rename_table( $from, $to, $patch ) {
74 global $wgDatabase;
75 if ( $wgDatabase->tableExists( $from ) ) {
76 if ( $wgDatabase->tableExists( $to ) ) {
77 echo "...can't move table $from to $to, $to already exists.\n";
78 } else {
79 echo "Moving table $from to $to...";
80 dbsource( archive($patch), $wgDatabase );
81 echo "ok\n";
82 }
83 } else {
84 // Source table does not exist
85 // Renames are done before creations, so this is typical for a new installation
86 // Ignore silently
87 }
88 }
89
90 function add_table( $name, $patch ) {
91 global $wgDatabase;
92 if ( $wgDatabase->tableExists( $name ) ) {
93 echo "...$name table already exists.\n";
94 } else {
95 echo "Creating $name table...";
96 dbsource( archive($patch), $wgDatabase );
97 echo "ok\n";
98 }
99 }
100
101 function add_field( $table, $field, $patch ) {
102 global $wgDatabase;
103 if ( !$wgDatabase->tableExists( $table ) ) {
104 echo "...$table table does not exist, skipping new field patch\n";
105 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
106 echo "...have $field field in $table table.\n";
107 } else {
108 echo "Adding $field field to table $table...";
109 dbsource( archive($patch) , $wgDatabase );
110 echo "ok\n";
111 }
112 }
113
114 function do_revision_updates() {
115 global $wgSoftwareRevision;
116 if ( $wgSoftwareRevision < 1001 ) {
117 update_passwords();
118 }
119 }
120
121 function update_passwords() {
122 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
123
124 global $wgDatabase;
125 $fname = "Update script: update_passwords()";
126 print "\nIt appears that you need to update the user passwords in your\n" .
127 "database. If you have already done this (if you've run this update\n" .
128 "script once before, for example), doing so again will make all your\n" .
129 "user accounts inaccessible, so be sure you only do this once.\n" .
130 "Update user passwords? (yes/no)";
131
132 $resp = readconsole();
133 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
134
135 $sql = "SELECT user_id,user_password FROM user";
136 $source = $wgDatabase->query( $sql, $fname );
137
138 while ( $row = $wgDatabase->fetchObject( $source ) ) {
139 $id = $row->user_id;
140 $oldpass = $row->user_password;
141 $newpass = md5( "{$id}-{$oldpass}" );
142
143 $sql = "UPDATE user SET user_password='{$newpass}' " .
144 "WHERE user_id={$id}";
145 $wgDatabase->query( $sql, $fname );
146 }
147 }
148
149 function do_interwiki_update() {
150 # Check that interwiki table exists; if it doesn't source it
151 global $wgDatabase, $IP;
152 if( $wgDatabase->tableExists( "interwiki" ) ) {
153 echo "...already have interwiki table\n";
154 return true;
155 }
156 echo "Creating interwiki table: ";
157 dbsource( archive("patch-interwiki.sql") );
158 echo "ok\n";
159 echo "Adding default interwiki definitions: ";
160 dbsource( "$IP/maintenance/interwiki.sql" );
161 echo "ok\n";
162 }
163
164 function do_index_update() {
165 # Check that proper indexes are in place
166 global $wgDatabase;
167 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
168 if( $meta->multiple_key == 0 ) {
169 echo "Updating indexes to 20031107: ";
170 dbsource( archive("patch-indexes.sql") );
171 echo "ok\n";
172 return true;
173 }
174 echo "...indexes seem up to 20031107 standards\n";
175 return false;
176 }
177
178 function do_image_index_update() {
179 global $wgDatabase;
180
181 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
182 if( $meta->multiple_key == 0 ) {
183 echo "Updating indexes to 20050912: ";
184 dbsource( archive("patch-mimesearch-indexes.sql") );
185 echo "ok\n";
186 return true;
187 }
188 echo "...indexes seem up to 20050912 standards\n";
189 return false;
190 }
191
192 function do_image_name_unique_update() {
193 global $wgDatabase;
194 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
195 echo "...image primary key already set.\n";
196 } else {
197 echo "Making img_name the primary key... ";
198 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
199 echo "ok\n";
200 }
201 }
202
203 function do_logging_timestamp_index() {
204 global $wgDatabase;
205 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
206 echo "...timestamp key on logging already exists.\n";
207 } else {
208 echo "Adding timestamp key on logging table... ";
209 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
210 echo "ok\n";
211 }
212 }
213
214
215 function do_watchlist_update() {
216 global $wgDatabase;
217 $fname = 'do_watchlist_update';
218 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
219 echo "The watchlist table is already set up for email notification.\n";
220 } else {
221 echo "Adding wl_notificationtimestamp field for email notification management.";
222 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
223 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
224 echo "ok\n";
225 }
226 # Check if we need to add talk page rows to the watchlist
227 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
228 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
229 if ( $talk != $nontalk ) {
230 echo "Adding missing watchlist talk page rows... ";
231 flush();
232
233 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
234 array(
235 'wl_user' => 'wl_user',
236 'wl_namespace' => 'wl_namespace | 1',
237 'wl_title' => 'wl_title',
238 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
239 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
240 echo "ok\n";
241 } else {
242 echo "...watchlist talk page rows already present\n";
243 }
244 }
245
246 function do_copy_newtalk_to_watchlist() {
247 global $wgDatabase;
248 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
249
250 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
251 $wgDatabase->tableName( 'user_newtalk' ) );
252 $num_newtalks=$wgDatabase->numRows($res);
253 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
254
255 $user = new User();
256 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
257 $wluser = $wgDatabase->fetchObject( $res );
258 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
259 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
260 $wgDatabase->replace( 'watchlist',
261 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
262 array('wl_user' => 0,
263 'wl_namespace' => NS_USER_TALK,
264 'wl_title' => $wluser->user_ip,
265 'wl_notificationtimestamp' => '19700101000000'
266 ), 'updaters.inc::do_watchlist_update2'
267 );
268 }
269 } else { # normal users ... have user_ids
270 $user->setID($wluser->user_id);
271 $wgDatabase->replace( 'watchlist',
272 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
273 array('wl_user' => $user->getID(),
274 'wl_namespace' => NS_USER_TALK,
275 'wl_title' => $user->getName(),
276 'wl_notificationtimestamp' => '19700101000000'
277 ), 'updaters.inc::do_watchlist_update3'
278 );
279 }
280 }
281 echo "Done.\n";
282 }
283
284
285 function do_user_update() {
286 global $wgDatabase;
287 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
288 echo "User table contains old email authentication field. Dropping... ";
289 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
290 echo "ok\n";
291 } else {
292 echo "...user table does not contain old email authentication field.\n";
293 }
294 }
295
296 /**
297 * 1.4 betas were missing the 'binary' marker from logging.log_title,
298 * which causes a collation mismatch error on joins in MySQL 4.1.
299 */
300 function do_logging_encoding() {
301 global $wgDatabase, $wgDBtype;
302 if ($wgDBtype != 'mysql')
303 return;
304 $logging = $wgDatabase->tableName( 'logging' );
305 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
306 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
307 $wgDatabase->freeResult( $res );
308
309 if( in_array( 'binary', $flags ) ) {
310 echo "Logging table has correct title encoding.\n";
311 } else {
312 echo "Fixing title encoding on logging table... ";
313 dbsource( archive( 'patch-logging-title.sql' ), $wgDatabase );
314 echo "ok\n";
315 }
316 }
317
318 function do_schema_restructuring() {
319 global $wgDatabase;
320 $fname="do_schema_restructuring";
321 if ( $wgDatabase->tableExists( 'page' ) ) {
322 echo "...page table already exists.\n";
323 } else {
324 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
325 echo wfTimestamp( TS_DB );
326 echo "......checking for duplicate entries.\n"; flush();
327
328 list ($cur, $old, $page, $revision, $text) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
329
330 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
331 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
332
333 if ( $wgDatabase->numRows( $rows ) > 0 ) {
334 echo wfTimestamp( TS_DB );
335 echo "......<b>Found duplicate entries</b>\n";
336 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
337 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
338 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
339 $duplicate[$row->cur_namespace] = array();
340 }
341 $duplicate[$row->cur_namespace][] = $row->cur_title;
342 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
343 }
344 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
345 $firstCond = true;
346 foreach ( $duplicate as $ns => $titles ) {
347 if ( $firstCond ) {
348 $firstCond = false;
349 } else {
350 $sql .= ' OR ';
351 }
352 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
353 $first = true;
354 foreach ( $titles as $t ) {
355 if ( $first ) {
356 $sql .= $wgDatabase->addQuotes( $t );
357 $first = false;
358 } else {
359 $sql .= ', ' . $wgDatabase->addQuotes( $t );
360 }
361 }
362 $sql .= ") ) \n";
363 }
364 # By sorting descending, the most recent entry will be the first in the list.
365 # All following entries will be deleted by the next while-loop.
366 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
367
368 $rows = $wgDatabase->query( $sql, $fname );
369
370 $prev_title = $prev_namespace = false;
371 $deleteId = array();
372
373 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
374 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
375 $deleteId[] = $row->cur_id;
376 }
377 $prev_title = $row->cur_title;
378 $prev_namespace = $row->cur_namespace;
379 }
380 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
381 $rows = $wgDatabase->query( $sql, $fname );
382 echo wfTimestamp( TS_DB );
383 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
384 }
385
386
387 echo wfTimestamp( TS_DB );
388 echo "......Creating tables.\n";
389 $wgDatabase->query("CREATE TABLE $page (
390 page_id int(8) unsigned NOT NULL auto_increment,
391 page_namespace int NOT NULL,
392 page_title varchar(255) binary NOT NULL,
393 page_restrictions tinyblob NOT NULL,
394 page_counter bigint(20) unsigned NOT NULL default '0',
395 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
396 page_is_new tinyint(1) unsigned NOT NULL default '0',
397 page_random real unsigned NOT NULL,
398 page_touched char(14) binary NOT NULL default '',
399 page_latest int(8) unsigned NOT NULL,
400 page_len int(8) unsigned NOT NULL,
401
402 PRIMARY KEY page_id (page_id),
403 UNIQUE INDEX name_title (page_namespace,page_title),
404 INDEX (page_random),
405 INDEX (page_len)
406 ) TYPE=InnoDB", $fname );
407 $wgDatabase->query("CREATE TABLE $revision (
408 rev_id int(8) unsigned NOT NULL auto_increment,
409 rev_page int(8) unsigned NOT NULL,
410 rev_comment tinyblob NOT NULL,
411 rev_user int(5) unsigned NOT NULL default '0',
412 rev_user_text varchar(255) binary NOT NULL default '',
413 rev_timestamp char(14) binary NOT NULL default '',
414 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
415 rev_deleted tinyint(1) unsigned NOT NULL default '0',
416
417 PRIMARY KEY rev_page_id (rev_page, rev_id),
418 UNIQUE INDEX rev_id (rev_id),
419 INDEX rev_timestamp (rev_timestamp),
420 INDEX page_timestamp (rev_page,rev_timestamp),
421 INDEX user_timestamp (rev_user,rev_timestamp),
422 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
423 ) TYPE=InnoDB", $fname );
424
425 echo wfTimestamp( TS_DB );
426 echo "......Locking tables.\n";
427 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
428
429 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
430 echo wfTimestamp( TS_DB );
431 echo "......maxold is {$maxold}\n";
432
433 echo wfTimestamp( TS_DB );
434 global $wgLegacySchemaConversion;
435 if( $wgLegacySchemaConversion ) {
436 // Create HistoryBlobCurStub entries.
437 // Text will be pulled from the leftover 'cur' table at runtime.
438 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
439 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
440 $cur_flags = "'object'";
441 } else {
442 // Copy all cur text in immediately: this may take longer but avoids
443 // having to keep an extra table around.
444 echo "......Moving text from cur.\n";
445 $cur_text = 'cur_text';
446 $cur_flags = "''";
447 }
448 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
449 old_timestamp, old_minor_edit, old_flags)
450 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
451 FROM $cur", $fname );
452
453 echo wfTimestamp( TS_DB );
454 echo "......Setting up revision table.\n";
455 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
456 rev_minor_edit)
457 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
458 old_timestamp, old_minor_edit
459 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
460
461 echo wfTimestamp( TS_DB );
462 echo "......Setting up page table.\n";
463 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
464 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
465 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
466 cur_random, cur_touched, rev_id, LENGTH(cur_text)
467 FROM $cur,$revision
468 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
469
470 echo wfTimestamp( TS_DB );
471 echo "......Unlocking tables.\n";
472 $wgDatabase->query( "UNLOCK TABLES", $fname );
473
474 echo wfTimestamp( TS_DB );
475 echo "......Renaming old.\n";
476 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
477
478 echo wfTimestamp( TS_DB );
479 echo "...done.\n";
480 }
481 }
482
483 function do_inverse_timestamp() {
484 global $wgDatabase;
485 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
486 echo "Removing revision.inverse_timestamp and fixing indexes... ";
487 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
488 echo "ok\n";
489 } else {
490 echo "revision timestamp indexes already up to 2005-03-13\n";
491 }
492 }
493
494 function do_text_id() {
495 global $wgDatabase;
496 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
497 echo "...rev_text_id already in place.\n";
498 } else {
499 echo "Adding rev_text_id field... ";
500 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
501 echo "ok\n";
502 }
503 }
504
505 function do_namespace_size() {
506 $tables = array(
507 'page' => 'page',
508 'archive' => 'ar',
509 'recentchanges' => 'rc',
510 'watchlist' => 'wl',
511 'querycache' => 'qc',
512 'logging' => 'log',
513 );
514 foreach( $tables as $table => $prefix ) {
515 do_namespace_size_on( $table, $prefix );
516 flush();
517 }
518 }
519
520 function do_namespace_size_on( $table, $prefix ) {
521 global $wgDatabase, $wgDBtype;
522 if ($wgDBtype != 'mysql')
523 return;
524 $field = $prefix . '_namespace';
525
526 $tablename = $wgDatabase->tableName( $table );
527 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
528 $info = $wgDatabase->fetchObject( $result );
529 $wgDatabase->freeResult( $result );
530
531 if( substr( $info->Type, 0, 3 ) == 'int' ) {
532 echo "...$field is already a full int ($info->Type).\n";
533 } else {
534 echo "Promoting $field from $info->Type to int... ";
535
536 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
537 $wgDatabase->query( $sql );
538
539 echo "ok\n";
540 }
541 }
542
543 function do_pagelinks_update() {
544 global $wgDatabase;
545 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
546 echo "...already have pagelinks table.\n";
547 } else {
548 echo "Converting links and brokenlinks tables to pagelinks... ";
549 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
550 echo "ok\n";
551 flush();
552
553 global $wgCanonicalNamespaceNames;
554 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
555 if( $ns != 0 ) {
556 do_pagelinks_namespace( $ns );
557 }
558 }
559 }
560 }
561
562 function do_pagelinks_namespace( $namespace ) {
563 global $wgDatabase, $wgContLang;
564
565 $ns = intval( $namespace );
566 echo "Cleaning up broken links for namespace $ns... ";
567
568 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
569 $name = $wgContLang->getNsText( $ns );
570 $prefix = $wgDatabase->strencode( $name );
571 $likeprefix = str_replace( '_', '\\_', $prefix);
572
573 $sql = "UPDATE $pagelinks
574 SET pl_namespace=$ns,
575 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
576 WHERE pl_namespace=0
577 AND pl_title LIKE '$likeprefix:%'";
578
579 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
580 echo "ok\n";
581 }
582
583 function do_drop_img_type() {
584 global $wgDatabase;
585
586 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
587 echo "Dropping unused img_type field in image table... ";
588 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
589 echo "ok\n";
590 } else {
591 echo "No img_type field in image table; Good.\n";
592 }
593 }
594
595 function do_old_links_update() {
596 global $wgDatabase;
597 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
598 echo "Already have pagelinks; skipping old links table updates.\n";
599 } else {
600 convertLinks(); flush();
601 }
602 }
603
604 function do_user_unique_update() {
605 global $wgDatabase;
606 $duper = new UserDupes( $wgDatabase );
607 if( $duper->hasUniqueIndex() ) {
608 echo "Already have unique user_name index.\n";
609 } else {
610 if( !$duper->clearDupes() ) {
611 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
612 }
613 echo "Adding unique index on user_name... ";
614 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
615 echo "ok\n";
616 }
617 }
618
619 function do_user_groups_update() {
620 $fname = 'do_user_groups_update';
621 global $wgDatabase;
622
623 if( $wgDatabase->tableExists( 'user_groups' ) ) {
624 echo "...user_groups table already exists.\n";
625 return do_user_groups_reformat();
626 }
627
628 echo "Adding user_groups table... ";
629 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
630 echo "ok\n";
631
632 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
633 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
634 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
635 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
636 echo "ok\n";
637 } else {
638 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
639 echo "*** You may need to manually configure some sysops by manipulating\n";
640 echo "*** the user_groups table.\n";
641 return;
642 }
643 }
644
645 echo "Converting user_rights table to user_groups... ";
646 $result = $wgDatabase->select( 'user_rights',
647 array( 'ur_user', 'ur_rights' ),
648 array( "ur_rights != ''" ),
649 $fname );
650
651 while( $row = $wgDatabase->fetchObject( $result ) ) {
652 $groups = array_unique(
653 array_map( 'trim',
654 explode( ',', $row->ur_rights ) ) );
655
656 foreach( $groups as $group ) {
657 $wgDatabase->insert( 'user_groups',
658 array(
659 'ug_user' => $row->ur_user,
660 'ug_group' => $group ),
661 $fname );
662 }
663 }
664 $wgDatabase->freeResult( $result );
665 echo "ok\n";
666 }
667
668 function do_user_groups_reformat() {
669 # Check for bogus formats from previous 1.5 alpha code.
670 global $wgDatabase;
671 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
672
673 if( $info->type == 'int' ) {
674 $oldug = $wgDatabase->tableName( 'user_groups' );
675 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
676 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
677 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
678 echo "ok\n";
679
680 echo "Re-adding fresh user_groups table... ";
681 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
682 echo "ok\n";
683
684 echo "***\n";
685 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
686 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
687 echo "***\n";
688 } else {
689 echo "...user_groups is in current format.\n";
690 }
691
692 }
693
694 function do_watchlist_null() {
695 # Make sure wl_notificationtimestamp can be NULL,
696 # and update old broken items.
697 global $wgDatabase;
698 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
699
700 if( $info->not_null ) {
701 echo "Making wl_notificationtimestamp nullable... ";
702 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
703 echo "ok\n";
704 } else {
705 echo "...wl_notificationtimestamp is already nullable.\n";
706 }
707
708 }
709
710 /**
711 * @bug 3946
712 */
713 function do_page_random_update() {
714 global $wgDatabase;
715
716 echo "Setting page_random to a random value on rows where it equals 0...";
717
718 $page = $wgDatabase->tableName( 'page' );
719 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
720 $rows = $wgDatabase->affectedRows();
721
722 echo "changed $rows rows\n";
723 }
724
725 function do_templatelinks_update() {
726 global $wgDatabase, $wgLoadBalancer;
727 $fname = 'do_templatelinks_update';
728
729 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
730 echo "...templatelinks table already exists\n";
731 return;
732 }
733 echo "Creating templatelinks table...\n";
734 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
735 echo "Populating...\n";
736 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
737 // Slow, replication-friendly update
738 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
739 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
740 $count = 0;
741 while ( $row = $wgDatabase->fetchObject( $res ) ) {
742 $count = ($count + 1) % 100;
743 if ( $count == 0 ) {
744 if ( function_exists( 'wfWaitForSlaves' ) ) {
745 wfWaitForSlaves( 10 );
746 } else {
747 sleep( 1 );
748 }
749 }
750 $wgDatabase->insert( 'templatelinks',
751 array(
752 'tl_from' => $row->pl_from,
753 'tl_namespace' => $row->pl_namespace,
754 'tl_title' => $row->pl_title,
755 ), $fname
756 );
757
758 }
759 $wgDatabase->freeResult( $res );
760 } else {
761 // Fast update
762 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
763 array(
764 'tl_from' => 'pl_from',
765 'tl_namespace' => 'pl_namespace',
766 'tl_title' => 'pl_title'
767 ), array(
768 'pl_namespace' => 10
769 ), $fname
770 );
771 }
772 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
773 }
774
775 # July 2006
776 # Add ( rc_namespace, rc_user_text ) index [R. Church]
777 function do_rc_indices_update() {
778 global $wgDatabase;
779 echo( "Checking for additional recent changes indices...\n" );
780 # See if we can find the index we want
781 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_ns_usertext', __METHOD__ );
782 if( !$info ) {
783 # None, so create
784 echo( "...index on ( rc_namespace, rc_user_text ) not found; creating\n" );
785 dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
786 } else {
787 # Index seems to exist
788 echo( "...index on ( rc_namespace, rc_user_text ) seems to be ok\n" );
789 }
790
791 #Add (rc_user_text, rc_timestamp) index [A. Garrett], November 2006
792 # See if we can find the index we want
793 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_user_text', __METHOD__ );
794 if( !$info ) {
795 # None, so create
796 echo( "...index on ( rc_user_text, rc_timestamp ) not found; creating\n" );
797 dbsource( archive( 'patch-rc_user_text-index.sql' ) );
798 } else {
799 # Index seems to exist
800 echo( "...index on ( rc_user_text, rc_timestamp ) seems to be ok\n" );
801 }
802 }
803
804 function index_has_field($table, $index, $field) {
805 global $wgDatabase;
806 echo( "Checking if $table index $index includes field $field...\n" );
807 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
808 if( $info ) {
809 foreach($info as $row) {
810 if($row->Column_name == $field) {
811 echo( "...index $index on table $table seems to be ok\n" );
812 return true;
813 }
814 }
815 }
816 echo( "...index $index on table $table has no field $field; adding\n" );
817 return false;
818 }
819
820 function do_backlinking_indices_update() {
821 echo( "Checking for backlinking indices...\n" );
822 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
823 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
824 !index_has_field('imagelinks', 'il_to', 'il_from'))
825 {
826 dbsource( archive( 'patch-backlinkindexes.sql' ) );
827 }
828 }
829
830 function do_stats_init() {
831 // Sometimes site_stats table is not properly populated.
832 global $wgDatabase;
833 echo "Checking site_stats row...";
834 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
835 if( $row === false ) {
836 echo "data is missing! rebuilding...\n";
837
838 global $IP;
839 require_once "$IP/maintenance/initStats.inc";
840 wfInitStats();
841 } else {
842 echo "ok.\n";
843 }
844 }
845
846 function purge_cache() {
847 global $wgDatabase;
848 # We can't guarantee that the user will be able to use TRUNCATE,
849 # but we know that DELETE is available to us
850 echo( "Purging caches..." );
851 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
852 echo( "done.\n" );
853 }
854
855 function do_all_updates( $shared = false, $purge = true ) {
856 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
857
858 $doUser = !$wgSharedDB || $shared;
859
860 if ($wgDBtype === 'postgres') {
861 do_postgres_updates();
862 return;
863 }
864
865 # Rename tables
866 foreach ( $wgRenamedTables as $tableRecord ) {
867 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
868 }
869
870 # Add missing tables
871 foreach ( $wgNewTables as $tableRecord ) {
872 add_table( $tableRecord[0], $tableRecord[1] );
873 flush();
874 }
875
876 # Add missing fields
877 foreach ( $wgNewFields as $fieldRecord ) {
878 if ( $fieldRecord[0] != 'user' || $doUser ) {
879 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
880 }
881 flush();
882 }
883
884 # Do schema updates which require special handling
885 do_interwiki_update(); flush();
886 do_index_update(); flush();
887 do_old_links_update(); flush();
888 do_image_name_unique_update(); flush();
889 do_watchlist_update(); flush();
890 if ( $doUser ) {
891 do_user_update(); flush();
892 }
893 ###### do_copy_newtalk_to_watchlist(); flush();
894 do_logging_encoding(); flush();
895
896 do_schema_restructuring(); flush();
897 do_inverse_timestamp(); flush();
898 do_text_id(); flush();
899 do_namespace_size(); flush();
900
901 do_pagelinks_update(); flush();
902 do_templatelinks_update(); flush(); // after pagelinks
903
904 do_drop_img_type(); flush();
905
906 if ( $doUser ) {
907 do_user_unique_update(); flush();
908 }
909 do_user_groups_update(); flush();
910
911 do_watchlist_null(); flush();
912
913 //do_image_index_update(); flush();
914
915 do_logging_timestamp_index(); flush();
916
917 do_page_random_update(); flush();
918
919 do_rc_indices_update(); flush();
920
921 add_table( 'redirect', 'patch-redirect.sql' );
922
923 do_backlinking_indices_update(); flush();
924
925 do_restrictions_update(); flush ();
926
927 echo "Deleting old default messages (this may take a long time!)..."; flush();
928 deleteDefaultMessages();
929 echo "Done\n"; flush();
930
931 do_stats_init(); flush();
932
933 if( $purge ) {
934 purge_cache();
935 flush();
936 }
937 }
938
939 function archive($name) {
940 global $wgDBtype, $IP;
941 switch ($wgDBtype) {
942 case "postgres":
943 return "$IP/maintenance/postgres/archives/$name";
944 default:
945 return "$IP/maintenance/archives/$name";
946 }
947 }
948
949 function do_restrictions_update() {
950 # Adding page_restrictions table, obsoleting page.page_restrictions.
951 # Migrating old restrictions to new table
952 # -- Andrew Garrett, January 2007.
953
954 global $wgDatabase;
955
956 $name = 'page_restrictions';
957 $patch = 'patch-page_restrictions.sql';
958
959 if ( $wgDatabase->tableExists( $name ) ) {
960 echo "...$name table already exists.\n";
961 } else {
962 echo "Creating $name table...";
963 dbsource( archive($patch), $wgDatabase );
964 echo "ok\n";
965
966 echo "Migrating old restrictions to new table...";
967
968 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array("page_restrictions!=''", "page_restrictions!='edit=:move='"), __METHOD__ );
969
970 $count = 0;
971
972 while ($row = $wgDatabase->fetchObject($res) ) {
973 $count = ($count + 1) % 100;
974
975 if ($count == 0) {
976 if ( function_exists( 'wfWaitForSlaves' ) ) {
977 wfWaitForSlaves( 10 );
978 } else {
979 sleep( 1 );
980 }
981 }
982
983 # Figure out what the restrictions are..
984 $id = $row->page_id;
985 $flatrestrictions = explode( ':', $row->page_restrictions );
986
987 $restrictions = array ();
988 foreach( $flatrestrictions as $restriction ) {
989 $thisrestriction = explode( '=', $restriction, 2 );
990 if( count( $thisrestriction ) == 1 ) {
991 // Compatibility with old protections from before
992 // separate move protection was added.
993 list( $level ) = $thisrestriction;
994 if( $level ) {
995 $restrictions['edit'] = $level;
996 $restrictions['move'] = $level;
997 }
998 } else {
999 list( $type, $level ) = $thisrestriction;
1000 if( $level ) {
1001 $restrictions[$type] = $level;
1002 }
1003 }
1004
1005 $wgDatabase->update( 'page', array ( 'page_restrictions' => ''), array( 'page_id' => $id ), __METHOD__ );
1006
1007 }
1008
1009 foreach( $restrictions as $type => $level ) {
1010 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
1011 'pr_type' => $type,
1012 'pr_level' => $level,
1013 'pr_cascade' => 0 ),
1014 __METHOD__ );
1015 }
1016 }
1017 print "ok\n";
1018 }
1019
1020 }
1021
1022 function
1023 pg_column_has_type($table, $column, $wanttype)
1024 {
1025 global $wgDatabase, $wgDBname, $wgDBmwschema;
1026
1027 $q = <<<END
1028 SELECT typname FROM pg_class, pg_namespace, pg_attribute, pg_type
1029 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1030 AND attrelid=pg_class.oid AND atttypid=pg_type.oid
1031 AND nspname=%s AND relname=%s AND attname=%s;
1032 END;
1033 $res = $wgDatabase->query(sprintf($q,
1034 $wgDatabase->addQuotes($wgDBmwschema),
1035 $wgDatabase->addQuotes($table),
1036 $wgDatabase->addQuotes($column)));
1037 $row = $wgDatabase->fetchRow($res);
1038 $istype = false;
1039 if ($row)
1040 $istype = $row[0] === $wanttype;
1041 $wgDatabase->freeResult($res);
1042 return $istype;
1043 }
1044
1045 function
1046 pg_column_exists($table, $column)
1047 {
1048 global $wgDatabase, $wgDBname, $wgDBmwschema;
1049
1050 $q = <<<END
1051 SELECT 1 FROM pg_class, pg_namespace, pg_attribute
1052 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1053 AND attrelid=pg_class.oid
1054 AND nspname=%s AND relname=%s AND attname=%s;
1055 END;
1056 $res = $wgDatabase->query(sprintf($q,
1057 $wgDatabase->addQuotes($wgDBmwschema),
1058 $wgDatabase->addQuotes($table),
1059 $wgDatabase->addQuotes($column)));
1060 $row = $wgDatabase->fetchRow($res);
1061 $exists = !!$row;
1062 $wgDatabase->freeResult($res);
1063 return $exists;
1064 }
1065
1066 function
1067 pg_table_exists($table)
1068 {
1069 global $wgDatabase, $wgDBname, $wgDBmwschema;
1070
1071 $q = <<<END
1072 SELECT 1 FROM pg_class, pg_namespace
1073 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1074 AND nspname=%s AND relname=%s
1075 END;
1076 $res = $wgDatabase->query(sprintf($q,
1077 $wgDatabase->addQuotes($wgDBmwschema),
1078 $wgDatabase->addQuotes($table)));
1079 $row = $wgDatabase->fetchRow($res);
1080 $exists = !!$row;
1081 $wgDatabase->freeResult($res);
1082 return $exists;
1083 }
1084
1085 function
1086 pg_trigger_exists($table, $trigger)
1087 {
1088 global $wgDatabase, $wgDBname, $wgDBmwschema;
1089
1090 $q = <<<END
1091 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1092 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1093 AND tgrelid=pg_class.oid
1094 AND nspname=%s AND relname=%s AND tgname=%s
1095 END;
1096 $res = $wgDatabase->query(sprintf($q,
1097 $wgDatabase->addQuotes($wgDBmwschema),
1098 $wgDatabase->addQuotes($table),
1099 $wgDatabase->addQuotes($trigger)));
1100 $row = $wgDatabase->fetchRow($res);
1101 $exists = !!$row;
1102 $wgDatabase->freeResult($res);
1103 return $exists;
1104 }
1105
1106
1107 function
1108 pg_index_exists($table, $index)
1109 {
1110 global $wgDatabase, $wgDBmwschema;
1111 $exists = $wgDatabase->selectField("pg_indexes", "indexname",
1112 array( "indexname" => $index,
1113 "tablename" => $table,
1114 "schemaname" => $wgDBmwschema));
1115 return $exists === $index;
1116 }
1117
1118 function
1119 pg_rule_exists($table, $rule)
1120 {
1121 global $wgDatabase, $wgDBmwschema;
1122 $exists = $wgDatabase->selectField("pg_rules", "rulename",
1123 array( "rulename" => $rule,
1124 "tablename" => $table,
1125 "schemaname" => $wgDBmwschema));
1126 return $exists === $rule;
1127 }
1128
1129 function do_postgres_updates() {
1130 global $wgDatabase, $wgVersion, $wgDBmwschema;
1131
1132 # Just in case their LocalSetings.php does not have this:
1133 if ( !isset( $wgDBmwschema ))
1134 $wgDBmwschema = 'mediawiki';
1135
1136 ## Default to the oldest supported version
1137 $version = 1.7;
1138
1139 $thisver = array();
1140 if ($wgDatabase->tableExists("mediawiki_version")) {
1141 $version = "1.8";
1142 $sql = "SELECT mw_version FROM mediawiki_version ORDER BY cdate DESC LIMIT 1";
1143 $tempversion = pg_fetch_result($wgDatabase->doQuery($sql),0,0);
1144 if (preg_match('/(\d+\.\d+)/', $tempversion, $thisver)) {
1145 $version = $thisver[1];
1146 }
1147 }
1148
1149 print " Detected version: $version\n";
1150
1151 ## Transform to a standard format
1152 if (! preg_match('/(\d+)\.(\d+)/', $version, $thisver)) {
1153 die ("Sorry, could not figure out what version \"$version\" means");
1154 }
1155 $oldversion = $version;
1156 $version = sprintf("%d%03d",$thisver[1],$thisver[2]);
1157 print " Standardized version: $version\n";
1158
1159 $upgrade = '';
1160
1161 $typechanges = array(
1162 array("oldimage", "oi_size", "integer"),
1163 array("oldimage", "oi_width", "integer"),
1164 array("oldimage", "oi_height", "integer"),
1165 array("image", "img_size", "integer"),
1166 array("image", "img_width", "integer"),
1167 array("image", "img_height", "integer"),
1168 );
1169
1170 $newcols = array(
1171 array("mwuser", "user_newpass_time", "TIMESTAMPTZ"),
1172 array("mwuser", "user_editcount", "INTEGER"),
1173 array("ipblocks", "ipb_anon_only", "CHAR NOT NULL DEFAULT '0'"),
1174 array("ipblocks", "ipb_create_account", "CHAR NOT NULL DEFAULT '1'"),
1175 array("ipblocks", "ipb_enable_autoblock", "CHAR NOT NULL DEFAULT '1'"),
1176 array("recentchanges", "rc_old_len", "INT"),
1177 array("recentchanges", "rc_new_len", "INT")
1178 );
1179
1180 $newtables = array(
1181 array("mwuser", "patch-mwuser.sql"),
1182 array("pagecontent", "patch-pagecontent.sql"),
1183 array("querycachetwo", "patch-querycachetwo.sql"),
1184 array("redirect", "patch-redirect.sql"),
1185 array("page_restrictions", "patch-page_restrictions.sql"),
1186 array("profiling", "patch-profiling.sql"),
1187 array("mediawiki_version", "patch-mediawiki_version.sql"),
1188 array("archive2", "patch-archive2.sql")
1189 );
1190
1191 $newindexes = array(
1192 array("revision", "rev_text_id_idx", "patch-rev_text_id_idx.sql")
1193 );
1194
1195 $newrules = array(
1196 array("archive", "archive_delete", "patch-archive_delete.sql")
1197 );
1198
1199 foreach ($newtables as $nt) {
1200 if (pg_table_exists($nt[0])) {
1201 echo "... table $nt[0] already exists\n";
1202 continue;
1203 }
1204
1205 echo "... create table $nt[0]\n";
1206 dbsource(archive($nt[1]));
1207 }
1208
1209 foreach ($newcols as $nc) {
1210 if (pg_column_exists($nc[0], $nc[1])) {
1211 echo "... column $nc[0].$nc[1] already exists\n";
1212 continue;
1213 }
1214
1215 echo "... add column $nc[0].$nc[1]\n";
1216 $wgDatabase->query("ALTER TABLE $nc[0] ADD $nc[1] $nc[2]");
1217 }
1218
1219 foreach ($typechanges as $tc) {
1220 if (!pg_column_exists($tc[0], $tc[1])) {
1221 echo "... error: expected column $tc[0].$tc[1] to exist\n";
1222 exit(1);
1223 }
1224
1225 if (pg_column_has_type($tc[0], $tc[1], $tc[2]))
1226 echo "... $tc[0].$tc[1] is already $tc[2]\n";
1227 else {
1228 echo "... change $tc[0].$tc[1] to $tc[2]\n";
1229 $wgDatabase->query("ALTER TABLE $tc[0] ALTER $tc[1] TYPE $tc[2];\nCOMMIT;\n");
1230 }
1231 }
1232
1233 if (!pg_column_has_type("user_newtalk", "user_ip", "text")) {
1234 echo "... convert user_newtalk.user_ip to text\n";
1235 $wgDatabase->query("ALTER TABLE user_newtalk ALTER user_ip TYPE TEXT USING host(user_ip)");
1236 }
1237
1238 foreach ($newindexes as $ni) {
1239 if (pg_index_exists($ni[0], $ni[1])) {
1240 echo "... index $ni[1] on $ni[0] already exists\n";
1241 continue;
1242 }
1243 dbsource(archive($ni[2]));
1244 }
1245
1246 foreach ($newrules as $nr) {
1247 if (pg_rule_exists($nr[0], $nr[1])) {
1248 echo "... rule $nr[1] on $nr[0] already exists\n";
1249 continue;
1250 }
1251 dbsource(archive($nr[2]));
1252 }
1253
1254 if (!pg_trigger_exists("page", "page_deleted")) {
1255 echo "... create page_deleted trigger\n";
1256 dbsource(archive('patch-page_deleted.sql'));
1257 }
1258
1259 ## 1.8 Updater
1260 if ($version < 1008) {
1261 $upgrade .= <<<PGEND
1262
1263 -- Constraint tweaking:
1264 ALTER TABLE recentchanges ALTER rc_cur_id DROP NOT NULL;
1265
1266 -- Index order rearrangements:
1267 DROP INDEX pagelink_unique;
1268 CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title);
1269
1270 -- Note this upgrade
1271 INSERT INTO mediawiki_version (type,mw_version,notes)
1272 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.8 version THISVERSION aka SVERSION');
1273
1274 PGEND;
1275
1276 } ## end version 1.8
1277
1278 ## 1.9 Updater
1279 if ($version < 1009) {
1280 $upgrade = <<<PGEND
1281
1282 -- Tighten up restrictions on the revision table so we don't lose data:
1283 ALTER TABLE revision DROP CONSTRAINT revision_rev_user_fkey;
1284 ALTER TABLE revision ADD CONSTRAINT revision_rev_user_fkey
1285 FOREIGN KEY (rev_user) REFERENCES mwuser(user_id) ON DELETE RESTRICT;
1286
1287 -- Despite it's name, ipb_address does not necessarily contain IP addresses :)
1288 ALTER TABLE ipblocks ALTER ipb_address TYPE TEXT USING ipb_address::TEXT;
1289
1290 -- Note this upgrade
1291 INSERT INTO mediawiki_version (type,mw_version,notes)
1292 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.9 version THISVERSION aka SVERSION');
1293
1294 PGEND;
1295
1296 } ## end version 1.9
1297
1298 ## 1.10 updater
1299 if ($version <= 1010) {
1300 $upgrade = <<<PGEND
1301
1302 -- Fix the archive rule ar_timestamp field
1303 CREATE OR REPLACE RULE archive_insert AS ON INSERT TO archive
1304 DO INSTEAD INSERT INTO archive2 VALUES (
1305 NEW.ar_namespace, NEW.ar_title, NEW.ar_text, NEW.ar_comment, NEW.ar_user, NEW.ar_user_text,
1306 TO_TIMESTAMP(NEW.ar_timestamp, 'YYYYMMDDHH24MISS'),
1307 NEW.ar_minor_edit, NEW.ar_flags, NEW.ar_rev_id, NEW.ar_text_id
1308 );
1309
1310 -- Note this upgrade
1311 INSERT INTO mediawiki_version (type,mw_version,notes)
1312 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.10 version THISVERSION aka SVERSION');
1313
1314
1315 PGEND;
1316 } ## end version 1.10
1317
1318 if ( !strlen($upgrade)) {
1319 print "No updates needed for this version ($oldversion)\n";
1320 return;
1321 }
1322
1323 $upgrade = str_replace( 'MWVERSION', $wgVersion, $upgrade );
1324 $upgrade = str_replace( 'THISVERSION', $oldversion, $upgrade );
1325 $upgrade = str_replace( 'SVERSION', $version, $upgrade );
1326 $wgDatabase->query("BEGIN;\n\n $upgrade\n\nCOMMIT;\n");
1327
1328 return;
1329 }
1330
1331 ?>