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