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