Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / installer / DatabaseUpdater.php
blob6a8a99ff092c232edafcdf765e9f9bfbf4cfcfda
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
23 use MediaWiki\MediaWikiServices;
25 require_once __DIR__ . '/../../maintenance/Maintenance.php';
27 /**
28 * Class for handling database updates. Roughly based off of updaters.inc, with
29 * a few improvements :)
31 * @ingroup Deployment
32 * @since 1.17
34 abstract class DatabaseUpdater {
35 /**
36 * Array of updates to perform on the database
38 * @var array
40 protected $updates = [];
42 /**
43 * Array of updates that were skipped
45 * @var array
47 protected $updatesSkipped = [];
49 /**
50 * List of extension-provided database updates
51 * @var array
53 protected $extensionUpdates = [];
55 /**
56 * Handle to the database subclass
58 * @var Database
60 protected $db;
62 protected $shared = false;
64 /**
65 * @var string[] Scripts to run after database update
66 * Should be a subclass of LoggedUpdateMaintenance
68 protected $postDatabaseUpdateMaintenance = [
69 DeleteDefaultMessages::class,
70 PopulateRevisionLength::class,
71 PopulateRevisionSha1::class,
72 PopulateImageSha1::class,
73 FixExtLinksProtocolRelative::class,
74 PopulateFilearchiveSha1::class,
75 PopulateBacklinkNamespace::class,
76 FixDefaultJsonContentPages::class,
77 CleanupEmptyCategories::class,
78 AddRFCAndPMIDInterwiki::class,
81 /**
82 * File handle for SQL output.
84 * @var resource
86 protected $fileHandle = null;
88 /**
89 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
91 * @var bool
93 protected $skipSchema = false;
95 /**
96 * Hold the value of $wgContentHandlerUseDB during the upgrade.
98 protected $holdContentHandlerUseDB = true;
101 * Constructor
103 * @param Database $db To perform updates on
104 * @param bool $shared Whether to perform updates on shared tables
105 * @param Maintenance $maintenance Maintenance object which created us
107 protected function __construct( Database &$db, $shared, Maintenance $maintenance = null ) {
108 $this->db = $db;
109 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
110 $this->shared = $shared;
111 if ( $maintenance ) {
112 $this->maintenance = $maintenance;
113 $this->fileHandle = $maintenance->fileHandle;
114 } else {
115 $this->maintenance = new FakeMaintenance;
117 $this->maintenance->setDB( $db );
118 $this->initOldGlobals();
119 $this->loadExtensions();
120 Hooks::run( 'LoadExtensionSchemaUpdates', [ $this ] );
124 * Initialize all of the old globals. One day this should all become
125 * something much nicer
127 private function initOldGlobals() {
128 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
129 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
131 # For extensions only, should be populated via hooks
132 # $wgDBtype should be checked to specifiy the proper file
133 $wgExtNewTables = []; // table, dir
134 $wgExtNewFields = []; // table, column, dir
135 $wgExtPGNewFields = []; // table, column, column attributes; for PostgreSQL
136 $wgExtPGAlteredFields = []; // table, column, new type, conversion method; for PostgreSQL
137 $wgExtNewIndexes = []; // table, index, dir
138 $wgExtModifiedFields = []; // table, index, dir
142 * Loads LocalSettings.php, if needed, and initialises everything needed for
143 * LoadExtensionSchemaUpdates hook.
145 private function loadExtensions() {
146 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
147 return; // already loaded
149 $vars = Installer::getExistingLocalSettings();
151 $registry = ExtensionRegistry::getInstance();
152 $queue = $registry->getQueue();
153 // Don't accidentally load extensions in the future
154 $registry->clearQueue();
156 // This will automatically add "AutoloadClasses" to $wgAutoloadClasses
157 $data = $registry->readFromQueue( $queue );
158 $hooks = [ 'wgHooks' => [ 'LoadExtensionSchemaUpdates' => [] ] ];
159 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
160 $hooks = $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'];
162 if ( $vars && isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
163 $hooks = array_merge_recursive( $hooks, $vars['wgHooks']['LoadExtensionSchemaUpdates'] );
165 global $wgHooks, $wgAutoloadClasses;
166 $wgHooks['LoadExtensionSchemaUpdates'] = $hooks;
167 if ( $vars && isset( $vars['wgAutoloadClasses'] ) ) {
168 $wgAutoloadClasses += $vars['wgAutoloadClasses'];
173 * @param Database $db
174 * @param bool $shared
175 * @param Maintenance $maintenance
177 * @throws MWException
178 * @return DatabaseUpdater
180 public static function newForDB( Database $db, $shared = false, $maintenance = null ) {
181 $type = $db->getType();
182 if ( in_array( $type, Installer::getDBTypes() ) ) {
183 $class = ucfirst( $type ) . 'Updater';
185 return new $class( $db, $shared, $maintenance );
186 } else {
187 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
192 * Get a database connection to run updates
194 * @return Database
196 public function getDB() {
197 return $this->db;
201 * Output some text. If we're running from web, escape the text first.
203 * @param string $str Text to output
205 public function output( $str ) {
206 if ( $this->maintenance->isQuiet() ) {
207 return;
209 global $wgCommandLineMode;
210 if ( !$wgCommandLineMode ) {
211 $str = htmlspecialchars( $str );
213 echo $str;
214 flush();
218 * Add a new update coming from an extension. This should be called by
219 * extensions while executing the LoadExtensionSchemaUpdates hook.
221 * @since 1.17
223 * @param array $update The update to run. Format is [ $callback, $params... ]
224 * $callback is the method to call; either a DatabaseUpdater method name or a callable.
225 * Must be serializable (ie. no anonymous functions allowed). The rest of the parameters
226 * (if any) will be passed to the callback. The first parameter passed to the callback
227 * is always this object.
229 public function addExtensionUpdate( array $update ) {
230 $this->extensionUpdates[] = $update;
234 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
235 * is the most common usage of updaters in an extension)
237 * @since 1.18
239 * @param string $tableName Name of table to create
240 * @param string $sqlPath Full path to the schema file
242 public function addExtensionTable( $tableName, $sqlPath ) {
243 $this->extensionUpdates[] = [ 'addTable', $tableName, $sqlPath, true ];
247 * @since 1.19
249 * @param string $tableName
250 * @param string $indexName
251 * @param string $sqlPath
253 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
254 $this->extensionUpdates[] = [ 'addIndex', $tableName, $indexName, $sqlPath, true ];
259 * @since 1.19
261 * @param string $tableName
262 * @param string $columnName
263 * @param string $sqlPath
265 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
266 $this->extensionUpdates[] = [ 'addField', $tableName, $columnName, $sqlPath, true ];
271 * @since 1.20
273 * @param string $tableName
274 * @param string $columnName
275 * @param string $sqlPath
277 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
278 $this->extensionUpdates[] = [ 'dropField', $tableName, $columnName, $sqlPath, true ];
282 * Drop an index from an extension table
284 * @since 1.21
286 * @param string $tableName The table name
287 * @param string $indexName The index name
288 * @param string $sqlPath The path to the SQL change path
290 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
291 $this->extensionUpdates[] = [ 'dropIndex', $tableName, $indexName, $sqlPath, true ];
296 * @since 1.20
298 * @param string $tableName
299 * @param string $sqlPath
301 public function dropExtensionTable( $tableName, $sqlPath ) {
302 $this->extensionUpdates[] = [ 'dropTable', $tableName, $sqlPath, true ];
306 * Rename an index on an extension table
308 * @since 1.21
310 * @param string $tableName The table name
311 * @param string $oldIndexName The old index name
312 * @param string $newIndexName The new index name
313 * @param string $sqlPath The path to the SQL change path
314 * @param bool $skipBothIndexExistWarning Whether to warn if both the old
315 * and the new indexes exist. [facultative; by default, false]
317 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
318 $sqlPath, $skipBothIndexExistWarning = false
320 $this->extensionUpdates[] = [
321 'renameIndex',
322 $tableName,
323 $oldIndexName,
324 $newIndexName,
325 $skipBothIndexExistWarning,
326 $sqlPath,
327 true
332 * @since 1.21
334 * @param string $tableName The table name
335 * @param string $fieldName The field to be modified
336 * @param string $sqlPath The path to the SQL change path
338 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
339 $this->extensionUpdates[] = [ 'modifyField', $tableName, $fieldName, $sqlPath, true ];
344 * @since 1.20
346 * @param string $tableName
347 * @return bool
349 public function tableExists( $tableName ) {
350 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
354 * Add a maintenance script to be run after the database updates are complete.
356 * Script should subclass LoggedUpdateMaintenance
358 * @since 1.19
360 * @param string $class Name of a Maintenance subclass
362 public function addPostDatabaseUpdateMaintenance( $class ) {
363 $this->postDatabaseUpdateMaintenance[] = $class;
367 * Get the list of extension-defined updates
369 * @return array
371 protected function getExtensionUpdates() {
372 return $this->extensionUpdates;
376 * @since 1.17
378 * @return string[]
380 public function getPostDatabaseUpdateMaintenance() {
381 return $this->postDatabaseUpdateMaintenance;
385 * @since 1.21
387 * Writes the schema updates desired to a file for the DB Admin to run.
388 * @param array $schemaUpdate
390 private function writeSchemaUpdateFile( $schemaUpdate = [] ) {
391 $updates = $this->updatesSkipped;
392 $this->updatesSkipped = [];
394 foreach ( $updates as $funcList ) {
395 $func = $funcList[0];
396 $arg = $funcList[1];
397 $origParams = $funcList[2];
398 call_user_func_array( $func, $arg );
399 flush();
400 $this->updatesSkipped[] = $origParams;
405 * Get appropriate schema variables in the current database connection.
407 * This should be called after any request data has been imported, but before
408 * any write operations to the database. The result should be passed to the DB
409 * setSchemaVars() method.
411 * @return array
412 * @since 1.28
414 public function getSchemaVars() {
415 return []; // DB-type specific
419 * Do all the updates
421 * @param array $what What updates to perform
423 public function doUpdates( $what = [ 'core', 'extensions', 'stats' ] ) {
424 $this->db->setSchemaVars( $this->getSchemaVars() );
426 $what = array_flip( $what );
427 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
428 if ( isset( $what['core'] ) ) {
429 $this->runUpdates( $this->getCoreUpdateList(), false );
431 if ( isset( $what['extensions'] ) ) {
432 $this->runUpdates( $this->getOldGlobalUpdates(), false );
433 $this->runUpdates( $this->getExtensionUpdates(), true );
436 if ( isset( $what['stats'] ) ) {
437 $this->checkStats();
440 if ( $this->fileHandle ) {
441 $this->skipSchema = false;
442 $this->writeSchemaUpdateFile();
447 * Helper function for doUpdates()
449 * @param array $updates Array of updates to run
450 * @param bool $passSelf Whether to pass this object we calling external functions
452 private function runUpdates( array $updates, $passSelf ) {
453 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
455 $updatesDone = [];
456 $updatesSkipped = [];
457 foreach ( $updates as $params ) {
458 $origParams = $params;
459 $func = array_shift( $params );
460 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
461 $func = [ $this, $func ];
462 } elseif ( $passSelf ) {
463 array_unshift( $params, $this );
465 $ret = call_user_func_array( $func, $params );
466 flush();
467 if ( $ret !== false ) {
468 $updatesDone[] = $origParams;
469 $lbFactory->waitForReplication();
470 } else {
471 $updatesSkipped[] = [ $func, $params, $origParams ];
474 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
475 $this->updates = array_merge( $this->updates, $updatesDone );
479 * Helper function: check if the given key is present in the updatelog table.
480 * Obviously, only use this for updates that occur after the updatelog table was
481 * created!
482 * @param string $key Name of the key to check for
483 * @return bool
485 public function updateRowExists( $key ) {
486 $row = $this->db->selectRow(
487 'updatelog',
488 # Bug 65813
489 '1 AS X',
490 [ 'ul_key' => $key ],
491 __METHOD__
494 return (bool)$row;
498 * Helper function: Add a key to the updatelog table
499 * Obviously, only use this for updates that occur after the updatelog table was
500 * created!
501 * @param string $key Name of key to insert
502 * @param string $val [optional] Value to insert along with the key
504 public function insertUpdateRow( $key, $val = null ) {
505 $this->db->clearFlag( DBO_DDLMODE );
506 $values = [ 'ul_key' => $key ];
507 if ( $val && $this->canUseNewUpdatelog() ) {
508 $values['ul_value'] = $val;
510 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
511 $this->db->setFlag( DBO_DDLMODE );
515 * Updatelog was changed in 1.17 to have a ul_value column so we can record
516 * more information about what kind of updates we've done (that's what this
517 * class does). Pre-1.17 wikis won't have this column, and really old wikis
518 * might not even have updatelog at all
520 * @return bool
522 protected function canUseNewUpdatelog() {
523 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
524 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
528 * Returns whether updates should be executed on the database table $name.
529 * Updates will be prevented if the table is a shared table and it is not
530 * specified to run updates on shared tables.
532 * @param string $name Table name
533 * @return bool
535 protected function doTable( $name ) {
536 global $wgSharedDB, $wgSharedTables;
538 // Don't bother to check $wgSharedTables if there isn't a shared database
539 // or the user actually also wants to do updates on the shared database.
540 if ( $wgSharedDB === null || $this->shared ) {
541 return true;
544 if ( in_array( $name, $wgSharedTables ) ) {
545 $this->output( "...skipping update to shared table $name.\n" );
546 return false;
547 } else {
548 return true;
553 * Before 1.17, we used to handle updates via stuff like
554 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
555 * of this in 1.17 but we want to remain back-compatible for a while. So
556 * load up these old global-based things into our update list.
558 * @return array
560 protected function getOldGlobalUpdates() {
561 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
562 $wgExtNewIndexes;
564 $updates = [];
566 foreach ( $wgExtNewTables as $tableRecord ) {
567 $updates[] = [
568 'addTable', $tableRecord[0], $tableRecord[1], true
572 foreach ( $wgExtNewFields as $fieldRecord ) {
573 $updates[] = [
574 'addField', $fieldRecord[0], $fieldRecord[1],
575 $fieldRecord[2], true
579 foreach ( $wgExtNewIndexes as $fieldRecord ) {
580 $updates[] = [
581 'addIndex', $fieldRecord[0], $fieldRecord[1],
582 $fieldRecord[2], true
586 foreach ( $wgExtModifiedFields as $fieldRecord ) {
587 $updates[] = [
588 'modifyField', $fieldRecord[0], $fieldRecord[1],
589 $fieldRecord[2], true
593 return $updates;
597 * Get an array of updates to perform on the database. Should return a
598 * multi-dimensional array. The main key is the MediaWiki version (1.12,
599 * 1.13...) with the values being arrays of updates, identical to how
600 * updaters.inc did it (for now)
602 * @return array
604 abstract protected function getCoreUpdateList();
607 * Append an SQL fragment to the open file handle.
609 * @param string $filename File name to open
611 public function copyFile( $filename ) {
612 $this->db->sourceFile(
613 $filename,
614 null,
615 null,
616 __METHOD__,
617 [ $this, 'appendLine' ]
622 * Append a line to the open filehandle. The line is assumed to
623 * be a complete SQL statement.
625 * This is used as a callback for sourceLine().
627 * @param string $line Text to append to the file
628 * @return bool False to skip actually executing the file
629 * @throws MWException
631 public function appendLine( $line ) {
632 $line = rtrim( $line ) . ";\n";
633 if ( fwrite( $this->fileHandle, $line ) === false ) {
634 throw new MWException( "trouble writing file" );
637 return false;
641 * Applies a SQL patch
643 * @param string $path Path to the patch file
644 * @param bool $isFullPath Whether to treat $path as a relative or not
645 * @param string $msg Description of the patch
646 * @return bool False if patch is skipped.
648 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
649 if ( $msg === null ) {
650 $msg = "Applying $path patch";
652 if ( $this->skipSchema ) {
653 $this->output( "...skipping schema change ($msg).\n" );
655 return false;
658 $this->output( "$msg ..." );
660 if ( !$isFullPath ) {
661 $path = $this->patchPath( $this->db, $path );
663 if ( $this->fileHandle !== null ) {
664 $this->copyFile( $path );
665 } else {
666 $this->db->sourceFile( $path );
668 $this->output( "done.\n" );
670 return true;
674 * Get the full path of a patch file. Originally based on archive()
675 * from updaters.inc. Keep in mind this always returns a patch, as
676 * it fails back to MySQL if no DB-specific patch can be found
678 * @param IDatabase $db
679 * @param string $patch The name of the patch, like patch-something.sql
680 * @return string Full path to patch file
682 public function patchPath( IDatabase $db, $patch ) {
683 global $IP;
685 $dbType = $db->getType();
686 if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
687 return "$IP/maintenance/$dbType/archives/$patch";
688 } else {
689 return "$IP/maintenance/archives/$patch";
694 * Add a new table to the database
696 * @param string $name Name of the new table
697 * @param string $patch Path to the patch file
698 * @param bool $fullpath Whether to treat $patch path as a relative or not
699 * @return bool False if this was skipped because schema changes are skipped
701 protected function addTable( $name, $patch, $fullpath = false ) {
702 if ( !$this->doTable( $name ) ) {
703 return true;
706 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
707 $this->output( "...$name table already exists.\n" );
708 } else {
709 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
712 return true;
716 * Add a new field to an existing table
718 * @param string $table Name of the table to modify
719 * @param string $field Name of the new field
720 * @param string $patch Path to the patch file
721 * @param bool $fullpath Whether to treat $patch path as a relative or not
722 * @return bool False if this was skipped because schema changes are skipped
724 protected function addField( $table, $field, $patch, $fullpath = false ) {
725 if ( !$this->doTable( $table ) ) {
726 return true;
729 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
730 $this->output( "...$table table does not exist, skipping new field patch.\n" );
731 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
732 $this->output( "...have $field field in $table table.\n" );
733 } else {
734 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
737 return true;
741 * Add a new index to an existing table
743 * @param string $table Name of the table to modify
744 * @param string $index Name of the new index
745 * @param string $patch Path to the patch file
746 * @param bool $fullpath Whether to treat $patch path as a relative or not
747 * @return bool False if this was skipped because schema changes are skipped
749 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
750 if ( !$this->doTable( $table ) ) {
751 return true;
754 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
755 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
756 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
757 $this->output( "...index $index already set on $table table.\n" );
758 } else {
759 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
762 return true;
766 * Drop a field from an existing table
768 * @param string $table Name of the table to modify
769 * @param string $field Name of the old field
770 * @param string $patch Path to the patch file
771 * @param bool $fullpath Whether to treat $patch path as a relative or not
772 * @return bool False if this was skipped because schema changes are skipped
774 protected function dropField( $table, $field, $patch, $fullpath = false ) {
775 if ( !$this->doTable( $table ) ) {
776 return true;
779 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
780 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
781 } else {
782 $this->output( "...$table table does not contain $field field.\n" );
785 return true;
789 * Drop an index from an existing table
791 * @param string $table Name of the table to modify
792 * @param string $index Name of the index
793 * @param string $patch Path to the patch file
794 * @param bool $fullpath Whether to treat $patch path as a relative or not
795 * @return bool False if this was skipped because schema changes are skipped
797 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
798 if ( !$this->doTable( $table ) ) {
799 return true;
802 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
803 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
804 } else {
805 $this->output( "...$index key doesn't exist.\n" );
808 return true;
812 * Rename an index from an existing table
814 * @param string $table Name of the table to modify
815 * @param string $oldIndex Old name of the index
816 * @param string $newIndex New name of the index
817 * @param bool $skipBothIndexExistWarning Whether to warn if both the
818 * old and the new indexes exist.
819 * @param string $patch Path to the patch file
820 * @param bool $fullpath Whether to treat $patch path as a relative or not
821 * @return bool False if this was skipped because schema changes are skipped
823 protected function renameIndex( $table, $oldIndex, $newIndex,
824 $skipBothIndexExistWarning, $patch, $fullpath = false
826 if ( !$this->doTable( $table ) ) {
827 return true;
830 // First requirement: the table must exist
831 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
832 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
834 return true;
837 // Second requirement: the new index must be missing
838 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
839 $this->output( "...index $newIndex already set on $table table.\n" );
840 if ( !$skipBothIndexExistWarning &&
841 $this->db->indexExists( $table, $oldIndex, __METHOD__ )
843 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
844 "been renamed into $newIndex (which also exists).\n" .
845 " $oldIndex should be manually removed if not needed anymore.\n" );
848 return true;
851 // Third requirement: the old index must exist
852 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
853 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
855 return true;
858 // Requirements have been satisfied, patch can be applied
859 return $this->applyPatch(
860 $patch,
861 $fullpath,
862 "Renaming index $oldIndex into $newIndex to table $table"
867 * If the specified table exists, drop it, or execute the
868 * patch if one is provided.
870 * Public @since 1.20
872 * @param string $table Table to drop.
873 * @param string|bool $patch String of patch file that will drop the table. Default: false.
874 * @param bool $fullpath Whether $patch is a full path. Default: false.
875 * @return bool False if this was skipped because schema changes are skipped
877 public function dropTable( $table, $patch = false, $fullpath = false ) {
878 if ( !$this->doTable( $table ) ) {
879 return true;
882 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
883 $msg = "Dropping table $table";
885 if ( $patch === false ) {
886 $this->output( "$msg ..." );
887 $this->db->dropTable( $table, __METHOD__ );
888 $this->output( "done.\n" );
889 } else {
890 return $this->applyPatch( $patch, $fullpath, $msg );
892 } else {
893 $this->output( "...$table doesn't exist.\n" );
896 return true;
900 * Modify an existing field
902 * @param string $table Name of the table to which the field belongs
903 * @param string $field Name of the field to modify
904 * @param string $patch Path to the patch file
905 * @param bool $fullpath Whether to treat $patch path as a relative or not
906 * @return bool False if this was skipped because schema changes are skipped
908 public function modifyField( $table, $field, $patch, $fullpath = false ) {
909 if ( !$this->doTable( $table ) ) {
910 return true;
913 $updateKey = "$table-$field-$patch";
914 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
915 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
916 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
917 $this->output( "...$field field does not exist in $table table, " .
918 "skipping modify field patch.\n" );
919 } elseif ( $this->updateRowExists( $updateKey ) ) {
920 $this->output( "...$field in table $table already modified by patch $patch.\n" );
921 } else {
922 $this->insertUpdateRow( $updateKey );
924 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
927 return true;
931 * Set any .htaccess files or equivilent for storage repos
933 * Some zones (e.g. "temp") used to be public and may have been initialized as such
935 public function setFileAccess() {
936 $repo = RepoGroup::singleton()->getLocalRepo();
937 $zonePath = $repo->getZonePath( 'temp' );
938 if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
939 // If the directory was never made, then it will have the right ACLs when it is made
940 $status = $repo->getBackend()->secure( [
941 'dir' => $zonePath,
942 'noAccess' => true,
943 'noListing' => true
944 ] );
945 if ( $status->isOK() ) {
946 $this->output( "Set the local repo temp zone container to be private.\n" );
947 } else {
948 $this->output( "Failed to set the local repo temp zone container to be private.\n" );
954 * Purge the objectcache table
956 public function purgeCache() {
957 global $wgLocalisationCacheConf;
958 # We can't guarantee that the user will be able to use TRUNCATE,
959 # but we know that DELETE is available to us
960 $this->output( "Purging caches..." );
961 $this->db->delete( 'objectcache', '*', __METHOD__ );
962 if ( $wgLocalisationCacheConf['manualRecache'] ) {
963 $this->rebuildLocalisationCache();
965 $blobStore = new MessageBlobStore();
966 $blobStore->clear();
967 $this->db->delete( 'module_deps', '*', __METHOD__ );
968 $this->output( "done.\n" );
972 * Check the site_stats table is not properly populated.
974 protected function checkStats() {
975 $this->output( "...site_stats is populated..." );
976 $row = $this->db->selectRow( 'site_stats', '*', [ 'ss_row_id' => 1 ], __METHOD__ );
977 if ( $row === false ) {
978 $this->output( "data is missing! rebuilding...\n" );
979 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
980 $this->output( "missing ss_total_pages, rebuilding...\n" );
981 } else {
982 $this->output( "done.\n" );
984 return;
986 SiteStatsInit::doAllAndCommit( $this->db );
989 # Common updater functions
992 * Sets the number of active users in the site_stats table
994 protected function doActiveUsersInit() {
995 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
996 if ( $activeUsers == -1 ) {
997 $activeUsers = $this->db->selectField( 'recentchanges',
998 'COUNT( DISTINCT rc_user_text )',
999 [ 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ], __METHOD__
1001 $this->db->update( 'site_stats',
1002 [ 'ss_active_users' => intval( $activeUsers ) ],
1003 [ 'ss_row_id' => 1 ], __METHOD__, [ 'LIMIT' => 1 ]
1006 $this->output( "...ss_active_users user count set...\n" );
1010 * Populates the log_user_text field in the logging table
1012 protected function doLogUsertextPopulation() {
1013 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
1014 $this->output(
1015 "Populating log_user_text field, printing progress markers. For large\n" .
1016 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1017 "maintenance/populateLogUsertext.php.\n"
1020 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
1021 $task->execute();
1022 $this->output( "done.\n" );
1027 * Migrate log params to new table and index for searching
1029 protected function doLogSearchPopulation() {
1030 if ( !$this->updateRowExists( 'populate log_search' ) ) {
1031 $this->output(
1032 "Populating log_search table, printing progress markers. For large\n" .
1033 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1034 "maintenance/populateLogSearch.php.\n" );
1036 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
1037 $task->execute();
1038 $this->output( "done.\n" );
1043 * Updates the timestamps in the transcache table
1044 * @return bool
1046 protected function doUpdateTranscacheField() {
1047 if ( $this->updateRowExists( 'convert transcache field' ) ) {
1048 $this->output( "...transcache tc_time already converted.\n" );
1050 return true;
1053 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
1054 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
1058 * Update CategoryLinks collation
1060 protected function doCollationUpdate() {
1061 global $wgCategoryCollation;
1062 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
1063 if ( $this->db->selectField(
1064 'categorylinks',
1065 'COUNT(*)',
1066 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1067 __METHOD__
1068 ) == 0
1070 $this->output( "...collations up-to-date.\n" );
1072 return;
1075 $this->output( "Updating category collations..." );
1076 $task = $this->maintenance->runChild( 'UpdateCollation' );
1077 $task->execute();
1078 $this->output( "...done.\n" );
1083 * Migrates user options from the user table blob to user_properties
1085 protected function doMigrateUserOptions() {
1086 if ( $this->db->tableExists( 'user_properties' ) ) {
1087 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
1088 $cl->execute();
1089 $this->output( "done.\n" );
1094 * Enable profiling table when it's turned on
1096 protected function doEnableProfiling() {
1097 global $wgProfiler;
1099 if ( !$this->doTable( 'profiling' ) ) {
1100 return;
1103 $profileToDb = false;
1104 if ( isset( $wgProfiler['output'] ) ) {
1105 $out = $wgProfiler['output'];
1106 if ( $out === 'db' ) {
1107 $profileToDb = true;
1108 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
1109 $profileToDb = true;
1113 if ( $profileToDb && !$this->db->tableExists( 'profiling', __METHOD__ ) ) {
1114 $this->applyPatch( 'patch-profiling.sql', false, 'Add profiling table' );
1119 * Rebuilds the localisation cache
1121 protected function rebuildLocalisationCache() {
1123 * @var $cl RebuildLocalisationCache
1125 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
1126 $this->output( "Rebuilding localisation cache...\n" );
1127 $cl->setForce();
1128 $cl->execute();
1129 $this->output( "done.\n" );
1133 * Turns off content handler fields during parts of the upgrade
1134 * where they aren't available.
1136 protected function disableContentHandlerUseDB() {
1137 global $wgContentHandlerUseDB;
1139 if ( $wgContentHandlerUseDB ) {
1140 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1141 $this->holdContentHandlerUseDB = $wgContentHandlerUseDB;
1142 $wgContentHandlerUseDB = false;
1147 * Turns content handler fields back on.
1149 protected function enableContentHandlerUseDB() {
1150 global $wgContentHandlerUseDB;
1152 if ( $this->holdContentHandlerUseDB ) {
1153 $this->output( "Content Handler DB fields should be usable now.\n" );
1154 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB;