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