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