7 if ( !defined( 'MEDIAWIKI' ) ) {
8 echo "This file is not a valid entry point\n";
12 require_once 'userDupes.inc';
14 require_once( "$IP/includes/Hooks.php" );
17 * @deprecated. Do not use, ever.
22 # For extensions only, should be populated via hooks
23 # $wgDBtype should be checked to specifiy the proper file
24 $wgExtNewTables = array(); // table, dir
25 $wgExtNewFields = array(); // table, column, dir
26 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
27 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
28 $wgExtNewIndexes = array(); // table, index, dir
29 $wgExtModifiedFields = array(); // table, index, dir
31 # Helper function: check if the given key is present in the updatelog table.
32 # Obviously, only use this for updates that occur after the updatelog table was
34 function update_row_exists( $key ) {
36 $row = $wgDatabase->selectRow(
39 array( 'ul_key' => $key ),
45 function modify_field( $table, $field, $patch, $fullpath = false ) {
47 if ( !$wgDatabase->tableExists( $table ) ) {
48 wfOut( "...$table table does not exist, skipping modify field patch\n" );
49 } elseif ( ! $wgDatabase->fieldExists( $table, $field ) ) {
50 wfOut( "...$field field does not exist in $table table, skipping modify field patch\n" );
52 wfOut( "Modifying $field field of table $table..." );
54 $wgDatabase->sourceFile( $patch );
56 $wgDatabase->sourceFile( archive( $patch ) );
62 function drop_index_if_exists( $table, $index, $patch, $fullpath = false ) {
64 if ( $wgDatabase->indexExists( $table, $index ) ) {
65 wfOut( "Dropping $index from table $table... " );
67 $wgDatabase->sourceFile( $patch );
69 $wgDatabase->sourceFile( archive( $patch ) );
73 wfOut( "...$index doesn't exist.\n" );
77 function do_all_updates( $shared = false, $purge = true ) {
78 global $wgDatabase, $wgDBtype;
80 $updater = DatabaseUpdater::newForDb( $wgDatabase, $shared );
82 wfRunHooks( 'LoadExtensionSchemaUpdates', array( &$updater ) );
84 $updater->doUpdates();
86 if ( !defined( 'MW_NO_SETUP' ) ) {
87 define( 'MW_NO_SETUP', true );
90 foreach( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
91 call_user_func_array( array( new $maint, 'execute' ), array() );
94 if ( $wgDBtype === 'postgres' ) {
105 function archive( $name ) {
106 global $wgDBtype, $IP;
107 if ( file_exists( "$IP/maintenance/$wgDBtype/archives/$name" ) ) {
108 return "$IP/maintenance/$wgDBtype/archives/$name";
110 return "$IP/maintenance/archives/$name";
114 function do_interwiki_update() {
115 # Check that interwiki table exists; if it doesn't source it
116 global $wgDatabase, $IP;
117 if ( $wgDatabase->tableExists( "interwiki" ) ) {
118 wfOut( "...already have interwiki table\n" );
121 wfOut( "Creating interwiki table: " );
122 $wgDatabase->sourceFile( archive( "patch-interwiki.sql" ) );
124 wfOut( "Adding default interwiki definitions: " );
125 $wgDatabase->sourceFile( "$IP/maintenance/interwiki.sql" );
129 function do_index_update() {
130 # Check that proper indexes are in place
132 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
133 if ( !$meta->isMultipleKey() ) {
134 wfOut( "Updating indexes to 20031107: " );
135 $wgDatabase->sourceFile( archive( "patch-indexes.sql" ) );
139 wfOut( "...indexes seem up to 20031107 standards\n" );
143 function do_image_index_update() {
146 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
147 if ( !$meta->isMultipleKey() ) {
148 wfOut( "Updating indexes to 20050912: " );
149 $wgDatabase->sourceFile( archive( "patch-mimesearch-indexes.sql" ) );
153 wfOut( "...indexes seem up to 20050912 standards\n" );
157 function do_image_name_unique_update() {
159 if ( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
160 wfOut( "...image primary key already set.\n" );
162 wfOut( "Making img_name the primary key... " );
163 $wgDatabase->sourceFile( archive( "patch-image_name_primary.sql" ) );
168 function do_logging_timestamp_index() {
170 if ( $wgDatabase->indexExists( 'logging', 'times' ) ) {
171 wfOut( "...timestamp key on logging already exists.\n" );
173 wfOut( "Adding timestamp key on logging table... " );
174 $wgDatabase->sourceFile( archive( "patch-logging-times-index.sql" ) );
179 function do_archive_user_index() {
181 if ( $wgDatabase->indexExists( 'archive', 'usertext_timestamp' ) ) {
182 wfOut( "...usertext,timestamp key on archive already exists.\n" );
184 wfOut( "Adding usertext,timestamp key on archive table... " );
185 $wgDatabase->sourceFile( archive( "patch-archive-user-index.sql" ) );
190 function do_image_user_index() {
192 if ( $wgDatabase->indexExists( 'image', 'img_usertext_timestamp' ) ) {
193 wfOut( "...usertext,timestamp key on image already exists.\n" );
195 wfOut( "Adding usertext,timestamp key on image table... " );
196 $wgDatabase->sourceFile( archive( "patch-image-user-index.sql" ) );
201 function do_oldimage_user_index() {
203 if ( $wgDatabase->indexExists( 'oldimage', 'oi_usertext_timestamp' ) ) {
204 wfOut( "...usertext,timestamp key on oldimage already exists.\n" );
206 wfOut( "Adding usertext,timestamp key on oldimage table... " );
207 $wgDatabase->sourceFile( archive( "patch-oldimage-user-index.sql" ) );
212 function do_watchlist_update() {
214 $fname = 'do_watchlist_update';
215 if ( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
216 wfOut( "...the watchlist table is already set up for email notification.\n" );
218 wfOut( "Adding wl_notificationtimestamp field for email notification management." );
219 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
220 $wgDatabase->sourceFile( archive( 'patch-email-notification.sql' ) );
223 # Check if we need to add talk page rows to the watchlist
224 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
225 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
226 if ( $talk != $nontalk ) {
227 wfOut( "Adding missing watchlist talk page rows... " );
230 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
232 'wl_user' => 'wl_user',
233 'wl_namespace' => 'wl_namespace | 1',
234 'wl_title' => 'wl_title',
235 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
236 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
239 wfOut( "...watchlist talk page rows already present\n" );
243 function do_copy_newtalk_to_watchlist() {
246 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
247 $wgDatabase->tableName( 'user_newtalk' ) );
248 $num_newtalks = $wgDatabase->numRows( $res );
249 wfOut( "Now converting $num_newtalks user_newtalk entries to watchlist table entries ... \n" );
252 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
253 $wluser = $wgDatabase->fetchObject( $res );
254 if ( $wluser->user_id == 0 ) { # anonymous users ... have IP numbers as "names"
255 if ( $user->isIP( $wluser->user_ip ) ) { # do only if it really looks like an IP number (double checked)
256 $wgDatabase->replace( 'watchlist',
257 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
258 array( 'wl_user' => 0,
259 'wl_namespace' => NS_USER_TALK,
260 'wl_title' => $wluser->user_ip,
261 'wl_notificationtimestamp' => '19700101000000'
262 ), 'updaters.inc::do_watchlist_update2'
265 } else { # normal users ... have user_ids
266 $user->setID( $wluser->user_id );
267 $wgDatabase->replace( 'watchlist',
268 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
269 array( 'wl_user' => $user->getID(),
270 'wl_namespace' => NS_USER_TALK,
271 'wl_title' => $user->getName(),
272 'wl_notificationtimestamp' => '19700101000000'
273 ), 'updaters.inc::do_watchlist_update3'
280 function do_user_update() {
282 if ( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
283 wfOut( "User table contains old email authentication field. Dropping... " );
284 $wgDatabase->sourceFile( archive( 'patch-email-authentication.sql' ) );
287 wfOut( "...user table does not contain old email authentication field.\n" );
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.
295 function check_bin( $table, $field, $patchFile ) {
296 global $wgDatabase, $wgDBtype;
297 if ( $wgDBtype != 'mysql' )
299 $tableName = $wgDatabase->tableName( $table );
300 $res = $wgDatabase->query( "SELECT $field FROM $tableName LIMIT 0" );
301 $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
303 if ( in_array( 'binary', $flags ) ) {
304 wfOut( "...$table table has correct $field encoding.\n" );
306 wfOut( "Fixing $field encoding on $table table... " );
307 $wgDatabase->sourceFile( archive( $patchFile ) );
312 function do_schema_restructuring() {
314 $fname = "do_schema_restructuring";
315 if ( $wgDatabase->tableExists( 'page' ) ) {
316 wfOut( "...page table already exists.\n" );
318 wfOut( "...converting from cur/old to page/revision/text DB structure.\n" );
319 wfOut( wfTimestamp( TS_DB ) );
320 wfOut( "......checking for duplicate entries.\n" );
322 list ( $cur, $old, $page, $revision, $text ) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
324 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
325 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
327 if ( $wgDatabase->numRows( $rows ) > 0 ) {
328 wfOut( wfTimestamp( TS_DB ) );
329 wfOut( "......<b>Found duplicate entries</b>\n" );
330 wfOut( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
331 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
332 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
333 $duplicate[$row->cur_namespace] = array();
335 $duplicate[$row->cur_namespace][] = $row->cur_title;
336 wfOut( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
338 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
340 foreach ( $duplicate as $ns => $titles ) {
346 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
348 foreach ( $titles as $t ) {
350 $sql .= $wgDatabase->addQuotes( $t );
353 $sql .= ', ' . $wgDatabase->addQuotes( $t );
358 # By sorting descending, the most recent entry will be the first in the list.
359 # All following entries will be deleted by the next while-loop.
360 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
362 $rows = $wgDatabase->query( $sql, $fname );
364 $prev_title = $prev_namespace = false;
367 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
368 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
369 $deleteId[] = $row->cur_id;
371 $prev_title = $row->cur_title;
372 $prev_namespace = $row->cur_namespace;
374 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
375 $rows = $wgDatabase->query( $sql, $fname );
376 wfOut( wfTimestamp( TS_DB ) );
377 wfOut( "......<b>Deleted</b> " . $wgDatabase->affectedRows() . " records.\n" );
381 wfOut( wfTimestamp( TS_DB ) );
382 wfOut( "......Creating tables.\n" );
383 $wgDatabase->query( "CREATE TABLE $page (
384 page_id int(8) unsigned NOT NULL auto_increment,
385 page_namespace int NOT NULL,
386 page_title varchar(255) binary NOT NULL,
387 page_restrictions tinyblob NOT NULL,
388 page_counter bigint(20) unsigned NOT NULL default '0',
389 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
390 page_is_new tinyint(1) unsigned NOT NULL default '0',
391 page_random real unsigned NOT NULL,
392 page_touched char(14) binary NOT NULL default '',
393 page_latest int(8) unsigned NOT NULL,
394 page_len int(8) unsigned NOT NULL,
396 PRIMARY KEY page_id (page_id),
397 UNIQUE INDEX name_title (page_namespace,page_title),
400 ) ENGINE=InnoDB", $fname );
401 $wgDatabase->query( "CREATE TABLE $revision (
402 rev_id int(8) unsigned NOT NULL auto_increment,
403 rev_page int(8) unsigned NOT NULL,
404 rev_comment tinyblob NOT NULL,
405 rev_user int(5) unsigned NOT NULL default '0',
406 rev_user_text varchar(255) binary NOT NULL default '',
407 rev_timestamp char(14) binary NOT NULL default '',
408 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
409 rev_deleted tinyint(1) unsigned NOT NULL default '0',
410 rev_len int(8) unsigned,
411 rev_parent_id int(8) unsigned default NULL,
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 ) ENGINE=InnoDB", $fname );
420 wfOut( wfTimestamp( TS_DB ) );
421 wfOut( "......Locking tables.\n" );
422 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
424 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
425 wfOut( wfTimestamp( TS_DB ) );
426 wfOut( "......maxold is {$maxold}\n" );
428 wfOut( wfTimestamp( TS_DB ) );
429 global $wgLegacySchemaConversion;
430 if ( $wgLegacySchemaConversion ) {
431 // Create HistoryBlobCurStub entries.
432 // Text will be pulled from the leftover 'cur' table at runtime.
433 wfOut( "......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'";
437 // Copy all cur text in immediately: this may take longer but avoids
438 // having to keep an extra table around.
439 wfOut( "......Moving text from cur.\n" );
440 $cur_text = 'cur_text';
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 );
448 wfOut( wfTimestamp( TS_DB ) );
449 wfOut( "......Setting up revision table.\n" );
450 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
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 );
456 wfOut( wfTimestamp( TS_DB ) );
457 wfOut( "......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)
463 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
465 wfOut( wfTimestamp( TS_DB ) );
466 wfOut( "......Unlocking tables.\n" );
467 $wgDatabase->query( "UNLOCK TABLES", $fname );
469 wfOut( wfTimestamp( TS_DB ) );
470 wfOut( "......Renaming old.\n" );
471 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
473 wfOut( wfTimestamp( TS_DB ) );
474 wfOut( "...done.\n" );
478 function do_inverse_timestamp() {
480 if ( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
481 wfOut( "Removing revision.inverse_timestamp and fixing indexes... " );
482 $wgDatabase->sourceFile( archive( 'patch-inverse_timestamp.sql' ) );
485 wfOut( "...revision timestamp indexes already up to 2005-03-13\n" );
489 function do_text_id() {
491 if ( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
492 wfOut( "...rev_text_id already in place.\n" );
494 wfOut( "Adding rev_text_id field... " );
495 $wgDatabase->sourceFile( archive( 'patch-rev_text_id.sql' ) );
500 function do_pagelinks_update() {
502 if ( $wgDatabase->tableExists( 'pagelinks' ) ) {
503 wfOut( "...already have pagelinks table.\n" );
505 wfOut( "Converting links and brokenlinks tables to pagelinks... " );
506 $wgDatabase->sourceFile( archive( 'patch-pagelinks.sql' ) );
510 global $wgCanonicalNamespaceNames;
511 foreach ( $wgCanonicalNamespaceNames as $ns => $name ) {
513 do_pagelinks_namespace( $ns );
519 function do_pagelinks_namespace( $namespace ) {
520 global $wgDatabase, $wgContLang;
522 $ns = intval( $namespace );
523 wfOut( "Cleaning up broken links for namespace $ns... " );
525 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
526 $name = $wgContLang->getNsText( $ns );
527 $prefix = $wgDatabase->strencode( $name );
528 $likeprefix = str_replace( '_', '\\_', $prefix );
530 $sql = "UPDATE $pagelinks
531 SET pl_namespace=$ns,
532 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
534 AND pl_title LIKE '$likeprefix:%'";
536 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
540 function do_drop_img_type() {
543 if ( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
544 wfOut( "Dropping unused img_type field in image table... " );
545 $wgDatabase->sourceFile( archive( 'patch-drop_img_type.sql' ) );
548 wfOut( "...no img_type field in image table; Good.\n" );
552 function do_old_links_update() {
553 if( !defined( 'MW_NO_SETUP' ) ) {
554 define( 'MW_NO_SETUP', true );
556 require( "convertLinks.php" );
557 $cl = new ConvertLinks();
561 function fix_ancient_imagelinks() {
563 $info = $wgDatabase->fieldInfo( 'imagelinks', 'il_from' );
564 if ( $info && $info->type() === 'string' ) {
565 wfOut( "Fixing ancient broken imagelinks table.\n" );
566 wfOut( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
567 $wgDatabase->sourceFile( archive( 'patch-fix-il_from.sql' ) );
570 wfOut( "...il_from OK\n" );
574 function do_user_unique_update() {
576 $duper = new UserDupes( $wgDatabase );
577 if ( $duper->hasUniqueIndex() ) {
578 wfOut( "...already have unique user_name index.\n" );
580 if ( !$duper->clearDupes() ) {
581 wfOut( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
583 wfOut( "Adding unique index on user_name... " );
584 $wgDatabase->sourceFile( archive( 'patch-user_nameindex.sql' ) );
589 function do_user_groups_update() {
590 $fname = 'do_user_groups_update';
593 if ( $wgDatabase->tableExists( 'user_groups' ) ) {
594 wfOut( "...user_groups table already exists.\n" );
595 return do_user_groups_reformat();
598 wfOut( "Adding user_groups table... " );
599 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
602 if ( !$wgDatabase->tableExists( 'user_rights' ) ) {
603 if ( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
604 wfOut( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
605 $wgDatabase->sourceFile( archive( 'patch-user_rights.sql' ) );
608 wfOut( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
609 wfOut( "*** You may need to manually configure some sysops by manipulating\n" );
610 wfOut( "*** the user_groups table.\n" );
615 wfOut( "Converting user_rights table to user_groups... " );
616 $result = $wgDatabase->select( 'user_rights',
617 array( 'ur_user', 'ur_rights' ),
618 array( "ur_rights != ''" ),
621 while ( $row = $wgDatabase->fetchObject( $result ) ) {
622 $groups = array_unique(
624 explode( ',', $row->ur_rights ) ) );
626 foreach ( $groups as $group ) {
627 $wgDatabase->insert( 'user_groups',
629 'ug_user' => $row->ur_user,
630 'ug_group' => $group ),
637 function do_user_groups_reformat() {
638 # Check for bogus formats from previous 1.5 alpha code.
640 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
642 if ( $info->type() == 'int' ) {
643 $oldug = $wgDatabase->tableName( 'user_groups' );
644 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
645 wfOut( "user_groups is in bogus intermediate format. Renaming to $newug... " );
646 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
649 wfOut( "Re-adding fresh user_groups table... " );
650 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
654 wfOut( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
655 wfOut( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
658 wfOut( "...user_groups is in current format.\n" );
663 function do_watchlist_null() {
664 # Make sure wl_notificationtimestamp can be NULL,
665 # and update old broken items.
667 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
669 if ( !$info->nullable() ) {
670 wfOut( "Making wl_notificationtimestamp nullable... " );
671 $wgDatabase->sourceFile( archive( 'patch-watchlist-null.sql' ) );
674 wfOut( "...wl_notificationtimestamp is already nullable.\n" );
682 function do_page_random_update() {
685 wfOut( "Setting page_random to a random value on rows where it equals 0..." );
687 $page = $wgDatabase->tableName( 'page' );
688 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
689 $rows = $wgDatabase->affectedRows();
691 wfOut( "changed $rows rows\n" );
694 function do_templatelinks_update() {
696 $fname = 'do_templatelinks_update';
698 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
699 wfOut( "...templatelinks table already exists\n" );
702 wfOut( "Creating templatelinks table...\n" );
703 $wgDatabase->sourceFile( archive( 'patch-templatelinks.sql' ) );
704 wfOut( "Populating...\n" );
705 if ( wfGetLB()->getServerCount() > 1 ) {
706 // Slow, replication-friendly update
707 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
708 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
710 while ( $row = $wgDatabase->fetchObject( $res ) ) {
711 $count = ( $count + 1 ) % 100;
713 if ( function_exists( 'wfWaitForSlaves' ) ) {
714 wfWaitForSlaves( 10 );
719 $wgDatabase->insert( 'templatelinks',
721 'tl_from' => $row->pl_from,
722 'tl_namespace' => $row->pl_namespace,
723 'tl_title' => $row->pl_title,
730 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
732 'tl_from' => 'pl_from',
733 'tl_namespace' => 'pl_namespace',
734 'tl_title' => 'pl_title'
740 wfOut( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
743 // Add index on ( rc_namespace, rc_user_text ) [Jul. 2006]
744 // Add index on ( rc_user_text, rc_timestamp ) [Nov. 2006]
745 function do_rc_indices_update() {
747 wfOut( "Checking for additional recent changes indices...\n" );
750 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
751 'rc_user_text' => 'patch-rc_user_text-index.sql',
754 foreach ( $indexes as $index => $patch ) {
755 $info = $wgDatabase->indexInfo( 'recentchanges', $index, __METHOD__ );
757 wfOut( "...index `{$index}` not found; adding..." );
758 $wgDatabase->sourceFile( archive( $patch ) );
761 wfOut( "...index `{$index}` seems ok.\n" );
766 function index_has_field( $table, $index, $field ) {
768 wfOut( "Checking if $table index $index includes field $field...\n" );
769 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
771 foreach ( $info as $row ) {
772 if ( $row->Column_name == $field ) {
773 wfOut( "...index $index on table $table seems to be ok\n" );
778 wfOut( "...index $index on table $table has no field $field; adding\n" );
782 function do_backlinking_indices_update() {
784 wfOut( "Checking for backlinking indices...\n" );
785 if ( !index_has_field( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
786 !index_has_field( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
787 !index_has_field( 'imagelinks', 'il_to', 'il_from' ) )
789 $wgDatabase->sourceFile( archive( 'patch-backlinkindexes.sql' ) );
790 wfOut( "...backlinking indices updated\n" );
794 function do_categorylinks_indices_update() {
796 wfOut( "Checking for categorylinks indices...\n" );
797 if ( !index_has_field( 'categorylinks', 'cl_sortkey', 'cl_from' ) )
799 $wgDatabase->sourceFile( archive( 'patch-categorylinksindex.sql' ) );
800 wfOut( "...categorylinks indices updated\n" );
804 function do_filearchive_indices_update() {
806 wfOut( "Checking filearchive indices...\n" );
807 $info = $wgDatabase->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
810 $wgDatabase->sourceFile( archive( 'patch-filearchive-user-index.sql' ) );
811 wfOut( "...filearchive indices updated\n" );
815 function maybe_do_profiling_memory_update() {
817 if ( !$wgDatabase->tableExists( 'profiling' ) ) {
819 } elseif ( $wgDatabase->fieldExists( 'profiling', 'pf_memory' ) ) {
820 wfOut( "...profiling table has pf_memory field.\n" );
822 wfOut( "Adding pf_memory field to table profiling..." );
823 $wgDatabase->sourceFile( archive( 'patch-profiling-memory.sql' ) );
828 function do_stats_init() {
829 // Sometimes site_stats table is not properly populated.
831 wfOut( "\nChecking site_stats row..." );
832 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
833 if ( $row === false ) {
834 wfOut( "data is missing! rebuilding...\n" );
835 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
836 wfOut( "missing ss_total_pages, rebuilding...\n" );
841 SiteStatsInit::doAllAndCommit( false );
844 function do_active_users_init() {
846 $activeUsers = $wgDatabase->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
847 if ( $activeUsers == -1 ) {
848 $activeUsers = $wgDatabase->selectField( 'recentchanges',
849 'COUNT( DISTINCT rc_user_text )',
850 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
852 $wgDatabase->update( 'site_stats',
853 array( 'ss_active_users' => intval( $activeUsers ) ),
854 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
857 wfOut( "...ss_active_users user count set...\n" );
860 function purge_cache() {
862 # We can't guarantee that the user will be able to use TRUNCATE,
863 # but we know that DELETE is available to us
864 wfOut( "Purging caches..." );
865 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
869 function do_restrictions_update() {
870 # Adding page_restrictions table, obsoleting page.page_restrictions.
871 # Migrating old restrictions to new table
872 # -- Andrew Garrett, January 2007.
876 $name = 'page_restrictions';
877 $patch = 'patch-page_restrictions.sql';
878 $patch2 = 'patch-page_restrictions_sortkey.sql';
880 if ( $wgDatabase->tableExists( $name ) ) {
881 wfOut( "...$name table already exists.\n" );
883 wfOut( "Creating $name table..." );
884 $wgDatabase->sourceFile( archive( $patch ) );
885 $wgDatabase->sourceFile( archive( $patch2 ) );
888 wfOut( "Migrating old restrictions to new table..." );
890 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array( "page_restrictions!=''", "page_restrictions!='edit=:move='" ), __METHOD__ );
894 while ( $row = $wgDatabase->fetchObject( $res ) ) {
895 $count = ( $count + 1 ) % 100;
898 if ( function_exists( 'wfWaitForSlaves' ) ) {
899 wfWaitForSlaves( 10 );
905 # Figure out what the restrictions are..
907 $flatrestrictions = explode( ':', $row->page_restrictions );
909 $restrictions = array ();
910 foreach ( $flatrestrictions as $restriction ) {
911 $thisrestriction = explode( '=', $restriction, 2 );
912 if ( count( $thisrestriction ) == 1 ) {
913 // Compatibility with old protections from before
914 // separate move protection was added.
915 list( $level ) = $thisrestriction;
917 $restrictions['edit'] = $level;
918 $restrictions['move'] = $level;
921 list( $type, $level ) = $thisrestriction;
923 $restrictions[$type] = $level;
927 $wgDatabase->update( 'page', array ( 'page_restrictions' => '' ), array( 'page_id' => $id ), __METHOD__ );
931 foreach ( $restrictions as $type => $level ) {
932 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
934 'pr_level' => $level,
936 'pr_expiry' => 'infinity' ),
944 function do_category_population() {
945 if ( update_row_exists( 'populate category' ) ) {
946 wfOut( "...category table already populated.\n" );
949 require_once( 'populateCategory.php' );
951 "Populating category table, printing progress markers. " .
952 "For large databases, you\n" .
953 "may want to hit Ctrl-C and do this manually with maintenance/\n" .
954 "populateCategory.php.\n"
956 $task = new PopulateCategory();
958 wfOut( "Done populating category table.\n" );
961 function do_populate_parent_id() {
962 if ( update_row_exists( 'populate rev_parent_id' ) ) {
963 wfOut( "...rev_parent_id column already populated.\n" );
966 require_once( 'populateParentId.php' );
967 $task = new PopulateParentId();
971 function do_populate_rev_len() {
972 if ( update_row_exists( 'populate rev_len' ) ) {
973 wfOut( "...rev_len column already populated.\n" );
976 require_once( 'populateRevisionLength.php' );
977 $task = new PopulateRevisionLength();
981 function do_collation_update() {
982 global $wgDatabase, $wgCollationVersion;
983 if ( $wgDatabase->selectField(
986 'cl_collation != ' . $wgDatabase->addQuotes( $wgCollationVersion ),
989 wfOut( "...collations up-to-date.\n" );
992 require_once( 'updateCollation.php' );
993 $task = new UpdateCollation();
997 function sqlite_initial_indexes() {
999 // initial-indexes.sql fails if the indexes are already present, so we perform a quick check if our database is newer.
1000 if ( update_row_exists( 'initial_indexes' ) || $wgDatabase->indexExists( 'user', 'user_name' ) ) {
1001 wfOut( "...have initial indexes\n" );
1004 wfOut( "Adding initial indexes..." );
1005 $wgDatabase->sourceFile( archive( 'initial-indexes.sql' ) );
1009 function sqlite_setup_searchindex() {
1011 $module = $wgDatabase->getFulltextSearchModule();
1012 $fts3tTable = update_row_exists( 'fts3' );
1013 if ( $fts3tTable && !$module ) {
1014 wfOut( '...PHP is missing FTS3 support, downgrading tables...' );
1015 $wgDatabase->sourceFile( archive( 'searchindex-no-fts.sql' ) );
1017 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
1018 wfOut( '...adding FTS3 search capabilities...' );
1019 $wgDatabase->sourceFile( archive( 'searchindex-fts3.sql' ) );
1022 wfOut( "...fulltext search table appears to be in order.\n" );
1026 function do_unique_pl_tl_il() {
1028 $info = $wgDatabase->indexInfo( 'pagelinks', 'pl_namespace' );
1029 if ( is_array( $info ) && !$info[0]->Non_unique ) {
1030 wfOut( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
1032 wfOut( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
1033 $wgDatabase->sourceFile( archive( 'patch-pl-tl-il-unique.sql' ) );
1038 function do_log_search_population() {
1039 if ( update_row_exists( 'populate log_search' ) ) {
1040 wfOut( "...log_search table already populated.\n" );
1043 require_once( 'populateLogSearch.php' );
1045 "Populating log_search table, printing progress markers. For large\n" .
1046 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1047 "maintenance/populateLogSearch.php.\n" );
1048 $task = new PopulateLogSearch();
1050 wfOut( "Done populating log_search table.\n" );
1053 function rename_eu_wiki_id() {
1055 if ( $wgDatabase->fieldExists( 'external_user', 'eu_local_id' ) ) {
1056 wfOut( "...eu_wiki_id already renamed to eu_local_id.\n" );
1059 wfOut( "Renaming eu_wiki_id -> eu_local_id... " );
1060 $wgDatabase->sourceFile( archive( 'patch-eu_local_id.sql' ) );
1064 function do_update_transcache_field() {
1066 if ( update_row_exists( 'convert transcache field' ) ) {
1067 wfOut( "...transcache tc_time already converted.\n" );
1070 wfOut( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
1071 $wgDatabase->sourceFile( archive( 'patch-tc-timestamp.sql' ) );
1076 function do_update_mime_minor_field() {
1077 if ( update_row_exists( 'mime_minor_length' ) ) {
1078 wfOut( "...*_mime_minor fields are already long enough.\n" );
1081 wfOut( "Altering all *_mime_minor fields to 100 bytes in size ... " );
1082 $wgDatabase->sourceFile( archive( 'patch-mime_minor_length.sql' ) );