e941f657923e28551ff34ce85b0b646abb79d4aa
[lhc/web/wiklou.git] / maintenance / updaters.inc
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 */
6
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 echo "This file is not a valid entry point\n";
9 exit( 1 );
10 }
11
12 require_once 'convertLinks.inc';
13 require_once 'userDupes.inc';
14 # Extension updates
15 require_once( "$IP/includes/Hooks.php" );
16
17 /**
18 * @deprecated. Do not use, ever.
19 */
20 $wgUpdates = array();
21
22
23 # For extensions only, should be populated via hooks
24 # $wgDBtype should be checked to specifiy the proper file
25 $wgExtNewTables = array(); // table, dir
26 $wgExtNewFields = array(); // table, column, dir
27 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
28 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
29 $wgExtNewIndexes = array(); // table, index, dir
30 $wgExtModifiedFields = array(); // table, index, dir
31
32 # Helper function: check if the given key is present in the updatelog table.
33 # Obviously, only use this for updates that occur after the updatelog table was
34 # created!
35 function update_row_exists( $key ) {
36 global $wgDatabase;
37 $row = $wgDatabase->selectRow(
38 'updatelog',
39 '1',
40 array( 'ul_key' => $key ),
41 __FUNCTION__
42 );
43 return (bool)$row;
44 }
45
46 function modify_field( $table, $field, $patch, $fullpath = false ) {
47 global $wgDatabase;
48 if ( !$wgDatabase->tableExists( $table ) ) {
49 wfOut( "...$table table does not exist, skipping modify field patch\n" );
50 } elseif ( ! $wgDatabase->fieldExists( $table, $field ) ) {
51 wfOut( "...$field field does not exist in $table table, skipping modify field patch\n" );
52 } else {
53 wfOut( "Modifying $field field of table $table..." );
54 if ( $fullpath ) {
55 $wgDatabase->sourceFile( $patch );
56 } else {
57 $wgDatabase->sourceFile( archive( $patch ) );
58 }
59 wfOut( "ok\n" );
60 }
61 }
62
63 function add_field( $table, $field, $patch, $fullpath = false ) {
64 global $wgDatabase;
65 if ( !$wgDatabase->tableExists( $table ) ) {
66 wfOut( "...$table table does not exist, skipping new field patch\n" );
67 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
68 wfOut( "...have $field field in $table table.\n" );
69 } else {
70 wfOut( "Adding $field field to table $table..." );
71 if ( $fullpath ) {
72 $wgDatabase->sourceFile( $patch );
73 } else {
74 $wgDatabase->sourceFile( archive( $patch ) );
75 }
76 wfOut( "ok\n" );
77 }
78 }
79
80 function add_index( $table, $index, $patch, $fullpath = false ) {
81 global $wgDatabase;
82 if ( $wgDatabase->indexExists( $table, $index ) ) {
83 wfOut( "...$index key already set on $table table.\n" );
84 } else {
85 wfOut( "Adding $index key to table $table... " );
86 if ( $fullpath ) {
87 $wgDatabase->sourceFile( $patch );
88 } else {
89 $wgDatabase->sourceFile( archive( $patch ) );
90 }
91 wfOut( "ok\n" );
92 }
93 }
94
95 function drop_index_if_exists( $table, $index, $patch, $fullpath = false ) {
96 global $wgDatabase;
97 if ( $wgDatabase->indexExists( $table, $index ) ) {
98 wfOut( "Dropping $index from table $table... " );
99 if ( $fullpath ) {
100 $wgDatabase->sourceFile( $patch );
101 } else {
102 $wgDatabase->sourceFile( archive( $patch ) );
103 }
104 wfOut( "ok\n" );
105 } else {
106 wfOut( "...$index doesn't exist.\n" );
107 }
108 }
109
110 function do_interwiki_update() {
111 # Check that interwiki table exists; if it doesn't source it
112 global $wgDatabase, $IP;
113 if ( $wgDatabase->tableExists( "interwiki" ) ) {
114 wfOut( "...already have interwiki table\n" );
115 return true;
116 }
117 wfOut( "Creating interwiki table: " );
118 $wgDatabase->sourceFile( archive( "patch-interwiki.sql" ) );
119 wfOut( "ok\n" );
120 wfOut( "Adding default interwiki definitions: " );
121 $wgDatabase->sourceFile( "$IP/maintenance/interwiki.sql" );
122 wfOut( "ok\n" );
123 }
124
125 function do_index_update() {
126 # Check that proper indexes are in place
127 global $wgDatabase;
128 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
129 if ( !$meta->isMultipleKey() ) {
130 wfOut( "Updating indexes to 20031107: " );
131 $wgDatabase->sourceFile( archive( "patch-indexes.sql" ) );
132 wfOut( "ok\n" );
133 return true;
134 }
135 wfOut( "...indexes seem up to 20031107 standards\n" );
136 return false;
137 }
138
139 function do_image_index_update() {
140 global $wgDatabase;
141
142 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
143 if ( !$meta->isMultipleKey() ) {
144 wfOut( "Updating indexes to 20050912: " );
145 $wgDatabase->sourceFile( archive( "patch-mimesearch-indexes.sql" ) );
146 wfOut( "ok\n" );
147 return true;
148 }
149 wfOut( "...indexes seem up to 20050912 standards\n" );
150 return false;
151 }
152
153 function do_image_name_unique_update() {
154 global $wgDatabase;
155 if ( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
156 wfOut( "...image primary key already set.\n" );
157 } else {
158 wfOut( "Making img_name the primary key... " );
159 $wgDatabase->sourceFile( archive( "patch-image_name_primary.sql" ) );
160 wfOut( "ok\n" );
161 }
162 }
163
164 function do_logging_timestamp_index() {
165 global $wgDatabase;
166 if ( $wgDatabase->indexExists( 'logging', 'times' ) ) {
167 wfOut( "...timestamp key on logging already exists.\n" );
168 } else {
169 wfOut( "Adding timestamp key on logging table... " );
170 $wgDatabase->sourceFile( archive( "patch-logging-times-index.sql" ) );
171 wfOut( "ok\n" );
172 }
173 }
174
175 function do_archive_user_index() {
176 global $wgDatabase;
177 if ( $wgDatabase->indexExists( 'archive', 'usertext_timestamp' ) ) {
178 wfOut( "...usertext,timestamp key on archive already exists.\n" );
179 } else {
180 wfOut( "Adding usertext,timestamp key on archive table... " );
181 $wgDatabase->sourceFile( archive( "patch-archive-user-index.sql" ) );
182 wfOut( "ok\n" );
183 }
184 }
185
186 function do_image_user_index() {
187 global $wgDatabase;
188 if ( $wgDatabase->indexExists( 'image', 'img_usertext_timestamp' ) ) {
189 wfOut( "...usertext,timestamp key on image already exists.\n" );
190 } else {
191 wfOut( "Adding usertext,timestamp key on image table... " );
192 $wgDatabase->sourceFile( archive( "patch-image-user-index.sql" ) );
193 wfOut( "ok\n" );
194 }
195 }
196
197 function do_oldimage_user_index() {
198 global $wgDatabase;
199 if ( $wgDatabase->indexExists( 'oldimage', 'oi_usertext_timestamp' ) ) {
200 wfOut( "...usertext,timestamp key on oldimage already exists.\n" );
201 } else {
202 wfOut( "Adding usertext,timestamp key on oldimage table... " );
203 $wgDatabase->sourceFile( archive( "patch-oldimage-user-index.sql" ) );
204 wfOut( "ok\n" );
205 }
206 }
207
208 function do_watchlist_update() {
209 global $wgDatabase;
210 $fname = 'do_watchlist_update';
211 if ( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
212 wfOut( "...the watchlist table is already set up for email notification.\n" );
213 } else {
214 wfOut( "Adding wl_notificationtimestamp field for email notification management." );
215 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
216 $wgDatabase->sourceFile( archive( 'patch-email-notification.sql' ) );
217 wfOut( "ok\n" );
218 }
219 # Check if we need to add talk page rows to the watchlist
220 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
221 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
222 if ( $talk != $nontalk ) {
223 wfOut( "Adding missing watchlist talk page rows... " );
224 flush();
225
226 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
227 array(
228 'wl_user' => 'wl_user',
229 'wl_namespace' => 'wl_namespace | 1',
230 'wl_title' => 'wl_title',
231 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
232 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
233 wfOut( "ok\n" );
234 } else {
235 wfOut( "...watchlist talk page rows already present\n" );
236 }
237 }
238
239 function do_copy_newtalk_to_watchlist() {
240 global $wgDatabase;
241
242 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
243 $wgDatabase->tableName( 'user_newtalk' ) );
244 $num_newtalks = $wgDatabase->numRows( $res );
245 wfOut( "Now converting $num_newtalks user_newtalk entries to watchlist table entries ... \n" );
246
247 $user = new User();
248 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
249 $wluser = $wgDatabase->fetchObject( $res );
250 if ( $wluser->user_id == 0 ) { # anonymous users ... have IP numbers as "names"
251 if ( $user->isIP( $wluser->user_ip ) ) { # do only if it really looks like an IP number (double checked)
252 $wgDatabase->replace( 'watchlist',
253 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
254 array( 'wl_user' => 0,
255 'wl_namespace' => NS_USER_TALK,
256 'wl_title' => $wluser->user_ip,
257 'wl_notificationtimestamp' => '19700101000000'
258 ), 'updaters.inc::do_watchlist_update2'
259 );
260 }
261 } else { # normal users ... have user_ids
262 $user->setID( $wluser->user_id );
263 $wgDatabase->replace( 'watchlist',
264 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
265 array( 'wl_user' => $user->getID(),
266 'wl_namespace' => NS_USER_TALK,
267 'wl_title' => $user->getName(),
268 'wl_notificationtimestamp' => '19700101000000'
269 ), 'updaters.inc::do_watchlist_update3'
270 );
271 }
272 }
273 wfOut( "Done.\n" );
274 }
275
276 function do_user_update() {
277 global $wgDatabase;
278 if ( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
279 wfOut( "User table contains old email authentication field. Dropping... " );
280 $wgDatabase->sourceFile( archive( 'patch-email-authentication.sql' ) );
281 wfOut( "ok\n" );
282 } else {
283 wfOut( "...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 check_bin( $table, $field, $patchFile ) {
292 global $wgDatabase, $wgDBtype;
293 if ( $wgDBtype != 'mysql' )
294 return;
295 $tableName = $wgDatabase->tableName( $table );
296 $res = $wgDatabase->query( "SELECT $field FROM $tableName LIMIT 0" );
297 $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
298
299 if ( in_array( 'binary', $flags ) ) {
300 wfOut( "...$table table has correct $field encoding.\n" );
301 } else {
302 wfOut( "Fixing $field encoding on $table table... " );
303 $wgDatabase->sourceFile( archive( $patchFile ) );
304 wfOut( "ok\n" );
305 }
306 }
307
308 function do_schema_restructuring() {
309 global $wgDatabase;
310 $fname = "do_schema_restructuring";
311 if ( $wgDatabase->tableExists( 'page' ) ) {
312 wfOut( "...page table already exists.\n" );
313 } else {
314 wfOut( "...converting from cur/old to page/revision/text DB structure.\n" );
315 wfOut( wfTimestamp( TS_DB ) );
316 wfOut( "......checking for duplicate entries.\n" );
317
318 list ( $cur, $old, $page, $revision, $text ) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
319
320 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
321 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
322
323 if ( $wgDatabase->numRows( $rows ) > 0 ) {
324 wfOut( wfTimestamp( TS_DB ) );
325 wfOut( "......<b>Found duplicate entries</b>\n" );
326 wfOut( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
327 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
328 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
329 $duplicate[$row->cur_namespace] = array();
330 }
331 $duplicate[$row->cur_namespace][] = $row->cur_title;
332 wfOut( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
333 }
334 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
335 $firstCond = true;
336 foreach ( $duplicate as $ns => $titles ) {
337 if ( $firstCond ) {
338 $firstCond = false;
339 } else {
340 $sql .= ' OR ';
341 }
342 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
343 $first = true;
344 foreach ( $titles as $t ) {
345 if ( $first ) {
346 $sql .= $wgDatabase->addQuotes( $t );
347 $first = false;
348 } else {
349 $sql .= ', ' . $wgDatabase->addQuotes( $t );
350 }
351 }
352 $sql .= ") ) \n";
353 }
354 # By sorting descending, the most recent entry will be the first in the list.
355 # All following entries will be deleted by the next while-loop.
356 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
357
358 $rows = $wgDatabase->query( $sql, $fname );
359
360 $prev_title = $prev_namespace = false;
361 $deleteId = array();
362
363 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
364 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
365 $deleteId[] = $row->cur_id;
366 }
367 $prev_title = $row->cur_title;
368 $prev_namespace = $row->cur_namespace;
369 }
370 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
371 $rows = $wgDatabase->query( $sql, $fname );
372 wfOut( wfTimestamp( TS_DB ) );
373 wfOut( "......<b>Deleted</b> " . $wgDatabase->affectedRows() . " records.\n" );
374 }
375
376
377 wfOut( wfTimestamp( TS_DB ) );
378 wfOut( "......Creating tables.\n" );
379 $wgDatabase->query( "CREATE TABLE $page (
380 page_id int(8) unsigned NOT NULL auto_increment,
381 page_namespace int NOT NULL,
382 page_title varchar(255) binary NOT NULL,
383 page_restrictions tinyblob NOT NULL,
384 page_counter bigint(20) unsigned NOT NULL default '0',
385 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
386 page_is_new tinyint(1) unsigned NOT NULL default '0',
387 page_random real unsigned NOT NULL,
388 page_touched char(14) binary NOT NULL default '',
389 page_latest int(8) unsigned NOT NULL,
390 page_len int(8) unsigned NOT NULL,
391
392 PRIMARY KEY page_id (page_id),
393 UNIQUE INDEX name_title (page_namespace,page_title),
394 INDEX (page_random),
395 INDEX (page_len)
396 ) ENGINE=InnoDB", $fname );
397 $wgDatabase->query( "CREATE TABLE $revision (
398 rev_id int(8) unsigned NOT NULL auto_increment,
399 rev_page int(8) unsigned NOT NULL,
400 rev_comment tinyblob NOT NULL,
401 rev_user int(5) unsigned NOT NULL default '0',
402 rev_user_text varchar(255) binary NOT NULL default '',
403 rev_timestamp char(14) binary NOT NULL default '',
404 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
405 rev_deleted tinyint(1) unsigned NOT NULL default '0',
406 rev_len int(8) unsigned,
407 rev_parent_id int(8) unsigned default NULL,
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 ) ENGINE=InnoDB", $fname );
415
416 wfOut( wfTimestamp( TS_DB ) );
417 wfOut( "......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 wfOut( wfTimestamp( TS_DB ) );
422 wfOut( "......maxold is {$maxold}\n" );
423
424 wfOut( wfTimestamp( TS_DB ) );
425 global $wgLegacySchemaConversion;
426 if ( $wgLegacySchemaConversion ) {
427 // Create HistoryBlobCurStub entries.
428 // Text will be pulled from the leftover 'cur' table at runtime.
429 wfOut( "......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 wfOut( "......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 wfOut( wfTimestamp( TS_DB ) );
445 wfOut( "......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 wfOut( wfTimestamp( TS_DB ) );
453 wfOut( "......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 wfOut( wfTimestamp( TS_DB ) );
462 wfOut( "......Unlocking tables.\n" );
463 $wgDatabase->query( "UNLOCK TABLES", $fname );
464
465 wfOut( wfTimestamp( TS_DB ) );
466 wfOut( "......Renaming old.\n" );
467 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
468
469 wfOut( wfTimestamp( TS_DB ) );
470 wfOut( "...done.\n" );
471 }
472 }
473
474 function do_inverse_timestamp() {
475 global $wgDatabase;
476 if ( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
477 wfOut( "Removing revision.inverse_timestamp and fixing indexes... " );
478 $wgDatabase->sourceFile( archive( 'patch-inverse_timestamp.sql' ) );
479 wfOut( "ok\n" );
480 } else {
481 wfOut( "...revision timestamp indexes already up to 2005-03-13\n" );
482 }
483 }
484
485 function do_text_id() {
486 global $wgDatabase;
487 if ( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
488 wfOut( "...rev_text_id already in place.\n" );
489 } else {
490 wfOut( "Adding rev_text_id field... " );
491 $wgDatabase->sourceFile( archive( 'patch-rev_text_id.sql' ) );
492 wfOut( "ok\n" );
493 }
494 }
495
496 function do_pagelinks_update() {
497 global $wgDatabase;
498 if ( $wgDatabase->tableExists( 'pagelinks' ) ) {
499 wfOut( "...already have pagelinks table.\n" );
500 } else {
501 wfOut( "Converting links and brokenlinks tables to pagelinks... " );
502 $wgDatabase->sourceFile( archive( 'patch-pagelinks.sql' ) );
503 wfOut( "ok\n" );
504 flush();
505
506 global $wgCanonicalNamespaceNames;
507 foreach ( $wgCanonicalNamespaceNames as $ns => $name ) {
508 if ( $ns != 0 ) {
509 do_pagelinks_namespace( $ns );
510 }
511 }
512 }
513 }
514
515 function do_pagelinks_namespace( $namespace ) {
516 global $wgDatabase, $wgContLang;
517
518 $ns = intval( $namespace );
519 wfOut( "Cleaning up broken links for namespace $ns... " );
520
521 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
522 $name = $wgContLang->getNsText( $ns );
523 $prefix = $wgDatabase->strencode( $name );
524 $likeprefix = str_replace( '_', '\\_', $prefix );
525
526 $sql = "UPDATE $pagelinks
527 SET pl_namespace=$ns,
528 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
529 WHERE pl_namespace=0
530 AND pl_title LIKE '$likeprefix:%'";
531
532 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
533 wfOut( "ok\n" );
534 }
535
536 function do_drop_img_type() {
537 global $wgDatabase;
538
539 if ( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
540 wfOut( "Dropping unused img_type field in image table... " );
541 $wgDatabase->sourceFile( archive( 'patch-drop_img_type.sql' ) );
542 wfOut( "ok\n" );
543 } else {
544 wfOut( "...no img_type field in image table; Good.\n" );
545 }
546 }
547
548 function do_old_links_update() {
549 global $wgDatabase;
550 if ( $wgDatabase->tableExists( 'pagelinks' ) ) {
551 wfOut( "...have pagelinks; skipping old links table updates.\n" );
552 } else {
553 convertLinks(); flush();
554 }
555 }
556
557 function fix_ancient_imagelinks() {
558 global $wgDatabase;
559 $info = $wgDatabase->fieldInfo( 'imagelinks', 'il_from' );
560 if ( $info && $info->type() === 'string' ) {
561 wfOut( "Fixing ancient broken imagelinks table.\n" );
562 wfOut( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
563 $wgDatabase->sourceFile( archive( 'patch-fix-il_from.sql' ) );
564 wfOut( "ok\n" );
565 } else {
566 wfOut( "...il_from OK\n" );
567 }
568 }
569
570 function do_user_unique_update() {
571 global $wgDatabase;
572 $duper = new UserDupes( $wgDatabase );
573 if ( $duper->hasUniqueIndex() ) {
574 wfOut( "...already have unique user_name index.\n" );
575 } else {
576 if ( !$duper->clearDupes() ) {
577 wfOut( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
578 }
579 wfOut( "Adding unique index on user_name... " );
580 $wgDatabase->sourceFile( archive( 'patch-user_nameindex.sql' ) );
581 wfOut( "ok\n" );
582 }
583 }
584
585 function do_user_groups_update() {
586 $fname = 'do_user_groups_update';
587 global $wgDatabase;
588
589 if ( $wgDatabase->tableExists( 'user_groups' ) ) {
590 wfOut( "...user_groups table already exists.\n" );
591 return do_user_groups_reformat();
592 }
593
594 wfOut( "Adding user_groups table... " );
595 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
596 wfOut( "ok\n" );
597
598 if ( !$wgDatabase->tableExists( 'user_rights' ) ) {
599 if ( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
600 wfOut( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
601 $wgDatabase->sourceFile( archive( 'patch-user_rights.sql' ) );
602 wfOut( "ok\n" );
603 } else {
604 wfOut( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
605 wfOut( "*** You may need to manually configure some sysops by manipulating\n" );
606 wfOut( "*** the user_groups table.\n" );
607 return;
608 }
609 }
610
611 wfOut( "Converting user_rights table to user_groups... " );
612 $result = $wgDatabase->select( 'user_rights',
613 array( 'ur_user', 'ur_rights' ),
614 array( "ur_rights != ''" ),
615 $fname );
616
617 while ( $row = $wgDatabase->fetchObject( $result ) ) {
618 $groups = array_unique(
619 array_map( 'trim',
620 explode( ',', $row->ur_rights ) ) );
621
622 foreach ( $groups as $group ) {
623 $wgDatabase->insert( 'user_groups',
624 array(
625 'ug_user' => $row->ur_user,
626 'ug_group' => $group ),
627 $fname );
628 }
629 }
630 wfOut( "ok\n" );
631 }
632
633 function do_user_groups_reformat() {
634 # Check for bogus formats from previous 1.5 alpha code.
635 global $wgDatabase;
636 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
637
638 if ( $info->type() == 'int' ) {
639 $oldug = $wgDatabase->tableName( 'user_groups' );
640 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
641 wfOut( "user_groups is in bogus intermediate format. Renaming to $newug... " );
642 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
643 wfOut( "ok\n" );
644
645 wfOut( "Re-adding fresh user_groups table... " );
646 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
647 wfOut( "ok\n" );
648
649 wfOut( "***\n" );
650 wfOut( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
651 wfOut( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
652 wfOut( "***\n" );
653 } else {
654 wfOut( "...user_groups is in current format.\n" );
655 }
656
657 }
658
659 function do_watchlist_null() {
660 # Make sure wl_notificationtimestamp can be NULL,
661 # and update old broken items.
662 global $wgDatabase;
663 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
664
665 if ( !$info->nullable() ) {
666 wfOut( "Making wl_notificationtimestamp nullable... " );
667 $wgDatabase->sourceFile( archive( 'patch-watchlist-null.sql' ) );
668 wfOut( "ok\n" );
669 } else {
670 wfOut( "...wl_notificationtimestamp is already nullable.\n" );
671 }
672
673 }
674
675 /**
676 * @bug 3946
677 */
678 function do_page_random_update() {
679 global $wgDatabase;
680
681 wfOut( "Setting page_random to a random value on rows where it equals 0..." );
682
683 $page = $wgDatabase->tableName( 'page' );
684 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
685 $rows = $wgDatabase->affectedRows();
686
687 wfOut( "changed $rows rows\n" );
688 }
689
690 function do_templatelinks_update() {
691 global $wgDatabase;
692 $fname = 'do_templatelinks_update';
693
694 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
695 wfOut( "...templatelinks table already exists\n" );
696 return;
697 }
698 wfOut( "Creating templatelinks table...\n" );
699 $wgDatabase->sourceFile( archive( 'patch-templatelinks.sql' ) );
700 wfOut( "Populating...\n" );
701 if ( wfGetLB()->getServerCount() > 1 ) {
702 // Slow, replication-friendly update
703 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
704 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
705 $count = 0;
706 while ( $row = $wgDatabase->fetchObject( $res ) ) {
707 $count = ( $count + 1 ) % 100;
708 if ( $count == 0 ) {
709 if ( function_exists( 'wfWaitForSlaves' ) ) {
710 wfWaitForSlaves( 10 );
711 } else {
712 sleep( 1 );
713 }
714 }
715 $wgDatabase->insert( 'templatelinks',
716 array(
717 'tl_from' => $row->pl_from,
718 'tl_namespace' => $row->pl_namespace,
719 'tl_title' => $row->pl_title,
720 ), $fname
721 );
722
723 }
724 } else {
725 // Fast update
726 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
727 array(
728 'tl_from' => 'pl_from',
729 'tl_namespace' => 'pl_namespace',
730 'tl_title' => 'pl_title'
731 ), array(
732 'pl_namespace' => 10
733 ), $fname
734 );
735 }
736 wfOut( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
737 }
738
739 // Add index on ( rc_namespace, rc_user_text ) [Jul. 2006]
740 // Add index on ( rc_user_text, rc_timestamp ) [Nov. 2006]
741 function do_rc_indices_update() {
742 global $wgDatabase;
743 wfOut( "Checking for additional recent changes indices...\n" );
744
745 $indexes = array(
746 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
747 'rc_user_text' => 'patch-rc_user_text-index.sql',
748 );
749
750 foreach ( $indexes as $index => $patch ) {
751 $info = $wgDatabase->indexInfo( 'recentchanges', $index, __METHOD__ );
752 if ( !$info ) {
753 wfOut( "...index `{$index}` not found; adding..." );
754 $wgDatabase->sourceFile( archive( $patch ) );
755 wfOut( "done.\n" );
756 } else {
757 wfOut( "...index `{$index}` seems ok.\n" );
758 }
759 }
760 }
761
762 function index_has_field( $table, $index, $field ) {
763 global $wgDatabase;
764 wfOut( "Checking if $table index $index includes field $field...\n" );
765 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
766 if ( $info ) {
767 foreach ( $info as $row ) {
768 if ( $row->Column_name == $field ) {
769 wfOut( "...index $index on table $table seems to be ok\n" );
770 return true;
771 }
772 }
773 }
774 wfOut( "...index $index on table $table has no field $field; adding\n" );
775 return false;
776 }
777
778 function do_backlinking_indices_update() {
779 global $wgDatabase;
780 wfOut( "Checking for backlinking indices...\n" );
781 if ( !index_has_field( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
782 !index_has_field( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
783 !index_has_field( 'imagelinks', 'il_to', 'il_from' ) )
784 {
785 $wgDatabase->sourceFile( archive( 'patch-backlinkindexes.sql' ) );
786 wfOut( "...backlinking indices updated\n" );
787 }
788 }
789
790 function do_categorylinks_indices_update() {
791 global $wgDatabase;
792 wfOut( "Checking for categorylinks indices...\n" );
793 if ( !index_has_field( 'categorylinks', 'cl_sortkey', 'cl_from' ) )
794 {
795 $wgDatabase->sourceFile( archive( 'patch-categorylinksindex.sql' ) );
796 wfOut( "...categorylinks indices updated\n" );
797 }
798 }
799
800 function do_filearchive_indices_update() {
801 global $wgDatabase;
802 wfOut( "Checking filearchive indices...\n" );
803 $info = $wgDatabase->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
804 if ( !$info )
805 {
806 $wgDatabase->sourceFile( archive( 'patch-filearchive-user-index.sql' ) );
807 wfOut( "...filearchive indices updated\n" );
808 }
809 }
810
811 function maybe_do_profiling_memory_update() {
812 global $wgDatabase;
813 if ( !$wgDatabase->tableExists( 'profiling' ) ) {
814 // Simply ignore
815 } elseif ( $wgDatabase->fieldExists( 'profiling', 'pf_memory' ) ) {
816 wfOut( "...profiling table has pf_memory field.\n" );
817 } else {
818 wfOut( "Adding pf_memory field to table profiling..." );
819 $wgDatabase->sourceFile( archive( 'patch-profiling-memory.sql' ) );
820 wfOut( "ok\n" );
821 }
822 }
823
824 function do_stats_init() {
825 // Sometimes site_stats table is not properly populated.
826 global $wgDatabase;
827 wfOut( "\nChecking site_stats row..." );
828 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
829 if ( $row === false ) {
830 wfOut( "data is missing! rebuilding...\n" );
831 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
832 wfOut( "missing ss_total_pages, rebuilding...\n" );
833 } else {
834 wfOut( "ok.\n" );
835 return;
836 }
837 SiteStatsInit::doAllAndCommit( false );
838 }
839
840 function do_active_users_init() {
841 global $wgDatabase;
842 $activeUsers = $wgDatabase->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
843 if ( $activeUsers == -1 ) {
844 $activeUsers = $wgDatabase->selectField( 'recentchanges',
845 'COUNT( DISTINCT rc_user_text )',
846 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
847 );
848 $wgDatabase->update( 'site_stats',
849 array( 'ss_active_users' => intval( $activeUsers ) ),
850 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
851 );
852 }
853 wfOut( "...ss_active_users user count set...\n" );
854 }
855
856 function purge_cache() {
857 global $wgDatabase;
858 # We can't guarantee that the user will be able to use TRUNCATE,
859 # but we know that DELETE is available to us
860 wfOut( "Purging caches..." );
861 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
862 wfOut( "done.\n" );
863 }
864
865 function do_all_updates( $shared = false, $purge = true ) {
866 global $wgDatabase, $wgDBtype;
867
868 $updater = DatabaseUpdater::newForDb( $wgDatabase, $shared );
869
870 wfRunHooks( 'LoadExtensionSchemaUpdates', array( &$updater ) );
871
872 $updater->doUpdates();
873
874 if ( !defined( 'MW_NO_SETUP' ) ) {
875 define( 'MW_NO_SETUP', true );
876 }
877
878 foreach( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
879 call_user_func_array( array( new $maint, 'execute' ), array() );
880 }
881
882 if ( $wgDBtype === 'postgres' ) {
883 return;
884 }
885
886 do_stats_init();
887
888 if ( $purge ) {
889 purge_cache();
890 }
891 }
892
893 function archive( $name ) {
894 global $wgDBtype, $IP;
895 if ( file_exists( "$IP/maintenance/$wgDBtype/archives/$name" ) ) {
896 return "$IP/maintenance/$wgDBtype/archives/$name";
897 } else {
898 return "$IP/maintenance/archives/$name";
899 }
900 }
901
902 function do_restrictions_update() {
903 # Adding page_restrictions table, obsoleting page.page_restrictions.
904 # Migrating old restrictions to new table
905 # -- Andrew Garrett, January 2007.
906
907 global $wgDatabase;
908
909 $name = 'page_restrictions';
910 $patch = 'patch-page_restrictions.sql';
911 $patch2 = 'patch-page_restrictions_sortkey.sql';
912
913 if ( $wgDatabase->tableExists( $name ) ) {
914 wfOut( "...$name table already exists.\n" );
915 } else {
916 wfOut( "Creating $name table..." );
917 $wgDatabase->sourceFile( archive( $patch ) );
918 $wgDatabase->sourceFile( archive( $patch2 ) );
919 wfOut( "ok\n" );
920
921 wfOut( "Migrating old restrictions to new table..." );
922
923 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array( "page_restrictions!=''", "page_restrictions!='edit=:move='" ), __METHOD__ );
924
925 $count = 0;
926
927 while ( $row = $wgDatabase->fetchObject( $res ) ) {
928 $count = ( $count + 1 ) % 100;
929
930 if ( $count == 0 ) {
931 if ( function_exists( 'wfWaitForSlaves' ) ) {
932 wfWaitForSlaves( 10 );
933 } else {
934 sleep( 1 );
935 }
936 }
937
938 # Figure out what the restrictions are..
939 $id = $row->page_id;
940 $flatrestrictions = explode( ':', $row->page_restrictions );
941
942 $restrictions = array ();
943 foreach ( $flatrestrictions as $restriction ) {
944 $thisrestriction = explode( '=', $restriction, 2 );
945 if ( count( $thisrestriction ) == 1 ) {
946 // Compatibility with old protections from before
947 // separate move protection was added.
948 list( $level ) = $thisrestriction;
949 if ( $level ) {
950 $restrictions['edit'] = $level;
951 $restrictions['move'] = $level;
952 }
953 } else {
954 list( $type, $level ) = $thisrestriction;
955 if ( $level ) {
956 $restrictions[$type] = $level;
957 }
958 }
959
960 $wgDatabase->update( 'page', array ( 'page_restrictions' => '' ), array( 'page_id' => $id ), __METHOD__ );
961
962 }
963
964 foreach ( $restrictions as $type => $level ) {
965 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
966 'pr_type' => $type,
967 'pr_level' => $level,
968 'pr_cascade' => 0,
969 'pr_expiry' => 'infinity' ),
970 __METHOD__ );
971 }
972 }
973 wfOut( "ok\n" );
974 }
975 }
976
977 function do_category_population() {
978 if ( update_row_exists( 'populate category' ) ) {
979 wfOut( "...category table already populated.\n" );
980 return;
981 }
982 require_once( 'populateCategory.php' );
983 wfOut(
984 "Populating category table, printing progress markers. " .
985 "For large databases, you\n" .
986 "may want to hit Ctrl-C and do this manually with maintenance/\n" .
987 "populateCategory.php.\n"
988 );
989 $task = new PopulateCategory();
990 $task->execute();
991 wfOut( "Done populating category table.\n" );
992 }
993
994 function do_populate_parent_id() {
995 if ( update_row_exists( 'populate rev_parent_id' ) ) {
996 wfOut( "...rev_parent_id column already populated.\n" );
997 return;
998 }
999 require_once( 'populateParentId.php' );
1000 $task = new PopulateParentId();
1001 $task->execute();
1002 }
1003
1004 function do_populate_rev_len() {
1005 if ( update_row_exists( 'populate rev_len' ) ) {
1006 wfOut( "...rev_len column already populated.\n" );
1007 return;
1008 }
1009 require_once( 'populateRevisionLength.php' );
1010 $task = new PopulateRevisionLength();
1011 $task->execute();
1012 }
1013
1014 function do_collation_update() {
1015 global $wgDatabase, $wgCollationVersion;
1016 if ( $wgDatabase->selectField(
1017 'categorylinks',
1018 'COUNT(*)',
1019 'cl_collation != ' . $wgDatabase->addQuotes( $wgCollationVersion ),
1020 __FUNCTION__
1021 ) == 0 ) {
1022 wfOut( "...collations up-to-date.\n" );
1023 return;
1024 }
1025 require_once( 'updateCollation.php' );
1026 $task = new UpdateCollation();
1027 $task->execute();
1028 }
1029
1030 function sqlite_initial_indexes() {
1031 global $wgDatabase;
1032 // initial-indexes.sql fails if the indexes are already present, so we perform a quick check if our database is newer.
1033 if ( update_row_exists( 'initial_indexes' ) || $wgDatabase->indexExists( 'user', 'user_name' ) ) {
1034 wfOut( "...have initial indexes\n" );
1035 return;
1036 }
1037 wfOut( "Adding initial indexes..." );
1038 $wgDatabase->sourceFile( archive( 'initial-indexes.sql' ) );
1039 wfOut( "done\n" );
1040 }
1041
1042 function sqlite_setup_searchindex() {
1043 global $wgDatabase;
1044 $module = $wgDatabase->getFulltextSearchModule();
1045 $fts3tTable = update_row_exists( 'fts3' );
1046 if ( $fts3tTable && !$module ) {
1047 wfOut( '...PHP is missing FTS3 support, downgrading tables...' );
1048 $wgDatabase->sourceFile( archive( 'searchindex-no-fts.sql' ) );
1049 wfOut( "done\n" );
1050 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
1051 wfOut( '...adding FTS3 search capabilities...' );
1052 $wgDatabase->sourceFile( archive( 'searchindex-fts3.sql' ) );
1053 wfOut( "done\n" );
1054 } else {
1055 wfOut( "...fulltext search table appears to be in order.\n" );
1056 }
1057 }
1058
1059 function do_unique_pl_tl_il() {
1060 global $wgDatabase;
1061 $info = $wgDatabase->indexInfo( 'pagelinks', 'pl_namespace' );
1062 if ( is_array( $info ) && !$info[0]->Non_unique ) {
1063 wfOut( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
1064 } else {
1065 wfOut( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
1066 $wgDatabase->sourceFile( archive( 'patch-pl-tl-il-unique.sql' ) );
1067 wfOut( "ok\n" );
1068 }
1069 }
1070
1071 function do_log_search_population() {
1072 if ( update_row_exists( 'populate log_search' ) ) {
1073 wfOut( "...log_search table already populated.\n" );
1074 return;
1075 }
1076 require_once( 'populateLogSearch.php' );
1077 wfOut(
1078 "Populating log_search table, printing progress markers. For large\n" .
1079 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1080 "maintenance/populateLogSearch.php.\n" );
1081 $task = new PopulateLogSearch();
1082 $task->execute();
1083 wfOut( "Done populating log_search table.\n" );
1084 }
1085
1086 function rename_eu_wiki_id() {
1087 global $wgDatabase;
1088 if ( $wgDatabase->fieldExists( 'external_user', 'eu_local_id' ) ) {
1089 wfOut( "...eu_wiki_id already renamed to eu_local_id.\n" );
1090 return;
1091 }
1092 wfOut( "Renaming eu_wiki_id -> eu_local_id... " );
1093 $wgDatabase->sourceFile( archive( 'patch-eu_local_id.sql' ) );
1094 wfOut( "ok\n" );
1095 }
1096
1097 function do_update_transcache_field() {
1098 global $wgDatabase;
1099 if ( update_row_exists( 'convert transcache field' ) ) {
1100 wfOut( "...transcache tc_time already converted.\n" );
1101 return;
1102 } else {
1103 wfOut( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
1104 $wgDatabase->sourceFile( archive( 'patch-tc-timestamp.sql' ) );
1105 wfOut( "ok\n" );
1106 }
1107 }
1108
1109 function do_update_mime_minor_field() {
1110 if ( update_row_exists( 'mime_minor_length' ) ) {
1111 wfOut( "...*_mime_minor fields are already long enough.\n" );
1112 } else {
1113 global $wgDatabase;
1114 wfOut( "Altering all *_mime_minor fields to 100 bytes in size ... " );
1115 $wgDatabase->sourceFile( archive( 'patch-mime_minor_length.sql' ) );
1116 wfOut( "ok\n" );
1117 }
1118 }
1119
1120 /***********************************************************************
1121 * Start PG stuff
1122 * TODO: merge with above
1123 ***********************************************************************/
1124
1125 function pg_describe_table( $table ) {
1126 global $wgDatabase, $wgDBmwschema;
1127 $q = <<<END
1128 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
1129 WHERE pg_class.relnamespace = pg_namespace.oid
1130 AND attrelid=pg_class.oid AND attnum > 0
1131 AND relname=%s AND nspname=%s
1132 END;
1133 $res = $wgDatabase->query( sprintf( $q,
1134 $wgDatabase->addQuotes( $table ),
1135 $wgDatabase->addQuotes( $wgDBmwschema ) ) );
1136 if ( !$res ) {
1137 return null;
1138 }
1139
1140 $cols = array();
1141 while ( $r = $wgDatabase->fetchRow( $res ) ) {
1142 $cols[] = array(
1143 "name" => $r[0],
1144 "ord" => $r[1],
1145 );
1146 }
1147 return $cols;
1148 }
1149
1150 function pg_describe_index( $idx ) {
1151 global $wgDatabase, $wgDBmwschema;
1152
1153 // first fetch the key (which is a list of columns ords) and
1154 // the table the index applies to (an oid)
1155 $q = <<<END
1156 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
1157 WHERE nspname=%s
1158 AND pg_class.relnamespace = pg_namespace.oid
1159 AND relname=%s
1160 AND indexrelid=pg_class.oid
1161 END;
1162 $res = $wgDatabase->query( sprintf( $q,
1163 $wgDatabase->addQuotes( $wgDBmwschema ),
1164 $wgDatabase->addQuotes( $idx ) ) );
1165 if ( !$res ) {
1166 return null;
1167 }
1168 if ( !( $r = $wgDatabase->fetchRow( $res ) ) ) {
1169 return null;
1170 }
1171
1172 $indkey = $r[0];
1173 $relid = intval( $r[1] );
1174 $indkeys = explode( " ", $indkey );
1175
1176 $colnames = array();
1177 foreach ( $indkeys as $rid ) {
1178 $query = <<<END
1179 SELECT attname FROM pg_class, pg_attribute
1180 WHERE attrelid=$relid
1181 AND attnum=%d
1182 AND attrelid=pg_class.oid
1183 END;
1184 $r2 = $wgDatabase->query( sprintf( $query, $rid ) );
1185 if ( !$r2 ) {
1186 return null;
1187 }
1188 if ( !( $row2 = $wgDatabase->fetchRow( $r2 ) ) ) {
1189 return null;
1190 }
1191 $colnames[] = $row2[0];
1192 }
1193
1194 return $colnames;
1195 }
1196
1197 function pg_index_exists( $table, $index ) {
1198 global $wgDatabase, $wgDBmwschema;
1199 $exists = $wgDatabase->selectField( "pg_indexes", "indexname",
1200 array( "indexname" => $index,
1201 "tablename" => $table,
1202 "schemaname" => $wgDBmwschema ) );
1203 return $exists === $index;
1204 }
1205
1206 function pg_fkey_deltype( $fkey ) {
1207 global $wgDatabase, $wgDBmwschema;
1208 $q = <<<END
1209 SELECT confdeltype FROM pg_constraint, pg_namespace
1210 WHERE connamespace=pg_namespace.oid
1211 AND nspname=%s
1212 AND conname=%s;
1213 END;
1214 $r = $wgDatabase->query( sprintf( $q,
1215 $wgDatabase->addQuotes( $wgDBmwschema ),
1216 $wgDatabase->addQuotes( $fkey ) ) );
1217 if ( !( $row = $wgDatabase->fetchRow( $r ) ) ) {
1218 return null;
1219 }
1220 return $row[0];
1221 }
1222
1223 function pg_rule_def( $table, $rule ) {
1224 global $wgDatabase, $wgDBmwschema;
1225 $q = <<<END
1226 SELECT definition FROM pg_rules
1227 WHERE schemaname = %s
1228 AND tablename = %s
1229 AND rulename = %s
1230 END;
1231 $r = $wgDatabase->query( sprintf( $q,
1232 $wgDatabase->addQuotes( $wgDBmwschema ),
1233 $wgDatabase->addQuotes( $table ),
1234 $wgDatabase->addQuotes( $rule ) ) );
1235 $row = $wgDatabase->fetchRow( $r );
1236 if ( !$row ) {
1237 return null;
1238 }
1239 $d = $row[0];
1240 return $d;
1241 }
1242
1243 function do_postgres_updates() {
1244 global $wgDatabase, $wgDBmwschema, $wgDBts2schema, $wgShowExceptionDetails, $wgDBuser;
1245
1246 # # Gather version numbers in case we need them
1247 $version = $wgDatabase->getServerVersion(); # # long string
1248 $numver = $wgDatabase->numeric_version; # # X.Y e.g. 8.3
1249
1250 # Just in case their LocalSettings.php does not have this:
1251 if ( !isset( $wgDBmwschema ) ) {
1252 $wgDBmwschema = 'mediawiki';
1253 }
1254
1255 # Verify that this user is configured correctly
1256 $safeuser = $wgDatabase->addQuotes( $wgDBuser );
1257 $SQL = "SELECT array_to_string(useconfig,'*') FROM pg_catalog.pg_user WHERE usename = $safeuser";
1258 $config = pg_fetch_result( $wgDatabase->doQuery( $SQL ), 0, 0 );
1259 $conf = array();
1260 foreach ( explode( '*', $config ) as $c ) {
1261 list( $x, $y ) = explode( '=', $c );
1262 $conf[$x] = $y;
1263 }
1264 if ( !array_key_exists( 'search_path', $conf ) ) {
1265 $search_path = '';
1266 } else {
1267 $search_path = $conf['search_path'];
1268 }
1269 if ( strpos( $search_path, $wgDBmwschema ) === false ) {
1270 wfOut( "Adding in schema \"$wgDBmwschema\" to search_path for user \"$wgDBuser\"\n" );
1271 $search_path = "$wgDBmwschema, $search_path";
1272 }
1273 if ( strpos( $search_path, $wgDBts2schema ) === false ) {
1274 wfOut( "Adding in schema \"$wgDBts2schema\" to search_path for user \"$wgDBuser\"\n" );
1275 $search_path = "$search_path, $wgDBts2schema";
1276 }
1277 $search_path = str_replace( ', ,', ',', $search_path );
1278 if ( array_key_exists( 'search_path', $conf ) === false || $search_path != $conf['search_path'] ) {
1279 $wgDatabase->doQuery( "ALTER USER $wgDBuser SET search_path = $search_path" );
1280 $wgDatabase->doQuery( "SET search_path = $search_path" );
1281 } else {
1282 $path = $conf['search_path'];
1283 wfOut( "... search_path for user \"$wgDBuser\" looks correct ($path)\n" );
1284 }
1285 $goodconf = array(
1286 'client_min_messages' => 'error',
1287 'DateStyle' => 'ISO, YMD',
1288 'TimeZone' => 'GMT'
1289 );
1290 foreach ( array_keys( $goodconf ) AS $key ) {
1291 $value = $goodconf[$key];
1292 if ( !array_key_exists( $key, $conf ) or $conf[$key] !== $value ) {
1293 wfOut( "Setting $key to '$value' for user \"$wgDBuser\"\n" );
1294 $wgDatabase->doQuery( "ALTER USER $wgDBuser SET $key = '$value'" );
1295 $wgDatabase->doQuery( "SET $key = '$value'" );
1296 } else {
1297 wfOut( "... default value of \"$key\" is correctly set to \"$value\" for user \"$wgDBuser\"\n" );
1298 }
1299 }
1300
1301 $newsequences = array(
1302 "logging_log_id_seq",
1303 "page_restrictions_pr_id_seq",
1304 );
1305
1306 $renamed_sequences = array(
1307 array('ipblocks_ipb_id_val', 'ipblocks_ipb_id_seq'),
1308 array('rev_rev_id_val', 'revision_rev_id_seq'),
1309 array('text_old_id_val', 'text_old_id_seq'),
1310 array('category_id_seq', 'category_cat_id_seq'),
1311 array('rc_rc_id_seq', 'recentchanges_rc_id_seq'),
1312 array('log_log_id_seq', 'logging_log_id_seq'),
1313 array('pr_id_val', 'page_restrictions_pr_id_seq'),
1314 );
1315
1316 $newtables = array(
1317 array( "category", "patch-category.sql" ),
1318 array( "mwuser", "patch-mwuser.sql" ),
1319 array( "pagecontent", "patch-pagecontent.sql" ),
1320 array( "querycachetwo", "patch-querycachetwo.sql" ),
1321 array( "page_props", "patch-page_props.sql" ),
1322 array( "page_restrictions", "patch-page_restrictions.sql" ),
1323 array( "profiling", "patch-profiling.sql" ),
1324 array( "protected_titles", "patch-protected_titles.sql" ),
1325 array( "redirect", "patch-redirect.sql" ),
1326 array( "updatelog", "patch-updatelog.sql" ),
1327 array( 'change_tag', 'patch-change_tag.sql' ),
1328 array( 'tag_summary', 'patch-change_tag.sql' ),
1329 array( 'valid_tag', 'patch-change_tag.sql' ),
1330 array( 'user_properties', 'patch-user_properties.sql' ),
1331 array( 'log_search', 'patch-log_search.sql' ),
1332 array( 'l10n_cache', 'patch-l10n_cache.sql' ),
1333 array( 'iwlinks', 'patch-iwlinks.sql' ),
1334 );
1335
1336 $newcols = array(
1337 array( "archive", "ar_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1338 array( "archive", "ar_len", "INTEGER" ),
1339 array( "archive", "ar_page_id", "INTEGER" ),
1340 array( "archive", "ar_parent_id", "INTEGER" ),
1341 array( "image", "img_sha1", "TEXT NOT NULL DEFAULT ''" ),
1342 array( "ipblocks", "ipb_allow_usertalk", "SMALLINT NOT NULL DEFAULT 0" ),
1343 array( "ipblocks", "ipb_anon_only", "SMALLINT NOT NULL DEFAULT 0" ),
1344 array( "ipblocks", "ipb_by_text", "TEXT NOT NULL DEFAULT ''" ),
1345 array( "ipblocks", "ipb_block_email", "SMALLINT NOT NULL DEFAULT 0" ),
1346 array( "ipblocks", "ipb_create_account", "SMALLINT NOT NULL DEFAULT 1" ),
1347 array( "ipblocks", "ipb_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1348 array( "ipblocks", "ipb_enable_autoblock", "SMALLINT NOT NULL DEFAULT 1" ),
1349 array( "filearchive", "fa_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1350 array( "logging", "log_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1351 array( "logging", "log_id", "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq')" ),
1352 array( "logging", "log_params", "TEXT" ),
1353 array( "mwuser", "user_editcount", "INTEGER" ),
1354 array( "mwuser", "user_hidden", "SMALLINT NOT NULL DEFAULT 0" ),
1355 array( "mwuser", "user_newpass_time", "TIMESTAMPTZ" ),
1356 array( "oldimage", "oi_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1357 array( "oldimage", "oi_major_mime", "TEXT NOT NULL DEFAULT 'unknown'" ),
1358 array( "oldimage", "oi_media_type", "TEXT" ),
1359 array( "oldimage", "oi_metadata", "BYTEA NOT NULL DEFAULT ''" ),
1360 array( "oldimage", "oi_minor_mime", "TEXT NOT NULL DEFAULT 'unknown'" ),
1361 array( "oldimage", "oi_sha1", "TEXT NOT NULL DEFAULT ''" ),
1362 array( "page_restrictions", "pr_id", "INTEGER NOT NULL UNIQUE DEFAULT nextval('page_restrictions_pr_id_val')" ),
1363 array( "profiling", "pf_memory", "NUMERIC(18,10) NOT NULL DEFAULT 0" ),
1364 array( "recentchanges", "rc_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1365 array( "recentchanges", "rc_log_action", "TEXT" ),
1366 array( "recentchanges", "rc_log_type", "TEXT" ),
1367 array( "recentchanges", "rc_logid", "INTEGER NOT NULL DEFAULT 0" ),
1368 array( "recentchanges", "rc_new_len", "INTEGER" ),
1369 array( "recentchanges", "rc_old_len", "INTEGER" ),
1370 array( "recentchanges", "rc_params", "TEXT" ),
1371 array( "redirect", "rd_interwiki", "TEXT NULL" ),
1372 array( "redirect", "rd_fragment", "TEXT NULL" ),
1373 array( "revision", "rev_deleted", "SMALLINT NOT NULL DEFAULT 0" ),
1374 array( "revision", "rev_len", "INTEGER" ),
1375 array( "revision", "rev_parent_id", "INTEGER DEFAULT NULL" ),
1376 array( "site_stats", "ss_active_users", "INTEGER DEFAULT '-1'" ),
1377 array( "user_newtalk", "user_last_timestamp", "TIMESTAMPTZ" ),
1378 array( "logging", "log_user_text", "TEXT NOT NULL DEFAULT ''" ),
1379 array( "logging", "log_page", "INTEGER" ),
1380 array( "interwiki", "iw_api", "TEXT NOT NULL DEFAULT ''"),
1381 array( "interwiki", "iw_wikiid", "TEXT NOT NULL DEFAULT ''"),
1382 );
1383
1384
1385 # table, column, desired type, USING clause if needed (with new default if needed)
1386 $typechanges = array(
1387 array( "archive", "ar_deleted", "smallint", "" ),
1388 array( "archive", "ar_minor_edit", "smallint", "ar_minor_edit::smallint DEFAULT 0" ),
1389 array( "filearchive", "fa_deleted", "smallint", "" ),
1390 array( "filearchive", "fa_height", "integer", "" ),
1391 array( "filearchive", "fa_metadata", "bytea", "decode(fa_metadata,'escape')" ),
1392 array( "filearchive", "fa_size", "integer", "" ),
1393 array( "filearchive", "fa_width", "integer", "" ),
1394 array( "filearchive", "fa_storage_group", "text", "" ),
1395 array( "filearchive", "fa_storage_key", "text", "" ),
1396 array( "image", "img_metadata", "bytea", "decode(img_metadata,'escape')" ),
1397 array( "image", "img_size", "integer", "" ),
1398 array( "image", "img_width", "integer", "" ),
1399 array( "image", "img_height", "integer", "" ),
1400 array( "interwiki", "iw_local", "smallint", "iw_local::smallint DEFAULT 0" ),
1401 array( "interwiki", "iw_trans", "smallint", "iw_trans::smallint DEFAULT 0" ),
1402 array( "ipblocks", "ipb_auto", "smallint", "ipb_auto::smallint DEFAULT 0" ),
1403 array( "ipblocks", "ipb_anon_only", "smallint", "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ),
1404 array( "ipblocks", "ipb_create_account", "smallint", "CASE WHEN ipb_create_account=' ' THEN 0 ELSE ipb_create_account::smallint END DEFAULT 1" ),
1405 array( "ipblocks", "ipb_enable_autoblock", "smallint", "CASE WHEN ipb_enable_autoblock=' ' THEN 0 ELSE ipb_enable_autoblock::smallint END DEFAULT 1" ),
1406 array( "ipblocks", "ipb_block_email", "smallint", "CASE WHEN ipb_block_email=' ' THEN 0 ELSE ipb_block_email::smallint END DEFAULT 0" ),
1407 array( "ipblocks", "ipb_address", "text", "ipb_address::text" ),
1408 array( "ipblocks", "ipb_deleted", "smallint", "ipb_deleted::smallint DEFAULT 0" ),
1409 array( "math", "math_inputhash", "bytea", "decode(math_inputhash,'escape')" ),
1410 array( "math", "math_outputhash", "bytea", "decode(math_outputhash,'escape')" ),
1411 array( "mwuser", "user_token", "text", "" ),
1412 array( "mwuser", "user_email_token", "text", "" ),
1413 array( "objectcache", "keyname", "text", "" ),
1414 array( "oldimage", "oi_height", "integer", "" ),
1415 array( "oldimage", "oi_metadata", "bytea", "decode(img_metadata,'escape')" ),
1416 array( "oldimage", "oi_size", "integer", "" ),
1417 array( "oldimage", "oi_width", "integer", "" ),
1418 array( "page", "page_is_redirect", "smallint", "page_is_redirect::smallint DEFAULT 0" ),
1419 array( "page", "page_is_new", "smallint", "page_is_new::smallint DEFAULT 0" ),
1420 array( "querycache", "qc_value", "integer", "" ),
1421 array( "querycachetwo", "qcc_value", "integer", "" ),
1422 array( "recentchanges", "rc_bot", "smallint", "rc_bot::smallint DEFAULT 0" ),
1423 array( "recentchanges", "rc_deleted", "smallint", "" ),
1424 array( "recentchanges", "rc_minor", "smallint", "rc_minor::smallint DEFAULT 0" ),
1425 array( "recentchanges", "rc_new", "smallint", "rc_new::smallint DEFAULT 0" ),
1426 array( "recentchanges", "rc_type", "smallint", "rc_type::smallint DEFAULT 0" ),
1427 array( "recentchanges", "rc_patrolled", "smallint", "rc_patrolled::smallint DEFAULT 0" ),
1428 array( "revision", "rev_deleted", "smallint", "rev_deleted::smallint DEFAULT 0" ),
1429 array( "revision", "rev_minor_edit", "smallint", "rev_minor_edit::smallint DEFAULT 0" ),
1430 array( "templatelinks", "tl_namespace", "smallint", "tl_namespace::smallint" ),
1431 array( "user_newtalk", "user_ip", "text", "host(user_ip)" ),
1432 );
1433
1434 # table, column, nullability
1435 $nullchanges = array(
1436 array( "oldimage", "oi_bits", "NULL" ),
1437 array( "oldimage", "oi_timestamp", "NULL" ),
1438 array( "oldimage", "oi_major_mime", "NULL" ),
1439 array( "oldimage", "oi_minor_mime", "NULL" ),
1440 );
1441
1442 $newindexes = array(
1443 array( "archive", "archive_user_text", "(ar_user_text)" ),
1444 array( "image", "img_sha1", "(img_sha1)" ),
1445 array( "oldimage", "oi_sha1", "(oi_sha1)" ),
1446 array( "page", "page_mediawiki_title", "(page_title) WHERE page_namespace = 8" ),
1447 array( "revision", "rev_text_id_idx", "(rev_text_id)" ),
1448 array( "recentchanges", "rc_timestamp_bot", "(rc_timestamp) WHERE rc_bot = 0" ),
1449 array( "templatelinks", "templatelinks_from", "(tl_from)" ),
1450 array( "watchlist", "wl_user", "(wl_user)" ),
1451 array( "logging", "logging_user_type_time", "(log_user, log_type, log_timestamp)" ),
1452 array( "logging", "logging_page_id_time", "(log_page,log_timestamp)" ),
1453 array( "iwlinks", "iwl_prefix_title_from", "(iwl_prefix, iwl_title, iwl_from)" ),
1454 );
1455
1456 $newrules = array(
1457 );
1458
1459 # # All FK columns should be deferred
1460 $deferredcols = array(
1461 array( "archive", "ar_user", "mwuser(user_id) ON DELETE SET NULL" ),
1462 array( "categorylinks", "cl_from", "page(page_id) ON DELETE CASCADE" ),
1463 array( "externallinks", "el_from", "page(page_id) ON DELETE CASCADE" ),
1464 array( "filearchive", "fa_deleted_user", "mwuser(user_id) ON DELETE SET NULL" ),
1465 array( "filearchive", "fa_user", "mwuser(user_id) ON DELETE SET NULL" ),
1466 array( "image", "img_user", "mwuser(user_id) ON DELETE SET NULL" ),
1467 array( "imagelinks", "il_from", "page(page_id) ON DELETE CASCADE" ),
1468 array( "ipblocks", "ipb_by", "mwuser(user_id) ON DELETE CASCADE" ),
1469 array( "ipblocks", "ipb_user", "mwuser(user_id) ON DELETE SET NULL" ),
1470 array( "langlinks", "ll_from", "page(page_id) ON DELETE CASCADE" ),
1471 array( "logging", "log_user", "mwuser(user_id) ON DELETE SET NULL" ),
1472 array( "oldimage", "oi_name", "image(img_name) ON DELETE CASCADE ON UPDATE CASCADE" ),
1473 array( "oldimage", "oi_user", "mwuser(user_id) ON DELETE SET NULL" ),
1474 array( "pagelinks", "pl_from", "page(page_id) ON DELETE CASCADE" ),
1475 array( "page_props", "pp_page", "page (page_id) ON DELETE CASCADE" ),
1476 array( "page_restrictions", "pr_page", "page(page_id) ON DELETE CASCADE" ),
1477 array( "protected_titles", "pt_user", "mwuser(user_id) ON DELETE SET NULL" ),
1478 array( "recentchanges", "rc_cur_id", "page(page_id) ON DELETE SET NULL" ),
1479 array( "recentchanges", "rc_user", "mwuser(user_id) ON DELETE SET NULL" ),
1480 array( "redirect", "rd_from", "page(page_id) ON DELETE CASCADE" ),
1481 array( "revision", "rev_page", "page (page_id) ON DELETE CASCADE" ),
1482 array( "revision", "rev_user", "mwuser(user_id) ON DELETE RESTRICT" ),
1483 array( "templatelinks", "tl_from", "page(page_id) ON DELETE CASCADE" ),
1484 array( "trackbacks", "tb_page", "page(page_id) ON DELETE CASCADE" ),
1485 array( "user_groups", "ug_user", "mwuser(user_id) ON DELETE CASCADE" ),
1486 array( "user_newtalk", "user_id", "mwuser(user_id) ON DELETE CASCADE" ),
1487 array( "user_properties", "up_user", "mwuser(user_id) ON DELETE CASCADE" ),
1488 array( "watchlist", "wl_user", "mwuser(user_id) ON DELETE CASCADE" ),
1489 );
1490
1491 # Create new sequences
1492 foreach ( $newsequences as $ns ) {
1493 if ( ! $wgDatabase->sequenceExists( $ns ) ) {
1494 wfOut( "Creating sequence $ns\n" );
1495 $wgDatabase->query( "CREATE SEQUENCE $ns" );
1496 }
1497 }
1498
1499 # Rename sequences
1500 foreach ( $renamed_sequences as $ren ) {
1501 if ( $wgDatabase->sequenceExists( $ren[0] ) ) {
1502 wfOut( "Renaming sequence $ren[0] to $ren[1]\n" );
1503 $wgDatabase->query( "ALTER SEQUENCE $ren[0] RENAME TO $ren[1]" );
1504 }
1505 }
1506
1507 # Create new tables
1508 foreach ( $newtables as $nt ) {
1509 if ( $wgDatabase->tableExists( $nt[0] ) ) {
1510 wfOut( "... table \"$nt[0]\" already exists\n" );
1511 continue;
1512 }
1513
1514 wfOut( "Creating table \"$nt[0]\"\n" );
1515 $wgDatabase->sourceFile( archive( $nt[1] ) );
1516 }
1517
1518 # Needed before newcols
1519 if ( $wgDatabase->tableExists( "archive2" ) ) {
1520 wfOut( "Converting \"archive2\" back to normal archive table\n" );
1521 if ( $wgDatabase->ruleExists( "archive", "archive_insert" ) ) {
1522 wfOut( "Dropping rule \"archive_insert\"\n" );
1523 $wgDatabase->query( "DROP RULE archive_insert ON archive" );
1524 }
1525 if ( $wgDatabase->ruleExists( "archive", "archive_delete" ) ) {
1526 wfOut( "Dropping rule \"archive_delete\"\n" );
1527 $wgDatabase->query( "DROP RULE archive_delete ON archive" );
1528 }
1529 $wgDatabase->sourceFile( archive( "patch-remove-archive2.sql" ) );
1530 }
1531 else
1532 wfOut( "... obsolete table \"archive2\" does not exist\n" );
1533
1534 foreach ( $newcols as $nc ) {
1535 $fi = $wgDatabase->fieldInfo( $nc[0], $nc[1] );
1536 if ( !is_null( $fi ) ) {
1537 wfOut( "... column \"$nc[0].$nc[1]\" already exists\n" );
1538 continue;
1539 }
1540
1541 wfOut( "Adding column \"$nc[0].$nc[1]\"\n" );
1542 $wgDatabase->query( "ALTER TABLE $nc[0] ADD $nc[1] $nc[2]" );
1543 }
1544
1545 foreach ( $typechanges as $tc ) {
1546 $fi = $wgDatabase->fieldInfo( $tc[0], $tc[1] );
1547 if ( is_null( $fi ) ) {
1548 wfOut( "... error: expected column $tc[0].$tc[1] to exist\n" );
1549 exit( 1 );
1550 }
1551
1552 if ( $fi->type() === $tc[2] )
1553 wfOut( "... column \"$tc[0].$tc[1]\" is already of type \"$tc[2]\"\n" );
1554 else {
1555 wfOut( "Changing column type of \"$tc[0].$tc[1]\" from \"{$fi->type()}\" to \"$tc[2]\"\n" );
1556 $sql = "ALTER TABLE $tc[0] ALTER $tc[1] TYPE $tc[2]";
1557 if ( strlen( $tc[3] ) ) {
1558 $default = array();
1559 if ( preg_match( '/DEFAULT (.+)/', $tc[3], $default ) ) {
1560 $sqldef = "ALTER TABLE $tc[0] ALTER $tc[1] SET DEFAULT $default[1]";
1561 $wgDatabase->query( $sqldef );
1562 $tc[3] = preg_replace( '/\s*DEFAULT .+/', '', $tc[3] );
1563 }
1564 $sql .= " USING $tc[3]";
1565 }
1566 $sql .= ";\nCOMMIT;\n";
1567 $wgDatabase->query( $sql );
1568 }
1569 }
1570
1571 foreach ( $nullchanges as $nc ) {
1572 $fi = $wgDatabase->fieldInfo( $nc[0], $nc[1] );
1573 if ( is_null( $fi ) ) {
1574 wfOut( "... error: expected column $nc[0].$nc[1] to exist\n" );
1575 exit( 1 );
1576 }
1577 if ( $fi->nullable() ) {
1578 # # It's NULL - does it need to be NOT NULL?
1579 if ( 'NOT NULL' === $nc[2] ) {
1580 wfOut( "Changing \"$nc[0].$nc[1]\" to not allow NULLs\n" );
1581 $wgDatabase->query( "ALTER TABLE $nc[0] ALTER $nc[1] SET NOT NULL" );
1582 } else {
1583 wfOut( "... column \"$nc[0].$nc[1]\" is already set as NULL\n" );
1584 }
1585 } else {
1586 # # It's NOT NULL - does it need to be NULL?
1587 if ( 'NULL' === $nc[2] ) {
1588 wfOut( "Changing \"$nc[0].$nc[1]\" to allow NULLs\n" );
1589 $wgDatabase->query( "ALTER TABLE $nc[0] ALTER $nc[1] DROP NOT NULL" );
1590 }
1591 else {
1592 wfOut( "... column \"$nc[0].$nc[1]\" is already set as NOT NULL\n" );
1593 }
1594 }
1595 }
1596
1597 if ( $wgDatabase->fieldInfo( 'oldimage', 'oi_deleted' )->type() !== 'smallint' ) {
1598 wfOut( "Changing \"oldimage.oi_deleted\" to type \"smallint\"\n" );
1599 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
1600 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
1601 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
1602 } else {
1603 wfOut( "... column \"oldimage.oi_deleted\" is already of type \"smallint\"\n" );
1604 }
1605
1606 foreach ( $newindexes as $ni ) {
1607 if ( pg_index_exists( $ni[0], $ni[1] ) ) {
1608 wfOut( "... index \"$ni[1]\" on table \"$ni[0]\" already exists\n" );
1609 continue;
1610 }
1611 wfOut( "Creating index \"$ni[1]\" on table \"$ni[0]\" $ni[2]\n" );
1612 $wgDatabase->query( "CREATE INDEX $ni[1] ON $ni[0] $ni[2]" );
1613 }
1614
1615 foreach ( $newrules as $nr ) {
1616 if ( $wgDatabase->ruleExists( $nr[0], $nr[1] ) ) {
1617 wfOut( "... rule \"$nr[1]\" on table \"$nr[0]\" already exists\n" );
1618 continue;
1619 }
1620 wfOut( "Adding rule \"$nr[1]\" to table \"$nr[0]\"\n" );
1621 $wgDatabase->sourceFile( archive( $nr[2] ) );
1622 }
1623
1624 if ( $wgDatabase->hasConstraint( "oldimage_oi_name_fkey_cascaded" ) ) {
1625 wfOut( "... table \"oldimage\" has correct cascading delete/update foreign key to image\n" );
1626 } else {
1627 if ( $wgDatabase->hasConstraint( "oldimage_oi_name_fkey" ) ) {
1628 $wgDatabase->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
1629 }
1630 if ( $wgDatabase->hasConstraint( "oldimage_oi_name_fkey_cascade" ) ) {
1631 $wgDatabase->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey_cascade" );
1632 }
1633 wfOut( "Making foreign key on table \"oldimage\" (to image) a cascade delete/update\n" );
1634 $wgDatabase->query( "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded " .
1635 "FOREIGN KEY (oi_name) REFERENCES image(img_name) ON DELETE CASCADE ON UPDATE CASCADE" );
1636 }
1637
1638 if ( !$wgDatabase->triggerExists( "page", "page_deleted" ) ) {
1639 wfOut( "Adding function and trigger \"page_deleted\" to table \"page\"\n" );
1640 $wgDatabase->sourceFile( archive( 'patch-page_deleted.sql' ) );
1641 } else {
1642 wfOut( "... table \"page\" has \"page_deleted\" trigger\n" );
1643 }
1644
1645 $fi = $wgDatabase->fieldInfo( "recentchanges", "rc_cur_id" );
1646 if ( !$fi->nullable() ) {
1647 wfOut( "Removing NOT NULL constraint from \"recentchanges.rc_cur_id\"\n" );
1648 $wgDatabase->sourceFile( archive( 'patch-rc_cur_id-not-null.sql' ) );
1649 } else {
1650 wfOut( "... column \"recentchanges.rc_cur_id\" has a NOT NULL constraint\n" );
1651 }
1652
1653 $pu = pg_describe_index( "pagelink_unique" );
1654 if ( !is_null( $pu ) && ( $pu[0] != "pl_from" || $pu[1] != "pl_namespace" || $pu[2] != "pl_title" ) ) {
1655 wfOut( "Dropping obsolete version of index \"pagelink_unique index\"\n" );
1656 $wgDatabase->query( "DROP INDEX pagelink_unique" );
1657 $pu = null;
1658 } else {
1659 wfOut( "... obsolete version of index \"pagelink_unique index\" does not exist\n" );
1660 }
1661
1662 if ( is_null( $pu ) ) {
1663 wfOut( "Creating index \"pagelink_unique index\"\n" );
1664 $wgDatabase->query( "CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)" );
1665 } else {
1666 wfOut( "... index \"pagelink_unique_index\" already exists\n" );
1667 }
1668
1669 if ( pg_fkey_deltype( "revision_rev_user_fkey" ) == 'r' ) {
1670 wfOut( "... constraint \"revision_rev_user_fkey\" is ON DELETE RESTRICT\n" );
1671 } else {
1672 wfOut( "Changing constraint \"revision_rev_user_fkey\" to ON DELETE RESTRICT\n" );
1673 $wgDatabase->sourceFile( archive( 'patch-revision_rev_user_fkey.sql' ) );
1674 }
1675
1676 # Fix ipb_address index
1677 if ( pg_index_exists( 'ipblocks', 'ipb_address' ) ) {
1678 wfOut( "Removing deprecated index 'ipb_address'...\n" );
1679 $wgDatabase->query( 'DROP INDEX ipb_address' );
1680 }
1681 if ( pg_index_exists( 'ipblocks', 'ipb_address_unique' ) ) {
1682 wfOut( "... have ipb_address_unique\n" );
1683 } else {
1684 wfOut( "Adding ipb_address_unique index\n" );
1685 $wgDatabase->sourceFile( archive( 'patch-ipb_address_unique.sql' ) );
1686 }
1687
1688 # Fix iwlinks index
1689 if ( pg_index_exists( 'iwlinks', 'iwl_prefix' ) ) {
1690 wfOut( "Replacing index 'iwl_prefix' with 'iwl_prefix_from_title'...\n" );
1691 $wgDatabase->sourceFile( archive( 'patch-rename-iwl_prefix.sql' ) );
1692 }
1693
1694 global $wgExtNewTables, $wgExtPGNewFields, $wgExtPGAlteredFields, $wgExtNewIndexes;
1695 # Add missing extension tables
1696 foreach ( $wgExtNewTables as $nt ) {
1697 if ( $wgDatabase->tableExists( $nt[0] ) ) {
1698 wfOut( "... table \"$nt[0]\" already exists\n" );
1699 continue;
1700 }
1701 wfOut( "Creating table \"$nt[0]\"\n" );
1702 $wgDatabase->sourceFile( $nt[1] );
1703 }
1704 # Add missing extension fields
1705 foreach ( $wgExtPGNewFields as $nc ) {
1706 $fi = $wgDatabase->fieldInfo( $nc[0], $nc[1] );
1707 if ( !is_null( $fi ) ) {
1708 wfOut( "... column \"$nc[0].$nc[1]\" already exists\n" );
1709 continue;
1710 }
1711 wfOut( "Adding column \"$nc[0].$nc[1]\"\n" );
1712 $wgDatabase->query( "ALTER TABLE $nc[0] ADD $nc[1] $nc[2]" );
1713 }
1714 # Change altered columns
1715 foreach ( $wgExtPGAlteredFields as $nc ) {
1716 $fi = $wgDatabase->fieldInfo( $nc[0], $nc[1] );
1717 if ( is_null( $fi ) ) {
1718 wfOut( "WARNING! Column \"$nc[0].$nc[1]\" does not exist but had an alter request! Please report this.\n" );
1719 continue;
1720 }
1721 $oldtype = $fi->type();
1722 $newtype = strtolower( $nc[2] );
1723 if ( $oldtype === $newtype ) {
1724 wfOut( "... column \"$nc[0].$nc[1]\" has correct type of \"$newtype\"\n" );
1725 continue;
1726 }
1727 $command = "ALTER TABLE $nc[0] ALTER $nc[1] TYPE $nc[2]";
1728 if ( isset( $nc[3] ) ) {
1729 $command .= " USING $nc[3]";
1730 }
1731 wfOut( "Altering column \"$nc[0].$nc[1]\" from type \"$oldtype\" to \"$newtype\"\n" );
1732 $wgDatabase->query( $command );
1733 }
1734 # Add missing extension indexes
1735 foreach ( $wgExtNewIndexes as $ni ) {
1736 if ( pg_index_exists( $ni[0], $ni[1] ) ) {
1737 wfOut( "... index \"$ni[1]\" on table \"$ni[0]\" already exists\n" );
1738 continue;
1739 }
1740 wfOut( "Creating index \"$ni[1]\" on table \"$ni[0]\"\n" );
1741 if ( preg_match( '/^\(/', $ni[2] ) ) {
1742 $wgDatabase->query( "CREATE INDEX $ni[1] ON $ni[0] $ni[2]" );
1743 }
1744 else {
1745 $wgDatabase->sourceFile( $ni[2] );
1746 }
1747 }
1748
1749 foreach ( $deferredcols AS $dc ) {
1750 $fi = $wgDatabase->fieldInfo( $dc[0], $dc[1] );
1751 if ( is_null( $fi ) ) {
1752 wfOut( "WARNING! Column \"$dc[0].$dc[1]\" does not exist but it should! Please report this.\n" );
1753 continue;
1754 }
1755 if ( $fi->is_deferred() && $fi->is_deferrable() ) {
1756 continue;
1757 }
1758 wfOut( "Altering column \"$dc[0].$dc[1]\" to be DEFERRABLE INITIALLY DEFERRED\n" );
1759 $conname = $fi->conname();
1760 $clause = $dc[2];
1761 $command = "ALTER TABLE $dc[0] DROP CONSTRAINT $conname";
1762 $wgDatabase->query( $command );
1763 $command = "ALTER TABLE $dc[0] ADD CONSTRAINT $conname FOREIGN KEY ($dc[1]) REFERENCES $clause DEFERRABLE INITIALLY DEFERRED";
1764 $wgDatabase->query( $command );
1765 }
1766
1767 # Tweak the page_title tsearch2 trigger to filter out slashes
1768 # This is create or replace, so harmless to call if not needed
1769 $wgDatabase->sourceFile( archive( 'patch-ts2pagetitle.sql' ) );
1770
1771 # # If the server is 8.3 or higher, rewrite the tsearch2 triggers
1772 # # in case they have the old 'default' versions
1773 if ( $numver >= 8.3 ) {
1774 $wgDatabase->sourceFile( archive( 'patch-tsearch2funcs.sql' ) );
1775 }
1776 return;
1777 }