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