Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / installer / DatabaseUpdater.php
blobf0c5a213bc9ea45776c876fbd37165e0ee9d611a
1 <?php
2 /**
3 * DBMS-specific updater helper.
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.
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.
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
20 * @file
21 * @ingroup Deployment
24 require_once __DIR__ . '/../../maintenance/Maintenance.php';
26 /**
27 * Class for handling database updates. Roughly based off of updaters.inc, with
28 * a few improvements :)
30 * @ingroup Deployment
31 * @since 1.17
33 abstract class DatabaseUpdater {
35 /**
36 * Array of updates to perform on the database
38 * @var array
40 protected $updates = array();
42 /**
43 * Array of updates that were skipped
45 * @var array
47 protected $updatesSkipped = array();
49 /**
50 * List of extension-provided database updates
51 * @var array
53 protected $extensionUpdates = array();
55 /**
56 * Handle to the database subclass
58 * @var DatabaseBase
60 protected $db;
62 protected $shared = false;
64 /**
65 * Scripts to run after database update
66 * Should be a subclass of LoggedUpdateMaintenance
68 protected $postDatabaseUpdateMaintenance = array(
69 'DeleteDefaultMessages',
70 'PopulateRevisionLength',
71 'PopulateRevisionSha1',
72 'PopulateImageSha1',
73 'FixExtLinksProtocolRelative',
74 'PopulateFilearchiveSha1',
77 /**
78 * File handle for SQL output.
80 * @var Filehandle
82 protected $fileHandle = null;
84 /**
85 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
87 * @var bool
89 protected $skipSchema = false;
91 /**
92 * Constructor
94 * @param $db DatabaseBase object to perform updates on
95 * @param bool $shared Whether to perform updates on shared tables
96 * @param $maintenance Maintenance Maintenance object which created us
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;
108 $this->maintenance->setDB( $db );
109 $this->initOldGlobals();
110 $this->loadExtensions();
111 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
115 * Initialize all of the old globals. One day this should all become
116 * something much nicer
118 private function initOldGlobals() {
119 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
120 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
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
133 * Loads LocalSettings.php, if needed, and initialises everything needed for LoadExtensionSchemaUpdates hook
135 private function loadExtensions() {
136 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
137 return; // already loaded
139 $vars = Installer::getExistingLocalSettings();
140 if ( !$vars ) {
141 return; // no LocalSettings found
143 if ( !isset( $vars['wgHooks'] ) || !isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
144 return;
146 global $wgHooks, $wgAutoloadClasses;
147 $wgHooks['LoadExtensionSchemaUpdates'] = $vars['wgHooks']['LoadExtensionSchemaUpdates'];
148 $wgAutoloadClasses = $wgAutoloadClasses + $vars['wgAutoloadClasses'];
152 * @throws MWException
153 * @param DatabaseBase $db
154 * @param bool $shared
155 * @param null $maintenance
156 * @return DatabaseUpdater
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' );
169 * Get a database connection to run updates
171 * @return DatabaseBase
173 public function getDB() {
174 return $this->db;
178 * Output some text. If we're running from web, escape the text first.
180 * @param string $str Text to output
182 public function output( $str ) {
183 if ( $this->maintenance->isQuiet() ) {
184 return;
186 global $wgCommandLineMode;
187 if ( !$wgCommandLineMode ) {
188 $str = htmlspecialchars( $str );
190 echo $str;
191 flush();
195 * Add a new update coming from an extension. This should be called by
196 * extensions while executing the LoadExtensionSchemaUpdates hook.
198 * @since 1.17
200 * @param array $update 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.
207 public function addExtensionUpdate( array $update ) {
208 $this->extensionUpdates[] = $update;
212 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
213 * is the most common usage of updaters in an extension)
215 * @since 1.18
217 * @param string $tableName Name of table to create
218 * @param string $sqlPath Full path to the schema file
220 public function addExtensionTable( $tableName, $sqlPath ) {
221 $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true );
225 * @since 1.19
227 * @param $tableName string
228 * @param $indexName string
229 * @param $sqlPath string
231 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
232 $this->extensionUpdates[] = array( 'addIndex', $tableName, $indexName, $sqlPath, true );
237 * @since 1.19
239 * @param $tableName string
240 * @param $columnName string
241 * @param $sqlPath string
243 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
244 $this->extensionUpdates[] = array( 'addField', $tableName, $columnName, $sqlPath, true );
249 * @since 1.20
251 * @param $tableName string
252 * @param $columnName string
253 * @param $sqlPath string
255 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
256 $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true );
260 * Drop an index from an extension table
262 * @since 1.21
264 * @param string $tableName The table name
265 * @param string $indexName The index name
266 * @param string $sqlPath The path to the SQL change path
268 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
269 $this->extensionUpdates[] = array( 'dropIndex', $tableName, $indexName, $sqlPath, true );
274 * @since 1.20
276 * @param $tableName string
277 * @param $sqlPath string
279 public function dropExtensionTable( $tableName, $sqlPath ) {
280 $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true );
284 * Rename an index on an extension table
286 * @since 1.21
288 * @param string $tableName The table name
289 * @param string $oldIndexName The old index name
290 * @param string $newIndexName The new index name
291 * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old and the new indexes exist. [facultative; by default, false]
292 * @param string $sqlPath The path to the SQL change path
294 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName, $sqlPath, $skipBothIndexExistWarning = false ) {
295 $this->extensionUpdates[] = array( 'renameIndex', $tableName, $oldIndexName, $newIndexName, $skipBothIndexExistWarning, $sqlPath, true );
299 * @since 1.21
301 * @param string $tableName The table name
302 * @param string $fieldName The field to be modified
303 * @param string $sqlPath The path to the SQL change path
305 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
306 $this->extensionUpdates[] = array( 'modifyField', $tableName, $fieldName, $sqlPath, true );
311 * @since 1.20
313 * @param $tableName string
314 * @return bool
316 public function tableExists( $tableName ) {
317 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
321 * Add a maintenance script to be run after the database updates are complete.
323 * Script should subclass LoggedUpdateMaintenance
325 * @since 1.19
327 * @param string $class Name of a Maintenance subclass
329 public function addPostDatabaseUpdateMaintenance( $class ) {
330 $this->postDatabaseUpdateMaintenance[] = $class;
334 * Get the list of extension-defined updates
336 * @return Array
338 protected function getExtensionUpdates() {
339 return $this->extensionUpdates;
343 * @since 1.17
345 * @return array
347 public function getPostDatabaseUpdateMaintenance() {
348 return $this->postDatabaseUpdateMaintenance;
352 * @since 1.21
354 * Writes the schema updates desired to a file for the DB Admin to run.
356 private function writeSchemaUpdateFile( $schemaUpdate = array() ) {
357 $updates = $this->updatesSkipped;
358 $this->updatesSkipped = array();
360 foreach ( $updates as $funcList ) {
361 $func = $funcList[0];
362 $arg = $funcList[1];
363 $origParams = $funcList[2];
364 call_user_func_array( $func, $arg );
365 flush();
366 $this->updatesSkipped[] = $origParams;
371 * Do all the updates
373 * @param array $what what updates to perform
375 public function doUpdates( $what = array( 'core', 'extensions', 'stats' ) ) {
376 global $wgVersion, $wgLocalisationCacheConf;
378 $this->db->begin( __METHOD__ );
379 $what = array_flip( $what );
380 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
381 if ( isset( $what['core'] ) ) {
382 $this->runUpdates( $this->getCoreUpdateList(), false );
384 if ( isset( $what['extensions'] ) ) {
385 $this->runUpdates( $this->getOldGlobalUpdates(), false );
386 $this->runUpdates( $this->getExtensionUpdates(), true );
389 if ( isset( $what['stats'] ) ) {
390 $this->checkStats();
393 if ( isset( $what['purge'] ) ) {
394 $this->purgeCache();
396 if ( $wgLocalisationCacheConf['manualRecache'] ) {
397 $this->rebuildLocalisationCache();
401 $this->setAppliedUpdates( $wgVersion, $this->updates );
403 if ( $this->fileHandle ) {
404 $this->skipSchema = false;
405 $this->writeSchemaUpdateFile();
406 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
409 $this->db->commit( __METHOD__ );
413 * Helper function for doUpdates()
415 * @param array $updates of updates to run
416 * @param $passSelf Boolean: whether to pass this object we calling external
417 * functions
419 private function runUpdates( array $updates, $passSelf ) {
420 $updatesDone = array();
421 $updatesSkipped = array();
422 foreach ( $updates as $params ) {
423 $origParams = $params;
424 $func = array_shift( $params );
425 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
426 $func = array( $this, $func );
427 } elseif ( $passSelf ) {
428 array_unshift( $params, $this );
430 $ret = call_user_func_array( $func, $params );
431 flush();
432 if ( $ret !== false ) {
433 $updatesDone[] = $origParams;
434 } else {
435 $updatesSkipped[] = array( $func, $params, $origParams );
438 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
439 $this->updates = array_merge( $this->updates, $updatesDone );
443 * @param $version
444 * @param $updates array
446 protected function setAppliedUpdates( $version, $updates = array() ) {
447 $this->db->clearFlag( DBO_DDLMODE );
448 if ( !$this->canUseNewUpdatelog() ) {
449 return;
451 $key = "updatelist-$version-" . time();
452 $this->db->insert( 'updatelog',
453 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
454 __METHOD__ );
455 $this->db->setFlag( DBO_DDLMODE );
459 * Helper function: check if the given key is present in the updatelog table.
460 * Obviously, only use this for updates that occur after the updatelog table was
461 * created!
462 * @param string $key Name of the key to check for
464 * @return bool
466 public function updateRowExists( $key ) {
467 $row = $this->db->selectRow(
468 'updatelog',
469 '1',
470 array( 'ul_key' => $key ),
471 __METHOD__
473 return (bool)$row;
477 * Helper function: Add a key to the updatelog table
478 * Obviously, only use this for updates that occur after the updatelog table was
479 * created!
480 * @param string $key Name of key to insert
481 * @param string $val [optional] value to insert along with the key
483 public function insertUpdateRow( $key, $val = null ) {
484 $this->db->clearFlag( DBO_DDLMODE );
485 $values = array( 'ul_key' => $key );
486 if ( $val && $this->canUseNewUpdatelog() ) {
487 $values['ul_value'] = $val;
489 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
490 $this->db->setFlag( DBO_DDLMODE );
494 * Updatelog was changed in 1.17 to have a ul_value column so we can record
495 * more information about what kind of updates we've done (that's what this
496 * class does). Pre-1.17 wikis won't have this column, and really old wikis
497 * might not even have updatelog at all
499 * @return boolean
501 protected function canUseNewUpdatelog() {
502 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
503 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
507 * Returns whether updates should be executed on the database table $name.
508 * Updates will be prevented if the table is a shared table and it is not
509 * specified to run updates on shared tables.
511 * @param string $name table name
512 * @return bool
514 protected function doTable( $name ) {
515 global $wgSharedDB, $wgSharedTables;
517 // Don't bother to check $wgSharedTables if there isn't a shared database
518 // or the user actually also wants to do updates on the shared database.
519 if ( $wgSharedDB === null || $this->shared ) {
520 return true;
523 return !in_array( $name, $wgSharedTables );
527 * Before 1.17, we used to handle updates via stuff like
528 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
529 * of this in 1.17 but we want to remain back-compatible for a while. So
530 * load up these old global-based things into our update list.
532 * @return array
534 protected function getOldGlobalUpdates() {
535 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
536 $wgExtNewIndexes;
538 $updates = array();
540 foreach ( $wgExtNewTables as $tableRecord ) {
541 $updates[] = array(
542 'addTable', $tableRecord[0], $tableRecord[1], true
546 foreach ( $wgExtNewFields as $fieldRecord ) {
547 $updates[] = array(
548 'addField', $fieldRecord[0], $fieldRecord[1],
549 $fieldRecord[2], true
553 foreach ( $wgExtNewIndexes as $fieldRecord ) {
554 $updates[] = array(
555 'addIndex', $fieldRecord[0], $fieldRecord[1],
556 $fieldRecord[2], true
560 foreach ( $wgExtModifiedFields as $fieldRecord ) {
561 $updates[] = array(
562 'modifyField', $fieldRecord[0], $fieldRecord[1],
563 $fieldRecord[2], true
567 return $updates;
571 * Get an array of updates to perform on the database. Should return a
572 * multi-dimensional array. The main key is the MediaWiki version (1.12,
573 * 1.13...) with the values being arrays of updates, identical to how
574 * updaters.inc did it (for now)
576 * @return Array
578 abstract protected function getCoreUpdateList();
581 * Append an SQL fragment to the open file handle.
583 * @param string $filename File name to open
585 public function copyFile( $filename ) {
586 $this->db->sourceFile( $filename, false, false, false,
587 array( $this, 'appendLine' )
592 * Append a line to the open filehandle. The line is assumed to
593 * be a complete SQL statement.
595 * This is used as a callback for for sourceLine().
597 * @param string $line text to append to the file
598 * @return Boolean false to skip actually executing the file
599 * @throws MWException
601 public function appendLine( $line ) {
602 $line = rtrim( $line ) . ";\n";
603 if ( fwrite( $this->fileHandle, $line ) === false ) {
604 throw new MWException( "trouble writing file" );
606 return false;
610 * Applies a SQL patch
612 * @param string $path Path to the patch file
613 * @param $isFullPath Boolean Whether to treat $path as a relative or not
614 * @param string $msg Description of the patch
615 * @return boolean false if patch is skipped.
617 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
618 if ( $msg === null ) {
619 $msg = "Applying $path patch";
621 if ( $this->skipSchema ) {
622 $this->output( "...skipping schema change ($msg).\n" );
623 return false;
626 $this->output( "$msg ..." );
628 if ( !$isFullPath ) {
629 $path = $this->db->patchPath( $path );
631 if ( $this->fileHandle !== null ) {
632 $this->copyFile( $path );
633 } else {
634 $this->db->sourceFile( $path );
636 $this->output( "done.\n" );
637 return true;
641 * Add a new table to the database
643 * @param string $name Name of the new table
644 * @param string $patch Path to the patch file
645 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
646 * @return Boolean false if this was skipped because schema changes are skipped
648 protected function addTable( $name, $patch, $fullpath = false ) {
649 if ( !$this->doTable( $name ) ) {
650 return true;
653 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
654 $this->output( "...$name table already exists.\n" );
655 } else {
656 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
658 return true;
662 * Add a new field to an existing table
664 * @param string $table Name of the table to modify
665 * @param string $field Name of the new field
666 * @param string $patch Path to the patch file
667 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
668 * @return Boolean false if this was skipped because schema changes are skipped
670 protected function addField( $table, $field, $patch, $fullpath = false ) {
671 if ( !$this->doTable( $table ) ) {
672 return true;
675 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
676 $this->output( "...$table table does not exist, skipping new field patch.\n" );
677 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
678 $this->output( "...have $field field in $table table.\n" );
679 } else {
680 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
682 return true;
686 * Add a new index to an existing table
688 * @param string $table Name of the table to modify
689 * @param string $index Name of the new index
690 * @param string $patch Path to the patch file
691 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
692 * @return Boolean false if this was skipped because schema changes are skipped
694 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
695 if ( !$this->doTable( $table ) ) {
696 return true;
699 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
700 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
701 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
702 $this->output( "...index $index already set on $table table.\n" );
703 } else {
704 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
706 return true;
710 * Drop a field from an existing table
712 * @param string $table Name of the table to modify
713 * @param string $field Name of the old field
714 * @param string $patch Path to the patch file
715 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
716 * @return Boolean false if this was skipped because schema changes are skipped
718 protected function dropField( $table, $field, $patch, $fullpath = false ) {
719 if ( !$this->doTable( $table ) ) {
720 return true;
723 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
724 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
725 } else {
726 $this->output( "...$table table does not contain $field field.\n" );
728 return true;
732 * Drop an index from an existing table
734 * @param string $table Name of the table to modify
735 * @param string $index Name of the index
736 * @param string $patch Path to the patch file
737 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
738 * @return Boolean false if this was skipped because schema changes are skipped
740 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
741 if ( !$this->doTable( $table ) ) {
742 return true;
745 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
746 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
747 } else {
748 $this->output( "...$index key doesn't exist.\n" );
750 return true;
754 * Rename an index from an existing table
756 * @param string $table Name of the table to modify
757 * @param string $oldIndex Old name of the index
758 * @param string $newIndex New name of the index
759 * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old and the new indexes exist.
760 * @param string $patch Path to the patch file
761 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
762 * @return Boolean false if this was skipped because schema changes are skipped
764 protected function renameIndex( $table, $oldIndex, $newIndex, $skipBothIndexExistWarning, $patch, $fullpath = false ) {
765 if ( !$this->doTable( $table ) ) {
766 return true;
769 // First requirement: the table must exist
770 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
771 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
772 return true;
775 // Second requirement: the new index must be missing
776 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
777 $this->output( "...index $newIndex already set on $table table.\n" );
778 if ( !$skipBothIndexExistWarning && $this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
779 $this->output( "...WARNING: $oldIndex still exists, despite it has been renamed into $newIndex (which also exists).\n" .
780 " $oldIndex should be manually removed if not needed anymore.\n" );
782 return true;
785 // Third requirement: the old index must exist
786 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
787 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
788 return true;
791 // Requirements have been satisfied, patch can be applied
792 return $this->applyPatch( $patch, $fullpath, "Renaming index $oldIndex into $newIndex to table $table" );
796 * If the specified table exists, drop it, or execute the
797 * patch if one is provided.
799 * Public @since 1.20
801 * @param $table string
802 * @param $patch string|false
803 * @param $fullpath bool
804 * @return Boolean false if this was skipped because schema changes are skipped
806 public function dropTable( $table, $patch = false, $fullpath = false ) {
807 if ( !$this->doTable( $table ) ) {
808 return true;
811 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
812 $msg = "Dropping table $table";
814 if ( $patch === false ) {
815 $this->output( "$msg ..." );
816 $this->db->dropTable( $table, __METHOD__ );
817 $this->output( "done.\n" );
819 else {
820 return $this->applyPatch( $patch, $fullpath, $msg );
822 } else {
823 $this->output( "...$table doesn't exist.\n" );
825 return true;
829 * Modify an existing field
831 * @param string $table name of the table to which the field belongs
832 * @param string $field name of the field to modify
833 * @param string $patch path to the patch file
834 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
835 * @return Boolean false if this was skipped because schema changes are skipped
837 public function modifyField( $table, $field, $patch, $fullpath = false ) {
838 if ( !$this->doTable( $table ) ) {
839 return true;
842 $updateKey = "$table-$field-$patch";
843 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
844 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
845 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
846 $this->output( "...$field field does not exist in $table table, skipping modify field patch.\n" );
847 } elseif ( $this->updateRowExists( $updateKey ) ) {
848 $this->output( "...$field in table $table already modified by patch $patch.\n" );
849 } else {
850 $this->insertUpdateRow( $updateKey );
851 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
853 return true;
857 * Purge the objectcache table
859 public function purgeCache() {
860 global $wgLocalisationCacheConf;
861 # We can't guarantee that the user will be able to use TRUNCATE,
862 # but we know that DELETE is available to us
863 $this->output( "Purging caches..." );
864 $this->db->delete( 'objectcache', '*', __METHOD__ );
865 if ( $wgLocalisationCacheConf['manualRecache'] ) {
866 $this->rebuildLocalisationCache();
868 MessageBlobStore::clear();
869 $this->output( "done.\n" );
873 * Check the site_stats table is not properly populated.
875 protected function checkStats() {
876 $this->output( "...site_stats is populated..." );
877 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
878 if ( $row === false ) {
879 $this->output( "data is missing! rebuilding...\n" );
880 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
881 $this->output( "missing ss_total_pages, rebuilding...\n" );
882 } else {
883 $this->output( "done.\n" );
884 return;
886 SiteStatsInit::doAllAndCommit( $this->db );
889 # Common updater functions
892 * Sets the number of active users in the site_stats table
894 protected function doActiveUsersInit() {
895 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
896 if ( $activeUsers == -1 ) {
897 $activeUsers = $this->db->selectField( 'recentchanges',
898 'COUNT( DISTINCT rc_user_text )',
899 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
901 $this->db->update( 'site_stats',
902 array( 'ss_active_users' => intval( $activeUsers ) ),
903 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
906 $this->output( "...ss_active_users user count set...\n" );
910 * Populates the log_user_text field in the logging table
912 protected function doLogUsertextPopulation() {
913 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
914 $this->output(
915 "Populating log_user_text field, printing progress markers. For large\n" .
916 "databases, you may want to hit Ctrl-C and do this manually with\n" .
917 "maintenance/populateLogUsertext.php.\n" );
919 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
920 $task->execute();
921 $this->output( "done.\n" );
926 * Migrate log params to new table and index for searching
928 protected function doLogSearchPopulation() {
929 if ( !$this->updateRowExists( 'populate log_search' ) ) {
930 $this->output(
931 "Populating log_search table, printing progress markers. For large\n" .
932 "databases, you may want to hit Ctrl-C and do this manually with\n" .
933 "maintenance/populateLogSearch.php.\n" );
935 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
936 $task->execute();
937 $this->output( "done.\n" );
942 * Updates the timestamps in the transcache table
944 protected function doUpdateTranscacheField() {
945 if ( $this->updateRowExists( 'convert transcache field' ) ) {
946 $this->output( "...transcache tc_time already converted.\n" );
947 return true;
950 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
951 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
955 * Update CategoryLinks collation
957 protected function doCollationUpdate() {
958 global $wgCategoryCollation;
959 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
960 if ( $this->db->selectField(
961 'categorylinks',
962 'COUNT(*)',
963 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
964 __METHOD__
965 ) == 0 ) {
966 $this->output( "...collations up-to-date.\n" );
967 return;
970 $this->output( "Updating category collations..." );
971 $task = $this->maintenance->runChild( 'UpdateCollation' );
972 $task->execute();
973 $this->output( "...done.\n" );
978 * Migrates user options from the user table blob to user_properties
980 protected function doMigrateUserOptions() {
981 if ( $this->db->tableExists( 'user_properties' ) ) {
982 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
983 $cl->execute();
984 $this->output( "done.\n" );
989 * Rebuilds the localisation cache
991 protected function rebuildLocalisationCache() {
993 * @var $cl RebuildLocalisationCache
995 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
996 $this->output( "Rebuilding localisation cache...\n" );
997 $cl->setForce();
998 $cl->execute();
999 $this->output( "done.\n" );