Restore patch that was temporarily reverted in order to rectify an issue where the...
[lhc/web/wiklou.git] / maintenance / updaters.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 /** */
8
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11 require_once 'userDupes.inc';
12
13 $wgRenamedTables = array(
14 # from to patch file
15 # array( 'group', 'groups', 'patch-rename-group.sql' ),
16 );
17
18 $wgNewTables = array(
19 # table patch file (in maintenance/archives)
20 array( 'hitcounter', 'patch-hitcounter.sql' ),
21 array( 'querycache', 'patch-querycache.sql' ),
22 array( 'objectcache', 'patch-objectcache.sql' ),
23 array( 'categorylinks', 'patch-categorylinks.sql' ),
24 array( 'logging', 'patch-logging.sql' ),
25 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
26 array( 'transcache', 'patch-transcache.sql' ),
27 array( 'trackbacks', 'patch-trackbacks.sql' ),
28 array( 'externallinks', 'patch-externallinks.sql' ),
29 array( 'job', 'patch-job.sql' ),
30 array( 'langlinks', 'patch-langlinks.sql' ),
31 array( 'querycache_info', 'patch-querycacheinfo.sql' ),
32 array( 'filearchive', 'patch-filearchive.sql' ),
33 array( 'redirect', 'patch-redirect.sql' ),
34 array( 'querycachetwo', 'patch-querycachetwo.sql' ),
35 );
36
37 $wgNewFields = array(
38 # table field patch file (in maintenance/archives)
39 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
40 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
41 array( 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
42 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
43 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
44 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
45 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
46 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
47 array( 'user', 'user_token', 'patch-user_token.sql' ),
48 array( 'user', 'user_email_token', 'patch-user_email_token.sql' ),
49 array( 'user', 'user_registration','patch-user_registration.sql' ),
50 array( 'logging', 'log_params', 'patch-log_params.sql' ),
51 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
52 array( 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
53 array( 'page', 'page_len', 'patch-page_len.sql' ),
54 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
55 array( 'image', 'img_width', 'patch-img_width.sql' ),
56 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
57 array( 'image', 'img_media_type', 'patch-img_media_type.sql' ),
58 array( 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ),
59 array( 'interwiki', 'iw_trans', 'patch-interwiki-trans.sql' ),
60 array( 'ipblocks', 'ipb_range_start', 'patch-ipb_range_start.sql' ),
61 array( 'site_stats', 'ss_images', 'patch-ss_images.sql' ),
62 array( 'ipblocks', 'ipb_anon_only', 'patch-ipb_anon_only.sql' ),
63 array( 'user', 'user_newpass_time','patch-user_newpass_time.sql' ),
64 );
65
66 function rename_table( $from, $to, $patch ) {
67 global $wgDatabase;
68 if ( $wgDatabase->tableExists( $from ) ) {
69 if ( $wgDatabase->tableExists( $to ) ) {
70 echo "...can't move table $from to $to, $to already exists.\n";
71 } else {
72 echo "Moving table $from to $to...";
73 dbsource( archive($patch), $wgDatabase );
74 echo "ok\n";
75 }
76 } else {
77 // Source table does not exist
78 // Renames are done before creations, so this is typical for a new installation
79 // Ignore silently
80 }
81 }
82
83 function add_table( $name, $patch ) {
84 global $wgDatabase;
85 if ( $wgDatabase->tableExists( $name ) ) {
86 echo "...$name table already exists.\n";
87 } else {
88 echo "Creating $name table...";
89 dbsource( archive($patch), $wgDatabase );
90 echo "ok\n";
91 }
92 }
93
94 function add_field( $table, $field, $patch ) {
95 global $wgDatabase;
96 if ( !$wgDatabase->tableExists( $table ) ) {
97 echo "...$table table does not exist, skipping new field patch\n";
98 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
99 echo "...have $field field in $table table.\n";
100 } else {
101 echo "Adding $field field to table $table...";
102 dbsource( archive($patch) , $wgDatabase );
103 echo "ok\n";
104 }
105 }
106
107 function do_revision_updates() {
108 global $wgSoftwareRevision;
109 if ( $wgSoftwareRevision < 1001 ) {
110 update_passwords();
111 }
112 }
113
114 function update_passwords() {
115 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
116
117 global $wgDatabase;
118 $fname = "Update script: update_passwords()";
119 print "\nIt appears that you need to update the user passwords in your\n" .
120 "database. If you have already done this (if you've run this update\n" .
121 "script once before, for example), doing so again will make all your\n" .
122 "user accounts inaccessible, so be sure you only do this once.\n" .
123 "Update user passwords? (yes/no)";
124
125 $resp = readconsole();
126 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
127
128 $sql = "SELECT user_id,user_password FROM user";
129 $source = $wgDatabase->query( $sql, $fname );
130
131 while ( $row = $wgDatabase->fetchObject( $source ) ) {
132 $id = $row->user_id;
133 $oldpass = $row->user_password;
134 $newpass = md5( "{$id}-{$oldpass}" );
135
136 $sql = "UPDATE user SET user_password='{$newpass}' " .
137 "WHERE user_id={$id}";
138 $wgDatabase->query( $sql, $fname );
139 }
140 }
141
142 function do_interwiki_update() {
143 # Check that interwiki table exists; if it doesn't source it
144 global $wgDatabase, $IP;
145 if( $wgDatabase->tableExists( "interwiki" ) ) {
146 echo "...already have interwiki table\n";
147 return true;
148 }
149 echo "Creating interwiki table: ";
150 dbsource( archive("patch-interwiki.sql") );
151 echo "ok\n";
152 echo "Adding default interwiki definitions: ";
153 dbsource( "$IP/maintenance/interwiki.sql" );
154 echo "ok\n";
155 }
156
157 function do_index_update() {
158 # Check that proper indexes are in place
159 global $wgDatabase;
160 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
161 if( $meta->multiple_key == 0 ) {
162 echo "Updating indexes to 20031107: ";
163 dbsource( archive("patch-indexes.sql") );
164 echo "ok\n";
165 return true;
166 }
167 echo "...indexes seem up to 20031107 standards\n";
168 return false;
169 }
170
171 function do_image_index_update() {
172 global $wgDatabase;
173
174 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
175 if( $meta->multiple_key == 0 ) {
176 echo "Updating indexes to 20050912: ";
177 dbsource( archive("patch-mimesearch-indexes.sql") );
178 echo "ok\n";
179 return true;
180 }
181 echo "...indexes seem up to 20050912 standards\n";
182 return false;
183 }
184
185 function do_image_name_unique_update() {
186 global $wgDatabase;
187 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
188 echo "...image primary key already set.\n";
189 } else {
190 echo "Making img_name the primary key... ";
191 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
192 echo "ok\n";
193 }
194 }
195
196 function do_logging_timestamp_index() {
197 global $wgDatabase;
198 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
199 echo "...timestamp key on logging already exists.\n";
200 } else {
201 echo "Adding timestamp key on logging table... ";
202 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
203 echo "ok\n";
204 }
205 }
206
207
208 function do_watchlist_update() {
209 global $wgDatabase;
210 $fname = 'do_watchlist_update';
211 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
212 echo "The watchlist table is already set up for email notification.\n";
213 } else {
214 echo "Adding wl_notificationtimestamp field for email notification management.";
215 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
216 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
217 echo "ok\n";
218 }
219 # Check if we need to add talk page rows to the watchlist
220 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
221 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
222 if ( $talk != $nontalk ) {
223 echo "Adding missing watchlist talk page rows... ";
224 flush();
225
226 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
227 array(
228 'wl_user' => 'wl_user',
229 'wl_namespace' => 'wl_namespace | 1',
230 'wl_title' => 'wl_title',
231 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
232 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
233 echo "ok\n";
234 } else {
235 echo "...watchlist talk page rows already present\n";
236 }
237 }
238
239 function do_copy_newtalk_to_watchlist() {
240 global $wgDatabase;
241 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
242
243 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
244 $wgDatabase->tableName( 'user_newtalk' ) );
245 $num_newtalks=$wgDatabase->numRows($res);
246 echo "Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
247
248 $user = new User();
249 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
250 $wluser = $wgDatabase->fetchObject( $res );
251 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
252 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
253 $wgDatabase->replace( 'watchlist',
254 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
255 array('wl_user' => 0,
256 'wl_namespace' => NS_USER_TALK,
257 'wl_title' => $wluser->user_ip,
258 'wl_notificationtimestamp' => '19700101000000'
259 ), 'updaters.inc::do_watchlist_update2'
260 );
261 }
262 } else { # normal users ... have user_ids
263 $user->setID($wluser->user_id);
264 $wgDatabase->replace( 'watchlist',
265 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
266 array('wl_user' => $user->getID(),
267 'wl_namespace' => NS_USER_TALK,
268 'wl_title' => $user->getName(),
269 'wl_notificationtimestamp' => '19700101000000'
270 ), 'updaters.inc::do_watchlist_update3'
271 );
272 }
273 }
274 echo "Done.\n";
275 }
276
277
278 function do_user_update() {
279 global $wgDatabase;
280 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
281 echo "User table contains old email authentication field. Dropping... ";
282 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
283 echo "ok\n";
284 } else {
285 echo "...user table does not contain old email authentication field.\n";
286 }
287 }
288
289 /**
290 * 1.4 betas were missing the 'binary' marker from logging.log_title,
291 * which causes a collation mismatch error on joins in MySQL 4.1.
292 */
293 function do_logging_encoding() {
294 global $wgDatabase, $wgDBtype;
295 if ($wgDBtype != 'mysql')
296 return;
297 $logging = $wgDatabase->tableName( 'logging' );
298 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
299 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
300 $wgDatabase->freeResult( $res );
301
302 if( in_array( 'binary', $flags ) ) {
303 echo "Logging table has correct title encoding.\n";
304 } else {
305 echo "Fixing title encoding on logging table... ";
306 dbsource( archive( 'patch-logging-title.sql' ), $wgDatabase );
307 echo "ok\n";
308 }
309 }
310
311 function do_schema_restructuring() {
312 global $wgDatabase;
313 $fname="do_schema_restructuring";
314 if ( $wgDatabase->tableExists( 'page' ) ) {
315 echo "...page table already exists.\n";
316 } else {
317 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
318 echo wfTimestamp();
319 echo "......checking for duplicate entries.\n"; flush();
320
321 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
322
323 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
324 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
325
326 if ( $wgDatabase->numRows( $rows ) > 0 ) {
327 echo wfTimestamp();
328 echo "......<b>Found duplicate entries</b>\n";
329 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
330 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
331 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
332 $duplicate[$row->cur_namespace] = array();
333 }
334 $duplicate[$row->cur_namespace][] = $row->cur_title;
335 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
336 }
337 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
338 $firstCond = true;
339 foreach ( $duplicate as $ns => $titles ) {
340 if ( $firstCond ) {
341 $firstCond = false;
342 } else {
343 $sql .= ' OR ';
344 }
345 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
346 $first = true;
347 foreach ( $titles as $t ) {
348 if ( $first ) {
349 $sql .= $wgDatabase->addQuotes( $t );
350 $first = false;
351 } else {
352 $sql .= ', ' . $wgDatabase->addQuotes( $t );
353 }
354 }
355 $sql .= ") ) \n";
356 }
357 # By sorting descending, the most recent entry will be the first in the list.
358 # All following entries will be deleted by the next while-loop.
359 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
360
361 $rows = $wgDatabase->query( $sql, $fname );
362
363 $prev_title = $prev_namespace = false;
364 $deleteId = array();
365
366 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
367 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
368 $deleteId[] = $row->cur_id;
369 }
370 $prev_title = $row->cur_title;
371 $prev_namespace = $row->cur_namespace;
372 }
373 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
374 $rows = $wgDatabase->query( $sql, $fname );
375 echo wfTimestamp();
376 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
377 }
378
379
380 echo wfTimestamp();
381 echo "......Creating tables.\n";
382 $wgDatabase->query("CREATE TABLE $page (
383 page_id int(8) unsigned NOT NULL auto_increment,
384 page_namespace int NOT NULL,
385 page_title varchar(255) binary NOT NULL,
386 page_restrictions tinyblob NOT NULL default '',
387 page_counter bigint(20) unsigned NOT NULL default '0',
388 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
389 page_is_new tinyint(1) unsigned NOT NULL default '0',
390 page_random real unsigned NOT NULL,
391 page_touched char(14) binary NOT NULL default '',
392 page_latest int(8) unsigned NOT NULL,
393 page_len int(8) unsigned NOT NULL,
394
395 PRIMARY KEY page_id (page_id),
396 UNIQUE INDEX name_title (page_namespace,page_title),
397 INDEX (page_random),
398 INDEX (page_len)
399 ) TYPE=InnoDB", $fname );
400 $wgDatabase->query("CREATE TABLE $revision (
401 rev_id int(8) unsigned NOT NULL auto_increment,
402 rev_page int(8) unsigned NOT NULL,
403 rev_comment tinyblob NOT NULL default '',
404 rev_user int(5) unsigned NOT NULL default '0',
405 rev_user_text varchar(255) binary NOT NULL default '',
406 rev_timestamp char(14) binary NOT NULL default '',
407 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
408 rev_deleted tinyint(1) unsigned NOT NULL default '0',
409
410 PRIMARY KEY rev_page_id (rev_page, rev_id),
411 UNIQUE INDEX rev_id (rev_id),
412 INDEX rev_timestamp (rev_timestamp),
413 INDEX page_timestamp (rev_page,rev_timestamp),
414 INDEX user_timestamp (rev_user,rev_timestamp),
415 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
416 ) TYPE=InnoDB", $fname );
417
418 echo wfTimestamp();
419 echo "......Locking tables.\n";
420 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
421
422 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
423 echo wfTimestamp();
424 echo "......maxold is {$maxold}\n";
425
426 echo wfTimestamp();
427 global $wgLegacySchemaConversion;
428 if( $wgLegacySchemaConversion ) {
429 // Create HistoryBlobCurStub entries.
430 // Text will be pulled from the leftover 'cur' table at runtime.
431 echo "......Moving metadata from cur; using blob references to text in cur table.\n";
432 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
433 $cur_flags = "'object'";
434 } else {
435 // Copy all cur text in immediately: this may take longer but avoids
436 // having to keep an extra table around.
437 echo "......Moving text from cur.\n";
438 $cur_text = 'cur_text';
439 $cur_flags = "''";
440 }
441 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
442 old_timestamp, old_minor_edit, old_flags)
443 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
444 FROM $cur", $fname );
445
446 echo wfTimestamp();
447 echo "......Setting up revision table.\n";
448 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
449 rev_minor_edit)
450 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
451 old_timestamp, old_minor_edit
452 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
453
454 echo wfTimestamp();
455 echo "......Setting up page table.\n";
456 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
457 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
458 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
459 cur_random, cur_touched, rev_id, LENGTH(cur_text)
460 FROM $cur,$revision
461 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
462
463 echo wfTimestamp();
464 echo "......Unlocking tables.\n";
465 $wgDatabase->query( "UNLOCK TABLES", $fname );
466
467 echo wfTimestamp();
468 echo "......Renaming old.\n";
469 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
470
471 echo wfTimestamp();
472 echo "...done.\n";
473 }
474 }
475
476 function do_inverse_timestamp() {
477 global $wgDatabase;
478 $fname="do_schema_restructuring";
479 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
480 echo "Removing revision.inverse_timestamp and fixing indexes... ";
481 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
482 echo "ok\n";
483 } else {
484 echo "revision timestamp indexes already up to 2005-03-13\n";
485 }
486 }
487
488 function do_text_id() {
489 global $wgDatabase;
490 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
491 echo "...rev_text_id already in place.\n";
492 } else {
493 echo "Adding rev_text_id field... ";
494 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
495 echo "ok\n";
496 }
497 }
498
499 function do_namespace_size() {
500 $tables = array(
501 'page' => 'page',
502 'archive' => 'ar',
503 'recentchanges' => 'rc',
504 'watchlist' => 'wl',
505 'querycache' => 'qc',
506 'logging' => 'log',
507 );
508 foreach( $tables as $table => $prefix ) {
509 do_namespace_size_on( $table, $prefix );
510 flush();
511 }
512 }
513
514 function do_namespace_size_on( $table, $prefix ) {
515 global $wgDatabase, $wgDBtype;
516 if ($wgDBtype != 'mysql')
517 return;
518 $field = $prefix . '_namespace';
519
520 $tablename = $wgDatabase->tableName( $table );
521 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
522 $info = $wgDatabase->fetchObject( $result );
523 $wgDatabase->freeResult( $result );
524
525 if( substr( $info->Type, 0, 3 ) == 'int' ) {
526 echo "...$field is already a full int ($info->Type).\n";
527 } else {
528 echo "Promoting $field from $info->Type to int... ";
529
530 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
531 $wgDatabase->query( $sql );
532
533 echo "ok\n";
534 }
535 }
536
537 function do_pagelinks_update() {
538 global $wgDatabase;
539 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
540 echo "...already have pagelinks table.\n";
541 } else {
542 echo "Converting links and brokenlinks tables to pagelinks... ";
543 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
544 echo "ok\n";
545 flush();
546
547 global $wgCanonicalNamespaceNames;
548 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
549 if( $ns != 0 ) {
550 do_pagelinks_namespace( $ns );
551 }
552 }
553 }
554 }
555
556 function do_pagelinks_namespace( $namespace ) {
557 global $wgDatabase, $wgContLang;
558
559 $ns = intval( $namespace );
560 echo "Cleaning up broken links for namespace $ns... ";
561
562 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
563 $name = $wgContLang->getNsText( $ns );
564 $prefix = $wgDatabase->strencode( $name );
565 $likeprefix = str_replace( '_', '\\_', $prefix);
566
567 $sql = "UPDATE $pagelinks
568 SET pl_namespace=$ns,
569 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
570 WHERE pl_namespace=0
571 AND pl_title LIKE '$likeprefix:%'";
572
573 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
574 echo "ok\n";
575 }
576
577 function do_drop_img_type() {
578 global $wgDatabase;
579
580 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
581 echo "Dropping unused img_type field in image table... ";
582 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
583 echo "ok\n";
584 } else {
585 echo "No img_type field in image table; Good.\n";
586 }
587 }
588
589 function do_old_links_update() {
590 global $wgDatabase;
591 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
592 echo "Already have pagelinks; skipping old links table updates.\n";
593 } else {
594 convertLinks(); flush();
595 }
596 }
597
598 function do_user_unique_update() {
599 global $wgDatabase;
600 $duper = new UserDupes( $wgDatabase );
601 if( $duper->hasUniqueIndex() ) {
602 echo "Already have unique user_name index.\n";
603 } else {
604 if( !$duper->clearDupes() ) {
605 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
606 }
607 echo "Adding unique index on user_name... ";
608 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
609 echo "ok\n";
610 }
611 }
612
613 function do_user_groups_update() {
614 $fname = 'do_user_groups_update';
615 global $wgDatabase;
616
617 if( $wgDatabase->tableExists( 'user_groups' ) ) {
618 echo "...user_groups table already exists.\n";
619 return do_user_groups_reformat();
620 }
621
622 echo "Adding user_groups table... ";
623 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
624 echo "ok\n";
625
626 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
627 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
628 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
629 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
630 echo "ok\n";
631 } else {
632 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
633 echo "*** You may need to manually configure some sysops by manipulating\n";
634 echo "*** the user_groups table.\n";
635 return;
636 }
637 }
638
639 echo "Converting user_rights table to user_groups... ";
640 $result = $wgDatabase->select( 'user_rights',
641 array( 'ur_user', 'ur_rights' ),
642 array( "ur_rights != ''" ),
643 $fname );
644
645 while( $row = $wgDatabase->fetchObject( $result ) ) {
646 $groups = array_unique(
647 array_map( 'trim',
648 explode( ',', $row->ur_rights ) ) );
649
650 foreach( $groups as $group ) {
651 $wgDatabase->insert( 'user_groups',
652 array(
653 'ug_user' => $row->ur_user,
654 'ug_group' => $group ),
655 $fname );
656 }
657 }
658 $wgDatabase->freeResult( $result );
659 echo "ok\n";
660 }
661
662 function do_user_groups_reformat() {
663 # Check for bogus formats from previous 1.5 alpha code.
664 global $wgDatabase;
665 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
666
667 if( $info->type == 'int' ) {
668 $oldug = $wgDatabase->tableName( 'user_groups' );
669 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
670 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
671 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
672 echo "ok\n";
673
674 echo "Re-adding fresh user_groups table... ";
675 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
676 echo "ok\n";
677
678 echo "***\n";
679 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
680 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
681 echo "***\n";
682 } else {
683 echo "...user_groups is in current format.\n";
684 }
685
686 }
687
688 function do_watchlist_null() {
689 # Make sure wl_notificationtimestamp can be NULL,
690 # and update old broken items.
691 global $wgDatabase;
692 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
693
694 if( $info->not_null ) {
695 echo "Making wl_notificationtimestamp nullable... ";
696 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
697 echo "ok\n";
698 } else {
699 echo "...wl_notificationtimestamp is already nullable.\n";
700 }
701
702 }
703
704 /**
705 * @bug 3946
706 */
707 function do_page_random_update() {
708 global $wgDatabase;
709
710 echo "Setting page_random to a random value on rows where it equals 0...";
711
712 $page = $wgDatabase->tableName( 'page' );
713 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
714 $rows = $wgDatabase->affectedRows();
715
716 echo "changed $rows rows\n";
717 }
718
719 function do_templatelinks_update() {
720 global $wgDatabase, $wgLoadBalancer;
721 $fname = 'do_templatelinks_update';
722
723 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
724 echo "...templatelinks table already exists\n";
725 return;
726 }
727 echo "Creating templatelinks table...\n";
728 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
729 echo "Populating...\n";
730 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
731 // Slow, replication-friendly update
732 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
733 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
734 $count = 0;
735 while ( $row = $wgDatabase->fetchObject( $res ) ) {
736 $count = ($count + 1) % 100;
737 if ( $count == 0 ) {
738 if ( function_exists( 'wfWaitForSlaves' ) ) {
739 wfWaitForSlaves( 10 );
740 } else {
741 sleep( 1 );
742 }
743 }
744 $wgDatabase->insert( 'templatelinks',
745 array(
746 'tl_from' => $row->pl_from,
747 'tl_namespace' => $row->pl_namespace,
748 'tl_title' => $row->pl_title,
749 ), $fname
750 );
751
752 }
753 $wgDatabase->freeResult( $res );
754 } else {
755 // Fast update
756 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
757 array(
758 'tl_from' => 'pl_from',
759 'tl_namespace' => 'pl_namespace',
760 'tl_title' => 'pl_title'
761 ), array(
762 'pl_namespace' => 10
763 ), $fname
764 );
765 }
766 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
767 }
768
769 # July 2006
770 # Add ( rc_namespace, rc_user_text ) index [R. Church]
771 function do_rc_indices_update() {
772 global $wgDatabase;
773 echo( "Checking for additional recent changes indices...\n" );
774 # See if we can find the index we want
775 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_ns_usertext', __METHOD__ );
776 if( !$info ) {
777 # None, so create
778 echo( "...index on ( rc_namespace, rc_user_text ) not found; creating\n" );
779 dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
780 } else {
781 # Index seems to exist
782 echo( "...seems to be ok\n" );
783 }
784 }
785
786 function do_all_updates( $doShared = false ) {
787 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype;
788
789 $doUser = !$wgSharedDB || $doShared;
790
791 if ($wgDBtype === 'postgres') {
792 do_postgres_updates();
793 return;
794 }
795
796 # Rename tables
797 foreach ( $wgRenamedTables as $tableRecord ) {
798 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
799 }
800
801 # Add missing tables
802 foreach ( $wgNewTables as $tableRecord ) {
803 add_table( $tableRecord[0], $tableRecord[1] );
804 flush();
805 }
806
807 # Add missing fields
808 foreach ( $wgNewFields as $fieldRecord ) {
809 if ( $fieldRecord[0] != 'user' || $doUser ) {
810 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
811 }
812 flush();
813 }
814
815 # Do schema updates which require special handling
816 do_interwiki_update(); flush();
817 do_index_update(); flush();
818 do_old_links_update(); flush();
819 do_image_name_unique_update(); flush();
820 do_watchlist_update(); flush();
821 if ( $doUser ) {
822 do_user_update(); flush();
823 }
824 ###### do_copy_newtalk_to_watchlist(); flush();
825 do_logging_encoding(); flush();
826
827 do_schema_restructuring(); flush();
828 do_inverse_timestamp(); flush();
829 do_text_id(); flush();
830 do_namespace_size(); flush();
831
832 do_pagelinks_update(); flush();
833 do_templatelinks_update(); flush(); // after pagelinks
834
835 do_drop_img_type(); flush();
836
837 if ( $doUser ) {
838 do_user_unique_update(); flush();
839 }
840 do_user_groups_update(); flush();
841
842 do_watchlist_null(); flush();
843
844 //do_image_index_update(); flush();
845
846 do_logging_timestamp_index(); flush();
847
848 do_page_random_update(); flush();
849
850 do_rc_indices_update(); flush();
851
852 initialiseMessages(); flush();
853 }
854
855 function archive($name) {
856 global $wgDBtype, $IP;
857 switch ($wgDBtype) {
858 case "oracle":
859 return "$IP/maintenance/oracle/archives/$name";
860 default:
861 return "$IP/maintenance/archives/$name";
862 }
863 }
864
865 function do_postgres_updates() {
866 global $wgDatabase, $wgVersion, $wgDBmwschema;
867
868 $version = "1.7.1";
869
870 # Just in case their LocalSetings.php does not have this:
871 if ( !isset( $wgDBmwschema ))
872 $wgDBmwschema = 'mediawiki';
873
874 if ($wgDatabase->tableExists("mediawiki_version")) {
875 $version = "1.8";
876 }
877
878 if ($version == '1.7.1') {
879 $upgrade = <<<PGEND
880
881 BEGIN;
882
883 -- Type tweaking:
884 ALTER TABLE oldimage ALTER oi_size TYPE INTEGER;
885 ALTER TABLE oldimage ALTER oi_width TYPE INTEGER;
886 ALTER TABLE oldimage ALTER oi_height TYPE INTEGER;
887
888 ALTER TABLE image ALTER img_size TYPE INTEGER;
889 ALTER TABLE image ALTER img_width TYPE INTEGER;
890 ALTER TABLE image ALTER img_height TYPE INTEGER;
891
892 -- Constraint tweaking:
893 ALTER TABLE recentchanges ALTER rc_cur_id DROP NOT NULL;
894
895 -- New columns:
896 ALTER TABLE ipblocks ADD ipb_anon_only CHAR NOT NULL DEFAULT '0';
897 ALTER TABLE ipblocks ADD ipb_create_account CHAR NOT NULL DEFAULT '1';
898 ALTER TABLE ipblocks ADD ipb_enable_autoblock CHAR NOT NULL default '1';
899
900 -- Index order rearrangements:
901 DROP INDEX pagelink_unique;
902 CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title);
903
904 -- Rename tables
905 ALTER TABLE "user" RENAME TO mwuser;
906 ALTER TABLE "text" RENAME to pagecontent;
907
908 -- New tables:
909 CREATE TABLE profiling (
910 pf_count INTEGER NOT NULL DEFAULT 0,
911 pf_time NUMERIC(18,10) NOT NULL DEFAULT 0,
912 pf_name TEXT NOT NULL,
913 pf_server TEXT NULL
914 );
915 CREATE UNIQUE INDEX pf_name_server ON profiling (pf_name, pf_server);
916
917 CREATE TABLE mediawiki_version (
918 type TEXT NOT NULL,
919 mw_version TEXT NOT NULL,
920 notes TEXT NULL,
921
922 pg_version TEXT NULL,
923 pg_dbname TEXT NULL,
924 pg_user TEXT NULL,
925 pg_port TEXT NULL,
926 mw_schema TEXT NULL,
927 ts2_schema TEXT NULL,
928 ctype TEXT NULL,
929
930 sql_version TEXT NULL,
931 sql_date TEXT NULL,
932 cdate TIMESTAMPTZ NOT NULL DEFAULT now()
933 );
934
935 INSERT INTO mediawiki_version (type,mw_version,notes)
936 VALUES ('Upgrade','MWVERSION','Upgrade from older version 1.7.1');
937
938 -- Special modifications
939 ALTER TABLE archive RENAME to archive2;
940 CREATE VIEW archive AS
941 SELECT
942 ar_namespace, ar_title, ar_text, ar_comment, ar_user, ar_user_text,
943 ar_minor_edit, ar_flags, ar_rev_id, ar_text_id,
944 TO_CHAR(ar_timestamp, 'YYYYMMDDHH24MISS') AS ar_timestamp
945 FROM archive2;
946
947 CREATE RULE archive_insert AS ON INSERT TO archive
948 DO INSTEAD INSERT INTO archive2 VALUES (
949 NEW.ar_namespace, NEW.ar_title, NEW.ar_text, NEW.ar_comment, NEW.ar_user, NEW.ar_user_text,
950 TO_DATE(NEW.ar_timestamp, 'YYYYMMDDHH24MISS'),
951 NEW.ar_minor_edit, NEW.ar_flags, NEW.ar_rev_id, NEW.ar_text_id
952 );
953
954 CREATE FUNCTION page_deleted() RETURNS TRIGGER LANGUAGE plpgsql AS
955 \$mw\$
956 BEGIN
957 DELETE FROM recentchanges WHERE rc_namespace = OLD.page_namespace AND rc_title = OLD.page_title;
958 RETURN NULL;
959 END;
960 \$mw\$;
961
962 CREATE TRIGGER page_deleted AFTER DELETE ON page
963 FOR EACH ROW EXECUTE PROCEDURE page_deleted();
964
965 COMMIT;
966
967 PGEND;
968
969 } ## end version 1.7.1 upgrade
970 else if ($version == '1.8') {
971 $upgrade = <<<PGEND
972
973 BEGIN;
974
975 -- Tighten up restrictions on the revision table so we don't lose data:
976 ALTER TABLE revision DROP CONSTRAINT revision_rev_user_fkey;
977 ALTER TABLE revision ADD CONSTRAINT revision_rev_user_fkey
978 FOREIGN KEY (rev_user) REFERENCES mwuser(user_id) ON DELETE RESTRICT;
979
980 -- New column for better password tracking:
981 ALTER TABLE mwuser ADD user_newpass_time TIMESTAMPTZ;
982
983 -- New column for autoblocking problem users
984 ALTER TABLE ipblocks ADD ipb_enable_autoblock CHAR NOT NULL DEFAULT '1';
985
986 -- New tables:
987 CREATE TABLE redirect (
988 rd_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE,
989 rd_namespace SMALLINT NOT NULL,
990 rd_title TEXT NOT NULL
991 );
992 CREATE INDEX redirect_ns_title ON redirect (rd_namespace,rd_title,rd_from);
993
994 CREATE TABLE querycachetwo (
995 qcc_type TEXT NOT NULL,
996 qcc_value SMALLINT NOT NULL DEFAULT 0,
997 qcc_namespace INTEGER NOT NULL DEFAULT 0,
998 qcc_title TEXT NOT NULL DEFAULT '',
999 qcc_namespacetwo INTEGER NOT NULL DEFAULT 0,
1000 qcc_titletwo TEXT NOT NULL DEFAULT ''
1001 );
1002 CREATE INDEX querycachetwo_type_value ON querycachetwo (qcc_type, qcc_value);
1003 CREATE INDEX querycachetwo_title ON querycachetwo (qcc_type,qcc_namespace,qcc_title);
1004 CREATE INDEX querycachetwo_titletwo ON querycachetwo (qcc_type,qcc_namespacetwo,qcc_titletwo);
1005
1006 -- Note this upgrade
1007 INSERT INTO mediawiki_version (type,mw_version,notes)
1008 VALUES ('Upgrade','MWVERSION','Upgrade from older version 1.8');
1009
1010 COMMIT;
1011
1012 PGEND;
1013
1014 }
1015
1016 else {
1017 print "No updates needed for version $version\n";
1018 return;
1019 }
1020
1021 $upgrade = str_replace( 'MWVERSION', $wgVersion, $upgrade );
1022 $res = $wgDatabase->query($upgrade);
1023
1024
1025 return;
1026 }
1027
1028 ?>