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