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