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