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