9e6433f60dd6b44015231698917297894babbfcf
[lhc/web/wiklou.git] / maintenance / updaters.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 /** */
8
9 if ( !defined( 'MEDIAWIKI' ) ) {
10 echo "This file is not a valid entry point\n";
11 exit( 1 );
12 }
13
14 require_once 'convertLinks.inc';
15 require_once 'userDupes.inc';
16 require_once 'deleteDefaultMessages.php';
17
18 $wgRenamedTables = array(
19 # from to patch file
20 # array( 'group', 'groups', 'patch-rename-group.sql' ),
21 );
22
23 $wgNewTables = array(
24 # table patch file (in maintenance/archives)
25 array( 'hitcounter', 'patch-hitcounter.sql' ),
26 array( 'querycache', 'patch-querycache.sql' ),
27 array( 'objectcache', 'patch-objectcache.sql' ),
28 array( 'categorylinks', 'patch-categorylinks.sql' ),
29 array( 'logging', 'patch-logging.sql' ),
30 array( 'user_newtalk', 'patch-usernewtalk2.sql' ),
31 array( 'transcache', 'patch-transcache.sql' ),
32 array( 'trackbacks', 'patch-trackbacks.sql' ),
33 array( 'externallinks', 'patch-externallinks.sql' ),
34 array( 'job', 'patch-job.sql' ),
35 array( 'langlinks', 'patch-langlinks.sql' ),
36 array( 'querycache_info', 'patch-querycacheinfo.sql' ),
37 array( 'filearchive', 'patch-filearchive.sql' ),
38 array( 'redirect', 'patch-redirect.sql' ),
39 array( 'querycachetwo', 'patch-querycachetwo.sql' ),
40 # array( 'page_restrictions', 'patch-page_restrictions.sql' ),
41 );
42
43 $wgNewFields = array(
44 # table field patch file (in maintenance/archives)
45 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
46 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
47 array( 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.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( 'user', 'user_newpass_time','patch-user_newpass_time.sql' ),
71 array( 'user', 'user_editcount', 'patch-user_editcount.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();
327 echo "......checking for duplicate entries.\n"; flush();
328
329 extract( $wgDatabase->tableNames( '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();
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();
384 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
385 }
386
387
388 echo wfTimestamp();
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();
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();
432 echo "......maxold is {$maxold}\n";
433
434 echo wfTimestamp();
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();
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();
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();
472 echo "......Unlocking tables.\n";
473 $wgDatabase->query( "UNLOCK TABLES", $fname );
474
475 echo wfTimestamp();
476 echo "......Renaming old.\n";
477 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
478
479 echo wfTimestamp();
480 echo "...done.\n";
481 }
482 }
483
484 function do_inverse_timestamp() {
485 global $wgDatabase;
486 $fname="do_schema_restructuring";
487 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
488 echo "Removing revision.inverse_timestamp and fixing indexes... ";
489 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
490 echo "ok\n";
491 } else {
492 echo "revision timestamp indexes already up to 2005-03-13\n";
493 }
494 }
495
496 function do_text_id() {
497 global $wgDatabase;
498 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
499 echo "...rev_text_id already in place.\n";
500 } else {
501 echo "Adding rev_text_id field... ";
502 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
503 echo "ok\n";
504 }
505 }
506
507 function do_namespace_size() {
508 $tables = array(
509 'page' => 'page',
510 'archive' => 'ar',
511 'recentchanges' => 'rc',
512 'watchlist' => 'wl',
513 'querycache' => 'qc',
514 'logging' => 'log',
515 );
516 foreach( $tables as $table => $prefix ) {
517 do_namespace_size_on( $table, $prefix );
518 flush();
519 }
520 }
521
522 function do_namespace_size_on( $table, $prefix ) {
523 global $wgDatabase, $wgDBtype;
524 if ($wgDBtype != 'mysql')
525 return;
526 $field = $prefix . '_namespace';
527
528 $tablename = $wgDatabase->tableName( $table );
529 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
530 $info = $wgDatabase->fetchObject( $result );
531 $wgDatabase->freeResult( $result );
532
533 if( substr( $info->Type, 0, 3 ) == 'int' ) {
534 echo "...$field is already a full int ($info->Type).\n";
535 } else {
536 echo "Promoting $field from $info->Type to int... ";
537
538 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
539 $wgDatabase->query( $sql );
540
541 echo "ok\n";
542 }
543 }
544
545 function do_pagelinks_update() {
546 global $wgDatabase;
547 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
548 echo "...already have pagelinks table.\n";
549 } else {
550 echo "Converting links and brokenlinks tables to pagelinks... ";
551 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
552 echo "ok\n";
553 flush();
554
555 global $wgCanonicalNamespaceNames;
556 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
557 if( $ns != 0 ) {
558 do_pagelinks_namespace( $ns );
559 }
560 }
561 }
562 }
563
564 function do_pagelinks_namespace( $namespace ) {
565 global $wgDatabase, $wgContLang;
566
567 $ns = intval( $namespace );
568 echo "Cleaning up broken links for namespace $ns... ";
569
570 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
571 $name = $wgContLang->getNsText( $ns );
572 $prefix = $wgDatabase->strencode( $name );
573 $likeprefix = str_replace( '_', '\\_', $prefix);
574
575 $sql = "UPDATE $pagelinks
576 SET pl_namespace=$ns,
577 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
578 WHERE pl_namespace=0
579 AND pl_title LIKE '$likeprefix:%'";
580
581 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
582 echo "ok\n";
583 }
584
585 function do_drop_img_type() {
586 global $wgDatabase;
587
588 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
589 echo "Dropping unused img_type field in image table... ";
590 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
591 echo "ok\n";
592 } else {
593 echo "No img_type field in image table; Good.\n";
594 }
595 }
596
597 function do_old_links_update() {
598 global $wgDatabase;
599 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
600 echo "Already have pagelinks; skipping old links table updates.\n";
601 } else {
602 convertLinks(); flush();
603 }
604 }
605
606 function do_user_unique_update() {
607 global $wgDatabase;
608 $duper = new UserDupes( $wgDatabase );
609 if( $duper->hasUniqueIndex() ) {
610 echo "Already have unique user_name index.\n";
611 } else {
612 if( !$duper->clearDupes() ) {
613 echo "WARNING: This next step will probably fail due to unfixed duplicates...\n";
614 }
615 echo "Adding unique index on user_name... ";
616 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
617 echo "ok\n";
618 }
619 }
620
621 function do_user_groups_update() {
622 $fname = 'do_user_groups_update';
623 global $wgDatabase;
624
625 if( $wgDatabase->tableExists( 'user_groups' ) ) {
626 echo "...user_groups table already exists.\n";
627 return do_user_groups_reformat();
628 }
629
630 echo "Adding user_groups table... ";
631 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
632 echo "ok\n";
633
634 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
635 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
636 echo "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion...";
637 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
638 echo "ok\n";
639 } else {
640 echo "*** WARNING: couldn't locate user_rights table or field for upgrade.\n";
641 echo "*** You may need to manually configure some sysops by manipulating\n";
642 echo "*** the user_groups table.\n";
643 return;
644 }
645 }
646
647 echo "Converting user_rights table to user_groups... ";
648 $result = $wgDatabase->select( 'user_rights',
649 array( 'ur_user', 'ur_rights' ),
650 array( "ur_rights != ''" ),
651 $fname );
652
653 while( $row = $wgDatabase->fetchObject( $result ) ) {
654 $groups = array_unique(
655 array_map( 'trim',
656 explode( ',', $row->ur_rights ) ) );
657
658 foreach( $groups as $group ) {
659 $wgDatabase->insert( 'user_groups',
660 array(
661 'ug_user' => $row->ur_user,
662 'ug_group' => $group ),
663 $fname );
664 }
665 }
666 $wgDatabase->freeResult( $result );
667 echo "ok\n";
668 }
669
670 function do_user_groups_reformat() {
671 # Check for bogus formats from previous 1.5 alpha code.
672 global $wgDatabase;
673 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
674
675 if( $info->type == 'int' ) {
676 $oldug = $wgDatabase->tableName( 'user_groups' );
677 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
678 echo "user_groups is in bogus intermediate format. Renaming to $newug... ";
679 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
680 echo "ok\n";
681
682 echo "Re-adding fresh user_groups table... ";
683 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
684 echo "ok\n";
685
686 echo "***\n";
687 echo "*** WARNING: You will need to manually fix up user permissions in the user_groups\n";
688 echo "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n";
689 echo "***\n";
690 } else {
691 echo "...user_groups is in current format.\n";
692 }
693
694 }
695
696 function do_watchlist_null() {
697 # Make sure wl_notificationtimestamp can be NULL,
698 # and update old broken items.
699 global $wgDatabase;
700 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
701
702 if( $info->not_null ) {
703 echo "Making wl_notificationtimestamp nullable... ";
704 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
705 echo "ok\n";
706 } else {
707 echo "...wl_notificationtimestamp is already nullable.\n";
708 }
709
710 }
711
712 /**
713 * @bug 3946
714 */
715 function do_page_random_update() {
716 global $wgDatabase;
717
718 echo "Setting page_random to a random value on rows where it equals 0...";
719
720 $page = $wgDatabase->tableName( 'page' );
721 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
722 $rows = $wgDatabase->affectedRows();
723
724 echo "changed $rows rows\n";
725 }
726
727 function do_templatelinks_update() {
728 global $wgDatabase, $wgLoadBalancer;
729 $fname = 'do_templatelinks_update';
730
731 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
732 echo "...templatelinks table already exists\n";
733 return;
734 }
735 echo "Creating templatelinks table...\n";
736 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
737 echo "Populating...\n";
738 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
739 // Slow, replication-friendly update
740 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
741 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
742 $count = 0;
743 while ( $row = $wgDatabase->fetchObject( $res ) ) {
744 $count = ($count + 1) % 100;
745 if ( $count == 0 ) {
746 if ( function_exists( 'wfWaitForSlaves' ) ) {
747 wfWaitForSlaves( 10 );
748 } else {
749 sleep( 1 );
750 }
751 }
752 $wgDatabase->insert( 'templatelinks',
753 array(
754 'tl_from' => $row->pl_from,
755 'tl_namespace' => $row->pl_namespace,
756 'tl_title' => $row->pl_title,
757 ), $fname
758 );
759
760 }
761 $wgDatabase->freeResult( $res );
762 } else {
763 // Fast update
764 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
765 array(
766 'tl_from' => 'pl_from',
767 'tl_namespace' => 'pl_namespace',
768 'tl_title' => 'pl_title'
769 ), array(
770 'pl_namespace' => 10
771 ), $fname
772 );
773 }
774 echo "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n";
775 }
776
777 # July 2006
778 # Add ( rc_namespace, rc_user_text ) index [R. Church]
779 function do_rc_indices_update() {
780 global $wgDatabase;
781 echo( "Checking for additional recent changes indices...\n" );
782 # See if we can find the index we want
783 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_ns_usertext', __METHOD__ );
784 if( !$info ) {
785 # None, so create
786 echo( "...index on ( rc_namespace, rc_user_text ) not found; creating\n" );
787 dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
788 } else {
789 # Index seems to exist
790 echo( "...index on ( rc_namespace, rc_user_text ) seems to be ok\n" );
791 }
792
793 #Add (rc_user_text, rc_timestamp) index [A. Garrett], November 2006
794 # See if we can find the index we want
795 $info = $wgDatabase->indexInfo( 'recentchanges', 'rc_user_text', __METHOD__ );
796 if( !$info ) {
797 # None, so create
798 echo( "...index on ( rc_user_text, rc_timestamp ) not found; creating\n" );
799 dbsource( archive( 'patch-rc_user_text-index.sql' ) );
800 } else {
801 # Index seems to exist
802 echo( "...index on ( rc_user_text, rc_timestamp ) seems to be ok\n" );
803 }
804 }
805
806 function index_has_field($table, $index, $field) {
807 global $wgDatabase;
808 echo( "Checking if $table index $index includes field $field...\n" );
809 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
810 if( $info ) {
811 foreach($info as $row) {
812 if($row->Column_name == $field) {
813 echo( "...index $index on table $table seems to be ok\n" );
814 return true;
815 }
816 }
817 }
818 echo( "...index $index on table $table has no field $field; adding\n" );
819 return false;
820 }
821
822 function do_backlinking_indices_update() {
823 echo( "Checking for backlinking indices...\n" );
824 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
825 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
826 !index_has_field('imagelinks', 'il_to', 'il_from'))
827 {
828 dbsource( archive( 'patch-backlinkindexes.sql' ) );
829 }
830 }
831
832 function purge_cache() {
833 global $wgDatabase;
834 # We can't guarantee that the user will be able to use TRUNCATE,
835 # but we know that DELETE is available to us
836 echo( "Purging caches..." );
837 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
838 echo( "done.\n" );
839 }
840
841 function do_all_updates( $shared = false, $purge = true ) {
842 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
843
844 $doUser = !$wgSharedDB || $doShared;
845
846 if ($wgDBtype === 'postgres') {
847 do_postgres_updates();
848 return;
849 }
850
851 # Rename tables
852 foreach ( $wgRenamedTables as $tableRecord ) {
853 rename_table( $tableRecord[0], $tableRecord[1], $tableRecord[2] );
854 }
855
856 # Add missing tables
857 foreach ( $wgNewTables as $tableRecord ) {
858 add_table( $tableRecord[0], $tableRecord[1] );
859 flush();
860 }
861
862 # Add missing fields
863 foreach ( $wgNewFields as $fieldRecord ) {
864 if ( $fieldRecord[0] != 'user' || $doUser ) {
865 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
866 }
867 flush();
868 }
869
870 # Do schema updates which require special handling
871 do_interwiki_update(); flush();
872 do_index_update(); flush();
873 do_old_links_update(); flush();
874 do_image_name_unique_update(); flush();
875 do_watchlist_update(); flush();
876 if ( $doUser ) {
877 do_user_update(); flush();
878 }
879 ###### do_copy_newtalk_to_watchlist(); flush();
880 do_logging_encoding(); flush();
881
882 do_schema_restructuring(); flush();
883 do_inverse_timestamp(); flush();
884 do_text_id(); flush();
885 do_namespace_size(); flush();
886
887 do_pagelinks_update(); flush();
888 do_templatelinks_update(); flush(); // after pagelinks
889
890 do_drop_img_type(); flush();
891
892 if ( $doUser ) {
893 do_user_unique_update(); flush();
894 }
895 do_user_groups_update(); flush();
896
897 do_watchlist_null(); flush();
898
899 //do_image_index_update(); flush();
900
901 do_logging_timestamp_index(); flush();
902
903 do_page_random_update(); flush();
904
905 do_rc_indices_update(); flush();
906
907 do_backlinking_indices_update(); flush();
908
909 do_restrictions_update(); flush ();
910
911 echo "Deleting old default messages..."; flush();
912 deleteDefaultMessages();
913 echo "Done\n"; flush();
914
915 if( $purge ) {
916 purge_cache();
917 flush();
918 }
919 }
920
921 function archive($name) {
922 global $wgDBtype, $IP;
923 switch ($wgDBtype) {
924 case "oracle":
925 return "$IP/maintenance/oracle/archives/$name";
926 default:
927 return "$IP/maintenance/archives/$name";
928 }
929 }
930
931 function do_restrictions_update() {
932 # Adding page_restrictions table, obsoleting page.page_restrictions.
933 # Migrating old restrictions to new table
934 # -- Andrew Garrett, January 2007.
935
936 global $wgDatabase;
937
938 $name = 'page_restrictions';
939 $patch = 'patch-page_restrictions.sql';
940
941 if ( $wgDatabase->tableExists( $name ) ) {
942 echo "...$name table already exists.\n";
943 } else {
944 echo "Creating $name table...";
945 dbsource( archive($patch), $wgDatabase );
946 echo "ok\n";
947
948 echo "Migrating old restrictions to new table...";
949
950 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array("page_restrictions!=''", "page_restrictions!='edit=:move='"), __METHOD__ );
951
952 $count = 0;
953
954 while ($row = $wgDatabase->fetchObject($res) ) {
955 $count = ($count + 1) % 100;
956
957 if ($count == 0) {
958 if ( function_exists( 'wfWaitForSlaves' ) ) {
959 wfWaitForSlaves( 10 );
960 } else {
961 sleep( 1 );
962 }
963 }
964
965 # Figure out what the restrictions are..
966 $id = $row->page_id;
967 $flatterrestrictions = $row->page_restrictions;
968
969 $flatrestrictions = explode( ':', $flatterrestrictions );
970
971 $restrictions = array ();
972
973 foreach( $flatrestrictions as $restriction ) {
974 $thisrestriction = explode('=', $restriction);
975
976 $restriction_type = $thisrestriction[0];
977 $restriction_level = $thisrestriction[1];
978
979 $restrictions[$restriction_type] = $restriction_level;
980
981 if ($restriction_level != '') {
982
983 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
984 'pr_type' => $restriction_type,
985 'pr_level' => $restriction_level,
986 'pr_cascade' => 0 ), __METHOD );
987 }
988 }
989 }
990 print "ok\n";
991 }
992
993 }
994
995 function do_postgres_updates() {
996 global $wgDatabase, $wgVersion, $wgDBmwschema;
997
998 # Just in case their LocalSetings.php does not have this:
999 if ( !isset( $wgDBmwschema ))
1000 $wgDBmwschema = 'mediawiki';
1001
1002 ## Default to the oldest supported version
1003 $version = 1.7;
1004
1005 if ($wgDatabase->tableExists("mediawiki_version")) {
1006 $version = "1.8";
1007 $sql = "SELECT mw_version FROM mediawiki_version ORDER BY cdate DESC LIMIT 1";
1008 $tempversion = pg_fetch_result($wgDatabase->doQuery($sql),0,0);
1009 $thisver = array();
1010 if (preg_match('/(\d+\.\d+)/', $tempversion, $thisver)) {
1011 $version = $thisver[1];
1012 }
1013 }
1014
1015 print " Detected version: $version ";
1016 $upgrade = '';
1017
1018 if ($version <= 1.7) {
1019 $upgrade = <<<PGEND
1020
1021 -- Type tweaking:
1022 ALTER TABLE oldimage ALTER oi_size TYPE INTEGER;
1023 ALTER TABLE oldimage ALTER oi_width TYPE INTEGER;
1024 ALTER TABLE oldimage ALTER oi_height TYPE INTEGER;
1025
1026 ALTER TABLE image ALTER img_size TYPE INTEGER;
1027 ALTER TABLE image ALTER img_width TYPE INTEGER;
1028 ALTER TABLE image ALTER img_height TYPE INTEGER;
1029
1030 -- Constraint tweaking:
1031 ALTER TABLE recentchanges ALTER rc_cur_id DROP NOT NULL;
1032
1033 -- New columns:
1034 ALTER TABLE ipblocks ADD ipb_anon_only CHAR NOT NULL DEFAULT '0';
1035 ALTER TABLE ipblocks ADD ipb_create_account CHAR NOT NULL DEFAULT '1';
1036
1037 -- Index order rearrangements:
1038 DROP INDEX pagelink_unique;
1039 CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title);
1040
1041 -- Rename tables
1042 ALTER TABLE "user" RENAME TO mwuser;
1043 ALTER TABLE "text" RENAME to pagecontent;
1044
1045 -- New tables:
1046 CREATE TABLE profiling (
1047 pf_count INTEGER NOT NULL DEFAULT 0,
1048 pf_time NUMERIC(18,10) NOT NULL DEFAULT 0,
1049 pf_name TEXT NOT NULL,
1050 pf_server TEXT NULL
1051 );
1052 CREATE UNIQUE INDEX pf_name_server ON profiling (pf_name, pf_server);
1053
1054 CREATE TABLE mediawiki_version (
1055 type TEXT NOT NULL,
1056 mw_version TEXT NOT NULL,
1057 notes TEXT NULL,
1058
1059 pg_version TEXT NULL,
1060 pg_dbname TEXT NULL,
1061 pg_user TEXT NULL,
1062 pg_port TEXT NULL,
1063 mw_schema TEXT NULL,
1064 ts2_schema TEXT NULL,
1065 ctype TEXT NULL,
1066
1067 sql_version TEXT NULL,
1068 sql_date TEXT NULL,
1069 cdate TIMESTAMPTZ NOT NULL DEFAULT now()
1070 );
1071
1072 -- Special modifications
1073 ALTER TABLE archive RENAME to archive2;
1074 CREATE VIEW archive AS
1075 SELECT
1076 ar_namespace, ar_title, ar_text, ar_comment, ar_user, ar_user_text,
1077 ar_minor_edit, ar_flags, ar_rev_id, ar_text_id,
1078 TO_CHAR(ar_timestamp, 'YYYYMMDDHH24MISS') AS ar_timestamp
1079 FROM archive2;
1080
1081 CREATE RULE archive_insert AS ON INSERT TO archive
1082 DO INSTEAD INSERT INTO archive2 VALUES (
1083 NEW.ar_namespace, NEW.ar_title, NEW.ar_text, NEW.ar_comment, NEW.ar_user, NEW.ar_user_text,
1084 TO_DATE(NEW.ar_timestamp, 'YYYYMMDDHH24MISS'),
1085 NEW.ar_minor_edit, NEW.ar_flags, NEW.ar_rev_id, NEW.ar_text_id
1086 );
1087
1088 CREATE FUNCTION page_deleted() RETURNS TRIGGER LANGUAGE plpgsql AS
1089 \$mw\$
1090 BEGIN
1091 DELETE FROM recentchanges WHERE rc_namespace = OLD.page_namespace AND rc_title = OLD.page_title;
1092 RETURN NULL;
1093 END;
1094 \$mw\$;
1095
1096 CREATE TRIGGER page_deleted AFTER DELETE ON page
1097 FOR EACH ROW EXECUTE PROCEDURE page_deleted();
1098
1099 PGEND;
1100
1101 } ## end version 1.7
1102
1103 else if ($version <= 1.8) {
1104 $upgrade = <<<PGEND
1105
1106 -- Tighten up restrictions on the revision table so we don't lose data:
1107 ALTER TABLE revision DROP CONSTRAINT revision_rev_user_fkey;
1108 ALTER TABLE revision ADD CONSTRAINT revision_rev_user_fkey
1109 FOREIGN KEY (rev_user) REFERENCES mwuser(user_id) ON DELETE RESTRICT;
1110
1111 -- New columns for better password tracking:
1112 ALTER TABLE mwuser ADD user_newpass_time TIMESTAMPTZ;
1113 ALTER TABLE mwuser ADD user_editcount INTEGER;
1114
1115 -- New column for autoblocking problem users
1116 ALTER TABLE ipblocks ADD ipb_enable_autoblock CHAR NOT NULL DEFAULT '1';
1117
1118 -- Despite it's name, ipb_address does not necessarily contain IP addresses :)
1119 ALTER TABLE ipblocks ALTER ipb_address TYPE TEXT USING ipb_address::TEXT;
1120
1121 -- New tables:
1122 CREATE TABLE redirect (
1123 rd_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE,
1124 rd_namespace SMALLINT NOT NULL,
1125 rd_title TEXT NOT NULL
1126 );
1127 CREATE INDEX redirect_ns_title ON redirect (rd_namespace,rd_title,rd_from);
1128
1129 CREATE TABLE querycachetwo (
1130 qcc_type TEXT NOT NULL,
1131 qcc_value SMALLINT NOT NULL DEFAULT 0,
1132 qcc_namespace INTEGER NOT NULL DEFAULT 0,
1133 qcc_title TEXT NOT NULL DEFAULT '',
1134 qcc_namespacetwo INTEGER NOT NULL DEFAULT 0,
1135 qcc_titletwo TEXT NOT NULL DEFAULT ''
1136 );
1137 CREATE INDEX querycachetwo_type_value ON querycachetwo (qcc_type, qcc_value);
1138 CREATE INDEX querycachetwo_title ON querycachetwo (qcc_type,qcc_namespace,qcc_title);
1139 CREATE INDEX querycachetwo_titletwo ON querycachetwo (qcc_type,qcc_namespacetwo,qcc_titletwo);
1140
1141 -- New columns for fancy recentchanges display
1142 ALTER TABLE recentchanges ADD rc_old_len INT;
1143 ALTER TABLE recentchanges ADD rc_new_len INT;
1144
1145 -- Note this upgrade
1146 INSERT INTO mediawiki_version (type,mw_version,notes)
1147 VALUES ('Upgrade','MWVERSION','Upgrade from older version THISVERSION');
1148
1149 PGEND;
1150
1151 }
1152
1153 if ( !strlen($upgrade)) {
1154 print "No updates needed for version $version\n";
1155 return;
1156 }
1157
1158 $upgrade = str_replace( 'MWVERSION', $wgVersion, $upgrade );
1159 $upgrade = str_replace( 'THISVERSION', $version, $upgrade );
1160 $res = $wgDatabase->query("BEGIN;\n\n $upgrade\n\nCOMMIT;\n");
1161
1162 return;
1163 }
1164
1165 ?>