Moved PostgreSQL schema update to PostgresUpdater:
[mediawiki.git] / maintenance / updaters.inc
blob371493a31354b143c4aaa7cb455df0b0ed2bee33
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_logging_timestamp_index() {
169         global $wgDatabase;
170         if ( $wgDatabase->indexExists( 'logging', 'times' ) ) {
171                 wfOut( "...timestamp key on logging already exists.\n" );
172         } else {
173                 wfOut( "Adding timestamp key on logging table... " );
174                 $wgDatabase->sourceFile( archive( "patch-logging-times-index.sql" ) );
175                 wfOut( "ok\n" );
176         }
179 function do_archive_user_index() {
180         global $wgDatabase;
181         if ( $wgDatabase->indexExists( 'archive', 'usertext_timestamp' ) ) {
182                 wfOut( "...usertext,timestamp key on archive already exists.\n" );
183         } else {
184                 wfOut( "Adding usertext,timestamp key on archive table... " );
185                 $wgDatabase->sourceFile( archive( "patch-archive-user-index.sql" ) );
186                 wfOut( "ok\n" );
187         }
190 function do_image_user_index() {
191         global $wgDatabase;
192         if ( $wgDatabase->indexExists( 'image', 'img_usertext_timestamp' ) ) {
193                 wfOut( "...usertext,timestamp key on image already exists.\n" );
194         } else {
195                 wfOut( "Adding usertext,timestamp key on image table... " );
196                 $wgDatabase->sourceFile( archive( "patch-image-user-index.sql" ) );
197                 wfOut( "ok\n" );
198         }
201 function do_oldimage_user_index() {
202         global $wgDatabase;
203         if ( $wgDatabase->indexExists( 'oldimage', 'oi_usertext_timestamp' ) ) {
204                 wfOut( "...usertext,timestamp key on oldimage already exists.\n" );
205         } else {
206                 wfOut( "Adding usertext,timestamp key on oldimage table... " );
207                 $wgDatabase->sourceFile( archive( "patch-oldimage-user-index.sql" ) );
208                 wfOut( "ok\n" );
209         }
212 function do_watchlist_update() {
213         global $wgDatabase;
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" );
217         } else {
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' ) );
221                 wfOut( "ok\n" );
222         }
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... " );
228                 flush();
230                 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
231                         array(
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' );
237                 wfOut( "ok\n" );
238         } else {
239                 wfOut( "...watchlist talk page rows already present\n" );
240         }
243 function do_copy_newtalk_to_watchlist() {
244         global $wgDatabase;
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" );
251         $user = new User();
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'
263                                         );
264                         }
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'
274                                 );
275                 }
276         }
277         wfOut( "Done.\n" );
280 function do_user_update() {
281         global $wgDatabase;
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' ) );
285                 wfOut( "ok\n" );
286         } else {
287                 wfOut( "...user table does not contain old email authentication field.\n" );
288         }
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.
294  */
295 function check_bin( $table, $field, $patchFile ) {
296         global $wgDatabase, $wgDBtype;
297         if ( $wgDBtype != 'mysql' )
298                 return;
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" );
305         } else {
306                 wfOut( "Fixing $field encoding on $table table... " );
307                 $wgDatabase->sourceFile( archive( $patchFile ) );
308                 wfOut( "ok\n" );
309         }
312 function do_schema_restructuring() {
313         global $wgDatabase;
314         $fname = "do_schema_restructuring";
315         if ( $wgDatabase->tableExists( 'page' ) ) {
316                 wfOut( "...page table already exists.\n" );
317         } else {
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();
334                                 }
335                                 $duplicate[$row->cur_namespace][] = $row->cur_title;
336                                 wfOut( sprintf( "      %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
337                         }
338                         $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
339                         $firstCond = true;
340                         foreach ( $duplicate as $ns => $titles ) {
341                                 if ( $firstCond ) {
342                                         $firstCond = false;
343                                 } else {
344                                         $sql .= ' OR ';
345                                 }
346                                 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
347                                 $first = true;
348                                 foreach ( $titles as $t ) {
349                                         if ( $first ) {
350                                                 $sql .= $wgDatabase->addQuotes( $t );
351                                                 $first = false;
352                                         } else {
353                                                 $sql .= ', ' . $wgDatabase->addQuotes( $t );
354                                         }
355                                 }
356                                 $sql .= ") ) \n";
357                         }
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;
365                         $deleteId = array();
367                         while ( $row = $wgDatabase->fetchObject( $rows ) ) {
368                                 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
369                                         $deleteId[] = $row->cur_id;
370                                 }
371                                 $prev_title     = $row->cur_title;
372                                 $prev_namespace = $row->cur_namespace;
373                         }
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" );
378                 }
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),
398                         INDEX (page_random),
399                         INDEX (page_len)
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'";
436                 } else {
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';
441                         $cur_flags = "''";
442                 }
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,
451                                 rev_minor_edit)
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)
462                         FROM $cur,$revision
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" );
475         }
478 function do_inverse_timestamp() {
479         global $wgDatabase;
480         if ( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
481                 wfOut( "Removing revision.inverse_timestamp and fixing indexes... " );
482                 $wgDatabase->sourceFile( archive( 'patch-inverse_timestamp.sql' ) );
483                 wfOut( "ok\n" );
484         } else {
485                 wfOut( "...revision timestamp indexes already up to 2005-03-13\n" );
486         }
489 function do_text_id() {
490         global $wgDatabase;
491         if ( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
492                 wfOut( "...rev_text_id already in place.\n" );
493         } else {
494                 wfOut( "Adding rev_text_id field... " );
495                 $wgDatabase->sourceFile( archive( 'patch-rev_text_id.sql' ) );
496                 wfOut( "ok\n" );
497         }
500 function do_pagelinks_update() {
501         global $wgDatabase;
502         if ( $wgDatabase->tableExists( 'pagelinks' ) ) {
503                 wfOut( "...already have pagelinks table.\n" );
504         } else {
505                 wfOut( "Converting links and brokenlinks tables to pagelinks... " );
506                 $wgDatabase->sourceFile( archive( 'patch-pagelinks.sql' ) );
507                 wfOut( "ok\n" );
508                 flush();
510                 global $wgCanonicalNamespaceNames;
511                 foreach ( $wgCanonicalNamespaceNames as $ns => $name ) {
512                         if ( $ns != 0 ) {
513                                 do_pagelinks_namespace( $ns );
514                         }
515                 }
516         }
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)
533                  WHERE pl_namespace=0
534                    AND pl_title LIKE '$likeprefix:%'";
536         $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
537         wfOut( "ok\n" );
540 function do_drop_img_type() {
541         global $wgDatabase;
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' ) );
546                 wfOut( "ok\n" );
547         } else {
548                 wfOut( "...no img_type field in image table; Good.\n" );
549         }
552 function do_old_links_update() {
553         if( !defined( 'MW_NO_SETUP' ) ) {
554                 define( 'MW_NO_SETUP', true );
555         }
556         require( "convertLinks.php" );
557         $cl = new ConvertLinks();
558         $cl->execute();
561 function fix_ancient_imagelinks() {
562         global $wgDatabase;
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' ) );
568                 wfOut( "ok\n" );
569         } else {
570                 wfOut( "...il_from OK\n" );
571         }
574 function do_user_unique_update() {
575         global $wgDatabase;
576         $duper = new UserDupes( $wgDatabase );
577         if ( $duper->hasUniqueIndex() ) {
578                 wfOut( "...already have unique user_name index.\n" );
579         } else {
580                 if ( !$duper->clearDupes() ) {
581                         wfOut( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
582                 }
583                 wfOut( "Adding unique index on user_name... " );
584                 $wgDatabase->sourceFile( archive( 'patch-user_nameindex.sql' ) );
585                 wfOut( "ok\n" );
586         }
589 function do_user_groups_update() {
590         $fname = 'do_user_groups_update';
591         global $wgDatabase;
593         if ( $wgDatabase->tableExists( 'user_groups' ) ) {
594                 wfOut( "...user_groups table already exists.\n" );
595                 return do_user_groups_reformat();
596         }
598         wfOut( "Adding user_groups table... " );
599         $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
600         wfOut( "ok\n" );
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' ) );
606                         wfOut( "ok\n" );
607                 } else {
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" );
611                         return;
612                 }
613         }
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 != ''" ),
619                 $fname );
621         while ( $row = $wgDatabase->fetchObject( $result ) ) {
622                 $groups = array_unique(
623                         array_map( 'trim',
624                                 explode( ',', $row->ur_rights ) ) );
626                 foreach ( $groups as $group ) {
627                         $wgDatabase->insert( 'user_groups',
628                                 array(
629                                         'ug_user'  => $row->ur_user,
630                                         'ug_group' => $group ),
631                                 $fname );
632                 }
633         }
634         wfOut( "ok\n" );
637 function do_user_groups_reformat() {
638         # Check for bogus formats from previous 1.5 alpha code.
639         global $wgDatabase;
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" );
647                 wfOut( "ok\n" );
649                 wfOut( "Re-adding fresh user_groups table... " );
650                 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
651                 wfOut( "ok\n" );
653                 wfOut( "***\n" );
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" );
656                 wfOut( "***\n" );
657         } else {
658                 wfOut( "...user_groups is in current format.\n" );
659         }
663 function do_watchlist_null() {
664         # Make sure wl_notificationtimestamp can be NULL,
665         # and update old broken items.
666         global $wgDatabase;
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' ) );
672                 wfOut( "ok\n" );
673         } else {
674                 wfOut( "...wl_notificationtimestamp is already nullable.\n" );
675         }
680  * @bug 3946
681  */
682 function do_page_random_update() {
683         global $wgDatabase;
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() {
695         global $wgDatabase;
696         $fname = 'do_templatelinks_update';
698         if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
699                 wfOut( "...templatelinks table already exists\n" );
700                 return;
701         }
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 );
709                 $count = 0;
710                 while ( $row = $wgDatabase->fetchObject( $res ) ) {
711                         $count = ( $count + 1 ) % 100;
712                         if ( $count == 0 ) {
713                                 if ( function_exists( 'wfWaitForSlaves' ) ) {
714                                         wfWaitForSlaves( 10 );
715                                 } else {
716                                         sleep( 1 );
717                                 }
718                         }
719                         $wgDatabase->insert( 'templatelinks',
720                                 array(
721                                         'tl_from' => $row->pl_from,
722                                         'tl_namespace' => $row->pl_namespace,
723                                         'tl_title' => $row->pl_title,
724                                 ), $fname
725                         );
727                 }
728         } else {
729                 // Fast update
730                 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
731                         array(
732                                 'tl_from' => 'pl_from',
733                                 'tl_namespace' => 'pl_namespace',
734                                 'tl_title' => 'pl_title'
735                         ), array(
736                                 'pl_namespace' => 10
737                         ), $fname
738                 );
739         }
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() {
746         global $wgDatabase;
747         wfOut( "Checking for additional recent changes indices...\n" );
749         $indexes = array(
750                 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
751                 'rc_user_text' => 'patch-rc_user_text-index.sql',
752         );
754         foreach ( $indexes as $index => $patch ) {
755                 $info = $wgDatabase->indexInfo( 'recentchanges', $index, __METHOD__ );
756                 if ( !$info ) {
757                         wfOut( "...index `{$index}` not found; adding..." );
758                         $wgDatabase->sourceFile( archive( $patch ) );
759                         wfOut( "done.\n" );
760                 } else {
761                         wfOut( "...index `{$index}` seems ok.\n" );
762                 }
763         }
766 function index_has_field( $table, $index, $field ) {
767         global $wgDatabase;
768         wfOut( "Checking if $table index $index includes field $field...\n" );
769         $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
770         if ( $info ) {
771                 foreach ( $info as $row ) {
772                         if ( $row->Column_name == $field ) {
773                                 wfOut( "...index $index on table $table seems to be ok\n" );
774                                 return true;
775                         }
776                 }
777         }
778         wfOut( "...index $index on table $table has no field $field; adding\n" );
779         return false;
782 function do_backlinking_indices_update() {
783         global $wgDatabase;
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' ) )
788         {
789                 $wgDatabase->sourceFile( archive( 'patch-backlinkindexes.sql' ) );
790                 wfOut( "...backlinking indices updated\n" );
791         }
794 function do_categorylinks_indices_update() {
795         global $wgDatabase;
796         wfOut( "Checking for categorylinks indices...\n" );
797         if ( !index_has_field( 'categorylinks', 'cl_sortkey', 'cl_from' ) )
798         {
799                 $wgDatabase->sourceFile( archive( 'patch-categorylinksindex.sql' ) );
800                 wfOut( "...categorylinks indices updated\n" );
801         }
804 function do_filearchive_indices_update() {
805         global $wgDatabase;
806         wfOut( "Checking filearchive indices...\n" );
807         $info = $wgDatabase->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
808         if ( !$info )
809         {
810                 $wgDatabase->sourceFile( archive( 'patch-filearchive-user-index.sql' ) );
811                 wfOut( "...filearchive indices updated\n" );
812         }
815 function maybe_do_profiling_memory_update() {
816         global $wgDatabase;
817         if ( !$wgDatabase->tableExists( 'profiling' ) ) {
818                 // Simply ignore
819         } elseif ( $wgDatabase->fieldExists( 'profiling', 'pf_memory' ) ) {
820                 wfOut( "...profiling table has pf_memory field.\n" );
821         } else {
822                 wfOut( "Adding pf_memory field to table profiling..." );
823                 $wgDatabase->sourceFile( archive( 'patch-profiling-memory.sql' ) );
824                 wfOut( "ok\n" );
825         }
828 function do_stats_init() {
829         // Sometimes site_stats table is not properly populated.
830         global $wgDatabase;
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" );
837         } else {
838                 wfOut( "ok.\n" );
839                 return;
840         }
841         SiteStatsInit::doAllAndCommit( false );
844 function do_active_users_init() {
845         global $wgDatabase;
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__
851                 );
852                 $wgDatabase->update( 'site_stats',
853                         array( 'ss_active_users' => intval( $activeUsers ) ),
854                         array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
855                 );
856         }
857         wfOut( "...ss_active_users user count set...\n" );
860 function purge_cache() {
861         global $wgDatabase;
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__ );
866         wfOut( "done.\n" );
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.
874         global $wgDatabase;
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" );
882         } else {
883                 wfOut( "Creating $name table..." );
884                 $wgDatabase->sourceFile( archive( $patch ) );
885                 $wgDatabase->sourceFile( archive( $patch2 ) );
886                 wfOut( "ok\n" );
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__ );
892                 $count = 0;
894                 while ( $row = $wgDatabase->fetchObject( $res ) ) {
895                         $count = ( $count + 1 ) % 100;
897                         if ( $count == 0 ) {
898                                 if ( function_exists( 'wfWaitForSlaves' ) ) {
899                                         wfWaitForSlaves( 10 );
900                                 } else {
901                                         sleep( 1 );
902                                 }
903                         }
905                         # Figure out what the restrictions are..
906                         $id = $row->page_id;
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;
916                                         if ( $level ) {
917                                                 $restrictions['edit'] = $level;
918                                                 $restrictions['move'] = $level;
919                                         }
920                                 } else {
921                                         list( $type, $level ) = $thisrestriction;
922                                         if ( $level ) {
923                                                 $restrictions[$type] = $level;
924                                         }
925                                 }
927                         $wgDatabase->update( 'page', array ( 'page_restrictions' => '' ), array( 'page_id' => $id ), __METHOD__ );
929                         }
931                         foreach ( $restrictions as $type => $level ) {
932                                 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
933                                                                                         'pr_type' => $type,
934                                                                                         'pr_level' => $level,
935                                                                                         'pr_cascade' => 0,
936                                                                                         'pr_expiry' => 'infinity' ),
937                                                                                         __METHOD__ );
938                         }
939                 }
940                 wfOut( "ok\n" );
941         }
944 function do_category_population() {
945         if ( update_row_exists( 'populate category' ) ) {
946                 wfOut( "...category table already populated.\n" );
947                 return;
948         }
949         require_once( 'populateCategory.php' );
950         wfOut(
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"
955         );
956         $task = new PopulateCategory();
957         $task->execute();
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" );
964                 return;
965         }
966         require_once( 'populateParentId.php' );
967         $task = new PopulateParentId();
968         $task->execute();
971 function do_populate_rev_len() {
972         if ( update_row_exists( 'populate rev_len' ) ) {
973                 wfOut( "...rev_len column already populated.\n" );
974                 return;
975         }
976         require_once( 'populateRevisionLength.php' );
977         $task = new PopulateRevisionLength();
978         $task->execute();
981 function do_collation_update() {
982         global $wgDatabase, $wgCollationVersion;
983         if ( $wgDatabase->selectField(
984                 'categorylinks',
985                 'COUNT(*)',
986                 'cl_collation != ' . $wgDatabase->addQuotes( $wgCollationVersion ),
987                 __FUNCTION__
988         ) == 0 ) {
989                 wfOut( "...collations up-to-date.\n" );
990                 return;
991         }
992         require_once( 'updateCollation.php' );
993         $task = new UpdateCollation();
994         $task->execute();
997 function sqlite_initial_indexes() {
998         global $wgDatabase;
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" );
1002                 return;
1003         }
1004         wfOut( "Adding initial indexes..." );
1005         $wgDatabase->sourceFile( archive( 'initial-indexes.sql' ) );
1006         wfOut( "done\n" );
1009 function sqlite_setup_searchindex() {
1010         global $wgDatabase;
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' ) );
1016                 wfOut( "done\n" );
1017         } elseif ( !$fts3tTable && $module == 'FTS3' ) {
1018                 wfOut( '...adding FTS3 search capabilities...' );
1019                 $wgDatabase->sourceFile( archive( 'searchindex-fts3.sql' ) );
1020                 wfOut( "done\n" );
1021         } else {
1022                 wfOut( "...fulltext search table appears to be in order.\n" );
1023         }
1026 function do_unique_pl_tl_il() {
1027         global $wgDatabase;
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" );
1031         } else {
1032                 wfOut( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
1033                 $wgDatabase->sourceFile( archive( 'patch-pl-tl-il-unique.sql' ) );
1034                 wfOut( "ok\n" );
1035         }
1038 function do_log_search_population() {
1039         if ( update_row_exists( 'populate log_search' ) ) {
1040                 wfOut( "...log_search table already populated.\n" );
1041                 return;
1042         }
1043         require_once( 'populateLogSearch.php' );
1044         wfOut(
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();
1049         $task->execute();
1050         wfOut( "Done populating log_search table.\n" );
1053 function rename_eu_wiki_id() {
1054         global $wgDatabase;
1055         if ( $wgDatabase->fieldExists( 'external_user', 'eu_local_id' ) ) {
1056                 wfOut( "...eu_wiki_id already renamed to eu_local_id.\n" );
1057                 return;
1058         }
1059         wfOut( "Renaming eu_wiki_id -> eu_local_id... " );
1060         $wgDatabase->sourceFile( archive( 'patch-eu_local_id.sql' ) );
1061         wfOut( "ok\n" );
1064 function do_update_transcache_field() {
1065         global $wgDatabase;
1066         if ( update_row_exists( 'convert transcache field' ) ) {
1067                 wfOut( "...transcache tc_time already converted.\n" );
1068                 return;
1069         } else {
1070                 wfOut( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
1071                 $wgDatabase->sourceFile( archive( 'patch-tc-timestamp.sql' ) );
1072                 wfOut( "ok\n" );
1073         }
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" );
1079         } else {
1080                 global $wgDatabase;
1081                 wfOut( "Altering all *_mime_minor fields to 100 bytes in size ... " );
1082                 $wgDatabase->sourceFile( archive( 'patch-mime_minor_length.sql' ) );
1083                 wfOut( "ok\n" );
1084         }