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