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