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