Removed some specific methods from updaters.inc that can use generic ones
[lhc/web/wiklou.git] / maintenance / updaters.inc
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 */
6
7 if ( !defined( 'MEDIAWIKI' ) ) {
8 echo "This file is not a valid entry point\n";
9 exit( 1 );
10 }
11
12 require_once 'userDupes.inc';
13 # Extension updates
14 require_once( "$IP/includes/Hooks.php" );
15
16 /**
17 * @deprecated. Do not use, ever.
18 */
19 $wgUpdates = array();
20
21
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
30
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;
43 }
44
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 }
60 }
61
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 }
75 }
76
77 function do_all_updates( $shared = false, $purge = true ) {
78 global $wgDatabase, $wgDBtype;
79
80 $updater = DatabaseUpdater::newForDb( $wgDatabase, $shared );
81
82 wfRunHooks( 'LoadExtensionSchemaUpdates', array( &$updater ) );
83
84 $updater->doUpdates();
85
86 if ( !defined( 'MW_NO_SETUP' ) ) {
87 define( 'MW_NO_SETUP', true );
88 }
89
90 foreach( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
91 call_user_func_array( array( new $maint, 'execute' ), array() );
92 }
93
94 if ( $wgDBtype === 'postgres' ) {
95 return;
96 }
97
98 do_stats_init();
99
100 if ( $purge ) {
101 purge_cache();
102 }
103 }
104
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 }
112 }
113
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" );
127 }
128
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;
141 }
142
143 function do_image_index_update() {
144 global $wgDatabase;
145
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;
155 }
156
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 }
166 }
167
168 function do_watchlist_update() {
169 global $wgDatabase;
170 $fname = 'do_watchlist_update';
171 if ( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
172 wfOut( "...the watchlist table is already set up for email notification.\n" );
173 } else {
174 wfOut( "Adding wl_notificationtimestamp field for email notification management." );
175 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
176 $wgDatabase->sourceFile( archive( 'patch-email-notification.sql' ) );
177 wfOut( "ok\n" );
178 }
179 # Check if we need to add talk page rows to the watchlist
180 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
181 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
182 if ( $talk != $nontalk ) {
183 wfOut( "Adding missing watchlist talk page rows... " );
184 flush();
185
186 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
187 array(
188 'wl_user' => 'wl_user',
189 'wl_namespace' => 'wl_namespace | 1',
190 'wl_title' => 'wl_title',
191 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
192 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
193 wfOut( "ok\n" );
194 } else {
195 wfOut( "...watchlist talk page rows already present\n" );
196 }
197 }
198
199 function do_copy_newtalk_to_watchlist() {
200 global $wgDatabase;
201
202 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
203 $wgDatabase->tableName( 'user_newtalk' ) );
204 $num_newtalks = $wgDatabase->numRows( $res );
205 wfOut( "Now converting $num_newtalks user_newtalk entries to watchlist table entries ... \n" );
206
207 $user = new User();
208 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
209 $wluser = $wgDatabase->fetchObject( $res );
210 if ( $wluser->user_id == 0 ) { # anonymous users ... have IP numbers as "names"
211 if ( $user->isIP( $wluser->user_ip ) ) { # do only if it really looks like an IP number (double checked)
212 $wgDatabase->replace( 'watchlist',
213 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
214 array( 'wl_user' => 0,
215 'wl_namespace' => NS_USER_TALK,
216 'wl_title' => $wluser->user_ip,
217 'wl_notificationtimestamp' => '19700101000000'
218 ), 'updaters.inc::do_watchlist_update2'
219 );
220 }
221 } else { # normal users ... have user_ids
222 $user->setID( $wluser->user_id );
223 $wgDatabase->replace( 'watchlist',
224 array( array( 'wl_user', 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ) ),
225 array( 'wl_user' => $user->getID(),
226 'wl_namespace' => NS_USER_TALK,
227 'wl_title' => $user->getName(),
228 'wl_notificationtimestamp' => '19700101000000'
229 ), 'updaters.inc::do_watchlist_update3'
230 );
231 }
232 }
233 wfOut( "Done.\n" );
234 }
235
236 /**
237 * 1.4 betas were missing the 'binary' marker from logging.log_title,
238 * which causes a collation mismatch error on joins in MySQL 4.1.
239 */
240 function check_bin( $table, $field, $patchFile ) {
241 global $wgDatabase, $wgDBtype;
242 if ( $wgDBtype != 'mysql' )
243 return;
244 $tableName = $wgDatabase->tableName( $table );
245 $res = $wgDatabase->query( "SELECT $field FROM $tableName LIMIT 0" );
246 $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
247
248 if ( in_array( 'binary', $flags ) ) {
249 wfOut( "...$table table has correct $field encoding.\n" );
250 } else {
251 wfOut( "Fixing $field encoding on $table table... " );
252 $wgDatabase->sourceFile( archive( $patchFile ) );
253 wfOut( "ok\n" );
254 }
255 }
256
257 function do_schema_restructuring() {
258 global $wgDatabase;
259 $fname = "do_schema_restructuring";
260 if ( $wgDatabase->tableExists( 'page' ) ) {
261 wfOut( "...page table already exists.\n" );
262 } else {
263 wfOut( "...converting from cur/old to page/revision/text DB structure.\n" );
264 wfOut( wfTimestamp( TS_DB ) );
265 wfOut( "......checking for duplicate entries.\n" );
266
267 list ( $cur, $old, $page, $revision, $text ) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
268
269 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
270 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
271
272 if ( $wgDatabase->numRows( $rows ) > 0 ) {
273 wfOut( wfTimestamp( TS_DB ) );
274 wfOut( "......<b>Found duplicate entries</b>\n" );
275 wfOut( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
276 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
277 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
278 $duplicate[$row->cur_namespace] = array();
279 }
280 $duplicate[$row->cur_namespace][] = $row->cur_title;
281 wfOut( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
282 }
283 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
284 $firstCond = true;
285 foreach ( $duplicate as $ns => $titles ) {
286 if ( $firstCond ) {
287 $firstCond = false;
288 } else {
289 $sql .= ' OR ';
290 }
291 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
292 $first = true;
293 foreach ( $titles as $t ) {
294 if ( $first ) {
295 $sql .= $wgDatabase->addQuotes( $t );
296 $first = false;
297 } else {
298 $sql .= ', ' . $wgDatabase->addQuotes( $t );
299 }
300 }
301 $sql .= ") ) \n";
302 }
303 # By sorting descending, the most recent entry will be the first in the list.
304 # All following entries will be deleted by the next while-loop.
305 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
306
307 $rows = $wgDatabase->query( $sql, $fname );
308
309 $prev_title = $prev_namespace = false;
310 $deleteId = array();
311
312 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
313 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
314 $deleteId[] = $row->cur_id;
315 }
316 $prev_title = $row->cur_title;
317 $prev_namespace = $row->cur_namespace;
318 }
319 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
320 $rows = $wgDatabase->query( $sql, $fname );
321 wfOut( wfTimestamp( TS_DB ) );
322 wfOut( "......<b>Deleted</b> " . $wgDatabase->affectedRows() . " records.\n" );
323 }
324
325
326 wfOut( wfTimestamp( TS_DB ) );
327 wfOut( "......Creating tables.\n" );
328 $wgDatabase->query( "CREATE TABLE $page (
329 page_id int(8) unsigned NOT NULL auto_increment,
330 page_namespace int NOT NULL,
331 page_title varchar(255) binary NOT NULL,
332 page_restrictions tinyblob NOT NULL,
333 page_counter bigint(20) unsigned NOT NULL default '0',
334 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
335 page_is_new tinyint(1) unsigned NOT NULL default '0',
336 page_random real unsigned NOT NULL,
337 page_touched char(14) binary NOT NULL default '',
338 page_latest int(8) unsigned NOT NULL,
339 page_len int(8) unsigned NOT NULL,
340
341 PRIMARY KEY page_id (page_id),
342 UNIQUE INDEX name_title (page_namespace,page_title),
343 INDEX (page_random),
344 INDEX (page_len)
345 ) ENGINE=InnoDB", $fname );
346 $wgDatabase->query( "CREATE TABLE $revision (
347 rev_id int(8) unsigned NOT NULL auto_increment,
348 rev_page int(8) unsigned NOT NULL,
349 rev_comment tinyblob NOT NULL,
350 rev_user int(5) unsigned NOT NULL default '0',
351 rev_user_text varchar(255) binary NOT NULL default '',
352 rev_timestamp char(14) binary NOT NULL default '',
353 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
354 rev_deleted tinyint(1) unsigned NOT NULL default '0',
355 rev_len int(8) unsigned,
356 rev_parent_id int(8) unsigned default NULL,
357 PRIMARY KEY rev_page_id (rev_page, rev_id),
358 UNIQUE INDEX rev_id (rev_id),
359 INDEX rev_timestamp (rev_timestamp),
360 INDEX page_timestamp (rev_page,rev_timestamp),
361 INDEX user_timestamp (rev_user,rev_timestamp),
362 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
363 ) ENGINE=InnoDB", $fname );
364
365 wfOut( wfTimestamp( TS_DB ) );
366 wfOut( "......Locking tables.\n" );
367 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
368
369 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
370 wfOut( wfTimestamp( TS_DB ) );
371 wfOut( "......maxold is {$maxold}\n" );
372
373 wfOut( wfTimestamp( TS_DB ) );
374 global $wgLegacySchemaConversion;
375 if ( $wgLegacySchemaConversion ) {
376 // Create HistoryBlobCurStub entries.
377 // Text will be pulled from the leftover 'cur' table at runtime.
378 wfOut( "......Moving metadata from cur; using blob references to text in cur table.\n" );
379 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
380 $cur_flags = "'object'";
381 } else {
382 // Copy all cur text in immediately: this may take longer but avoids
383 // having to keep an extra table around.
384 wfOut( "......Moving text from cur.\n" );
385 $cur_text = 'cur_text';
386 $cur_flags = "''";
387 }
388 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
389 old_timestamp, old_minor_edit, old_flags)
390 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
391 FROM $cur", $fname );
392
393 wfOut( wfTimestamp( TS_DB ) );
394 wfOut( "......Setting up revision table.\n" );
395 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
396 rev_minor_edit)
397 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
398 old_timestamp, old_minor_edit
399 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
400
401 wfOut( wfTimestamp( TS_DB ) );
402 wfOut( "......Setting up page table.\n" );
403 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
404 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
405 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
406 cur_random, cur_touched, rev_id, LENGTH(cur_text)
407 FROM $cur,$revision
408 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
409
410 wfOut( wfTimestamp( TS_DB ) );
411 wfOut( "......Unlocking tables.\n" );
412 $wgDatabase->query( "UNLOCK TABLES", $fname );
413
414 wfOut( wfTimestamp( TS_DB ) );
415 wfOut( "......Renaming old.\n" );
416 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
417
418 wfOut( wfTimestamp( TS_DB ) );
419 wfOut( "...done.\n" );
420 }
421 }
422
423 function do_pagelinks_update() {
424 global $wgDatabase;
425 if ( $wgDatabase->tableExists( 'pagelinks' ) ) {
426 wfOut( "...already have pagelinks table.\n" );
427 } else {
428 wfOut( "Converting links and brokenlinks tables to pagelinks... " );
429 $wgDatabase->sourceFile( archive( 'patch-pagelinks.sql' ) );
430 wfOut( "ok\n" );
431 flush();
432
433 global $wgCanonicalNamespaceNames;
434 foreach ( $wgCanonicalNamespaceNames as $ns => $name ) {
435 if ( $ns != 0 ) {
436 do_pagelinks_namespace( $ns );
437 }
438 }
439 }
440 }
441
442 function do_pagelinks_namespace( $namespace ) {
443 global $wgDatabase, $wgContLang;
444
445 $ns = intval( $namespace );
446 wfOut( "Cleaning up broken links for namespace $ns... " );
447
448 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
449 $name = $wgContLang->getNsText( $ns );
450 $prefix = $wgDatabase->strencode( $name );
451 $likeprefix = str_replace( '_', '\\_', $prefix );
452
453 $sql = "UPDATE $pagelinks
454 SET pl_namespace=$ns,
455 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
456 WHERE pl_namespace=0
457 AND pl_title LIKE '$likeprefix:%'";
458
459 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
460 wfOut( "ok\n" );
461 }
462
463 function do_old_links_update() {
464 if( !defined( 'MW_NO_SETUP' ) ) {
465 define( 'MW_NO_SETUP', true );
466 }
467 require( "convertLinks.php" );
468 $cl = new ConvertLinks();
469 $cl->execute();
470 }
471
472 function fix_ancient_imagelinks() {
473 global $wgDatabase;
474 $info = $wgDatabase->fieldInfo( 'imagelinks', 'il_from' );
475 if ( $info && $info->type() === 'string' ) {
476 wfOut( "Fixing ancient broken imagelinks table.\n" );
477 wfOut( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
478 $wgDatabase->sourceFile( archive( 'patch-fix-il_from.sql' ) );
479 wfOut( "ok\n" );
480 } else {
481 wfOut( "...il_from OK\n" );
482 }
483 }
484
485 function do_user_unique_update() {
486 global $wgDatabase;
487 $duper = new UserDupes( $wgDatabase );
488 if ( $duper->hasUniqueIndex() ) {
489 wfOut( "...already have unique user_name index.\n" );
490 } else {
491 if ( !$duper->clearDupes() ) {
492 wfOut( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
493 }
494 wfOut( "Adding unique index on user_name... " );
495 $wgDatabase->sourceFile( archive( 'patch-user_nameindex.sql' ) );
496 wfOut( "ok\n" );
497 }
498 }
499
500 function do_user_groups_update() {
501 $fname = 'do_user_groups_update';
502 global $wgDatabase;
503
504 if ( $wgDatabase->tableExists( 'user_groups' ) ) {
505 wfOut( "...user_groups table already exists.\n" );
506 return do_user_groups_reformat();
507 }
508
509 wfOut( "Adding user_groups table... " );
510 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
511 wfOut( "ok\n" );
512
513 if ( !$wgDatabase->tableExists( 'user_rights' ) ) {
514 if ( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
515 wfOut( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
516 $wgDatabase->sourceFile( archive( 'patch-user_rights.sql' ) );
517 wfOut( "ok\n" );
518 } else {
519 wfOut( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
520 wfOut( "*** You may need to manually configure some sysops by manipulating\n" );
521 wfOut( "*** the user_groups table.\n" );
522 return;
523 }
524 }
525
526 wfOut( "Converting user_rights table to user_groups... " );
527 $result = $wgDatabase->select( 'user_rights',
528 array( 'ur_user', 'ur_rights' ),
529 array( "ur_rights != ''" ),
530 $fname );
531
532 while ( $row = $wgDatabase->fetchObject( $result ) ) {
533 $groups = array_unique(
534 array_map( 'trim',
535 explode( ',', $row->ur_rights ) ) );
536
537 foreach ( $groups as $group ) {
538 $wgDatabase->insert( 'user_groups',
539 array(
540 'ug_user' => $row->ur_user,
541 'ug_group' => $group ),
542 $fname );
543 }
544 }
545 wfOut( "ok\n" );
546 }
547
548 function do_user_groups_reformat() {
549 # Check for bogus formats from previous 1.5 alpha code.
550 global $wgDatabase;
551 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
552
553 if ( $info->type() == 'int' ) {
554 $oldug = $wgDatabase->tableName( 'user_groups' );
555 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
556 wfOut( "user_groups is in bogus intermediate format. Renaming to $newug... " );
557 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
558 wfOut( "ok\n" );
559
560 wfOut( "Re-adding fresh user_groups table... " );
561 $wgDatabase->sourceFile( archive( 'patch-user_groups.sql' ) );
562 wfOut( "ok\n" );
563
564 wfOut( "***\n" );
565 wfOut( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
566 wfOut( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
567 wfOut( "***\n" );
568 } else {
569 wfOut( "...user_groups is in current format.\n" );
570 }
571
572 }
573
574 function do_watchlist_null() {
575 # Make sure wl_notificationtimestamp can be NULL,
576 # and update old broken items.
577 global $wgDatabase;
578 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
579
580 if ( !$info->nullable() ) {
581 wfOut( "Making wl_notificationtimestamp nullable... " );
582 $wgDatabase->sourceFile( archive( 'patch-watchlist-null.sql' ) );
583 wfOut( "ok\n" );
584 } else {
585 wfOut( "...wl_notificationtimestamp is already nullable.\n" );
586 }
587
588 }
589
590 /**
591 * @bug 3946
592 */
593 function do_page_random_update() {
594 global $wgDatabase;
595
596 wfOut( "Setting page_random to a random value on rows where it equals 0..." );
597
598 $page = $wgDatabase->tableName( 'page' );
599 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
600 $rows = $wgDatabase->affectedRows();
601
602 wfOut( "changed $rows rows\n" );
603 }
604
605 function do_templatelinks_update() {
606 global $wgDatabase;
607 $fname = 'do_templatelinks_update';
608
609 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
610 wfOut( "...templatelinks table already exists\n" );
611 return;
612 }
613 wfOut( "Creating templatelinks table...\n" );
614 $wgDatabase->sourceFile( archive( 'patch-templatelinks.sql' ) );
615 wfOut( "Populating...\n" );
616 if ( wfGetLB()->getServerCount() > 1 ) {
617 // Slow, replication-friendly update
618 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
619 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
620 $count = 0;
621 while ( $row = $wgDatabase->fetchObject( $res ) ) {
622 $count = ( $count + 1 ) % 100;
623 if ( $count == 0 ) {
624 if ( function_exists( 'wfWaitForSlaves' ) ) {
625 wfWaitForSlaves( 10 );
626 } else {
627 sleep( 1 );
628 }
629 }
630 $wgDatabase->insert( 'templatelinks',
631 array(
632 'tl_from' => $row->pl_from,
633 'tl_namespace' => $row->pl_namespace,
634 'tl_title' => $row->pl_title,
635 ), $fname
636 );
637
638 }
639 } else {
640 // Fast update
641 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
642 array(
643 'tl_from' => 'pl_from',
644 'tl_namespace' => 'pl_namespace',
645 'tl_title' => 'pl_title'
646 ), array(
647 'pl_namespace' => 10
648 ), $fname
649 );
650 }
651 wfOut( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
652 }
653
654 // Add index on ( rc_namespace, rc_user_text ) [Jul. 2006]
655 // Add index on ( rc_user_text, rc_timestamp ) [Nov. 2006]
656 function do_rc_indices_update() {
657 global $wgDatabase;
658 wfOut( "Checking for additional recent changes indices...\n" );
659
660 $indexes = array(
661 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
662 'rc_user_text' => 'patch-rc_user_text-index.sql',
663 );
664
665 foreach ( $indexes as $index => $patch ) {
666 $info = $wgDatabase->indexInfo( 'recentchanges', $index, __METHOD__ );
667 if ( !$info ) {
668 wfOut( "...index `{$index}` not found; adding..." );
669 $wgDatabase->sourceFile( archive( $patch ) );
670 wfOut( "done.\n" );
671 } else {
672 wfOut( "...index `{$index}` seems ok.\n" );
673 }
674 }
675 }
676
677 function index_has_field( $table, $index, $field ) {
678 global $wgDatabase;
679 wfOut( "Checking if $table index $index includes field $field...\n" );
680 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
681 if ( $info ) {
682 foreach ( $info as $row ) {
683 if ( $row->Column_name == $field ) {
684 wfOut( "...index $index on table $table seems to be ok\n" );
685 return true;
686 }
687 }
688 }
689 wfOut( "...index $index on table $table has no field $field; adding\n" );
690 return false;
691 }
692
693 function do_backlinking_indices_update() {
694 global $wgDatabase;
695 wfOut( "Checking for backlinking indices...\n" );
696 if ( !index_has_field( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
697 !index_has_field( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
698 !index_has_field( 'imagelinks', 'il_to', 'il_from' ) )
699 {
700 $wgDatabase->sourceFile( archive( 'patch-backlinkindexes.sql' ) );
701 wfOut( "...backlinking indices updated\n" );
702 }
703 }
704
705 function do_categorylinks_indices_update() {
706 global $wgDatabase;
707 wfOut( "Checking for categorylinks indices...\n" );
708 if ( !index_has_field( 'categorylinks', 'cl_sortkey', 'cl_from' ) )
709 {
710 $wgDatabase->sourceFile( archive( 'patch-categorylinksindex.sql' ) );
711 wfOut( "...categorylinks indices updated\n" );
712 }
713 }
714
715 function do_filearchive_indices_update() {
716 global $wgDatabase;
717 wfOut( "Checking filearchive indices...\n" );
718 $info = $wgDatabase->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
719 if ( !$info )
720 {
721 $wgDatabase->sourceFile( archive( 'patch-filearchive-user-index.sql' ) );
722 wfOut( "...filearchive indices updated\n" );
723 }
724 }
725
726 function maybe_do_profiling_memory_update() {
727 global $wgDatabase;
728 if ( !$wgDatabase->tableExists( 'profiling' ) ) {
729 // Simply ignore
730 } elseif ( $wgDatabase->fieldExists( 'profiling', 'pf_memory' ) ) {
731 wfOut( "...profiling table has pf_memory field.\n" );
732 } else {
733 wfOut( "Adding pf_memory field to table profiling..." );
734 $wgDatabase->sourceFile( archive( 'patch-profiling-memory.sql' ) );
735 wfOut( "ok\n" );
736 }
737 }
738
739 function do_stats_init() {
740 // Sometimes site_stats table is not properly populated.
741 global $wgDatabase;
742 wfOut( "\nChecking site_stats row..." );
743 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
744 if ( $row === false ) {
745 wfOut( "data is missing! rebuilding...\n" );
746 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
747 wfOut( "missing ss_total_pages, rebuilding...\n" );
748 } else {
749 wfOut( "ok.\n" );
750 return;
751 }
752 SiteStatsInit::doAllAndCommit( false );
753 }
754
755 function do_active_users_init() {
756 global $wgDatabase;
757 $activeUsers = $wgDatabase->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
758 if ( $activeUsers == -1 ) {
759 $activeUsers = $wgDatabase->selectField( 'recentchanges',
760 'COUNT( DISTINCT rc_user_text )',
761 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
762 );
763 $wgDatabase->update( 'site_stats',
764 array( 'ss_active_users' => intval( $activeUsers ) ),
765 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
766 );
767 }
768 wfOut( "...ss_active_users user count set...\n" );
769 }
770
771 function purge_cache() {
772 global $wgDatabase;
773 # We can't guarantee that the user will be able to use TRUNCATE,
774 # but we know that DELETE is available to us
775 wfOut( "Purging caches..." );
776 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
777 wfOut( "done.\n" );
778 }
779
780 function do_restrictions_update() {
781 # Adding page_restrictions table, obsoleting page.page_restrictions.
782 # Migrating old restrictions to new table
783 # -- Andrew Garrett, January 2007.
784
785 global $wgDatabase;
786
787 $name = 'page_restrictions';
788 $patch = 'patch-page_restrictions.sql';
789 $patch2 = 'patch-page_restrictions_sortkey.sql';
790
791 if ( $wgDatabase->tableExists( $name ) ) {
792 wfOut( "...$name table already exists.\n" );
793 } else {
794 wfOut( "Creating $name table..." );
795 $wgDatabase->sourceFile( archive( $patch ) );
796 $wgDatabase->sourceFile( archive( $patch2 ) );
797 wfOut( "ok\n" );
798
799 wfOut( "Migrating old restrictions to new table..." );
800
801 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array( "page_restrictions!=''", "page_restrictions!='edit=:move='" ), __METHOD__ );
802
803 $count = 0;
804
805 while ( $row = $wgDatabase->fetchObject( $res ) ) {
806 $count = ( $count + 1 ) % 100;
807
808 if ( $count == 0 ) {
809 if ( function_exists( 'wfWaitForSlaves' ) ) {
810 wfWaitForSlaves( 10 );
811 } else {
812 sleep( 1 );
813 }
814 }
815
816 # Figure out what the restrictions are..
817 $id = $row->page_id;
818 $flatrestrictions = explode( ':', $row->page_restrictions );
819
820 $restrictions = array ();
821 foreach ( $flatrestrictions as $restriction ) {
822 $thisrestriction = explode( '=', $restriction, 2 );
823 if ( count( $thisrestriction ) == 1 ) {
824 // Compatibility with old protections from before
825 // separate move protection was added.
826 list( $level ) = $thisrestriction;
827 if ( $level ) {
828 $restrictions['edit'] = $level;
829 $restrictions['move'] = $level;
830 }
831 } else {
832 list( $type, $level ) = $thisrestriction;
833 if ( $level ) {
834 $restrictions[$type] = $level;
835 }
836 }
837
838 $wgDatabase->update( 'page', array ( 'page_restrictions' => '' ), array( 'page_id' => $id ), __METHOD__ );
839
840 }
841
842 foreach ( $restrictions as $type => $level ) {
843 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
844 'pr_type' => $type,
845 'pr_level' => $level,
846 'pr_cascade' => 0,
847 'pr_expiry' => 'infinity' ),
848 __METHOD__ );
849 }
850 }
851 wfOut( "ok\n" );
852 }
853 }
854
855 function do_category_population() {
856 if ( update_row_exists( 'populate category' ) ) {
857 wfOut( "...category table already populated.\n" );
858 return;
859 }
860 require_once( 'populateCategory.php' );
861 wfOut(
862 "Populating category table, printing progress markers. " .
863 "For large databases, you\n" .
864 "may want to hit Ctrl-C and do this manually with maintenance/\n" .
865 "populateCategory.php.\n"
866 );
867 $task = new PopulateCategory();
868 $task->execute();
869 wfOut( "Done populating category table.\n" );
870 }
871
872 function do_populate_parent_id() {
873 if ( update_row_exists( 'populate rev_parent_id' ) ) {
874 wfOut( "...rev_parent_id column already populated.\n" );
875 return;
876 }
877 require_once( 'populateParentId.php' );
878 $task = new PopulateParentId();
879 $task->execute();
880 }
881
882 function do_populate_rev_len() {
883 if ( update_row_exists( 'populate rev_len' ) ) {
884 wfOut( "...rev_len column already populated.\n" );
885 return;
886 }
887 require_once( 'populateRevisionLength.php' );
888 $task = new PopulateRevisionLength();
889 $task->execute();
890 }
891
892 function do_collation_update() {
893 global $wgDatabase, $wgCollationVersion;
894 if ( $wgDatabase->selectField(
895 'categorylinks',
896 'COUNT(*)',
897 'cl_collation != ' . $wgDatabase->addQuotes( $wgCollationVersion ),
898 __FUNCTION__
899 ) == 0 ) {
900 wfOut( "...collations up-to-date.\n" );
901 return;
902 }
903 require_once( 'updateCollation.php' );
904 $task = new UpdateCollation();
905 $task->execute();
906 }
907
908 function sqlite_initial_indexes() {
909 global $wgDatabase;
910 // initial-indexes.sql fails if the indexes are already present, so we perform a quick check if our database is newer.
911 if ( update_row_exists( 'initial_indexes' ) || $wgDatabase->indexExists( 'user', 'user_name' ) ) {
912 wfOut( "...have initial indexes\n" );
913 return;
914 }
915 wfOut( "Adding initial indexes..." );
916 $wgDatabase->sourceFile( archive( 'initial-indexes.sql' ) );
917 wfOut( "done\n" );
918 }
919
920 function sqlite_setup_searchindex() {
921 global $wgDatabase;
922 $module = $wgDatabase->getFulltextSearchModule();
923 $fts3tTable = update_row_exists( 'fts3' );
924 if ( $fts3tTable && !$module ) {
925 wfOut( '...PHP is missing FTS3 support, downgrading tables...' );
926 $wgDatabase->sourceFile( archive( 'searchindex-no-fts.sql' ) );
927 wfOut( "done\n" );
928 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
929 wfOut( '...adding FTS3 search capabilities...' );
930 $wgDatabase->sourceFile( archive( 'searchindex-fts3.sql' ) );
931 wfOut( "done\n" );
932 } else {
933 wfOut( "...fulltext search table appears to be in order.\n" );
934 }
935 }
936
937 function do_unique_pl_tl_il() {
938 global $wgDatabase;
939 $info = $wgDatabase->indexInfo( 'pagelinks', 'pl_namespace' );
940 if ( is_array( $info ) && !$info[0]->Non_unique ) {
941 wfOut( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
942 } else {
943 wfOut( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
944 $wgDatabase->sourceFile( archive( 'patch-pl-tl-il-unique.sql' ) );
945 wfOut( "ok\n" );
946 }
947 }
948
949 function do_log_search_population() {
950 if ( update_row_exists( 'populate log_search' ) ) {
951 wfOut( "...log_search table already populated.\n" );
952 return;
953 }
954 require_once( 'populateLogSearch.php' );
955 wfOut(
956 "Populating log_search table, printing progress markers. For large\n" .
957 "databases, you may want to hit Ctrl-C and do this manually with\n" .
958 "maintenance/populateLogSearch.php.\n" );
959 $task = new PopulateLogSearch();
960 $task->execute();
961 wfOut( "Done populating log_search table.\n" );
962 }
963
964 function rename_eu_wiki_id() {
965 global $wgDatabase;
966 if ( $wgDatabase->fieldExists( 'external_user', 'eu_local_id' ) ) {
967 wfOut( "...eu_wiki_id already renamed to eu_local_id.\n" );
968 return;
969 }
970 wfOut( "Renaming eu_wiki_id -> eu_local_id... " );
971 $wgDatabase->sourceFile( archive( 'patch-eu_local_id.sql' ) );
972 wfOut( "ok\n" );
973 }
974
975 function do_update_transcache_field() {
976 global $wgDatabase;
977 if ( update_row_exists( 'convert transcache field' ) ) {
978 wfOut( "...transcache tc_time already converted.\n" );
979 return;
980 } else {
981 wfOut( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
982 $wgDatabase->sourceFile( archive( 'patch-tc-timestamp.sql' ) );
983 wfOut( "ok\n" );
984 }
985 }
986
987 function do_update_mime_minor_field() {
988 if ( update_row_exists( 'mime_minor_length' ) ) {
989 wfOut( "...*_mime_minor fields are already long enough.\n" );
990 } else {
991 global $wgDatabase;
992 wfOut( "Altering all *_mime_minor fields to 100 bytes in size ... " );
993 $wgDatabase->sourceFile( archive( 'patch-mime_minor_length.sql' ) );
994 wfOut( "ok\n" );
995 }
996 }