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