Tweak SpecialRecentChangesQuery hook. Need to be able to modify selected fields on...
[mediawiki.git] / maintenance / updaters.inc
blobb708476c35fbc6f547750a911e47e14998078765
1 <?php
2 /**
3  * @file
4  * @ingroup Maintenance
5  */
7 if ( !defined( 'MEDIAWIKI' ) ) {
8         echo "This file is not a valid entry point\n";
9         exit( 1 );
12 require_once 'userDupes.inc';
13 # Extension updates
14 require_once( "$IP/includes/Hooks.php" );
16 /**
17  * @deprecated. Do not use, ever.
18  */
19 $wgUpdates = array();
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
33 # created!
34 function update_row_exists( $key ) {
35         global $wgDatabase;
36         $row = $wgDatabase->selectRow(
37                 'updatelog',
38                 '1',
39                 array( 'ul_key' => $key ),
40                 __FUNCTION__
41         );
42         return (bool)$row;
45 function modify_field( $table, $field, $patch, $fullpath = false ) {
46         global $wgDatabase;
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" );
51         } else {
52                 wfOut( "Modifying $field field of table $table..." );
53                 if ( $fullpath ) {
54                         $wgDatabase->sourceFile( $patch );
55                 } else {
56                         $wgDatabase->sourceFile( archive( $patch ) );
57                 }
58                 wfOut( "ok\n" );
59         }
62 function drop_index_if_exists( $table, $index, $patch, $fullpath = false ) {
63         global $wgDatabase;
64         if ( $wgDatabase->indexExists( $table, $index ) ) {
65                 wfOut( "Dropping $index from table $table... " );
66                 if ( $fullpath ) {
67                         $wgDatabase->sourceFile( $patch );
68                 } else {
69                         $wgDatabase->sourceFile( archive( $patch ) );
70                 }
71                 wfOut( "ok\n" );
72         } else {
73                 wfOut( "...$index doesn't exist.\n" );
74         }
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 );
88         }
90         foreach( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
91                 call_user_func_array( array( new $maint, 'execute' ), array() );
92         }
94         if ( $wgDBtype === 'postgres' ) {
95                 return;
96         }
98         do_stats_init();
100         if ( $purge ) {
101                 purge_cache();
102         }
105 function archive( $name ) {
106         global $wgDBtype, $IP;
107         if ( file_exists( "$IP/maintenance/$wgDBtype/archives/$name" ) ) {
108                 return "$IP/maintenance/$wgDBtype/archives/$name";
109         } else {
110                 return "$IP/maintenance/archives/$name";
111         }
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" );
119                 return true;
120         }
121         wfOut( "Creating interwiki table: " );
122         $wgDatabase->sourceFile( archive( "patch-interwiki.sql" ) );
123         wfOut( "ok\n" );
124         wfOut( "Adding default interwiki definitions: " );
125         $wgDatabase->sourceFile( "$IP/maintenance/interwiki.sql" );
126         wfOut( "ok\n" );
129 function do_index_update() {
130         # Check that proper indexes are in place
131         global $wgDatabase;
132         $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
133         if ( !$meta->isMultipleKey() ) {
134                 wfOut( "Updating indexes to 20031107: " );
135                 $wgDatabase->sourceFile( archive( "patch-indexes.sql" ) );
136                 wfOut( "ok\n" );
137                 return true;
138         }
139         wfOut( "...indexes seem up to 20031107 standards\n" );
140         return false;
143 function do_image_index_update() {
144         global $wgDatabase;
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" ) );
150                 wfOut( "ok\n" );
151                 return true;
152         }
153         wfOut( "...indexes seem up to 20050912 standards\n" );
154         return false;
157 function do_image_name_unique_update() {
158         global $wgDatabase;
159         if ( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
160                 wfOut( "...image primary key already set.\n" );
161         } else {
162                 wfOut( "Making img_name the primary key... " );
163                 $wgDatabase->sourceFile( archive( "patch-image_name_primary.sql" ) );
164                 wfOut( "ok\n" );
165         }
168 function do_watchlist_update() {
169         global $wgDatabase;
170         $fname = 'do_watchlist_update';
171         if ( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
172                 wfOut( "...the watchlist table is already set up for email notification.\n" );
173         } else {
174                 wfOut( "Adding wl_notificationtimestamp field for email notification management." );
175                 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
176                 $wgDatabase->sourceFile( archive( 'patch-email-notification.sql' ) );
177                 wfOut( "ok\n" );
178         }
179         # Check if we need to add talk page rows to the watchlist
180         $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
181         $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
182         if ( $talk != $nontalk ) {
183                 wfOut( "Adding missing watchlist talk page rows... " );
184                 flush();
186                 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
187                         array(
188                                 'wl_user' => 'wl_user',
189                                 'wl_namespace' => 'wl_namespace | 1',
190                                 'wl_title' => 'wl_title',
191                                 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
192                         ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
193                 wfOut( "ok\n" );
194         } else {
195                 wfOut( "...watchlist talk page rows already present\n" );
196         }
199 function do_copy_newtalk_to_watchlist() {
200         global $wgDatabase;
202         $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
203                 $wgDatabase->tableName( 'user_newtalk' ) );
204         $num_newtalks = $wgDatabase->numRows( $res );
205         wfOut( "Now converting $num_newtalks user_newtalk entries to watchlist table entries ... \n" );
207         $user = new User();
208         for ( $i = 1; $i <= $num_newtalks; $i++ ) {
209                 $wluser = $wgDatabase->fetchObject( $res );
210                 if ( $wluser->user_id == 0 ) { # anonymous users ... have IP numbers as "names"
211                         if ( $user->isIP( $wluser->user_ip ) ) { # do only if it really looks like an IP number (double checked)
212                                 $wgDatabase->replace( 'watchlist',
213                                         array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
214                                         array( 'wl_user' => 0,
215                                                 'wl_namespace' => NS_USER_TALK,
216                                                 'wl_title' => $wluser->user_ip,
217                                                 'wl_notificationtimestamp' => '19700101000000'
218                                                 ), 'updaters.inc::do_watchlist_update2'
219                                         );
220                         }
221                 } else { # normal users ... have user_ids
222                         $user->setID( $wluser->user_id );
223                         $wgDatabase->replace( 'watchlist',
224                                 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
225                                 array( 'wl_user' => $user->getID(),
226                                         'wl_namespace' => NS_USER_TALK,
227                                         'wl_title' => $user->getName(),
228                                         'wl_notificationtimestamp' => '19700101000000'
229                                         ), 'updaters.inc::do_watchlist_update3'
230                                 );
231                 }
232         }
233         wfOut( "Done.\n" );
237  * 1.4 betas were missing the 'binary' marker from logging.log_title,
238  * which causes a collation mismatch error on joins in MySQL 4.1.
239  */
240 function check_bin( $table, $field, $patchFile ) {
241         global $wgDatabase, $wgDBtype;
242         if ( $wgDBtype != 'mysql' )
243                 return;
244         $tableName = $wgDatabase->tableName( $table );
245         $res = $wgDatabase->query( "SELECT $field FROM $tableName LIMIT 0" );
246         $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
248         if ( in_array( 'binary', $flags ) ) {
249                 wfOut( "...$table table has correct $field encoding.\n" );
250         } else {
251                 wfOut( "Fixing $field encoding on $table table... " );
252                 $wgDatabase->sourceFile( archive( $patchFile ) );
253                 wfOut( "ok\n" );
254         }
257 function do_schema_restructuring() {
258         global $wgDatabase;
259         $fname = "do_schema_restructuring";
260         if ( $wgDatabase->tableExists( 'page' ) ) {
261                 wfOut( "...page table already exists.\n" );
262         } else {
263                 wfOut( "...converting from cur/old to page/revision/text DB structure.\n" );
264                 wfOut( wfTimestamp( TS_DB ) );
265                 wfOut( "......checking for duplicate entries.\n" );
267                 list ( $cur, $old, $page, $revision, $text ) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
269                 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
270                                 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
272                 if ( $wgDatabase->numRows( $rows ) > 0 ) {
273                         wfOut( wfTimestamp( TS_DB ) );
274                         wfOut( "......<b>Found duplicate entries</b>\n" );
275                         wfOut( sprintf( "<b>      %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
276                         while ( $row = $wgDatabase->fetchObject( $rows ) ) {
277                                 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
278                                         $duplicate[$row->cur_namespace] = array();
279                                 }
280                                 $duplicate[$row->cur_namespace][] = $row->cur_title;
281                                 wfOut( sprintf( "      %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
282                         }
283                         $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
284                         $firstCond = true;
285                         foreach ( $duplicate as $ns => $titles ) {
286                                 if ( $firstCond ) {
287                                         $firstCond = false;
288                                 } else {
289                                         $sql .= ' OR ';
290                                 }
291                                 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
292                                 $first = true;
293                                 foreach ( $titles as $t ) {
294                                         if ( $first ) {
295                                                 $sql .= $wgDatabase->addQuotes( $t );
296                                                 $first = false;
297                                         } else {
298                                                 $sql .= ', ' . $wgDatabase->addQuotes( $t );
299                                         }
300                                 }
301                                 $sql .= ") ) \n";
302                         }
303                         # By sorting descending, the most recent entry will be the first in the list.
304                         # All following entries will be deleted by the next while-loop.
305                         $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
307                         $rows = $wgDatabase->query( $sql, $fname );
309                         $prev_title = $prev_namespace = false;
310                         $deleteId = array();
312                         while ( $row = $wgDatabase->fetchObject( $rows ) ) {
313                                 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
314                                         $deleteId[] = $row->cur_id;
315                                 }
316                                 $prev_title     = $row->cur_title;
317                                 $prev_namespace = $row->cur_namespace;
318                         }
319                         $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
320                         $rows = $wgDatabase->query( $sql, $fname );
321                         wfOut( wfTimestamp( TS_DB ) );
322                         wfOut( "......<b>Deleted</b> " . $wgDatabase->affectedRows() . " records.\n" );
323                 }
326                 wfOut( wfTimestamp( TS_DB ) );
327                 wfOut( "......Creating tables.\n" );
328                 $wgDatabase->query( "CREATE TABLE $page (
329                         page_id int(8) unsigned NOT NULL auto_increment,
330                         page_namespace int NOT NULL,
331                         page_title varchar(255) binary NOT NULL,
332                         page_restrictions tinyblob NOT NULL,
333                         page_counter bigint(20) unsigned NOT NULL default '0',
334                         page_is_redirect tinyint(1) unsigned NOT NULL default '0',
335                         page_is_new tinyint(1) unsigned NOT NULL default '0',
336                         page_random real unsigned NOT NULL,
337                         page_touched char(14) binary NOT NULL default '',
338                         page_latest int(8) unsigned NOT NULL,
339                         page_len int(8) unsigned NOT NULL,
341                         PRIMARY KEY page_id (page_id),
342                         UNIQUE INDEX name_title (page_namespace,page_title),
343                         INDEX (page_random),
344                         INDEX (page_len)
345                         ) ENGINE=InnoDB", $fname );
346                 $wgDatabase->query( "CREATE TABLE $revision (
347                         rev_id int(8) unsigned NOT NULL auto_increment,
348                         rev_page int(8) unsigned NOT NULL,
349                         rev_comment tinyblob NOT NULL,
350                         rev_user int(5) unsigned NOT NULL default '0',
351                         rev_user_text varchar(255) binary NOT NULL default '',
352                         rev_timestamp char(14) binary NOT NULL default '',
353                         rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
354                         rev_deleted tinyint(1) unsigned NOT NULL default '0',
355                         rev_len int(8) unsigned,
356                         rev_parent_id int(8) unsigned default NULL,
357                         PRIMARY KEY rev_page_id (rev_page, rev_id),
358                         UNIQUE INDEX rev_id (rev_id),
359                         INDEX rev_timestamp (rev_timestamp),
360                         INDEX page_timestamp (rev_page,rev_timestamp),
361                         INDEX user_timestamp (rev_user,rev_timestamp),
362                         INDEX usertext_timestamp (rev_user_text,rev_timestamp)
363                         ) ENGINE=InnoDB", $fname );
365                 wfOut( wfTimestamp( TS_DB ) );
366                 wfOut( "......Locking tables.\n" );
367                 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
369                 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
370                 wfOut( wfTimestamp( TS_DB ) );
371                 wfOut( "......maxold is {$maxold}\n" );
373                 wfOut( wfTimestamp( TS_DB ) );
374                 global $wgLegacySchemaConversion;
375                 if ( $wgLegacySchemaConversion ) {
376                         // Create HistoryBlobCurStub entries.
377                         // Text will be pulled from the leftover 'cur' table at runtime.
378                         wfOut( "......Moving metadata from cur; using blob references to text in cur table.\n" );
379                         $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
380                         $cur_flags = "'object'";
381                 } else {
382                         // Copy all cur text in immediately: this may take longer but avoids
383                         // having to keep an extra table around.
384                         wfOut( "......Moving text from cur.\n" );
385                         $cur_text = 'cur_text';
386                         $cur_flags = "''";
387                 }
388                 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
389                                         old_timestamp, old_minor_edit, old_flags)
390                         SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
391                         FROM $cur", $fname );
393                 wfOut( wfTimestamp( TS_DB ) );
394                 wfOut( "......Setting up revision table.\n" );
395                 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
396                                 rev_minor_edit)
397                         SELECT old_id, cur_id, old_comment, old_user, old_user_text,
398                                 old_timestamp, old_minor_edit
399                         FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
401                 wfOut( wfTimestamp( TS_DB ) );
402                 wfOut( "......Setting up page table.\n" );
403                 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
404                         page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
405                         SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
406                                 cur_random, cur_touched, rev_id, LENGTH(cur_text)
407                         FROM $cur,$revision
408                         WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
410                 wfOut( wfTimestamp( TS_DB ) );
411                 wfOut( "......Unlocking tables.\n" );
412                 $wgDatabase->query( "UNLOCK TABLES", $fname );
414                 wfOut( wfTimestamp( TS_DB ) );
415                 wfOut( "......Renaming old.\n" );
416                 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
418                 wfOut( wfTimestamp( TS_DB ) );
419                 wfOut( "...done.\n" );
420         }
423 function do_pagelinks_update() {
424         global $wgDatabase;
425         if ( $wgDatabase->tableExists( 'pagelinks' ) ) {
426                 wfOut( "...already have pagelinks table.\n" );
427         } else {
428                 wfOut( "Converting links and brokenlinks tables to pagelinks... " );
429                 $wgDatabase->sourceFile( archive( 'patch-pagelinks.sql' ) );
430                 wfOut( "ok\n" );
431                 flush();
433                 global $wgCanonicalNamespaceNames;
434                 foreach ( $wgCanonicalNamespaceNames as $ns => $name ) {
435                         if ( $ns != 0 ) {
436                                 do_pagelinks_namespace( $ns );
437                         }
438                 }
439         }
442 function do_pagelinks_namespace( $namespace ) {
443         global $wgDatabase, $wgContLang;
445         $ns = intval( $namespace );
446         wfOut( "Cleaning up broken links for namespace $ns... " );
448         $pagelinks = $wgDatabase->tableName( 'pagelinks' );
449         $name = $wgContLang->getNsText( $ns );
450         $prefix = $wgDatabase->strencode( $name );
451         $likeprefix = str_replace( '_', '\\_', $prefix );
453         $sql = "UPDATE $pagelinks
454                    SET pl_namespace=$ns,
455                        pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
456                  WHERE pl_namespace=0
457                    AND pl_title LIKE '$likeprefix:%'";
459         $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
460         wfOut( "ok\n" );
463 function do_old_links_update() {
464         if( !defined( 'MW_NO_SETUP' ) ) {
465                 define( 'MW_NO_SETUP', true );
466         }
467         require( "convertLinks.php" );
468         $cl = new ConvertLinks();
469         $cl->execute();
472 function fix_ancient_imagelinks() {
473         global $wgDatabase;
474         $info = $wgDatabase->fieldInfo( 'imagelinks', 'il_from' );
475         if ( $info && $info->type() === 'string' ) {
476                 wfOut( "Fixing ancient broken imagelinks table.\n" );
477                 wfOut( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
478                 $wgDatabase->sourceFile( archive( 'patch-fix-il_from.sql' ) );
479                 wfOut( "ok\n" );
480         } else {
481                 wfOut( "...il_from OK\n" );
482         }
485 function do_user_unique_update() {
486         global $wgDatabase;
487         $duper = new UserDupes( $wgDatabase );
488         if ( $duper->hasUniqueIndex() ) {
489                 wfOut( "...already have unique user_name index.\n" );
490         } else {
491                 if ( !$duper->clearDupes() ) {
492                         wfOut( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
493                 }
494                 wfOut( "Adding unique index on user_name... " );
495                 $wgDatabase->sourceFile( archive( 'patch-user_nameindex.sql' ) );
496                 wfOut( "ok\n" );
497         }
500 function do_user_groups_update() {
501         $fname = 'do_user_groups_update';
502         global $wgDatabase;
504         if ( $wgDatabase->tableExists( 'user_groups' ) ) {
505                 wfOut( "...user_groups table already exists.\n" );
506                 return do_user_groups_reformat();
507         }
509         wfOut( "Adding user_groups table... " );
510         $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
511         wfOut( "ok\n" );
513         if ( !$wgDatabase->tableExists( 'user_rights' ) ) {
514                 if ( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
515                         wfOut( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
516                         $wgDatabase->sourceFile( archive( 'patch-user_rights.sql' ) );
517                         wfOut( "ok\n" );
518                 } else {
519                         wfOut( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
520                         wfOut( "*** You may need to manually configure some sysops by manipulating\n" );
521                         wfOut( "*** the user_groups table.\n" );
522                         return;
523                 }
524         }
526         wfOut( "Converting user_rights table to user_groups... " );
527         $result = $wgDatabase->select( 'user_rights',
528                 array( 'ur_user', 'ur_rights' ),
529                 array( "ur_rights != ''" ),
530                 $fname );
532         while ( $row = $wgDatabase->fetchObject( $result ) ) {
533                 $groups = array_unique(
534                         array_map( 'trim',
535                                 explode( ',', $row->ur_rights ) ) );
537                 foreach ( $groups as $group ) {
538                         $wgDatabase->insert( 'user_groups',
539                                 array(
540                                         'ug_user'  => $row->ur_user,
541                                         'ug_group' => $group ),
542                                 $fname );
543                 }
544         }
545         wfOut( "ok\n" );
548 function do_user_groups_reformat() {
549         # Check for bogus formats from previous 1.5 alpha code.
550         global $wgDatabase;
551         $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
553         if ( $info->type() == 'int' ) {
554                 $oldug = $wgDatabase->tableName( 'user_groups' );
555                 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
556                 wfOut( "user_groups is in bogus intermediate format. Renaming to $newug... " );
557                 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
558                 wfOut( "ok\n" );
560                 wfOut( "Re-adding fresh user_groups table... " );
561                 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
562                 wfOut( "ok\n" );
564                 wfOut( "***\n" );
565                 wfOut( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
566                 wfOut( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
567                 wfOut( "***\n" );
568         } else {
569                 wfOut( "...user_groups is in current format.\n" );
570         }
574 function do_watchlist_null() {
575         # Make sure wl_notificationtimestamp can be NULL,
576         # and update old broken items.
577         global $wgDatabase;
578         $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
580         if ( !$info->nullable() ) {
581                 wfOut( "Making wl_notificationtimestamp nullable... " );
582                 $wgDatabase->sourceFile( archive( 'patch-watchlist-null.sql' ) );
583                 wfOut( "ok\n" );
584         } else {
585                 wfOut( "...wl_notificationtimestamp is already nullable.\n" );
586         }
591  * @bug 3946
592  */
593 function do_page_random_update() {
594         global $wgDatabase;
596         wfOut( "Setting page_random to a random value on rows where it equals 0..." );
598         $page = $wgDatabase->tableName( 'page' );
599         $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
600         $rows = $wgDatabase->affectedRows();
602         wfOut( "changed $rows rows\n" );
605 function do_templatelinks_update() {
606         global $wgDatabase;
607         $fname = 'do_templatelinks_update';
609         if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
610                 wfOut( "...templatelinks table already exists\n" );
611                 return;
612         }
613         wfOut( "Creating templatelinks table...\n" );
614         $wgDatabase->sourceFile( archive( 'patch-templatelinks.sql' ) );
615         wfOut( "Populating...\n" );
616         if ( wfGetLB()->getServerCount() > 1 ) {
617                 // Slow, replication-friendly update
618                 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
619                         array( 'pl_namespace' => NS_TEMPLATE ), $fname );
620                 $count = 0;
621                 while ( $row = $wgDatabase->fetchObject( $res ) ) {
622                         $count = ( $count + 1 ) % 100;
623                         if ( $count == 0 ) {
624                                 if ( function_exists( 'wfWaitForSlaves' ) ) {
625                                         wfWaitForSlaves( 10 );
626                                 } else {
627                                         sleep( 1 );
628                                 }
629                         }
630                         $wgDatabase->insert( 'templatelinks',
631                                 array(
632                                         'tl_from' => $row->pl_from,
633                                         'tl_namespace' => $row->pl_namespace,
634                                         'tl_title' => $row->pl_title,
635                                 ), $fname
636                         );
638                 }
639         } else {
640                 // Fast update
641                 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
642                         array(
643                                 'tl_from' => 'pl_from',
644                                 'tl_namespace' => 'pl_namespace',
645                                 'tl_title' => 'pl_title'
646                         ), array(
647                                 'pl_namespace' => 10
648                         ), $fname
649                 );
650         }
651         wfOut( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
654 // Add index on ( rc_namespace, rc_user_text ) [Jul. 2006]
655 // Add index on ( rc_user_text, rc_timestamp ) [Nov. 2006]
656 function do_rc_indices_update() {
657         global $wgDatabase;
658         wfOut( "Checking for additional recent changes indices...\n" );
660         $indexes = array(
661                 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
662                 'rc_user_text' => 'patch-rc_user_text-index.sql',
663         );
665         foreach ( $indexes as $index => $patch ) {
666                 $info = $wgDatabase->indexInfo( 'recentchanges', $index, __METHOD__ );
667                 if ( !$info ) {
668                         wfOut( "...index `{$index}` not found; adding..." );
669                         $wgDatabase->sourceFile( archive( $patch ) );
670                         wfOut( "done.\n" );
671                 } else {
672                         wfOut( "...index `{$index}` seems ok.\n" );
673                 }
674         }
677 function index_has_field( $table, $index, $field ) {
678         global $wgDatabase;
679         wfOut( "Checking if $table index $index includes field $field...\n" );
680         $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
681         if ( $info ) {
682                 foreach ( $info as $row ) {
683                         if ( $row->Column_name == $field ) {
684                                 wfOut( "...index $index on table $table seems to be ok\n" );
685                                 return true;
686                         }
687                 }
688         }
689         wfOut( "...index $index on table $table has no field $field; adding\n" );
690         return false;
693 function do_backlinking_indices_update() {
694         global $wgDatabase;
695         wfOut( "Checking for backlinking indices...\n" );
696         if ( !index_has_field( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
697                 !index_has_field( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
698                 !index_has_field( 'imagelinks', 'il_to', 'il_from' ) )
699         {
700                 $wgDatabase->sourceFile( archive( 'patch-backlinkindexes.sql' ) );
701                 wfOut( "...backlinking indices updated\n" );
702         }
705 function do_categorylinks_indices_update() {
706         global $wgDatabase;
707         wfOut( "Checking for categorylinks indices...\n" );
708         if ( !index_has_field( 'categorylinks', 'cl_sortkey', 'cl_from' ) )
709         {
710                 $wgDatabase->sourceFile( archive( 'patch-categorylinksindex.sql' ) );
711                 wfOut( "...categorylinks indices updated\n" );
712         }
715 function do_filearchive_indices_update() {
716         global $wgDatabase;
717         wfOut( "Checking filearchive indices...\n" );
718         $info = $wgDatabase->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
719         if ( !$info )
720         {
721                 $wgDatabase->sourceFile( archive( 'patch-filearchive-user-index.sql' ) );
722                 wfOut( "...filearchive indices updated\n" );
723         }
726 function maybe_do_profiling_memory_update() {
727         global $wgDatabase;
728         if ( !$wgDatabase->tableExists( 'profiling' ) ) {
729                 // Simply ignore
730         } elseif ( $wgDatabase->fieldExists( 'profiling', 'pf_memory' ) ) {
731                 wfOut( "...profiling table has pf_memory field.\n" );
732         } else {
733                 wfOut( "Adding pf_memory field to table profiling..." );
734                 $wgDatabase->sourceFile( archive( 'patch-profiling-memory.sql' ) );
735                 wfOut( "ok\n" );
736         }
739 function do_stats_init() {
740         // Sometimes site_stats table is not properly populated.
741         global $wgDatabase;
742         wfOut( "\nChecking site_stats row..." );
743         $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
744         if ( $row === false ) {
745                 wfOut( "data is missing! rebuilding...\n" );
746         } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
747                 wfOut( "missing ss_total_pages, rebuilding...\n" );
748         } else {
749                 wfOut( "ok.\n" );
750                 return;
751         }
752         SiteStatsInit::doAllAndCommit( false );
755 function do_active_users_init() {
756         global $wgDatabase;
757         $activeUsers = $wgDatabase->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
758         if ( $activeUsers == -1 ) {
759                 $activeUsers = $wgDatabase->selectField( 'recentchanges',
760                         'COUNT( DISTINCT rc_user_text )',
761                         array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
762                 );
763                 $wgDatabase->update( 'site_stats',
764                         array( 'ss_active_users' => intval( $activeUsers ) ),
765                         array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
766                 );
767         }
768         wfOut( "...ss_active_users user count set...\n" );
771 function purge_cache() {
772         global $wgDatabase;
773         # We can't guarantee that the user will be able to use TRUNCATE,
774         # but we know that DELETE is available to us
775         wfOut( "Purging caches..." );
776         $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
777         wfOut( "done.\n" );
781  * Adding page_restrictions table, obsoleting page.page_restrictions.
782  * Migrating old restrictions to new table
783  * -- Andrew Garrett, January 2007.
784  */
785 function do_restrictions_update() {
786         global $wgDatabase;
788         if ( $wgDatabase->tableExists( 'page_restrictions' ) ) {
789                 wfOut( "...page_restrictions table already exists.\n" );
790         } else {
791                 wfOut( "Creating page_restrictions table..." );
792                 $wgDatabase->sourceFile( archive( 'patch-page_restrictions.sql' ) );
793                 $wgDatabase->sourceFile( archive( 'patch-page_restrictions_sortkey.sql' ) );
794                 wfOut( "ok\n" );
796                 wfOut( "Migrating old restrictions to new table...\n" );
797                 require_once( 'updateRestrictions.php' );
798                 $task = new UpdateRestrictions();
799                 $task->execute();
800         }
803 function do_category_population() {
804         if ( update_row_exists( 'populate category' ) ) {
805                 wfOut( "...category table already populated.\n" );
806                 return;
807         }
808         require_once( 'populateCategory.php' );
809         wfOut(
810                 "Populating category table, printing progress markers. " .
811                 "For large databases, you\n" .
812                 "may want to hit Ctrl-C and do this manually with maintenance/\n" .
813                 "populateCategory.php.\n"
814         );
815         $task = new PopulateCategory();
816         $task->execute();
817         wfOut( "Done populating category table.\n" );
820 function do_populate_parent_id() {
821         if ( update_row_exists( 'populate rev_parent_id' ) ) {
822                 wfOut( "...rev_parent_id column already populated.\n" );
823                 return;
824         }
825         require_once( 'populateParentId.php' );
826         $task = new PopulateParentId();
827         $task->execute();
830 function do_populate_rev_len() {
831         if ( update_row_exists( 'populate rev_len' ) ) {
832                 wfOut( "...rev_len column already populated.\n" );
833                 return;
834         }
835         require_once( 'populateRevisionLength.php' );
836         $task = new PopulateRevisionLength();
837         $task->execute();
840 function do_collation_update() {
841         global $wgDatabase, $wgCollationVersion;
842         if ( $wgDatabase->selectField(
843                 'categorylinks',
844                 'COUNT(*)',
845                 'cl_collation != ' . $wgDatabase->addQuotes( $wgCollationVersion ),
846                 __FUNCTION__
847         ) == 0 ) {
848                 wfOut( "...collations up-to-date.\n" );
849                 return;
850         }
851         require_once( 'updateCollation.php' );
852         $task = new UpdateCollation();
853         $task->execute();
856 function sqlite_initial_indexes() {
857         global $wgDatabase;
858         // initial-indexes.sql fails if the indexes are already present, so we perform a quick check if our database is newer.
859         if ( update_row_exists( 'initial_indexes' ) || $wgDatabase->indexExists( 'user', 'user_name' ) ) {
860                 wfOut( "...have initial indexes\n" );
861                 return;
862         }
863         wfOut( "Adding initial indexes..." );
864         $wgDatabase->sourceFile( archive( 'initial-indexes.sql' ) );
865         wfOut( "done\n" );
868 function sqlite_setup_searchindex() {
869         global $wgDatabase;
870         $module = $wgDatabase->getFulltextSearchModule();
871         $fts3tTable = update_row_exists( 'fts3' );
872         if ( $fts3tTable &&  !$module ) {
873                 wfOut( '...PHP is missing FTS3 support, downgrading tables...' );
874                 $wgDatabase->sourceFile( archive( 'searchindex-no-fts.sql' ) );
875                 wfOut( "done\n" );
876         } elseif ( !$fts3tTable && $module == 'FTS3' ) {
877                 wfOut( '...adding FTS3 search capabilities...' );
878                 $wgDatabase->sourceFile( archive( 'searchindex-fts3.sql' ) );
879                 wfOut( "done\n" );
880         } else {
881                 wfOut( "...fulltext search table appears to be in order.\n" );
882         }
885 function do_unique_pl_tl_il() {
886         global $wgDatabase;
887         $info = $wgDatabase->indexInfo( 'pagelinks', 'pl_namespace' );
888         if ( is_array( $info ) && !$info[0]->Non_unique ) {
889                 wfOut( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
890         } else {
891                 wfOut( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
892                 $wgDatabase->sourceFile( archive( 'patch-pl-tl-il-unique.sql' ) );
893                 wfOut( "ok\n" );
894         }
897 function do_log_search_population() {
898         if ( update_row_exists( 'populate log_search' ) ) {
899                 wfOut( "...log_search table already populated.\n" );
900                 return;
901         }
902         require_once( 'populateLogSearch.php' );
903         wfOut(
904 "Populating log_search table, printing progress markers. For large\n" .
905 "databases, you may want to hit Ctrl-C and do this manually with\n" .
906 "maintenance/populateLogSearch.php.\n" );
907         $task = new PopulateLogSearch();
908         $task->execute();
909         wfOut( "Done populating log_search table.\n" );
912 function rename_eu_wiki_id() {
913         global $wgDatabase;
914         if ( $wgDatabase->fieldExists( 'external_user', 'eu_local_id' ) ) {
915                 wfOut( "...eu_wiki_id already renamed to eu_local_id.\n" );
916                 return;
917         }
918         wfOut( "Renaming eu_wiki_id -> eu_local_id... " );
919         $wgDatabase->sourceFile( archive( 'patch-eu_local_id.sql' ) );
920         wfOut( "ok\n" );
923 function do_update_transcache_field() {
924         global $wgDatabase;
925         if ( update_row_exists( 'convert transcache field' ) ) {
926                 wfOut( "...transcache tc_time already converted.\n" );
927                 return;
928         } else {
929                 wfOut( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
930                 $wgDatabase->sourceFile( archive( 'patch-tc-timestamp.sql' ) );
931                 wfOut( "ok\n" );
932         }
935 function do_update_mime_minor_field() {
936         if ( update_row_exists( 'mime_minor_length' ) ) {
937                 wfOut( "...*_mime_minor fields are already long enough.\n" );
938         } else {
939                 global $wgDatabase;
940                 wfOut( "Altering all *_mime_minor fields to 100 bytes in size ... " );
941                 $wgDatabase->sourceFile( archive( 'patch-mime_minor_length.sql' ) );
942                 wfOut( "ok\n" );
943         }