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