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