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