Major refactoring of site and user CSS, creating ResourceLoaderUserModule and Resourc...
[mediawiki.git] / maintenance / updaters.inc
blobb657b6d134f5a7d14cc1eed92108a7f97a3efa9e
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 function modify_field( $table, $field, $patch, $fullpath = false ) {
13         $dbw = wfGetDB( DB_MASTER );
14         if ( !$dbw->tableExists( $table ) ) {
15                 wfOut( "...$table table does not exist, skipping modify field patch\n" );
16         } elseif ( !$dbw->fieldExists( $table, $field ) ) {
17                 wfOut( "...$field field does not exist in $table table, skipping modify field patch\n" );
18         } else {
19                 wfOut( "Modifying $field field of table $table..." );
20                 if ( $fullpath ) {
21                         $dbw->sourceFile( $patch );
22                 } else {
23                         $dbw->sourceFile( archive( $patch ) );
24                 }
25                 wfOut( "ok\n" );
26         }
29 function drop_index_if_exists( $table, $index, $patch, $fullpath = false ) {
30         $dbw = wfGetDB( DB_MASTER );
31         if ( $dbw->indexExists( $table, $index ) ) {
32                 wfOut( "Dropping $index from table $table... " );
33                 if ( $fullpath ) {
34                         $dbw->sourceFile( $patch );
35                 } else {
36                         $dbw->sourceFile( archive( $patch ) );
37                 }
38                 wfOut( "ok\n" );
39         } else {
40                 wfOut( "...$index doesn't exist.\n" );
41         }
44 function archive( $name ) {
45         return DatabaseBase::patchPath( $name );
48 function do_interwiki_update() {
49         # Check that interwiki table exists; if it doesn't source it
50         global $IP;
51         $dbw = wfGetDB( DB_MASTER );
52         if ( $dbw->tableExists( "interwiki" ) ) {
53                 wfOut( "...already have interwiki table\n" );
54                 return true;
55         }
56         wfOut( "Creating interwiki table: " );
57         $dbw->sourceFile( archive( "patch-interwiki.sql" ) );
58         wfOut( "ok\n" );
59         wfOut( "Adding default interwiki definitions: " );
60         $dbw->sourceFile( "$IP/maintenance/interwiki.sql" );
61         wfOut( "ok\n" );
64 function do_index_update() {
65         # Check that proper indexes are in place
66         $dbw = wfGetDB( DB_MASTER );
67         $meta = $dbw->fieldInfo( "recentchanges", "rc_timestamp" );
68         if ( !$meta->isMultipleKey() ) {
69                 wfOut( "Updating indexes to 20031107: " );
70                 $dbw->sourceFile( archive( "patch-indexes.sql" ) );
71                 wfOut( "ok\n" );
72                 return true;
73         }
74         wfOut( "...indexes seem up to 20031107 standards\n" );
75         return false;
78 function do_image_index_update() {
79         $dbw = wfGetDB( DB_MASTER );
80         $meta = $dbw->fieldInfo( "image", "img_major_mime" );
81         if ( !$meta->isMultipleKey() ) {
82                 wfOut( "Updating indexes to 20050912: " );
83                 $dbw->sourceFile( archive( "patch-mimesearch-indexes.sql" ) );
84                 wfOut( "ok\n" );
85                 return true;
86         }
87         wfOut( "...indexes seem up to 20050912 standards\n" );
88         return false;
91 function do_image_name_unique_update() {
92         $dbw = wfGetDB( DB_MASTER );
93         if ( $dbw->indexExists( 'image', 'PRIMARY' ) ) {
94                 wfOut( "...image primary key already set.\n" );
95         } else {
96                 wfOut( "Making img_name the primary key... " );
97                 $dbw->sourceFile( archive( "patch-image_name_primary.sql" ) );
98                 wfOut( "ok\n" );
99         }
102 function do_watchlist_update() {
103         $dbw = wfGetDB( DB_MASTER );
104         if ( $dbw->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
105                 wfOut( "...the watchlist table is already set up for email notification.\n" );
106         } else {
107                 wfOut( "Adding wl_notificationtimestamp field for email notification management." );
108                 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
109                 $dbw->sourceFile( archive( 'patch-email-notification.sql' ) );
110                 wfOut( "ok\n" );
111         }
112         # Check if we need to add talk page rows to the watchlist
113         $talk = $dbw->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', __METHOD__ );
114         $nontalk = $dbw->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', __METHOD__ );
115         if ( $talk != $nontalk ) {
116                 wfOut( "Adding missing watchlist talk page rows... " );
117                 flush();
119                 $dbw->insertSelect( 'watchlist', 'watchlist',
120                         array(
121                                 'wl_user' => 'wl_user',
122                                 'wl_namespace' => 'wl_namespace | 1',
123                                 'wl_title' => 'wl_title',
124                                 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
125                         ), array( 'NOT (wl_namespace & 1)' ), __METHOD__, 'IGNORE' );
126                 wfOut( "ok\n" );
127         } else {
128                 wfOut( "...watchlist talk page rows already present\n" );
129         }
132 function do_copy_newtalk_to_watchlist() {
133         $dbw = wfGetDB( DB_MASTER );
134         $res = $dbw->safeQuery( 'SELECT user_id, user_ip FROM !',
135                 $dbw->tableName( 'user_newtalk' ) );
136         $num_newtalks = $dbw->numRows( $res );
137         wfOut( "Now converting $num_newtalks user_newtalk entries to watchlist table entries ... \n" );
139         $user = new User();
140         for ( $i = 1; $i <= $num_newtalks; $i++ ) {
141                 $wluser = $dbw->fetchObject( $res );
142                 if ( $wluser->user_id == 0 ) { # anonymous users ... have IP numbers as "names"
143                         if ( $user->isIP( $wluser->user_ip ) ) { # do only if it really looks like an IP number (double checked)
144                                 $dbw->replace( 'watchlist',
145                                         array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
146                                         array( 'wl_user' => 0,
147                                                 'wl_namespace' => NS_USER_TALK,
148                                                 'wl_title' => $wluser->user_ip,
149                                                 'wl_notificationtimestamp' => '19700101000000'
150                                                 ), 'updaters.inc::do_watchlist_update2'
151                                         );
152                         }
153                 } else { # normal users ... have user_ids
154                         $user->setID( $wluser->user_id );
155                         $dbw->replace( 'watchlist',
156                                 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
157                                 array( 'wl_user' => $user->getID(),
158                                         'wl_namespace' => NS_USER_TALK,
159                                         'wl_title' => $user->getName(),
160                                         'wl_notificationtimestamp' => '19700101000000'
161                                         ), 'updaters.inc::do_watchlist_update3'
162                                 );
163                 }
164         }
165         wfOut( "Done.\n" );
169  * 1.4 betas were missing the 'binary' marker from logging.log_title,
170  * which causes a collation mismatch error on joins in MySQL 4.1.
171  */
172 function check_bin( $table, $field, $patchFile ) {
173         $dbw = wfGetDB( DB_MASTER );
174         if ( $dbw->getType() != 'mysql' )
175                 return;
176         $tableName = $dbw->tableName( $table );
177         $res = $dbw->query( "SELECT $field FROM $tableName LIMIT 0" );
178         $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
180         if ( in_array( 'binary', $flags ) ) {
181                 wfOut( "...$table table has correct $field encoding.\n" );
182         } else {
183                 wfOut( "Fixing $field encoding on $table table... " );
184                 $dbw->sourceFile( archive( $patchFile ) );
185                 wfOut( "ok\n" );
186         }
189 function do_schema_restructuring() {
190         $dbw = wfGetDB( DB_MASTER );
191         if ( $dbw->tableExists( 'page' ) ) {
192                 wfOut( "...page table already exists.\n" );
193         } else {
194                 wfOut( "...converting from cur/old to page/revision/text DB structure.\n" );
195                 wfOut( wfTimestamp( TS_DB ) );
196                 wfOut( "......checking for duplicate entries.\n" );
198                 list ( $cur, $old, $page, $revision, $text ) = $dbw->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
200                 $rows = $dbw->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
201                                 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", __METHOD__ );
203                 if ( $dbw->numRows( $rows ) > 0 ) {
204                         wfOut( wfTimestamp( TS_DB ) );
205                         wfOut( "......<b>Found duplicate entries</b>\n" );
206                         wfOut( sprintf( "<b>      %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
207                         while ( $row = $dbw->fetchObject( $rows ) ) {
208                                 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
209                                         $duplicate[$row->cur_namespace] = array();
210                                 }
211                                 $duplicate[$row->cur_namespace][] = $row->cur_title;
212                                 wfOut( sprintf( "      %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
213                         }
214                         $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
215                         $firstCond = true;
216                         foreach ( $duplicate as $ns => $titles ) {
217                                 if ( $firstCond ) {
218                                         $firstCond = false;
219                                 } else {
220                                         $sql .= ' OR ';
221                                 }
222                                 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
223                                 $first = true;
224                                 foreach ( $titles as $t ) {
225                                         if ( $first ) {
226                                                 $sql .= $dbw->addQuotes( $t );
227                                                 $first = false;
228                                         } else {
229                                                 $sql .= ', ' . $dbw->addQuotes( $t );
230                                         }
231                                 }
232                                 $sql .= ") ) \n";
233                         }
234                         # By sorting descending, the most recent entry will be the first in the list.
235                         # All following entries will be deleted by the next while-loop.
236                         $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
238                         $rows = $dbw->query( $sql, __METHOD__ );
240                         $prev_title = $prev_namespace = false;
241                         $deleteId = array();
243                         while ( $row = $dbw->fetchObject( $rows ) ) {
244                                 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
245                                         $deleteId[] = $row->cur_id;
246                                 }
247                                 $prev_title     = $row->cur_title;
248                                 $prev_namespace = $row->cur_namespace;
249                         }
250                         $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
251                         $rows = $dbw->query( $sql, __METHOD__ );
252                         wfOut( wfTimestamp( TS_DB ) );
253                         wfOut( "......<b>Deleted</b> " . $dbw->affectedRows() . " records.\n" );
254                 }
257                 wfOut( wfTimestamp( TS_DB ) );
258                 wfOut( "......Creating tables.\n" );
259                 $dbw->query( "CREATE TABLE $page (
260                         page_id int(8) unsigned NOT NULL auto_increment,
261                         page_namespace int NOT NULL,
262                         page_title varchar(255) binary NOT NULL,
263                         page_restrictions tinyblob NOT NULL,
264                         page_counter bigint(20) unsigned NOT NULL default '0',
265                         page_is_redirect tinyint(1) unsigned NOT NULL default '0',
266                         page_is_new tinyint(1) unsigned NOT NULL default '0',
267                         page_random real unsigned NOT NULL,
268                         page_touched char(14) binary NOT NULL default '',
269                         page_latest int(8) unsigned NOT NULL,
270                         page_len int(8) unsigned NOT NULL,
272                         PRIMARY KEY page_id (page_id),
273                         UNIQUE INDEX name_title (page_namespace,page_title),
274                         INDEX (page_random),
275                         INDEX (page_len)
276                         ) ENGINE=InnoDB", __METHOD__ );
277                 $dbw->query( "CREATE TABLE $revision (
278                         rev_id int(8) unsigned NOT NULL auto_increment,
279                         rev_page int(8) unsigned NOT NULL,
280                         rev_comment tinyblob NOT NULL,
281                         rev_user int(5) unsigned NOT NULL default '0',
282                         rev_user_text varchar(255) binary NOT NULL default '',
283                         rev_timestamp char(14) binary NOT NULL default '',
284                         rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
285                         rev_deleted tinyint(1) unsigned NOT NULL default '0',
286                         rev_len int(8) unsigned,
287                         rev_parent_id int(8) unsigned default NULL,
288                         PRIMARY KEY rev_page_id (rev_page, rev_id),
289                         UNIQUE INDEX rev_id (rev_id),
290                         INDEX rev_timestamp (rev_timestamp),
291                         INDEX page_timestamp (rev_page,rev_timestamp),
292                         INDEX user_timestamp (rev_user,rev_timestamp),
293                         INDEX usertext_timestamp (rev_user_text,rev_timestamp)
294                         ) ENGINE=InnoDB", __METHOD__ );
296                 wfOut( wfTimestamp( TS_DB ) );
297                 wfOut( "......Locking tables.\n" );
298                 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", __METHOD__ );
300                 $maxold = intval( $dbw->selectField( 'old', 'max(old_id)', '', __METHOD__ ) );
301                 wfOut( wfTimestamp( TS_DB ) );
302                 wfOut( "......maxold is {$maxold}\n" );
304                 wfOut( wfTimestamp( TS_DB ) );
305                 global $wgLegacySchemaConversion;
306                 if ( $wgLegacySchemaConversion ) {
307                         // Create HistoryBlobCurStub entries.
308                         // Text will be pulled from the leftover 'cur' table at runtime.
309                         wfOut( "......Moving metadata from cur; using blob references to text in cur table.\n" );
310                         $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
311                         $cur_flags = "'object'";
312                 } else {
313                         // Copy all cur text in immediately: this may take longer but avoids
314                         // having to keep an extra table around.
315                         wfOut( "......Moving text from cur.\n" );
316                         $cur_text = 'cur_text';
317                         $cur_flags = "''";
318                 }
319                 $dbw->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
320                                         old_timestamp, old_minor_edit, old_flags)
321                         SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
322                         FROM $cur", __METHOD__ );
324                 wfOut( wfTimestamp( TS_DB ) );
325                 wfOut( "......Setting up revision table.\n" );
326                 $dbw->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
327                                 rev_minor_edit)
328                         SELECT old_id, cur_id, old_comment, old_user, old_user_text,
329                                 old_timestamp, old_minor_edit
330                         FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", __METHOD__ );
332                 wfOut( wfTimestamp( TS_DB ) );
333                 wfOut( "......Setting up page table.\n" );
334                 $dbw->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
335                         page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
336                         SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
337                                 cur_random, cur_touched, rev_id, LENGTH(cur_text)
338                         FROM $cur,$revision
339                         WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", __METHOD__ );
341                 wfOut( wfTimestamp( TS_DB ) );
342                 wfOut( "......Unlocking tables.\n" );
343                 $dbw->query( "UNLOCK TABLES", __METHOD__ );
345                 wfOut( wfTimestamp( TS_DB ) );
346                 wfOut( "......Renaming old.\n" );
347                 $dbw->query( "ALTER TABLE $old RENAME TO $text", __METHOD__ );
349                 wfOut( wfTimestamp( TS_DB ) );
350                 wfOut( "...done.\n" );
351         }
354 function do_pagelinks_update() {
355         $dbw = wfGetDB( DB_MASTER );
356         if ( $dbw->tableExists( 'pagelinks' ) ) {
357                 wfOut( "...already have pagelinks table.\n" );
358         } else {
359                 wfOut( "Converting links and brokenlinks tables to pagelinks... " );
360                 $dbw->sourceFile( archive( 'patch-pagelinks.sql' ) );
361                 wfOut( "ok\n" );
362                 flush();
364                 global $wgCanonicalNamespaceNames;
365                 foreach ( $wgCanonicalNamespaceNames as $ns => $name ) {
366                         if ( $ns != 0 ) {
367                                 do_pagelinks_namespace( $ns );
368                         }
369                 }
370         }
373 function do_pagelinks_namespace( $namespace ) {
374         global $wgContLang;
376         $dbw = wfGetDB( DB_MASTER );
377         $ns = intval( $namespace );
378         wfOut( "Cleaning up broken links for namespace $ns... " );
380         $pagelinks = $dbw->tableName( 'pagelinks' );
381         $name = $wgContLang->getNsText( $ns );
382         $prefix = $dbw->strencode( $name );
383         $likeprefix = str_replace( '_', '\\_', $prefix );
385         $sql = "UPDATE $pagelinks
386                    SET pl_namespace=$ns,
387                        pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
388                  WHERE pl_namespace=0
389                    AND pl_title LIKE '$likeprefix:%'";
391         $dbw->query( $sql, 'do_pagelinks_namespace' );
392         wfOut( "ok\n" );
395 function do_old_links_update() {
396         $cl = new ConvertLinks();
397         $cl->execute();
400 function fix_ancient_imagelinks() {
401         $dbw = wfGetDB( DB_MASTER );
402         $info = $dbw->fieldInfo( 'imagelinks', 'il_from' );
403         if ( $info && $info->type() === 'string' ) {
404                 wfOut( "Fixing ancient broken imagelinks table.\n" );
405                 wfOut( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
406                 $dbw->sourceFile( archive( 'patch-fix-il_from.sql' ) );
407                 wfOut( "ok\n" );
408         } else {
409                 wfOut( "...il_from OK\n" );
410         }
413 function do_user_unique_update() {
414         $dbw = wfGetDB( DB_MASTER );
415         $duper = new UserDupes( $dbw );
416         if ( $duper->hasUniqueIndex() ) {
417                 wfOut( "...already have unique user_name index.\n" );
418         } else {
419                 if ( !$duper->clearDupes() ) {
420                         wfOut( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
421                 }
422                 wfOut( "Adding unique index on user_name... " );
423                 $dbw->sourceFile( archive( 'patch-user_nameindex.sql' ) );
424                 wfOut( "ok\n" );
425         }
428 function do_user_groups_update() {
429         $dbw = wfGetDB( DB_MASTER );
431         if ( $dbw->tableExists( 'user_groups' ) ) {
432                 wfOut( "...user_groups table already exists.\n" );
433                 return do_user_groups_reformat();
434         }
436         wfOut( "Adding user_groups table... " );
437         $dbw->sourceFile( archive( 'patch-user_groups.sql' ) );
438         wfOut( "ok\n" );
440         if ( !$dbw->tableExists( 'user_rights' ) ) {
441                 if ( $dbw->fieldExists( 'user', 'user_rights' ) ) {
442                         wfOut( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
443                         $dbw->sourceFile( archive( 'patch-user_rights.sql' ) );
444                         wfOut( "ok\n" );
445                 } else {
446                         wfOut( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
447                         wfOut( "*** You may need to manually configure some sysops by manipulating\n" );
448                         wfOut( "*** the user_groups table.\n" );
449                         return;
450                 }
451         }
453         wfOut( "Converting user_rights table to user_groups... " );
454         $result = $dbw->select( 'user_rights',
455                 array( 'ur_user', 'ur_rights' ),
456                 array( "ur_rights != ''" ),
457                 __METHOD__ );
459         while ( $row = $dbw->fetchObject( $result ) ) {
460                 $groups = array_unique(
461                         array_map( 'trim',
462                                 explode( ',', $row->ur_rights ) ) );
464                 foreach ( $groups as $group ) {
465                         $dbw->insert( 'user_groups',
466                                 array(
467                                         'ug_user'  => $row->ur_user,
468                                         'ug_group' => $group ),
469                                 __METHOD__ );
470                 }
471         }
472         wfOut( "ok\n" );
475 function do_user_groups_reformat() {
476         # Check for bogus formats from previous 1.5 alpha code.
477         $dbw = wfGetDB( DB_MASTER );
478         $info = $dbw->fieldInfo( 'user_groups', 'ug_group' );
480         if ( $info->type() == 'int' ) {
481                 $oldug = $dbw->tableName( 'user_groups' );
482                 $newug = $dbw->tableName( 'user_groups_bogus' );
483                 wfOut( "user_groups is in bogus intermediate format. Renaming to $newug... " );
484                 $dbw->query( "ALTER TABLE $oldug RENAME TO $newug" );
485                 wfOut( "ok\n" );
487                 wfOut( "Re-adding fresh user_groups table... " );
488                 $dbw->sourceFile( archive( 'patch-user_groups.sql' ) );
489                 wfOut( "ok\n" );
491                 wfOut( "***\n" );
492                 wfOut( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
493                 wfOut( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
494                 wfOut( "***\n" );
495         } else {
496                 wfOut( "...user_groups is in current format.\n" );
497         }
501 function do_watchlist_null() {
502         # Make sure wl_notificationtimestamp can be NULL,
503         # and update old broken items.
504         $dbw = wfGetDB( DB_MASTER );
505         $info = $dbw->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
507         if ( !$info->nullable() ) {
508                 wfOut( "Making wl_notificationtimestamp nullable... " );
509                 $dbw->sourceFile( archive( 'patch-watchlist-null.sql' ) );
510                 wfOut( "ok\n" );
511         } else {
512                 wfOut( "...wl_notificationtimestamp is already nullable.\n" );
513         }
518  * @bug 3946
519  */
520 function do_page_random_update() {
521         wfOut( "Setting page_random to a random value on rows where it equals 0..." );
523         $dbw = wfGetDB( DB_MASTER );
524         $page = $dbw->tableName( 'page' );
525         $dbw->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
526         $rows = $dbw->affectedRows();
528         wfOut( "changed $rows rows\n" );
531 function do_templatelinks_update() {
532         $dbw = wfGetDB( DB_MASTER );
534         if ( $dbw->tableExists( 'templatelinks' ) ) {
535                 wfOut( "...templatelinks table already exists\n" );
536                 return;
537         }
538         wfOut( "Creating templatelinks table...\n" );
539         $dbw->sourceFile( archive( 'patch-templatelinks.sql' ) );
540         wfOut( "Populating...\n" );
541         if ( wfGetLB()->getServerCount() > 1 ) {
542                 // Slow, replication-friendly update
543                 $res = $dbw->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
544                         array( 'pl_namespace' => NS_TEMPLATE ), __METHOD__ );
545                 $count = 0;
546                 while ( $row = $dbw->fetchObject( $res ) ) {
547                         $count = ( $count + 1 ) % 100;
548                         if ( $count == 0 ) {
549                                 if ( function_exists( 'wfWaitForSlaves' ) ) {
550                                         wfWaitForSlaves( 10 );
551                                 } else {
552                                         sleep( 1 );
553                                 }
554                         }
555                         $dbw->insert( 'templatelinks',
556                                 array(
557                                         'tl_from' => $row->pl_from,
558                                         'tl_namespace' => $row->pl_namespace,
559                                         'tl_title' => $row->pl_title,
560                                 ), __METHOD__
561                         );
563                 }
564         } else {
565                 // Fast update
566                 $dbw->insertSelect( 'templatelinks', 'pagelinks',
567                         array(
568                                 'tl_from' => 'pl_from',
569                                 'tl_namespace' => 'pl_namespace',
570                                 'tl_title' => 'pl_title'
571                         ), array(
572                                 'pl_namespace' => 10
573                         ), __METHOD__
574                 );
575         }
576         wfOut( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
579 // Add index on ( rc_namespace, rc_user_text ) [Jul. 2006]
580 // Add index on ( rc_user_text, rc_timestamp ) [Nov. 2006]
581 function do_rc_indices_update() {
582         $dbw = wfGetDB( DB_MASTER );
583         wfOut( "Checking for additional recent changes indices...\n" );
585         $indexes = array(
586                 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
587                 'rc_user_text' => 'patch-rc_user_text-index.sql',
588         );
590         foreach ( $indexes as $index => $patch ) {
591                 $info = $dbw->indexInfo( 'recentchanges', $index, __METHOD__ );
592                 if ( !$info ) {
593                         wfOut( "...index `{$index}` not found; adding..." );
594                         $dbw->sourceFile( archive( $patch ) );
595                         wfOut( "done.\n" );
596                 } else {
597                         wfOut( "...index `{$index}` seems ok.\n" );
598                 }
599         }
602 function index_has_field( $table, $index, $field ) {
603         wfOut( "Checking if $table index $index includes field $field...\n" );
604         $info = wfGetDB( DB_MASTER )->indexInfo( $table, $index, __METHOD__ );
605         if ( $info ) {
606                 foreach ( $info as $row ) {
607                         if ( $row->Column_name == $field ) {
608                                 wfOut( "...index $index on table $table seems to be ok\n" );
609                                 return true;
610                         }
611                 }
612         }
613         wfOut( "...index $index on table $table has no field $field; adding\n" );
614         return false;
617 function do_backlinking_indices_update() {
618         wfOut( "Checking for backlinking indices...\n" );
619         if ( !index_has_field( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
620                 !index_has_field( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
621                 !index_has_field( 'imagelinks', 'il_to', 'il_from' ) )
622         {
623                 wfGetDB( DB_MASTER )->sourceFile( archive( 'patch-backlinkindexes.sql' ) );
624                 wfOut( "...backlinking indices updated\n" );
625         }
628 function do_categorylinks_indices_update() {
629         wfOut( "Checking for categorylinks indices...\n" );
630         if ( !index_has_field( 'categorylinks', 'cl_sortkey', 'cl_from' ) )
631         {
632                 wfGetDB( DB_MASTER )->sourceFile( archive( 'patch-categorylinksindex.sql' ) );
633                 wfOut( "...categorylinks indices updated\n" );
634         }
637 function do_filearchive_indices_update() {
638         $dbw = wfGetDB( DB_MASTER );
639         wfOut( "Checking filearchive indices...\n" );
640         $info = $dbw->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
641         if ( !$info )
642         {
643                 $dbw->sourceFile( archive( 'patch-filearchive-user-index.sql' ) );
644                 wfOut( "...filearchive indices updated\n" );
645         }
648 function maybe_do_profiling_memory_update() {
649         $dbw = wfGetDB( DB_MASTER );
650         if ( !$dbw->tableExists( 'profiling' ) ) {
651                 // Simply ignore
652         } elseif ( $dbw->fieldExists( 'profiling', 'pf_memory' ) ) {
653                 wfOut( "...profiling table has pf_memory field.\n" );
654         } else {
655                 wfOut( "Adding pf_memory field to table profiling..." );
656                 $dbw->sourceFile( archive( 'patch-profiling-memory.sql' ) );
657                 wfOut( "ok\n" );
658         }
661 function do_stats_init() {
662         // Sometimes site_stats table is not properly populated.
663         wfOut( "\nChecking site_stats row..." );
664         $row = wfGetDB( DB_MASTER )->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
665         if ( $row === false ) {
666                 wfOut( "data is missing! rebuilding...\n" );
667         } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
668                 wfOut( "missing ss_total_pages, rebuilding...\n" );
669         } else {
670                 wfOut( "ok.\n" );
671                 return;
672         }
673         SiteStatsInit::doAllAndCommit( false );
676 function do_active_users_init() {
677         $dbw = wfGetDB( DB_MASTER );
678         $activeUsers = $dbw->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
679         if ( $activeUsers == -1 ) {
680                 $activeUsers = $dbw->selectField( 'recentchanges',
681                         'COUNT( DISTINCT rc_user_text )',
682                         array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
683                 );
684                 $dbw->update( 'site_stats',
685                         array( 'ss_active_users' => intval( $activeUsers ) ),
686                         array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
687                 );
688         }
689         wfOut( "...ss_active_users user count set...\n" );
693  * Adding page_restrictions table, obsoleting page.page_restrictions.
694  * Migrating old restrictions to new table
695  * -- Andrew Garrett, January 2007.
696  */
697 function do_restrictions_update() {
698         $dbw = wfGetDB( DB_MASTER );
699         if ( $dbw->tableExists( 'page_restrictions' ) ) {
700                 wfOut( "...page_restrictions table already exists.\n" );
701         } else {
702                 wfOut( "Creating page_restrictions table..." );
703                 $dbw->sourceFile( archive( 'patch-page_restrictions.sql' ) );
704                 $dbw->sourceFile( archive( 'patch-page_restrictions_sortkey.sql' ) );
705                 wfOut( "ok\n" );
707                 wfOut( "Migrating old restrictions to new table...\n" );
708                 $task = new UpdateRestrictions();
709                 $task->execute();
710         }
713 function do_category_population() {
714         if ( update_row_exists( 'populate category' ) ) {
715                 wfOut( "...category table already populated.\n" );
716                 return;
717         }
719         wfOut(
720                 "Populating category table, printing progress markers. " .
721                 "For large databases, you\n" .
722                 "may want to hit Ctrl-C and do this manually with maintenance/\n" .
723                 "populateCategory.php.\n"
724         );
725         $task = new PopulateCategory();
726         $task->execute();
727         wfOut( "Done populating category table.\n" );
730 function do_populate_parent_id() {
731         if ( update_row_exists( 'populate rev_parent_id' ) ) {
732                 wfOut( "...rev_parent_id column already populated.\n" );
733                 return;
734         }
736         $task = new PopulateParentId();
737         $task->execute();
740 function do_populate_rev_len() {
741         if ( update_row_exists( 'populate rev_len' ) ) {
742                 wfOut( "...rev_len column already populated.\n" );
743                 return;
744         }
746         $task = new PopulateRevisionLength();
747         $task->execute();
750 function do_cl_fields_update() {
751         if ( update_row_exists( 'cl_fields_update' ) ) {
752                 wfOut( "...categorylinks up-to-date.\n" );
753                 return;
754         }
755         wfOut( 'Updating categorylinks (again)...' );
756         wfGetDB( DB_MASTER )->sourceFile( archive( 'patch-categorylinks-better-collation2.sql' ) );
757         wfOut( "done.\n" );
760 function do_collation_update() {
761         global $wgCategoryCollation;
762         $dbw = wfGetDB( DB_MASTER );
763         if ( $dbw->selectField(
764                 'categorylinks',
765                 'COUNT(*)',
766                 'cl_collation != ' . $dbw->addQuotes( $wgCategoryCollation ),
767                 __FUNCTION__
768         ) == 0 ) {
769                 wfOut( "...collations up-to-date.\n" );
770                 return;
771         }
773         $task = new UpdateCollation();
774         $task->execute();
777 function sqlite_initial_indexes() {
778         $dbw = wfGetDB( DB_MASTER );
779         // initial-indexes.sql fails if the indexes are already present, so we perform a quick check if our database is newer.
780         if ( update_row_exists( 'initial_indexes' ) || $dbw->indexExists( 'user', 'user_name' ) ) {
781                 wfOut( "...have initial indexes\n" );
782                 return;
783         }
784         wfOut( "Adding initial indexes..." );
785         $dbw->sourceFile( archive( 'initial-indexes.sql' ) );
786         wfOut( "done\n" );
789 function sqlite_setup_searchindex() {
790         $dbw = wfGetDB( DB_MASTER );
791         $module = $dbw->getFulltextSearchModule();
792         $fts3tTable = update_row_exists( 'fts3' );
793         if ( $fts3tTable &&  !$module ) {
794                 wfOut( '...PHP is missing FTS3 support, downgrading tables...' );
795                 $dbw->sourceFile( archive( 'searchindex-no-fts.sql' ) );
796                 wfOut( "done\n" );
797         } elseif ( !$fts3tTable && $module == 'FTS3' ) {
798                 wfOut( '...adding FTS3 search capabilities...' );
799                 $dbw->sourceFile( archive( 'searchindex-fts3.sql' ) );
800                 wfOut( "done\n" );
801         } else {
802                 wfOut( "...fulltext search table appears to be in order.\n" );
803         }
806 function do_unique_pl_tl_il() {
807         $dbw = wfGetDB( DB_MASTER );
808         $info = $dbw->indexInfo( 'pagelinks', 'pl_namespace' );
809         if ( is_array( $info ) && !$info[0]->Non_unique ) {
810                 wfOut( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
811         } else {
812                 wfOut( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
813                 $dbw->sourceFile( archive( 'patch-pl-tl-il-unique.sql' ) );
814                 wfOut( "ok\n" );
815         }
818 function do_log_search_population() {
819         if ( update_row_exists( 'populate log_search' ) ) {
820                 wfOut( "...log_search table already populated.\n" );
821                 return;
822         }
823         wfOut(
824 "Populating log_search table, printing progress markers. For large\n" .
825 "databases, you may want to hit Ctrl-C and do this manually with\n" .
826 "maintenance/populateLogSearch.php.\n" );
827         $task = new PopulateLogSearch();
828         $task->execute();
829         wfOut( "Done populating log_search table.\n" );
832 function rename_eu_wiki_id() {
833         $dbw = wfGetDB( DB_MASTER );
834         if ( $dbw->fieldExists( 'external_user', 'eu_local_id' ) ) {
835                 wfOut( "...eu_wiki_id already renamed to eu_local_id.\n" );
836                 return;
837         }
838         wfOut( "Renaming eu_wiki_id -> eu_local_id... " );
839         $dbw->sourceFile( archive( 'patch-eu_local_id.sql' ) );
840         wfOut( "ok\n" );
843 function do_update_transcache_field() {
844         if ( update_row_exists( 'convert transcache field' ) ) {
845                 wfOut( "...transcache tc_time already converted.\n" );
846                 return;
847         } else {
848                 wfOut( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
849                 wfGetDB( DB_MASTER )->sourceFile( archive( 'patch-tc-timestamp.sql' ) );
850                 wfOut( "ok\n" );
851         }
854 function do_update_mime_minor_field() {
855         if ( update_row_exists( 'mime_minor_length' ) ) {
856                 wfOut( "...*_mime_minor fields are already long enough.\n" );
857         } else {
858                 wfOut( "Altering all *_mime_minor fields to 100 bytes in size ... " );
859                 wfGetDB( DB_MASTER )->sourceFile( archive( 'patch-mime_minor_length.sql' ) );
860                 wfOut( "ok\n" );
861         }