Merge "Set $mLoadedItems to true in User::loadFromId()"
[lhc/web/wiklou.git] / includes / installer / DatabaseUpdater.php
1 <?php
2 /**
3 * DBMS-specific updater helper.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 require_once( __DIR__ . '/../../maintenance/Maintenance.php' );
25
26 /**
27 * Class for handling database updates. Roughly based off of updaters.inc, with
28 * a few improvements :)
29 *
30 * @ingroup Deployment
31 * @since 1.17
32 */
33 abstract class DatabaseUpdater {
34
35 /**
36 * Array of updates to perform on the database
37 *
38 * @var array
39 */
40 protected $updates = array();
41
42 /**
43 * Array of updates that were skipped
44 *
45 * @var array
46 */
47 protected $updatesSkipped = array();
48
49 /**
50 * List of extension-provided database updates
51 * @var array
52 */
53 protected $extensionUpdates = array();
54
55 /**
56 * Handle to the database subclass
57 *
58 * @var DatabaseBase
59 */
60 protected $db;
61
62 protected $shared = false;
63
64 /**
65 * Scripts to run after database update
66 * Should be a subclass of LoggedUpdateMaintenance
67 */
68 protected $postDatabaseUpdateMaintenance = array(
69 'DeleteDefaultMessages',
70 'PopulateRevisionLength',
71 'PopulateRevisionSha1',
72 'PopulateImageSha1',
73 'FixExtLinksProtocolRelative',
74 'PopulateFilearchiveSha1',
75 );
76
77 /**
78 * File handle for SQL output.
79 *
80 * @var Filehandle
81 */
82 protected $fileHandle = null;
83
84 /**
85 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
86 *
87 * @var bool
88 */
89 protected $skipSchema = false;
90
91 /**
92 * Constructor
93 *
94 * @param $db DatabaseBase object to perform updates on
95 * @param $shared bool Whether to perform updates on shared tables
96 * @param $maintenance Maintenance Maintenance object which created us
97 */
98 protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
99 $this->db = $db;
100 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
101 $this->shared = $shared;
102 if ( $maintenance ) {
103 $this->maintenance = $maintenance;
104 $this->fileHandle = $maintenance->fileHandle;
105 } else {
106 $this->maintenance = new FakeMaintenance;
107 }
108 $this->maintenance->setDB( $db );
109 $this->initOldGlobals();
110 $this->loadExtensions();
111 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
112 }
113
114 /**
115 * Initialize all of the old globals. One day this should all become
116 * something much nicer
117 */
118 private function initOldGlobals() {
119 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
120 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
121
122 # For extensions only, should be populated via hooks
123 # $wgDBtype should be checked to specifiy the proper file
124 $wgExtNewTables = array(); // table, dir
125 $wgExtNewFields = array(); // table, column, dir
126 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
127 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
128 $wgExtNewIndexes = array(); // table, index, dir
129 $wgExtModifiedFields = array(); // table, index, dir
130 }
131
132 /**
133 * Loads LocalSettings.php, if needed, and initialises everything needed for LoadExtensionSchemaUpdates hook
134 */
135 private function loadExtensions() {
136 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
137 return; // already loaded
138 }
139 $vars = Installer::getExistingLocalSettings();
140 if ( !$vars ) {
141 return; // no LocalSettings found
142 }
143 if ( !isset( $vars['wgHooks'] ) || !isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
144 return;
145 }
146 global $wgHooks, $wgAutoloadClasses;
147 $wgHooks['LoadExtensionSchemaUpdates'] = $vars['wgHooks']['LoadExtensionSchemaUpdates'];
148 $wgAutoloadClasses = $wgAutoloadClasses + $vars['wgAutoloadClasses'];
149 }
150
151 /**
152 * @throws MWException
153 * @param DatabaseBase $db
154 * @param bool $shared
155 * @param null $maintenance
156 * @return DatabaseUpdater
157 */
158 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
159 $type = $db->getType();
160 if( in_array( $type, Installer::getDBTypes() ) ) {
161 $class = ucfirst( $type ) . 'Updater';
162 return new $class( $db, $shared, $maintenance );
163 } else {
164 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
165 }
166 }
167
168 /**
169 * Get a database connection to run updates
170 *
171 * @return DatabaseBase
172 */
173 public function getDB() {
174 return $this->db;
175 }
176
177 /**
178 * Output some text. If we're running from web, escape the text first.
179 *
180 * @param $str String: Text to output
181 */
182 public function output( $str ) {
183 if ( $this->maintenance->isQuiet() ) {
184 return;
185 }
186 global $wgCommandLineMode;
187 if( !$wgCommandLineMode ) {
188 $str = htmlspecialchars( $str );
189 }
190 echo $str;
191 flush();
192 }
193
194 /**
195 * Add a new update coming from an extension. This should be called by
196 * extensions while executing the LoadExtensionSchemaUpdates hook.
197 *
198 * @since 1.17
199 *
200 * @param $update Array: the update to run. Format is the following:
201 * first item is the callback function, it also can be a
202 * simple string with the name of a function in this class,
203 * following elements are parameters to the function.
204 * Note that callback functions will receive this object as
205 * first parameter.
206 */
207 public function addExtensionUpdate( array $update ) {
208 $this->extensionUpdates[] = $update;
209 }
210
211 /**
212 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
213 * is the most common usage of updaters in an extension)
214 *
215 * @since 1.18
216 *
217 * @param $tableName String Name of table to create
218 * @param $sqlPath String Full path to the schema file
219 */
220 public function addExtensionTable( $tableName, $sqlPath ) {
221 $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true );
222 }
223
224 /**
225 * @since 1.19
226 *
227 * @param $tableName string
228 * @param $indexName string
229 * @param $sqlPath string
230 */
231 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
232 $this->extensionUpdates[] = array( 'addIndex', $tableName, $indexName, $sqlPath, true );
233 }
234
235 /**
236 *
237 * @since 1.19
238 *
239 * @param $tableName string
240 * @param $columnName string
241 * @param $sqlPath string
242 */
243 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
244 $this->extensionUpdates[] = array( 'addField', $tableName, $columnName, $sqlPath, true );
245 }
246
247 /**
248 *
249 * @since 1.20
250 *
251 * @param $tableName string
252 * @param $columnName string
253 * @param $sqlPath string
254 */
255 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
256 $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true );
257 }
258
259 /**
260 *
261 * @since 1.20
262 *
263 * @param $tableName string
264 * @param $sqlPath string
265 */
266 public function dropExtensionTable( $tableName, $sqlPath ) {
267 $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true );
268 }
269
270 /**
271 *
272 * @since 1.20
273 *
274 * @param $tableName string
275 * @return bool
276 */
277 public function tableExists( $tableName ) {
278 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
279 }
280
281 /**
282 * Add a maintenance script to be run after the database updates are complete.
283 *
284 * Script should subclass LoggedUpdateMaintenance
285 *
286 * @since 1.19
287 *
288 * @param $class string Name of a Maintenance subclass
289 */
290 public function addPostDatabaseUpdateMaintenance( $class ) {
291 $this->postDatabaseUpdateMaintenance[] = $class;
292 }
293
294 /**
295 * Get the list of extension-defined updates
296 *
297 * @return Array
298 */
299 protected function getExtensionUpdates() {
300 return $this->extensionUpdates;
301 }
302
303 /**
304 * @since 1.17
305 *
306 * @return array
307 */
308 public function getPostDatabaseUpdateMaintenance() {
309 return $this->postDatabaseUpdateMaintenance;
310 }
311
312 /**
313 * @since 1.21
314 *
315 * Writes the schema updates desired to a file for the DB Admin to run.
316 */
317 private function writeSchemaUpdateFile( $schemaUpdate = array() ) {
318 $updates = $this->updatesSkipped;
319 $this->updatesSkipped = array();
320
321 foreach( $updates as $funcList ) {
322 $func = $funcList[0];
323 $arg = $funcList[1];
324 $origParams = $funcList[2];
325 $ret = call_user_func_array( $func, $arg );
326 flush();
327 $this->updatesSkipped[] = $origParams;
328 }
329 }
330
331 /**
332 * Do all the updates
333 *
334 * @param $what Array: what updates to perform
335 */
336 public function doUpdates( $what = array( 'core', 'extensions', 'stats' ) ) {
337 global $wgVersion, $wgLocalisationCacheConf;
338
339 $this->db->begin( __METHOD__ );
340 $what = array_flip( $what );
341 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
342 if ( isset( $what['core'] ) ) {
343 $this->runUpdates( $this->getCoreUpdateList(), false );
344 }
345 if ( isset( $what['extensions'] ) ) {
346 $this->runUpdates( $this->getOldGlobalUpdates(), false );
347 $this->runUpdates( $this->getExtensionUpdates(), true );
348 }
349
350 if ( isset( $what['stats'] ) ) {
351 $this->checkStats();
352 }
353
354 if ( isset( $what['purge'] ) ) {
355 $this->purgeCache();
356
357 if ( $wgLocalisationCacheConf['manualRecache'] ) {
358 $this->rebuildLocalisationCache();
359 }
360 }
361
362 $this->setAppliedUpdates( $wgVersion, $this->updates );
363
364 if( $this->fileHandle ) {
365 $this->skipSchema = false;
366 $this->writeSchemaUpdateFile( );
367 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
368 }
369
370 $this->db->commit( __METHOD__ );
371 }
372
373 /**
374 * Helper function for doUpdates()
375 *
376 * @param $updates Array of updates to run
377 * @param $passSelf Boolean: whether to pass this object we calling external
378 * functions
379 */
380 private function runUpdates( array $updates, $passSelf ) {
381 $updatesDone = array();
382 $updatesSkipped = array();
383 foreach ( $updates as $params ) {
384 $origParams = $params;
385 $func = array_shift( $params );
386 if( !is_array( $func ) && method_exists( $this, $func ) ) {
387 $func = array( $this, $func );
388 } elseif ( $passSelf ) {
389 array_unshift( $params, $this );
390 }
391 $ret = call_user_func_array( $func, $params );
392 flush();
393 if( $ret !== false ) {
394 $updatesDone[] = $origParams;
395 } else {
396 $updatesSkipped[] = array( $func, $params, $origParams );
397 }
398 }
399 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
400 $this->updates = array_merge( $this->updates, $updatesDone );
401 }
402
403 /**
404 * @param $version
405 * @param $updates array
406 */
407 protected function setAppliedUpdates( $version, $updates = array() ) {
408 $this->db->clearFlag( DBO_DDLMODE );
409 if( !$this->canUseNewUpdatelog() ) {
410 return;
411 }
412 $key = "updatelist-$version-" . time();
413 $this->db->insert( 'updatelog',
414 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
415 __METHOD__ );
416 $this->db->setFlag( DBO_DDLMODE );
417 }
418
419 /**
420 * Helper function: check if the given key is present in the updatelog table.
421 * Obviously, only use this for updates that occur after the updatelog table was
422 * created!
423 * @param $key String Name of the key to check for
424 *
425 * @return bool
426 */
427 public function updateRowExists( $key ) {
428 $row = $this->db->selectRow(
429 'updatelog',
430 '1',
431 array( 'ul_key' => $key ),
432 __METHOD__
433 );
434 return (bool)$row;
435 }
436
437 /**
438 * Helper function: Add a key to the updatelog table
439 * Obviously, only use this for updates that occur after the updatelog table was
440 * created!
441 * @param $key String Name of key to insert
442 * @param $val String [optional] value to insert along with the key
443 */
444 public function insertUpdateRow( $key, $val = null ) {
445 $this->db->clearFlag( DBO_DDLMODE );
446 $values = array( 'ul_key' => $key );
447 if( $val && $this->canUseNewUpdatelog() ) {
448 $values['ul_value'] = $val;
449 }
450 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
451 $this->db->setFlag( DBO_DDLMODE );
452 }
453
454 /**
455 * Updatelog was changed in 1.17 to have a ul_value column so we can record
456 * more information about what kind of updates we've done (that's what this
457 * class does). Pre-1.17 wikis won't have this column, and really old wikis
458 * might not even have updatelog at all
459 *
460 * @return boolean
461 */
462 protected function canUseNewUpdatelog() {
463 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
464 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
465 }
466
467 /**
468 * Returns whether updates should be executed on the database table $name.
469 * Updates will be prevented if the table is a shared table and it is not
470 * specified to run updates on shared tables.
471 *
472 * @param $name String table name
473 * @return bool
474 */
475 protected function doTable( $name ) {
476 global $wgSharedDB, $wgSharedTables;
477
478 // Don't bother to check $wgSharedTables if there isn't a shared database
479 // or the user actually also wants to do updates on the shared database.
480 if ( $wgSharedDB === null || $this->shared ) {
481 return true;
482 }
483
484 return !in_array( $name, $wgSharedTables );
485 }
486
487 /**
488 * Before 1.17, we used to handle updates via stuff like
489 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
490 * of this in 1.17 but we want to remain back-compatible for a while. So
491 * load up these old global-based things into our update list.
492 *
493 * @return array
494 */
495 protected function getOldGlobalUpdates() {
496 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
497 $wgExtNewIndexes;
498
499 $updates = array();
500
501 foreach ( $wgExtNewTables as $tableRecord ) {
502 $updates[] = array(
503 'addTable', $tableRecord[0], $tableRecord[1], true
504 );
505 }
506
507 foreach ( $wgExtNewFields as $fieldRecord ) {
508 $updates[] = array(
509 'addField', $fieldRecord[0], $fieldRecord[1],
510 $fieldRecord[2], true
511 );
512 }
513
514 foreach ( $wgExtNewIndexes as $fieldRecord ) {
515 $updates[] = array(
516 'addIndex', $fieldRecord[0], $fieldRecord[1],
517 $fieldRecord[2], true
518 );
519 }
520
521 foreach ( $wgExtModifiedFields as $fieldRecord ) {
522 $updates[] = array(
523 'modifyField', $fieldRecord[0], $fieldRecord[1],
524 $fieldRecord[2], true
525 );
526 }
527
528 return $updates;
529 }
530
531 /**
532 * Get an array of updates to perform on the database. Should return a
533 * multi-dimensional array. The main key is the MediaWiki version (1.12,
534 * 1.13...) with the values being arrays of updates, identical to how
535 * updaters.inc did it (for now)
536 *
537 * @return Array
538 */
539 protected abstract function getCoreUpdateList();
540
541 /**
542 * Append an SQL fragment to the open file handle.
543 *
544 * @param $filename String: File name to open
545 */
546 public function copyFile( $filename ) {
547 $this->db->sourceFile( $filename, false, false, false,
548 array( $this, 'appendLine' )
549 );
550 }
551
552 /**
553 * Append a line to the open filehandle. The line is assumed to
554 * be a complete SQL statement.
555 *
556 * This is used as a callback for for sourceLine().
557 *
558 * @param $line String text to append to the file
559 * @return Boolean false to skip actually executing the file
560 * @throws MWException
561 */
562 public function appendLine( $line ) {
563 $line = rtrim( $line ) . ";\n";
564 if( fwrite( $this->fileHandle, $line ) === false ) {
565 throw new MWException( "trouble writing file" );
566 }
567 return false;
568 }
569
570 /**
571 * Applies a SQL patch
572 * @param $path String Path to the patch file
573 * @param $isFullPath Boolean Whether to treat $path as a relative or not
574 * @param $msg String Description of the patch
575 * @return boolean false if patch is skipped.
576 */
577 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
578 if ( $msg === null ) {
579 $msg = "Applying $path patch";
580 }
581 if ( $this->skipSchema ) {
582 $this->output( "...skipping schema change ($msg).\n" );
583 return false;
584 }
585
586 $this->output( "$msg ..." );
587
588 if ( !$isFullPath ) {
589 $path = $this->db->patchPath( $path );
590 }
591 if( $this->fileHandle !== null ) {
592 $this->copyFile( $path );
593 } else {
594 $this->db->sourceFile( $path );
595 }
596 $this->output( "done.\n" );
597 return true;
598 }
599
600 /**
601 * Add a new table to the database
602 * @param $name String Name of the new table
603 * @param $patch String Path to the patch file
604 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
605 * @return Boolean false if this was skipped because schema changes are skipped
606 */
607 protected function addTable( $name, $patch, $fullpath = false ) {
608 if ( !$this->doTable( $name ) ) {
609 return true;
610 }
611
612 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
613 $this->output( "...$name table already exists.\n" );
614 } else {
615 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
616 }
617 return true;
618 }
619
620 /**
621 * Add a new field to an existing table
622 * @param $table String Name of the table to modify
623 * @param $field String Name of the new field
624 * @param $patch String Path to the patch file
625 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
626 * @return Boolean false if this was skipped because schema changes are skipped
627 */
628 protected function addField( $table, $field, $patch, $fullpath = false ) {
629 if ( !$this->doTable( $table ) ) {
630 return true;
631 }
632
633 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
634 $this->output( "...$table table does not exist, skipping new field patch.\n" );
635 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
636 $this->output( "...have $field field in $table table.\n" );
637 } else {
638 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
639 }
640 return true;
641 }
642
643 /**
644 * Add a new index to an existing table
645 * @param $table String Name of the table to modify
646 * @param $index String Name of the new index
647 * @param $patch String Path to the patch file
648 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
649 * @return Boolean false if this was skipped because schema changes are skipped
650 */
651 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
652 if ( !$this->doTable( $table ) ) {
653 return true;
654 }
655
656 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
657 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
658 return false;
659 } else if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
660 $this->output( "...index $index already set on $table table.\n" );
661 } else {
662 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
663 }
664 return true;
665 }
666
667 /**
668 * Drop a field from an existing table
669 *
670 * @param $table String Name of the table to modify
671 * @param $field String Name of the old field
672 * @param $patch String Path to the patch file
673 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
674 * @return Boolean false if this was skipped because schema changes are skipped
675 */
676 protected function dropField( $table, $field, $patch, $fullpath = false ) {
677 if ( !$this->doTable( $table ) ) {
678 return true;
679 }
680
681 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
682 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
683 } else {
684 $this->output( "...$table table does not contain $field field.\n" );
685 }
686 return true;
687 }
688
689 /**
690 * Drop an index from an existing table
691 *
692 * @param $table String: Name of the table to modify
693 * @param $index String: Name of the old index
694 * @param $patch String: Path to the patch file
695 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
696 * @return Boolean false if this was skipped because schema changes are skipped
697 */
698 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
699 if ( !$this->doTable( $table ) ) {
700 return true;
701 }
702
703 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
704 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
705 } else {
706 $this->output( "...$index key doesn't exist.\n" );
707 }
708 return true;
709 }
710
711 /**
712 * If the specified table exists, drop it, or execute the
713 * patch if one is provided.
714 *
715 * Public @since 1.20
716 *
717 * @param $table string
718 * @param $patch string|false
719 * @param $fullpath bool
720 * @return Boolean false if this was skipped because schema changes are skipped
721 */
722 public function dropTable( $table, $patch = false, $fullpath = false ) {
723 if ( !$this->doTable( $table ) ) {
724 return true;
725 }
726
727 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
728 $msg = "Dropping table $table";
729
730 if ( $patch === false ) {
731 $this->output( "$msg ..." );
732 $this->db->dropTable( $table, __METHOD__ );
733 $this->output( "done.\n" );
734 }
735 else {
736 return $this->applyPatch( $patch, $fullpath, $msg );
737 }
738 } else {
739 $this->output( "...$table doesn't exist.\n" );
740 }
741 return true;
742 }
743
744 /**
745 * Modify an existing field
746 *
747 * @param $table String: name of the table to which the field belongs
748 * @param $field String: name of the field to modify
749 * @param $patch String: path to the patch file
750 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
751 * @return Boolean false if this was skipped because schema changes are skipped
752 */
753 public function modifyField( $table, $field, $patch, $fullpath = false ) {
754 if ( !$this->doTable( $table ) ) {
755 return true;
756 }
757
758 $updateKey = "$table-$field-$patch";
759 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
760 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
761 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
762 $this->output( "...$field field does not exist in $table table, skipping modify field patch.\n" );
763 } elseif( $this->updateRowExists( $updateKey ) ) {
764 $this->output( "...$field in table $table already modified by patch $patch.\n" );
765 } else {
766 $this->insertUpdateRow( $updateKey );
767 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
768 }
769 return true;
770 }
771
772 /**
773 * Purge the objectcache table
774 */
775 public function purgeCache() {
776 global $wgLocalisationCacheConf;
777 # We can't guarantee that the user will be able to use TRUNCATE,
778 # but we know that DELETE is available to us
779 $this->output( "Purging caches..." );
780 $this->db->delete( 'objectcache', '*', __METHOD__ );
781 if ( $wgLocalisationCacheConf['manualRecache'] ) {
782 $this->rebuildLocalisationCache();
783 }
784 $this->output( "done.\n" );
785 }
786
787 /**
788 * Check the site_stats table is not properly populated.
789 */
790 protected function checkStats() {
791 $this->output( "...site_stats is populated..." );
792 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
793 if ( $row === false ) {
794 $this->output( "data is missing! rebuilding...\n" );
795 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
796 $this->output( "missing ss_total_pages, rebuilding...\n" );
797 } else {
798 $this->output( "done.\n" );
799 return;
800 }
801 SiteStatsInit::doAllAndCommit( $this->db );
802 }
803
804 # Common updater functions
805
806 /**
807 * Sets the number of active users in the site_stats table
808 */
809 protected function doActiveUsersInit() {
810 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
811 if ( $activeUsers == -1 ) {
812 $activeUsers = $this->db->selectField( 'recentchanges',
813 'COUNT( DISTINCT rc_user_text )',
814 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
815 );
816 $this->db->update( 'site_stats',
817 array( 'ss_active_users' => intval( $activeUsers ) ),
818 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
819 );
820 }
821 $this->output( "...ss_active_users user count set...\n" );
822 }
823
824 /**
825 * Populates the log_user_text field in the logging table
826 */
827 protected function doLogUsertextPopulation() {
828 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
829 $this->output(
830 "Populating log_user_text field, printing progress markers. For large\n" .
831 "databases, you may want to hit Ctrl-C and do this manually with\n" .
832 "maintenance/populateLogUsertext.php.\n" );
833
834 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
835 $task->execute();
836 $this->output( "done.\n" );
837 }
838 }
839
840 /**
841 * Migrate log params to new table and index for searching
842 */
843 protected function doLogSearchPopulation() {
844 if ( !$this->updateRowExists( 'populate log_search' ) ) {
845 $this->output(
846 "Populating log_search table, printing progress markers. For large\n" .
847 "databases, you may want to hit Ctrl-C and do this manually with\n" .
848 "maintenance/populateLogSearch.php.\n" );
849
850 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
851 $task->execute();
852 $this->output( "done.\n" );
853 }
854 }
855
856 /**
857 * Updates the timestamps in the transcache table
858 */
859 protected function doUpdateTranscacheField() {
860 if ( $this->updateRowExists( 'convert transcache field' ) ) {
861 $this->output( "...transcache tc_time already converted.\n" );
862 return true;
863 }
864
865 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
866 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
867 }
868
869 /**
870 * Update CategoryLinks collation
871 */
872 protected function doCollationUpdate() {
873 global $wgCategoryCollation;
874 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
875 if ( $this->db->selectField(
876 'categorylinks',
877 'COUNT(*)',
878 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
879 __METHOD__
880 ) == 0 ) {
881 $this->output( "...collations up-to-date.\n" );
882 return;
883 }
884
885 $this->output( "Updating category collations..." );
886 $task = $this->maintenance->runChild( 'UpdateCollation' );
887 $task->execute();
888 $this->output( "...done.\n" );
889 }
890 }
891
892 /**
893 * Migrates user options from the user table blob to user_properties
894 */
895 protected function doMigrateUserOptions() {
896 if( $this->db->tableExists( 'user_properties' ) ) {
897 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
898 $cl->execute();
899 $this->output( "done.\n" );
900 }
901 }
902
903 /**
904 * Rebuilds the localisation cache
905 */
906 protected function rebuildLocalisationCache() {
907 /**
908 * @var $cl RebuildLocalisationCache
909 */
910 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
911 $this->output( "Rebuilding localisation cache...\n" );
912 $cl->setForce();
913 $cl->execute();
914 $this->output( "done.\n" );
915 }
916 }