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