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