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