use int4 instead of integer for image/oldimage type changes
[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
17 $wgRenamedTables = array(
18 # from to patch file
19 # array( 'group', 'groups', 'patch-rename-group.sql' ),
20 );
21
22 $wgNewTables = array(
23 # table patch file (in maintenance/archives)
24 array( 'hitcounter', 'patch-hitcounter.sql' ),
25 array( 'querycache', 'patch-querycache.sql' ),
26 array( 'objectcache', 'patch-objectcache.sql' ),
27 array( 'categorylinks', 'patch-categorylinks.sql' ),
28 array( 'logging', 'patch-logging.sql' ),
29 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
30 array( 'transcache', 'patch-transcache.sql' ),
31 array( 'trackbacks', 'patch-trackbacks.sql' ),
32 array( 'externallinks', 'patch-externallinks.sql' ),
33 array( 'job', 'patch-job.sql' ),
34 array( 'langlinks', 'patch-langlinks.sql' ),
35 array( 'querycache_info', 'patch-querycacheinfo.sql' ),
36 array( 'filearchive', 'patch-filearchive.sql' ),
37 array( 'querycachetwo', 'patch-querycachetwo.sql' ),
38 );
39
40 $wgNewFields = array(
41 # table field patch file (in maintenance/archives)
42 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
43 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
44 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
45 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
46 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
47 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
48 array( 'recentchanges', 'rc_old_len', 'patch-rc_len.sql' ),
49 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
50 array( 'user', 'user_token', 'patch-user_token.sql' ),
51 array( 'user', 'user_email_token', 'patch-user_email_token.sql' ),
52 array( 'user', 'user_registration','patch-user_registration.sql' ),
53 array( 'logging', 'log_params', 'patch-log_params.sql' ),
54 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
55 array( 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
56 array( 'page', 'page_len', 'patch-page_len.sql' ),
57 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
58 array( 'image', 'img_width', 'patch-img_width.sql' ),
59 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
60 array( 'image', 'img_media_type', 'patch-img_media_type.sql' ),
61 array( 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ),
62 array( 'interwiki', 'iw_trans', 'patch-interwiki-trans.sql' ),
63 array( 'ipblocks', 'ipb_range_start', 'patch-ipb_range_start.sql' ),
64 array( 'site_stats', 'ss_images', 'patch-ss_images.sql' ),
65 array( 'ipblocks', 'ipb_anon_only', 'patch-ipb_anon_only.sql' ),
66 array( 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
67 array( 'user', 'user_newpass_time','patch-user_newpass_time.sql' ),
68 array( 'user', 'user_editcount', 'patch-user_editcount.sql' ),
69 array( 'logging', 'log_id', 'patch-log_id.sql' ),
70 array( 'page_restrictions', 'pr_id', 'patch-page_restrictions_sortkey.sql' ),
71 array( 'revision', 'rev_len', 'patch-rev_len.sql' ),
72 );
73
74 function rename_table( $from, $to, $patch ) {
75 global $wgDatabase;
76 if ( $wgDatabase->tableExists( $from ) ) {
77 if ( $wgDatabase->tableExists( $to ) ) {
78 echo "...can't move table $from to $to, $to already exists.\n";
79 } else {
80 echo "Moving table $from to $to...";
81 dbsource( archive($patch), $wgDatabase );
82 echo "ok\n";
83 }
84 } else {
85 // Source table does not exist
86 // Renames are done before creations, so this is typical for a new installation
87 // Ignore silently
88 }
89 }
90
91 function add_table( $name, $patch ) {
92 global $wgDatabase;
93 if ( $wgDatabase->tableExists( $name ) ) {
94 echo "...$name table already exists.\n";
95 } else {
96 echo "Creating $name table...";
97 dbsource( archive($patch), $wgDatabase );
98 echo "ok\n";
99 }
100 }
101
102 function add_field( $table, $field, $patch ) {
103 global $wgDatabase;
104 if ( !$wgDatabase->tableExists( $table ) ) {
105 echo "...$table table does not exist, skipping new field patch\n";
106 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
107 echo "...have $field field in $table table.\n";
108 } else {
109 echo "Adding $field field to table $table...";
110 dbsource( archive($patch) , $wgDatabase );
111 echo "ok\n";
112 }
113 }
114
115 function do_revision_updates() {
116 global $wgSoftwareRevision;
117 if ( $wgSoftwareRevision < 1001 ) {
118 update_passwords();
119 }
120 }
121
122 function update_passwords() {
123 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
124
125 global $wgDatabase;
126 $fname = "Update script: update_passwords()";
127 print "\nIt appears that you need to update the user passwords in your\n" .
128 "database. If you have already done this (if you've run this update\n" .
129 "script once before, for example), doing so again will make all your\n" .
130 "user accounts inaccessible, so be sure you only do this once.\n" .
131 "Update user passwords? (yes/no)";
132
133 $resp = readconsole();
134 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
135
136 $sql = "SELECT user_id,user_password FROM user";
137 $source = $wgDatabase->query( $sql, $fname );
138
139 while ( $row = $wgDatabase->fetchObject( $source ) ) {
140 $id = $row->user_id;
141 $oldpass = $row->user_password;
142 $newpass = md5( "{$id}-{$oldpass}" );
143
144 $sql = "UPDATE user SET user_password='{$newpass}' " .
145 "WHERE user_id={$id}";
146 $wgDatabase->query( $sql, $fname );
147 }
148 }
149
150 function do_interwiki_update() {
151 # Check that interwiki table exists; if it doesn't source it
152 global $wgDatabase, $IP;
153 if( $wgDatabase->tableExists( "interwiki" ) ) {
154 echo "...already have interwiki table\n";
155 return true;
156 }
157 echo "Creating interwiki table: ";
158 dbsource( archive("patch-interwiki.sql") );
159 echo "ok\n";
160 echo "Adding default interwiki definitions: ";
161 dbsource( "$IP/maintenance/interwiki.sql" );
162 echo "ok\n";
163 }
164
165 function do_index_update() {
166 # Check that proper indexes are in place
167 global $wgDatabase;
168 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
169 if( $meta->multiple_key == 0 ) {
170 echo "Updating indexes to 20031107: ";
171 dbsource( archive("patch-indexes.sql") );
172 echo "ok\n";
173 return true;
174 }
175 echo "...indexes seem up to 20031107 standards\n";
176 return false;
177 }
178
179 function do_image_index_update() {
180 global $wgDatabase;
181
182 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
183 if( $meta->multiple_key == 0 ) {
184 echo "Updating indexes to 20050912: ";
185 dbsource( archive("patch-mimesearch-indexes.sql") );
186 echo "ok\n";
187 return true;
188 }
189 echo "...indexes seem up to 20050912 standards\n";
190 return false;
191 }
192
193 function do_image_name_unique_update() {
194 global $wgDatabase;
195 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
196 echo "...image primary key already set.\n";
197 } else {
198 echo "Making img_name the primary key... ";
199 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
200 echo "ok\n";
201 }
202 }
203
204 function do_logging_timestamp_index() {
205 global $wgDatabase;
206 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
207 echo "...timestamp key on logging already exists.\n";
208 } else {
209 echo "Adding timestamp key on logging table... ";
210 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
211 echo "ok\n";
212 }
213 }
214
215
216 function do_watchlist_update() {
217 global $wgDatabase;
218 $fname = 'do_watchlist_update';
219 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
220 echo "The watchlist table is already set up for email notification.\n";
221 } else {
222 echo "Adding wl_notificationtimestamp field for email notification management.";
223 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
224 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
225 echo "ok\n";
226 }
227 # Check if we need to add talk page rows to the watchlist
228 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
229 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
230 if ( $talk != $nontalk ) {
231 echo "Adding missing watchlist talk page rows... ";
232 flush();
233
234 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
235 array(
236 'wl_user' => 'wl_user',
237 'wl_namespace' => 'wl_namespace | 1',
238 'wl_title' => 'wl_title',
239 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
240 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
241 echo "ok\n";
242 } else {
243 echo "...watchlist talk page rows already present\n";
244 }
245 }
246
247 function do_copy_newtalk_to_watchlist() {
248 global $wgDatabase;
249 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
250
251 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
252 $wgDatabase->tableName( 'user_newtalk' ) );
253 $num_newtalks=$wgDatabase->numRows($res);
254 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
255
256 $user = new User();
257 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
258 $wluser = $wgDatabase->fetchObject( $res );
259 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
260 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
261 $wgDatabase->replace( 'watchlist',
262 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
263 array('wl_user' => 0,
264 'wl_namespace' => NS_USER_TALK,
265 'wl_title' => $wluser->user_ip,
266 'wl_notificationtimestamp' => '19700101000000'
267 ), 'updaters.inc::do_watchlist_update2'
268 );
269 }
270 } else { # normal users ... have user_ids
271 $user->setID($wluser->user_id);
272 $wgDatabase->replace( 'watchlist',
273 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
274 array('wl_user' => $user->getID(),
275 'wl_namespace' => NS_USER_TALK,
276 'wl_title' => $user->getName(),
277 'wl_notificationtimestamp' => '19700101000000'
278 ), 'updaters.inc::do_watchlist_update3'
279 );
280 }
281 }
282 echo "Done.\n";
283 }
284
285
286 function do_user_update() {
287 global $wgDatabase;
288 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
289 echo "User table contains old email authentication field. Dropping... ";
290 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
291 echo "ok\n";
292 } else {
293 echo "...user table does not contain old email authentication field.\n";
294 }
295 }
296
297 /**
298 * 1.4 betas were missing the 'binary' marker from logging.log_title,
299 * which causes a collation mismatch error on joins in MySQL 4.1.
300 */
301 function do_logging_encoding() {
302 global $wgDatabase, $wgDBtype;
303 if ($wgDBtype != 'mysql')
304 return;
305 $logging = $wgDatabase->tableName( 'logging' );
306 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
307 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
308 $wgDatabase->freeResult( $res );
309
310 if( in_array( 'binary', $flags ) ) {
311 echo "Logging table has correct title encoding.\n";
312 } else {
313 echo "Fixing title encoding on logging table... ";
314 dbsource( archive( 'patch-logging-title.sql' ), $wgDatabase );
315 echo "ok\n";
316 }
317 }
318
319 function do_schema_restructuring() {
320 global $wgDatabase;
321 $fname="do_schema_restructuring";
322 if ( $wgDatabase->tableExists( 'page' ) ) {
323 echo "...page table already exists.\n";
324 } else {
325 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
326 echo wfTimestamp( TS_DB );
327 echo "......checking for duplicate entries.\n"; flush();
328
329 list ($cur, $old, $page, $revision, $text) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
330
331 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
332 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
333
334 if ( $wgDatabase->numRows( $rows ) > 0 ) {
335 echo wfTimestamp( TS_DB );
336 echo "......<b>Found duplicate entries</b>\n";
337 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
338 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
339 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
340 $duplicate[$row->cur_namespace] = array();
341 }
342 $duplicate[$row->cur_namespace][] = $row->cur_title;
343 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
344 }
345 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
346 $firstCond = true;
347 foreach ( $duplicate as $ns => $titles ) {
348 if ( $firstCond ) {
349 $firstCond = false;
350 } else {
351 $sql .= ' OR ';
352 }
353 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
354 $first = true;
355 foreach ( $titles as $t ) {
356 if ( $first ) {
357 $sql .= $wgDatabase->addQuotes( $t );
358 $first = false;
359 } else {
360 $sql .= ', ' . $wgDatabase->addQuotes( $t );
361 }
362 }
363 $sql .= ") ) \n";
364 }
365 # By sorting descending, the most recent entry will be the first in the list.
366 # All following entries will be deleted by the next while-loop.
367 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
368
369 $rows = $wgDatabase->query( $sql, $fname );
370
371 $prev_title = $prev_namespace = false;
372 $deleteId = array();
373
374 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
375 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
376 $deleteId[] = $row->cur_id;
377 }
378 $prev_title = $row->cur_title;
379 $prev_namespace = $row->cur_namespace;
380 }
381 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
382 $rows = $wgDatabase->query( $sql, $fname );
383 echo wfTimestamp( TS_DB );
384 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
385 }
386
387
388 echo wfTimestamp( TS_DB );
389 echo "......Creating tables.\n";
390 $wgDatabase->query("CREATE TABLE $page (
391 page_id int(8) unsigned NOT NULL auto_increment,
392 page_namespace int NOT NULL,
393 page_title varchar(255) binary NOT NULL,
394 page_restrictions tinyblob NOT NULL,
395 page_counter bigint(20) unsigned NOT NULL default '0',
396 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
397 page_is_new tinyint(1) unsigned NOT NULL default '0',
398 page_random real unsigned NOT NULL,
399 page_touched char(14) binary NOT NULL default '',
400 page_latest int(8) unsigned NOT NULL,
401 page_len int(8) unsigned NOT NULL,
402
403 PRIMARY KEY page_id (page_id),
404 UNIQUE INDEX name_title (page_namespace,page_title),
405 INDEX (page_random),
406 INDEX (page_len)
407 ) TYPE=InnoDB", $fname );
408 $wgDatabase->query("CREATE TABLE $revision (
409 rev_id int(8) unsigned NOT NULL auto_increment,
410 rev_page int(8) unsigned NOT NULL,
411 rev_comment tinyblob NOT NULL,
412 rev_user int(5) unsigned NOT NULL default '0',
413 rev_user_text varchar(255) binary NOT NULL default '',
414 rev_timestamp char(14) binary NOT NULL default '',
415 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
416 rev_deleted tinyint(1) unsigned NOT NULL default '0',
417
418 PRIMARY KEY rev_page_id (rev_page, rev_id),
419 UNIQUE INDEX rev_id (rev_id),
420 INDEX rev_timestamp (rev_timestamp),
421 INDEX page_timestamp (rev_page,rev_timestamp),
422 INDEX user_timestamp (rev_user,rev_timestamp),
423 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
424 ) TYPE=InnoDB", $fname );
425
426 echo wfTimestamp( TS_DB );
427 echo "......Locking tables.\n";
428 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
429
430 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
431 echo wfTimestamp( TS_DB );
432 echo "......maxold is {$maxold}\n";
433
434 echo wfTimestamp( TS_DB );
435 global $wgLegacySchemaConversion;
436 if( $wgLegacySchemaConversion ) {
437 // Create HistoryBlobCurStub entries.
438 // Text will be pulled from the leftover 'cur' table at runtime.
439 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
440 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
441 $cur_flags = "'object'";
442 } else {
443 // Copy all cur text in immediately: this may take longer but avoids
444 // having to keep an extra table around.
445 echo "......Moving text from cur.\n";
446 $cur_text = 'cur_text';
447 $cur_flags = "''";
448 }
449 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
450 old_timestamp, old_minor_edit, old_flags)
451 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
452 FROM $cur", $fname );
453
454 echo wfTimestamp( TS_DB );
455 echo "......Setting up revision table.\n";
456 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
457 rev_minor_edit)
458 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
459 old_timestamp, old_minor_edit
460 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
461
462 echo wfTimestamp( TS_DB );
463 echo "......Setting up page table.\n";
464 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
465 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
466 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
467 cur_random, cur_touched, rev_id, LENGTH(cur_text)
468 FROM $cur,$revision
469 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
470
471 echo wfTimestamp( TS_DB );
472 echo "......Unlocking tables.\n";
473 $wgDatabase->query( "UNLOCK TABLES", $fname );
474
475 echo wfTimestamp( TS_DB );
476 echo "......Renaming old.\n";
477 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
478
479 echo wfTimestamp( TS_DB );
480 echo "...done.\n";
481 }
482 }
483
484 function do_inverse_timestamp() {
485 global $wgDatabase;
486 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
487 echo "Removing revision.inverse_timestamp and fixing indexes... ";
488 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
489 echo "ok\n";
490 } else {
491 echo "revision timestamp indexes already up to 2005-03-13\n";
492 }
493 }
494
495 function do_text_id() {
496 global $wgDatabase;
497 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
498 echo "...rev_text_id already in place.\n";
499 } else {
500 echo "Adding rev_text_id field... ";
501 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
502 echo "ok\n";
503 }
504 }
505
506 function do_namespace_size() {
507 $tables = array(
508 'page' => 'page',
509 'archive' => 'ar',
510 'recentchanges' => 'rc',
511 'watchlist' => 'wl',
512 'querycache' => 'qc',
513 'logging' => 'log',
514 );
515 foreach( $tables as $table => $prefix ) {
516 do_namespace_size_on( $table, $prefix );
517 flush();
518 }
519 }
520
521 function do_namespace_size_on( $table, $prefix ) {
522 global $wgDatabase, $wgDBtype;
523 if ($wgDBtype != 'mysql')
524 return;
525 $field = $prefix . '_namespace';
526
527 $tablename = $wgDatabase->tableName( $table );
528 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
529 $info = $wgDatabase->fetchObject( $result );
530 $wgDatabase->freeResult( $result );
531
532 if( substr( $info->Type, 0, 3 ) == 'int' ) {
533 echo "...$field is already a full int ($info->Type).\n";
534 } else {
535 echo "Promoting $field from $info->Type to int... ";
536
537 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
538 $wgDatabase->query( $sql );
539
540 echo "ok\n";
541 }
542 }
543
544 function do_pagelinks_update() {
545 global $wgDatabase;
546 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
547 echo "...already have pagelinks table.\n";
548 } else {
549 echo "Converting links and brokenlinks tables to pagelinks... ";
550 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
551 echo "ok\n";
552 flush();
553
554 global $wgCanonicalNamespaceNames;
555 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
556 if( $ns != 0 ) {
557 do_pagelinks_namespace( $ns );
558 }
559 }
560 }
561 }
562
563 function do_pagelinks_namespace( $namespace ) {
564 global $wgDatabase, $wgContLang;
565
566 $ns = intval( $namespace );
567 echo "Cleaning up broken links for namespace $ns... ";
568
569 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
570 $name = $wgContLang->getNsText( $ns );
571 $prefix = $wgDatabase->strencode( $name );
572 $likeprefix = str_replace( '_', '\\_', $prefix);
573
574 $sql = "UPDATE $pagelinks
575 SET pl_namespace=$ns,
576 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
577 WHERE pl_namespace=0
578 AND pl_title LIKE '$likeprefix:%'";
579
580 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
581 echo "ok\n";
582 }
583
584 function do_drop_img_type() {
585 global $wgDatabase;
586
587 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
588 echo "Dropping unused img_type field in image table... ";
589 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
590 echo "ok\n";
591 } else {
592 echo "No img_type field in image table; Good.\n";
593 }
594 }
595
596 function do_old_links_update() {
597 global $wgDatabase;
598 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
599 echo "Already have pagelinks; skipping old links table updates.\n";
600 } else {
601 convertLinks(); flush();
602 }
603 }
604
605 function do_user_unique_update() {
606 global $wgDatabase;
607 $duper = new UserDupes( $wgDatabase );
608 if( $duper->hasUniqueIndex() ) {
609 echo "Already have unique user_name index.\n";
610 } else {
611 if( !$duper->clearDupes() ) {
612 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
613 }
614 echo "Adding unique index on user_name... ";
615 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
616 echo "ok\n";
617 }
618 }
619
620 function do_user_groups_update() {
621 $fname = 'do_user_groups_update';
622 global $wgDatabase;
623
624 if( $wgDatabase->tableExists( 'user_groups' ) ) {
625 echo "...user_groups table already exists.\n";
626 return do_user_groups_reformat();
627 }
628
629 echo "Adding user_groups table... ";
630 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
631 echo "ok\n";
632
633 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
634 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
635 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
636 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
637 echo "ok\n";
638 } else {
639 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
640 echo "*** You may need to manually configure some sysops by manipulating\n";
641 echo "*** the user_groups table.\n";
642 return;
643 }
644 }
645
646 echo "Converting user_rights table to user_groups... ";
647 $result = $wgDatabase->select( 'user_rights',
648 array( 'ur_user', 'ur_rights' ),
649 array( "ur_rights != ''" ),
650 $fname );
651
652 while( $row = $wgDatabase->fetchObject( $result ) ) {
653 $groups = array_unique(
654 array_map( 'trim',
655 explode( ',', $row->ur_rights ) ) );
656
657 foreach( $groups as $group ) {
658 $wgDatabase->insert( 'user_groups',
659 array(
660 'ug_user' => $row->ur_user,
661 'ug_group' => $group ),
662 $fname );
663 }
664 }
665 $wgDatabase->freeResult( $result );
666 echo "ok\n";
667 }
668
669 function do_user_groups_reformat() {
670 # Check for bogus formats from previous 1.5 alpha code.
671 global $wgDatabase;
672 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
673
674 if( $info->type == 'int' ) {
675 $oldug = $wgDatabase->tableName( 'user_groups' );
676 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
677 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
678 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
679 echo "ok\n";
680
681 echo "Re-adding fresh user_groups table... ";
682 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
683 echo "ok\n";
684
685 echo "***\n";
686 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
687 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
688 echo "***\n";
689 } else {
690 echo "...user_groups is in current format.\n";
691 }
692
693 }
694
695 function do_watchlist_null() {
696 # Make sure wl_notificationtimestamp can be NULL,
697 # and update old broken items.
698 global $wgDatabase;
699 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
700
701 if( $info->not_null ) {
702 echo "Making wl_notificationtimestamp nullable... ";
703 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
704 echo "ok\n";
705 } else {
706 echo "...wl_notificationtimestamp is already nullable.\n";
707 }
708
709 }
710
711 /**
712 * @bug 3946
713 */
714 function do_page_random_update() {
715 global $wgDatabase;
716
717 echo "Setting page_random to a random value on rows where it equals 0...";
718
719 $page = $wgDatabase->tableName( 'page' );
720 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
721 $rows = $wgDatabase->affectedRows();
722
723 echo "changed $rows rows\n";
724 }
725
726 function do_templatelinks_update() {
727 global $wgDatabase, $wgLoadBalancer;
728 $fname = 'do_templatelinks_update';
729
730 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
731 echo "...templatelinks table already exists\n";
732 return;
733 }
734 echo "Creating templatelinks table...\n";
735 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
736 echo "Populating...\n";
737 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
738 // Slow, replication-friendly update
739 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
740 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
741 $count = 0;
742 while ( $row = $wgDatabase->fetchObject( $res ) ) {
743 $count = ($count + 1) % 100;
744 if ( $count == 0 ) {
745 if ( function_exists( 'wfWaitForSlaves' ) ) {
746 wfWaitForSlaves( 10 );
747 } else {
748 sleep( 1 );
749 }
750 }
751 $wgDatabase->insert( 'templatelinks',
752 array(
753 'tl_from' => $row->pl_from,
754 'tl_namespace' => $row->pl_namespace,
755 'tl_title' => $row->pl_title,
756 ), $fname
757 );
758
759 }
760 $wgDatabase->freeResult( $res );
761 } else {
762 // Fast update
763 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
764 array(
765 'tl_from' => 'pl_from',
766 'tl_namespace' => 'pl_namespace',
767 'tl_title' => 'pl_title'
768 ), array(
769 'pl_namespace' => 10
770 ), $fname
771 );
772 }
773 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
774 }
775
776 # July 2006
777 # Add ( rc_namespace, rc_user_text ) index [R. Church]
778 function do_rc_indices_update() {
779 global $wgDatabase;
780 echo( "Checking for additional recent changes indices...\n" );
781 # See if we can find the index we want
782 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_ns_usertext', __METHOD__ );
783 if( !$info ) {
784 # None, so create
785 echo( "...index on ( rc_namespace, rc_user_text ) not found; creating\n" );
786 dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
787 } else {
788 # Index seems to exist
789 echo( "...index on ( rc_namespace, rc_user_text ) seems to be ok\n" );
790 }
791
792 #Add (rc_user_text, rc_timestamp) index [A. Garrett], November 2006
793 # See if we can find the index we want
794 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_user_text', __METHOD__ );
795 if( !$info ) {
796 # None, so create
797 echo( "...index on ( rc_user_text, rc_timestamp ) not found; creating\n" );
798 dbsource( archive( 'patch-rc_user_text-index.sql' ) );
799 } else {
800 # Index seems to exist
801 echo( "...index on ( rc_user_text, rc_timestamp ) seems to be ok\n" );
802 }
803 }
804
805 function index_has_field($table, $index, $field) {
806 global $wgDatabase;
807 echo( "Checking if $table index $index includes field $field...\n" );
808 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
809 if( $info ) {
810 foreach($info as $row) {
811 if($row->Column_name == $field) {
812 echo( "...index $index on table $table seems to be ok\n" );
813 return true;
814 }
815 }
816 }
817 echo( "...index $index on table $table has no field $field; adding\n" );
818 return false;
819 }
820
821 function do_backlinking_indices_update() {
822 echo( "Checking for backlinking indices...\n" );
823 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
824 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
825 !index_has_field('imagelinks', 'il_to', 'il_from'))
826 {
827 dbsource( archive( 'patch-backlinkindexes.sql' ) );
828 }
829 }
830
831 function do_stats_init() {
832 // Sometimes site_stats table is not properly populated.
833 global $wgDatabase;
834 echo "Checking site_stats row...";
835 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
836 if( $row === false ) {
837 echo "data is missing! rebuilding...\n";
838
839 global $IP;
840 require_once "$IP/maintenance/initStats.inc";
841 wfInitStats();
842 } else {
843 echo "ok.\n";
844 }
845 }
846
847 function purge_cache() {
848 global $wgDatabase;
849 # We can't guarantee that the user will be able to use TRUNCATE,
850 # but we know that DELETE is available to us
851 echo( "Purging caches..." );
852 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
853 echo( "done.\n" );
854 }
855
856 function do_all_updates( $shared = false, $purge = true ) {
857 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
858
859 $doUser = !$wgSharedDB || $shared;
860
861 if ($wgDBtype === 'postgres') {
862 do_postgres_updates();
863 return;
864 }
865
866 # Rename tables
867 foreach ( $wgRenamedTables as $tableRecord ) {
868 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
869 }
870
871 # Add missing tables
872 foreach ( $wgNewTables as $tableRecord ) {
873 add_table( $tableRecord[0], $tableRecord[1] );
874 flush();
875 }
876
877 # Add missing fields
878 foreach ( $wgNewFields as $fieldRecord ) {
879 if ( $fieldRecord[0] != 'user' || $doUser ) {
880 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
881 }
882 flush();
883 }
884
885 # Do schema updates which require special handling
886 do_interwiki_update(); flush();
887 do_index_update(); flush();
888 do_old_links_update(); flush();
889 do_image_name_unique_update(); flush();
890 do_watchlist_update(); flush();
891 if ( $doUser ) {
892 do_user_update(); flush();
893 }
894 ###### do_copy_newtalk_to_watchlist(); flush();
895 do_logging_encoding(); flush();
896
897 do_schema_restructuring(); flush();
898 do_inverse_timestamp(); flush();
899 do_text_id(); flush();
900 do_namespace_size(); flush();
901
902 do_pagelinks_update(); flush();
903 do_templatelinks_update(); flush(); // after pagelinks
904
905 do_drop_img_type(); flush();
906
907 if ( $doUser ) {
908 do_user_unique_update(); flush();
909 }
910 do_user_groups_update(); flush();
911
912 do_watchlist_null(); flush();
913
914 //do_image_index_update(); flush();
915
916 do_logging_timestamp_index(); flush();
917
918 do_page_random_update(); flush();
919
920 do_rc_indices_update(); flush();
921
922 add_table( 'redirect', 'patch-redirect.sql' );
923
924 do_backlinking_indices_update(); flush();
925
926 do_restrictions_update(); flush ();
927
928 echo "Deleting old default messages (this may take a long time!)..."; flush();
929 deleteDefaultMessages();
930 echo "Done\n"; flush();
931
932 do_stats_init(); flush();
933
934 if( $purge ) {
935 purge_cache();
936 flush();
937 }
938 }
939
940 function archive($name) {
941 global $wgDBtype, $IP;
942 switch ($wgDBtype) {
943 case "postgres":
944 return "$IP/maintenance/postgres/archives/$name";
945 default:
946 return "$IP/maintenance/archives/$name";
947 }
948 }
949
950 function do_restrictions_update() {
951 # Adding page_restrictions table, obsoleting page.page_restrictions.
952 # Migrating old restrictions to new table
953 # -- Andrew Garrett, January 2007.
954
955 global $wgDatabase;
956
957 $name = 'page_restrictions';
958 $patch = 'patch-page_restrictions.sql';
959
960 if ( $wgDatabase->tableExists( $name ) ) {
961 echo "...$name table already exists.\n";
962 } else {
963 echo "Creating $name table...";
964 dbsource( archive($patch), $wgDatabase );
965 echo "ok\n";
966
967 echo "Migrating old restrictions to new table...";
968
969 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array("page_restrictions!=''", "page_restrictions!='edit=:move='"), __METHOD__ );
970
971 $count = 0;
972
973 while ($row = $wgDatabase->fetchObject($res) ) {
974 $count = ($count + 1) % 100;
975
976 if ($count == 0) {
977 if ( function_exists( 'wfWaitForSlaves' ) ) {
978 wfWaitForSlaves( 10 );
979 } else {
980 sleep( 1 );
981 }
982 }
983
984 # Figure out what the restrictions are..
985 $id = $row->page_id;
986 $flatrestrictions = explode( ':', $row->page_restrictions );
987
988 $restrictions = array ();
989 foreach( $flatrestrictions as $restriction ) {
990 $thisrestriction = explode( '=', $restriction, 2 );
991 if( count( $thisrestriction ) == 1 ) {
992 // Compatibility with old protections from before
993 // separate move protection was added.
994 list( $level ) = $thisrestriction;
995 if( $level ) {
996 $restrictions['edit'] = $level;
997 $restrictions['move'] = $level;
998 }
999 } else {
1000 list( $type, $level ) = $thisrestriction;
1001 if( $level ) {
1002 $restrictions[$type] = $level;
1003 }
1004 }
1005
1006 $wgDatabase->update( 'page', array ( 'page_restrictions' => ''), array( 'page_id' => $id ), __METHOD__ );
1007
1008 }
1009
1010 foreach( $restrictions as $type => $level ) {
1011 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
1012 'pr_type' => $type,
1013 'pr_level' => $level,
1014 'pr_cascade' => 0 ),
1015 __METHOD__ );
1016 }
1017 }
1018 print "ok\n";
1019 }
1020
1021 }
1022
1023 function
1024 pg_column_has_type($table, $column, $wanttype)
1025 {
1026 global $wgDatabase, $wgDBname, $wgDBmwschema;
1027
1028 $q = <<<END
1029 SELECT typname FROM pg_class, pg_namespace, pg_attribute, pg_type
1030 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1031 AND attrelid=pg_class.oid AND atttypid=pg_type.oid
1032 AND nspname=%s AND relname=%s AND attname=%s;
1033 END;
1034 $res = $wgDatabase->query(sprintf($q,
1035 $wgDatabase->addQuotes($wgDBmwschema),
1036 $wgDatabase->addQuotes($table),
1037 $wgDatabase->addQuotes($column)));
1038 $row = $wgDatabase->fetchRow($res);
1039 $istype = false;
1040 if ($row)
1041 $istype = $row[0] === $wanttype;
1042 $wgDatabase->freeResult($res);
1043 return $istype;
1044 }
1045
1046 function
1047 pg_column_exists($table, $column)
1048 {
1049 global $wgDatabase, $wgDBname, $wgDBmwschema;
1050
1051 $q = <<<END
1052 SELECT 1 FROM pg_class, pg_namespace, pg_attribute
1053 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1054 AND attrelid=pg_class.oid
1055 AND nspname=%s AND relname=%s AND attname=%s;
1056 END;
1057 $res = $wgDatabase->query(sprintf($q,
1058 $wgDatabase->addQuotes($wgDBmwschema),
1059 $wgDatabase->addQuotes($table),
1060 $wgDatabase->addQuotes($column)));
1061 $row = $wgDatabase->fetchRow($res);
1062 $exists = !!$row;
1063 $wgDatabase->freeResult($res);
1064 return $exists;
1065 }
1066
1067 function
1068 pg_column_is_nullable($table, $column)
1069 {
1070 global $wgDatabase, $wgDBname, $wgDBmwschema;
1071
1072 $q = <<<END
1073 SELECT attnotnull FROM pg_class, pg_namespace, pg_attribute
1074 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1075 AND attrelid=pg_class.oid
1076 AND nspname=%s AND relname=%s AND attname=%s;
1077 END;
1078 $res = $wgDatabase->query(sprintf($q,
1079 $wgDatabase->addQuotes($wgDBmwschema),
1080 $wgDatabase->addQuotes($table),
1081 $wgDatabase->addQuotes($column)));
1082 $row = $wgDatabase->fetchRow($res);
1083 $nullable = ($row[0] === 'f');
1084 $wgDatabase->freeResult($res);
1085 return $nullable;
1086 }
1087
1088 function
1089 pg_table_exists($table)
1090 {
1091 global $wgDatabase, $wgDBname, $wgDBmwschema;
1092
1093 $q = <<<END
1094 SELECT 1 FROM pg_class, pg_namespace
1095 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1096 AND nspname=%s AND relname=%s
1097 END;
1098 $res = $wgDatabase->query(sprintf($q,
1099 $wgDatabase->addQuotes($wgDBmwschema),
1100 $wgDatabase->addQuotes($table)));
1101 $row = $wgDatabase->fetchRow($res);
1102 $exists = !!$row;
1103 $wgDatabase->freeResult($res);
1104 return $exists;
1105 }
1106
1107 function
1108 pg_trigger_exists($table, $trigger)
1109 {
1110 global $wgDatabase, $wgDBname, $wgDBmwschema;
1111
1112 $q = <<<END
1113 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1114 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1115 AND tgrelid=pg_class.oid
1116 AND nspname=%s AND relname=%s AND tgname=%s
1117 END;
1118 $res = $wgDatabase->query(sprintf($q,
1119 $wgDatabase->addQuotes($wgDBmwschema),
1120 $wgDatabase->addQuotes($table),
1121 $wgDatabase->addQuotes($trigger)));
1122 $row = $wgDatabase->fetchRow($res);
1123 $exists = !!$row;
1124 $wgDatabase->freeResult($res);
1125 return $exists;
1126 }
1127
1128
1129 function
1130 pg_index_exists($table, $index)
1131 {
1132 global $wgDatabase, $wgDBmwschema;
1133 $exists = $wgDatabase->selectField("pg_indexes", "indexname",
1134 array( "indexname" => $index,
1135 "tablename" => $table,
1136 "schemaname" => $wgDBmwschema));
1137 return $exists === $index;
1138 }
1139
1140 function
1141 pg_rule_exists($table, $rule)
1142 {
1143 global $wgDatabase, $wgDBmwschema;
1144 $exists = $wgDatabase->selectField("pg_rules", "rulename",
1145 array( "rulename" => $rule,
1146 "tablename" => $table,
1147 "schemaname" => $wgDBmwschema));
1148 return $exists === $rule;
1149 }
1150
1151 function do_postgres_updates() {
1152 global $wgDatabase, $wgVersion, $wgDBmwschema;
1153
1154 # Just in case their LocalSetings.php does not have this:
1155 if ( !isset( $wgDBmwschema ))
1156 $wgDBmwschema = 'mediawiki';
1157
1158 ## Default to the oldest supported version
1159 $version = 1.7;
1160
1161 $thisver = array();
1162 if ($wgDatabase->tableExists("mediawiki_version")) {
1163 $version = "1.8";
1164 $sql = "SELECT mw_version FROM mediawiki_version ORDER BY cdate DESC LIMIT 1";
1165 $tempversion = pg_fetch_result($wgDatabase->doQuery($sql),0,0);
1166 if (preg_match('/(\d+\.\d+)/', $tempversion, $thisver)) {
1167 $version = $thisver[1];
1168 }
1169 }
1170
1171 print " Detected version: $version\n";
1172
1173 ## Transform to a standard format
1174 if (! preg_match('/(\d+)\.(\d+)/', $version, $thisver)) {
1175 die ("Sorry, could not figure out what version \"$version\" means");
1176 }
1177 $oldversion = $version;
1178 $version = sprintf("%d%03d",$thisver[1],$thisver[2]);
1179 print " Standardized version: $version\n";
1180
1181 $upgrade = '';
1182
1183 $typechanges = array(
1184 array("oldimage", "oi_size", "int4"),
1185 array("oldimage", "oi_width", "int4"),
1186 array("oldimage", "oi_height", "int4"),
1187 array("image", "img_size", "int4"),
1188 array("image", "img_width", "int4"),
1189 array("image", "img_height", "int4"),
1190 );
1191
1192 $newcols = array(
1193 array("mwuser", "user_newpass_time", "TIMESTAMPTZ"),
1194 array("mwuser", "user_editcount", "INTEGER"),
1195 array("ipblocks", "ipb_anon_only", "CHAR NOT NULL DEFAULT '0'"),
1196 array("ipblocks", "ipb_create_account", "CHAR NOT NULL DEFAULT '1'"),
1197 array("ipblocks", "ipb_enable_autoblock", "CHAR NOT NULL DEFAULT '1'"),
1198 array("recentchanges", "rc_old_len", "INT"),
1199 array("recentchanges", "rc_new_len", "INT"),
1200 array("revision", "rev_len", "INT")
1201 );
1202
1203 $newtables = array(
1204 array("mwuser", "patch-mwuser.sql"),
1205 array("pagecontent", "patch-pagecontent.sql"),
1206 array("querycachetwo", "patch-querycachetwo.sql"),
1207 array("redirect", "patch-redirect.sql"),
1208 array("page_restrictions", "patch-page_restrictions.sql"),
1209 array("profiling", "patch-profiling.sql"),
1210 array("mediawiki_version", "patch-mediawiki_version.sql"),
1211 array("archive2", "patch-archive2.sql")
1212 );
1213
1214 $newindexes = array(
1215 array("revision", "rev_text_id_idx", "patch-rev_text_id_idx.sql")
1216 );
1217
1218 $newrules = array(
1219 array("archive", "archive_delete", "patch-archive_delete.sql")
1220 );
1221
1222 foreach ($newtables as $nt) {
1223 if (pg_table_exists($nt[0])) {
1224 echo "... table $nt[0] already exists\n";
1225 continue;
1226 }
1227
1228 echo "... create table $nt[0]\n";
1229 dbsource(archive($nt[1]));
1230 }
1231
1232 foreach ($newcols as $nc) {
1233 if (pg_column_exists($nc[0], $nc[1])) {
1234 echo "... column $nc[0].$nc[1] already exists\n";
1235 continue;
1236 }
1237
1238 echo "... add column $nc[0].$nc[1]\n";
1239 $wgDatabase->query("ALTER TABLE $nc[0] ADD $nc[1] $nc[2]");
1240 }
1241
1242 foreach ($typechanges as $tc) {
1243 if (!pg_column_exists($tc[0], $tc[1])) {
1244 echo "... error: expected column $tc[0].$tc[1] to exist\n";
1245 exit(1);
1246 }
1247
1248 if (pg_column_has_type($tc[0], $tc[1], $tc[2]))
1249 echo "... $tc[0].$tc[1] is already $tc[2]\n";
1250 else {
1251 echo "... change $tc[0].$tc[1] to $tc[2]\n";
1252 $wgDatabase->query("ALTER TABLE $tc[0] ALTER $tc[1] TYPE $tc[2];\nCOMMIT;\n");
1253 }
1254 }
1255
1256 if (!pg_column_has_type("user_newtalk", "user_ip", "text")) {
1257 echo "... convert user_newtalk.user_ip to text\n";
1258 $wgDatabase->query("ALTER TABLE user_newtalk ALTER user_ip TYPE TEXT USING host(user_ip)");
1259 }
1260
1261 foreach ($newindexes as $ni) {
1262 if (pg_index_exists($ni[0], $ni[1])) {
1263 echo "... index $ni[1] on $ni[0] already exists\n";
1264 continue;
1265 }
1266 dbsource(archive($ni[2]));
1267 }
1268
1269 foreach ($newrules as $nr) {
1270 if (pg_rule_exists($nr[0], $nr[1])) {
1271 echo "... rule $nr[1] on $nr[0] already exists\n";
1272 continue;
1273 }
1274 dbsource(archive($nr[2]));
1275 }
1276
1277 if (!pg_column_has_type("ipblocks", "ipb_address", "text")) {
1278 echo "... change ipblocks.ipb_address to TEXT\n";
1279 $wgDatabase->query("ALTER TABLE ipblocks ALTER ipb_address TYPE TEXT USING ipb_address::TEXT");
1280 } else
1281 echo "... ipblocks.ipb_address is already TEXT\n";
1282
1283 if (!pg_trigger_exists("page", "page_deleted")) {
1284 echo "... create page_deleted trigger\n";
1285 dbsource(archive('patch-page_deleted.sql'));
1286 }
1287
1288 if (!pg_column_is_nullable("recentchanges", "rc_cur_id")) {
1289 echo "... remove NOT NULL constraint on recentchanges.rc_cur_id\n";
1290 dbsource(archive('patch-rc_cur_id-not-null.sql'));
1291 }
1292
1293 ## 1.8 Updater
1294 if ($version < 1008) {
1295 $upgrade .= <<<PGEND
1296
1297 -- Index order rearrangements:
1298 DROP INDEX pagelink_unique;
1299 CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title);
1300
1301 -- Note this upgrade
1302 INSERT INTO mediawiki_version (type,mw_version,notes)
1303 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.8 version THISVERSION aka SVERSION');
1304
1305 PGEND;
1306
1307 } ## end version 1.8
1308
1309 ## 1.9 Updater
1310 if ($version < 1009) {
1311 $upgrade = <<<PGEND
1312
1313 -- Tighten up restrictions on the revision table so we don't lose data:
1314 ALTER TABLE revision DROP CONSTRAINT revision_rev_user_fkey;
1315 ALTER TABLE revision ADD CONSTRAINT revision_rev_user_fkey
1316 FOREIGN KEY (rev_user) REFERENCES mwuser(user_id) ON DELETE RESTRICT;
1317
1318 -- Note this upgrade
1319 INSERT INTO mediawiki_version (type,mw_version,notes)
1320 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.9 version THISVERSION aka SVERSION');
1321
1322 PGEND;
1323
1324 } ## end version 1.9
1325
1326 ## 1.10 updater
1327 if ($version <= 1010) {
1328 $upgrade = <<<PGEND
1329
1330 -- Fix the archive rule ar_timestamp field
1331 CREATE OR REPLACE RULE archive_insert AS ON INSERT TO archive
1332 DO INSTEAD INSERT INTO archive2 VALUES (
1333 NEW.ar_namespace, NEW.ar_title, NEW.ar_text, NEW.ar_comment, NEW.ar_user, NEW.ar_user_text,
1334 TO_TIMESTAMP(NEW.ar_timestamp, 'YYYYMMDDHH24MISS'),
1335 NEW.ar_minor_edit, NEW.ar_flags, NEW.ar_rev_id, NEW.ar_text_id
1336 );
1337
1338 -- Note this upgrade
1339 INSERT INTO mediawiki_version (type,mw_version,notes)
1340 VALUES ('Upgrade','MWVERSION','Upgrade from older pre 1.10 version THISVERSION aka SVERSION');
1341
1342
1343 PGEND;
1344 } ## end version 1.10
1345
1346 if ( !strlen($upgrade)) {
1347 print "No updates needed for this version ($oldversion)\n";
1348 return;
1349 }
1350
1351 $upgrade = str_replace( 'MWVERSION', $wgVersion, $upgrade );
1352 $upgrade = str_replace( 'THISVERSION', $oldversion, $upgrade );
1353 $upgrade = str_replace( 'SVERSION', $version, $upgrade );
1354 $wgDatabase->query("BEGIN;\n\n $upgrade\n\nCOMMIT;\n");
1355
1356 return;
1357 }
1358
1359 ?>