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
23 use Wikimedia\Rdbms\Database
;
24 use Wikimedia\Rdbms\IDatabase
;
25 use MediaWiki\MediaWikiServices
;
27 require_once __DIR__
. '/../../maintenance/Maintenance.php';
30 * Class for handling database updates. Roughly based off of updaters.inc, with
31 * a few improvements :)
36 abstract class DatabaseUpdater
{
38 * Array of updates to perform on the database
42 protected $updates = [];
45 * Array of updates that were skipped
49 protected $updatesSkipped = [];
52 * List of extension-provided database updates
55 protected $extensionUpdates = [];
58 * Handle to the database subclass
67 protected $maintenance;
69 protected $shared = false;
72 * @var string[] Scripts to run after database update
73 * Should be a subclass of LoggedUpdateMaintenance
75 protected $postDatabaseUpdateMaintenance = [
76 DeleteDefaultMessages
::class,
77 PopulateRevisionLength
::class,
78 PopulateRevisionSha1
::class,
79 PopulateImageSha1
::class,
80 FixExtLinksProtocolRelative
::class,
81 PopulateFilearchiveSha1
::class,
82 PopulateBacklinkNamespace
::class,
83 FixDefaultJsonContentPages
::class,
84 CleanupEmptyCategories
::class,
85 AddRFCAndPMIDInterwiki
::class,
86 PopulatePPSortKey
::class,
87 PopulateIpChanges
::class,
91 * File handle for SQL output.
95 protected $fileHandle = null;
98 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
102 protected $skipSchema = false;
105 * Hold the value of $wgContentHandlerUseDB during the upgrade.
107 protected $holdContentHandlerUseDB = true;
110 * @param Database &$db To perform updates on
111 * @param bool $shared Whether to perform updates on shared tables
112 * @param Maintenance $maintenance Maintenance object which created us
114 protected function __construct( Database
&$db, $shared, Maintenance
$maintenance = null ) {
116 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
117 $this->shared
= $shared;
118 if ( $maintenance ) {
119 $this->maintenance
= $maintenance;
120 $this->fileHandle
= $maintenance->fileHandle
;
122 $this->maintenance
= new FakeMaintenance
;
124 $this->maintenance
->setDB( $db );
125 $this->initOldGlobals();
126 $this->loadExtensions();
127 Hooks
::run( 'LoadExtensionSchemaUpdates', [ $this ] );
131 * Initialize all of the old globals. One day this should all become
132 * something much nicer
134 private function initOldGlobals() {
135 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
136 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
138 # For extensions only, should be populated via hooks
139 # $wgDBtype should be checked to specifiy the proper file
140 $wgExtNewTables = []; // table, dir
141 $wgExtNewFields = []; // table, column, dir
142 $wgExtPGNewFields = []; // table, column, column attributes; for PostgreSQL
143 $wgExtPGAlteredFields = []; // table, column, new type, conversion method; for PostgreSQL
144 $wgExtNewIndexes = []; // table, index, dir
145 $wgExtModifiedFields = []; // table, index, dir
149 * Loads LocalSettings.php, if needed, and initialises everything needed for
150 * LoadExtensionSchemaUpdates hook.
152 private function loadExtensions() {
153 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
154 return; // already loaded
156 $vars = Installer
::getExistingLocalSettings();
158 $registry = ExtensionRegistry
::getInstance();
159 $queue = $registry->getQueue();
160 // Don't accidentally load extensions in the future
161 $registry->clearQueue();
163 // This will automatically add "AutoloadClasses" to $wgAutoloadClasses
164 $data = $registry->readFromQueue( $queue );
165 $hooks = [ 'wgHooks' => [ 'LoadExtensionSchemaUpdates' => [] ] ];
166 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
167 $hooks = $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'];
169 if ( $vars && isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
170 $hooks = array_merge_recursive( $hooks, $vars['wgHooks']['LoadExtensionSchemaUpdates'] );
172 global $wgHooks, $wgAutoloadClasses;
173 $wgHooks['LoadExtensionSchemaUpdates'] = $hooks;
174 if ( $vars && isset( $vars['wgAutoloadClasses'] ) ) {
175 $wgAutoloadClasses +
= $vars['wgAutoloadClasses'];
180 * @param Database $db
181 * @param bool $shared
182 * @param Maintenance|null $maintenance
184 * @throws MWException
185 * @return DatabaseUpdater
187 public static function newForDB( Database
$db, $shared = false, Maintenance
$maintenance = null ) {
188 $type = $db->getType();
189 if ( in_array( $type, Installer
::getDBTypes() ) ) {
190 $class = ucfirst( $type ) . 'Updater';
192 return new $class( $db, $shared, $maintenance );
194 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
199 * Get a database connection to run updates
203 public function getDB() {
208 * Output some text. If we're running from web, escape the text first.
210 * @param string $str Text to output
212 public function output( $str ) {
213 if ( $this->maintenance
->isQuiet() ) {
216 global $wgCommandLineMode;
217 if ( !$wgCommandLineMode ) {
218 $str = htmlspecialchars( $str );
225 * Add a new update coming from an extension. This should be called by
226 * extensions while executing the LoadExtensionSchemaUpdates hook.
230 * @param array $update The update to run. Format is [ $callback, $params... ]
231 * $callback is the method to call; either a DatabaseUpdater method name or a callable.
232 * Must be serializable (ie. no anonymous functions allowed). The rest of the parameters
233 * (if any) will be passed to the callback. The first parameter passed to the callback
234 * is always this object.
236 public function addExtensionUpdate( array $update ) {
237 $this->extensionUpdates
[] = $update;
241 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
242 * is the most common usage of updaters in an extension)
246 * @param string $tableName Name of table to create
247 * @param string $sqlPath Full path to the schema file
249 public function addExtensionTable( $tableName, $sqlPath ) {
250 $this->extensionUpdates
[] = [ 'addTable', $tableName, $sqlPath, true ];
256 * @param string $tableName
257 * @param string $indexName
258 * @param string $sqlPath
260 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
261 $this->extensionUpdates
[] = [ 'addIndex', $tableName, $indexName, $sqlPath, true ];
268 * @param string $tableName
269 * @param string $columnName
270 * @param string $sqlPath
272 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
273 $this->extensionUpdates
[] = [ 'addField', $tableName, $columnName, $sqlPath, true ];
280 * @param string $tableName
281 * @param string $columnName
282 * @param string $sqlPath
284 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
285 $this->extensionUpdates
[] = [ 'dropField', $tableName, $columnName, $sqlPath, true ];
289 * Drop an index from an extension table
293 * @param string $tableName The table name
294 * @param string $indexName The index name
295 * @param string $sqlPath The path to the SQL change path
297 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
298 $this->extensionUpdates
[] = [ 'dropIndex', $tableName, $indexName, $sqlPath, true ];
305 * @param string $tableName
306 * @param string $sqlPath
308 public function dropExtensionTable( $tableName, $sqlPath ) {
309 $this->extensionUpdates
[] = [ 'dropTable', $tableName, $sqlPath, true ];
313 * Rename an index on an extension table
317 * @param string $tableName The table name
318 * @param string $oldIndexName The old index name
319 * @param string $newIndexName The new index name
320 * @param string $sqlPath The path to the SQL change path
321 * @param bool $skipBothIndexExistWarning Whether to warn if both the old
322 * and the new indexes exist. [facultative; by default, false]
324 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
325 $sqlPath, $skipBothIndexExistWarning = false
327 $this->extensionUpdates
[] = [
332 $skipBothIndexExistWarning,
341 * @param string $tableName The table name
342 * @param string $fieldName The field to be modified
343 * @param string $sqlPath The path to the SQL patch
345 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
346 $this->extensionUpdates
[] = [ 'modifyField', $tableName, $fieldName, $sqlPath, true ];
352 * @param string $tableName The table name
353 * @param string $sqlPath The path to the SQL patch
355 public function modifyExtensionTable( $tableName, $sqlPath ) {
356 $this->extensionUpdates
[] = [ 'modifyTable', $tableName, $sqlPath, true ];
363 * @param string $tableName
366 public function tableExists( $tableName ) {
367 return ( $this->db
->tableExists( $tableName, __METHOD__
) );
371 * Add a maintenance script to be run after the database updates are complete.
373 * Script should subclass LoggedUpdateMaintenance
377 * @param string $class Name of a Maintenance subclass
379 public function addPostDatabaseUpdateMaintenance( $class ) {
380 $this->postDatabaseUpdateMaintenance
[] = $class;
384 * Get the list of extension-defined updates
388 protected function getExtensionUpdates() {
389 return $this->extensionUpdates
;
397 public function getPostDatabaseUpdateMaintenance() {
398 return $this->postDatabaseUpdateMaintenance
;
404 * Writes the schema updates desired to a file for the DB Admin to run.
405 * @param array $schemaUpdate
407 private function writeSchemaUpdateFile( array $schemaUpdate = [] ) {
408 $updates = $this->updatesSkipped
;
409 $this->updatesSkipped
= [];
411 foreach ( $updates as $funcList ) {
412 $func = $funcList[0];
414 $origParams = $funcList[2];
415 call_user_func_array( $func, $arg );
417 $this->updatesSkipped
[] = $origParams;
422 * Get appropriate schema variables in the current database connection.
424 * This should be called after any request data has been imported, but before
425 * any write operations to the database. The result should be passed to the DB
426 * setSchemaVars() method.
431 public function getSchemaVars() {
432 return []; // DB-type specific
438 * @param array $what What updates to perform
440 public function doUpdates( array $what = [ 'core', 'extensions', 'stats' ] ) {
441 $this->db
->setSchemaVars( $this->getSchemaVars() );
443 $what = array_flip( $what );
444 $this->skipSchema
= isset( $what['noschema'] ) ||
$this->fileHandle
!== null;
445 if ( isset( $what['core'] ) ) {
446 $this->runUpdates( $this->getCoreUpdateList(), false );
448 if ( isset( $what['extensions'] ) ) {
449 $this->runUpdates( $this->getOldGlobalUpdates(), false );
450 $this->runUpdates( $this->getExtensionUpdates(), true );
453 if ( isset( $what['stats'] ) ) {
457 if ( $this->fileHandle
) {
458 $this->skipSchema
= false;
459 $this->writeSchemaUpdateFile();
464 * Helper function for doUpdates()
466 * @param array $updates Array of updates to run
467 * @param bool $passSelf Whether to pass this object we calling external functions
469 private function runUpdates( array $updates, $passSelf ) {
470 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
473 $updatesSkipped = [];
474 foreach ( $updates as $params ) {
475 $origParams = $params;
476 $func = array_shift( $params );
477 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
478 $func = [ $this, $func ];
479 } elseif ( $passSelf ) {
480 array_unshift( $params, $this );
482 $ret = call_user_func_array( $func, $params );
484 if ( $ret !== false ) {
485 $updatesDone[] = $origParams;
486 $lbFactory->waitForReplication();
488 $updatesSkipped[] = [ $func, $params, $origParams ];
491 $this->updatesSkipped
= array_merge( $this->updatesSkipped
, $updatesSkipped );
492 $this->updates
= array_merge( $this->updates
, $updatesDone );
496 * Helper function: check if the given key is present in the updatelog table.
497 * Obviously, only use this for updates that occur after the updatelog table was
499 * @param string $key Name of the key to check for
502 public function updateRowExists( $key ) {
503 $row = $this->db
->selectRow(
507 [ 'ul_key' => $key ],
515 * Helper function: Add a key to the updatelog table
516 * Obviously, only use this for updates that occur after the updatelog table was
518 * @param string $key Name of key to insert
519 * @param string $val [optional] Value to insert along with the key
521 public function insertUpdateRow( $key, $val = null ) {
522 $this->db
->clearFlag( DBO_DDLMODE
);
523 $values = [ 'ul_key' => $key ];
524 if ( $val && $this->canUseNewUpdatelog() ) {
525 $values['ul_value'] = $val;
527 $this->db
->insert( 'updatelog', $values, __METHOD__
, 'IGNORE' );
528 $this->db
->setFlag( DBO_DDLMODE
);
532 * Updatelog was changed in 1.17 to have a ul_value column so we can record
533 * more information about what kind of updates we've done (that's what this
534 * class does). Pre-1.17 wikis won't have this column, and really old wikis
535 * might not even have updatelog at all
539 protected function canUseNewUpdatelog() {
540 return $this->db
->tableExists( 'updatelog', __METHOD__
) &&
541 $this->db
->fieldExists( 'updatelog', 'ul_value', __METHOD__
);
545 * Returns whether updates should be executed on the database table $name.
546 * Updates will be prevented if the table is a shared table and it is not
547 * specified to run updates on shared tables.
549 * @param string $name Table name
552 protected function doTable( $name ) {
553 global $wgSharedDB, $wgSharedTables;
555 // Don't bother to check $wgSharedTables if there isn't a shared database
556 // or the user actually also wants to do updates on the shared database.
557 if ( $wgSharedDB === null ||
$this->shared
) {
561 if ( in_array( $name, $wgSharedTables ) ) {
562 $this->output( "...skipping update to shared table $name.\n" );
570 * Before 1.17, we used to handle updates via stuff like
571 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
572 * of this in 1.17 but we want to remain back-compatible for a while. So
573 * load up these old global-based things into our update list.
577 protected function getOldGlobalUpdates() {
578 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
583 foreach ( $wgExtNewTables as $tableRecord ) {
585 'addTable', $tableRecord[0], $tableRecord[1], true
589 foreach ( $wgExtNewFields as $fieldRecord ) {
591 'addField', $fieldRecord[0], $fieldRecord[1],
592 $fieldRecord[2], true
596 foreach ( $wgExtNewIndexes as $fieldRecord ) {
598 'addIndex', $fieldRecord[0], $fieldRecord[1],
599 $fieldRecord[2], true
603 foreach ( $wgExtModifiedFields as $fieldRecord ) {
605 'modifyField', $fieldRecord[0], $fieldRecord[1],
606 $fieldRecord[2], true
614 * Get an array of updates to perform on the database. Should return a
615 * multi-dimensional array. The main key is the MediaWiki version (1.12,
616 * 1.13...) with the values being arrays of updates, identical to how
617 * updaters.inc did it (for now)
621 abstract protected function getCoreUpdateList();
624 * Append an SQL fragment to the open file handle.
626 * @param string $filename File name to open
628 public function copyFile( $filename ) {
629 $this->db
->sourceFile(
634 [ $this, 'appendLine' ]
639 * Append a line to the open filehandle. The line is assumed to
640 * be a complete SQL statement.
642 * This is used as a callback for sourceLine().
644 * @param string $line Text to append to the file
645 * @return bool False to skip actually executing the file
646 * @throws MWException
648 public function appendLine( $line ) {
649 $line = rtrim( $line ) . ";\n";
650 if ( fwrite( $this->fileHandle
, $line ) === false ) {
651 throw new MWException( "trouble writing file" );
658 * Applies a SQL patch
660 * @param string $path Path to the patch file
661 * @param bool $isFullPath Whether to treat $path as a relative or not
662 * @param string $msg Description of the patch
663 * @return bool False if patch is skipped.
665 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
666 if ( $msg === null ) {
667 $msg = "Applying $path patch";
669 if ( $this->skipSchema
) {
670 $this->output( "...skipping schema change ($msg).\n" );
675 $this->output( "$msg ..." );
677 if ( !$isFullPath ) {
678 $path = $this->patchPath( $this->db
, $path );
680 if ( $this->fileHandle
!== null ) {
681 $this->copyFile( $path );
683 $this->db
->sourceFile( $path );
685 $this->output( "done.\n" );
691 * Get the full path of a patch file. Originally based on archive()
692 * from updaters.inc. Keep in mind this always returns a patch, as
693 * it fails back to MySQL if no DB-specific patch can be found
695 * @param IDatabase $db
696 * @param string $patch The name of the patch, like patch-something.sql
697 * @return string Full path to patch file
699 public function patchPath( IDatabase
$db, $patch ) {
702 $dbType = $db->getType();
703 if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
704 return "$IP/maintenance/$dbType/archives/$patch";
706 return "$IP/maintenance/archives/$patch";
711 * Add a new table to the database
713 * @param string $name Name of the new table
714 * @param string $patch Path to the patch file
715 * @param bool $fullpath Whether to treat $patch path as a relative or not
716 * @return bool False if this was skipped because schema changes are skipped
718 protected function addTable( $name, $patch, $fullpath = false ) {
719 if ( !$this->doTable( $name ) ) {
723 if ( $this->db
->tableExists( $name, __METHOD__
) ) {
724 $this->output( "...$name table already exists.\n" );
726 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
733 * Add a new field to an existing table
735 * @param string $table Name of the table to modify
736 * @param string $field Name of the new field
737 * @param string $patch Path to the patch file
738 * @param bool $fullpath Whether to treat $patch path as a relative or not
739 * @return bool False if this was skipped because schema changes are skipped
741 protected function addField( $table, $field, $patch, $fullpath = false ) {
742 if ( !$this->doTable( $table ) ) {
746 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
747 $this->output( "...$table table does not exist, skipping new field patch.\n" );
748 } elseif ( $this->db
->fieldExists( $table, $field, __METHOD__
) ) {
749 $this->output( "...have $field field in $table table.\n" );
751 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
758 * Add a new index to an existing table
760 * @param string $table Name of the table to modify
761 * @param string $index Name of the new index
762 * @param string $patch Path to the patch file
763 * @param bool $fullpath Whether to treat $patch path as a relative or not
764 * @return bool False if this was skipped because schema changes are skipped
766 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
767 if ( !$this->doTable( $table ) ) {
771 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
772 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
773 } elseif ( $this->db
->indexExists( $table, $index, __METHOD__
) ) {
774 $this->output( "...index $index already set on $table table.\n" );
776 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
783 * Drop a field from an existing table
785 * @param string $table Name of the table to modify
786 * @param string $field Name of the old field
787 * @param string $patch Path to the patch file
788 * @param bool $fullpath Whether to treat $patch path as a relative or not
789 * @return bool False if this was skipped because schema changes are skipped
791 protected function dropField( $table, $field, $patch, $fullpath = false ) {
792 if ( !$this->doTable( $table ) ) {
796 if ( $this->db
->fieldExists( $table, $field, __METHOD__
) ) {
797 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
799 $this->output( "...$table table does not contain $field field.\n" );
806 * Drop an index from an existing table
808 * @param string $table Name of the table to modify
809 * @param string $index Name of the index
810 * @param string $patch Path to the patch file
811 * @param bool $fullpath Whether to treat $patch path as a relative or not
812 * @return bool False if this was skipped because schema changes are skipped
814 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
815 if ( !$this->doTable( $table ) ) {
819 if ( $this->db
->indexExists( $table, $index, __METHOD__
) ) {
820 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
822 $this->output( "...$index key doesn't exist.\n" );
829 * Rename an index from an existing table
831 * @param string $table Name of the table to modify
832 * @param string $oldIndex Old name of the index
833 * @param string $newIndex New name of the index
834 * @param bool $skipBothIndexExistWarning Whether to warn if both the
835 * old and the new indexes exist.
836 * @param string $patch Path to the patch file
837 * @param bool $fullpath Whether to treat $patch path as a relative or not
838 * @return bool False if this was skipped because schema changes are skipped
840 protected function renameIndex( $table, $oldIndex, $newIndex,
841 $skipBothIndexExistWarning, $patch, $fullpath = false
843 if ( !$this->doTable( $table ) ) {
847 // First requirement: the table must exist
848 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
849 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
854 // Second requirement: the new index must be missing
855 if ( $this->db
->indexExists( $table, $newIndex, __METHOD__
) ) {
856 $this->output( "...index $newIndex already set on $table table.\n" );
857 if ( !$skipBothIndexExistWarning &&
858 $this->db
->indexExists( $table, $oldIndex, __METHOD__
)
860 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
861 "been renamed into $newIndex (which also exists).\n" .
862 " $oldIndex should be manually removed if not needed anymore.\n" );
868 // Third requirement: the old index must exist
869 if ( !$this->db
->indexExists( $table, $oldIndex, __METHOD__
) ) {
870 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
875 // Requirements have been satisfied, patch can be applied
876 return $this->applyPatch(
879 "Renaming index $oldIndex into $newIndex to table $table"
884 * If the specified table exists, drop it, or execute the
885 * patch if one is provided.
889 * @param string $table Table to drop.
890 * @param string|bool $patch String of patch file that will drop the table. Default: false.
891 * @param bool $fullpath Whether $patch is a full path. Default: false.
892 * @return bool False if this was skipped because schema changes are skipped
894 public function dropTable( $table, $patch = false, $fullpath = false ) {
895 if ( !$this->doTable( $table ) ) {
899 if ( $this->db
->tableExists( $table, __METHOD__
) ) {
900 $msg = "Dropping table $table";
902 if ( $patch === false ) {
903 $this->output( "$msg ..." );
904 $this->db
->dropTable( $table, __METHOD__
);
905 $this->output( "done.\n" );
907 return $this->applyPatch( $patch, $fullpath, $msg );
910 $this->output( "...$table doesn't exist.\n" );
917 * Modify an existing field
919 * @param string $table Name of the table to which the field belongs
920 * @param string $field Name of the field to modify
921 * @param string $patch Path to the patch file
922 * @param bool $fullpath Whether to treat $patch path as a relative or not
923 * @return bool False if this was skipped because schema changes are skipped
925 public function modifyField( $table, $field, $patch, $fullpath = false ) {
926 if ( !$this->doTable( $table ) ) {
930 $updateKey = "$table-$field-$patch";
931 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
932 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
933 } elseif ( !$this->db
->fieldExists( $table, $field, __METHOD__
) ) {
934 $this->output( "...$field field does not exist in $table table, " .
935 "skipping modify field patch.\n" );
936 } elseif ( $this->updateRowExists( $updateKey ) ) {
937 $this->output( "...$field in table $table already modified by patch $patch.\n" );
939 $apply = $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
941 $this->insertUpdateRow( $updateKey );
949 * Modify an existing table, similar to modifyField. Intended for changes that
950 * touch more than one column on a table.
952 * @param string $table Name of the table to modify
953 * @param string $patch Name of the patch file to apply
954 * @param string|bool $fullpath Whether to treat $patch path as relative or not, defaults to false
955 * @return bool False if this was skipped because of schema changes being skipped
957 public function modifyTable( $table, $patch, $fullpath = false ) {
958 if ( !$this->doTable( $table ) ) {
962 $updateKey = "$table-$patch";
963 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
964 $this->output( "...$table table does not exist, skipping modify table patch.\n" );
965 } elseif ( $this->updateRowExists( $updateKey ) ) {
966 $this->output( "...table $table already modified by patch $patch.\n" );
968 $apply = $this->applyPatch( $patch, $fullpath, "Modifying table $table" );
970 $this->insertUpdateRow( $updateKey );
978 * Set any .htaccess files or equivilent for storage repos
980 * Some zones (e.g. "temp") used to be public and may have been initialized as such
982 public function setFileAccess() {
983 $repo = RepoGroup
::singleton()->getLocalRepo();
984 $zonePath = $repo->getZonePath( 'temp' );
985 if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
986 // If the directory was never made, then it will have the right ACLs when it is made
987 $status = $repo->getBackend()->secure( [
992 if ( $status->isOK() ) {
993 $this->output( "Set the local repo temp zone container to be private.\n" );
995 $this->output( "Failed to set the local repo temp zone container to be private.\n" );
1001 * Purge various database caches
1003 public function purgeCache() {
1004 global $wgLocalisationCacheConf;
1005 // We can't guarantee that the user will be able to use TRUNCATE,
1006 // but we know that DELETE is available to us
1007 $this->output( "Purging caches..." );
1010 $this->db
->delete( 'objectcache', '*', __METHOD__
);
1012 // LocalisationCache
1013 if ( $wgLocalisationCacheConf['manualRecache'] ) {
1014 $this->rebuildLocalisationCache();
1017 // ResourceLoader: Message cache
1018 $blobStore = new MessageBlobStore();
1019 $blobStore->clear();
1021 // ResourceLoader: File-dependency cache
1022 $this->db
->delete( 'module_deps', '*', __METHOD__
);
1023 $this->output( "done.\n" );
1027 * Check the site_stats table is not properly populated.
1029 protected function checkStats() {
1030 $this->output( "...site_stats is populated..." );
1031 $row = $this->db
->selectRow( 'site_stats', '*', [ 'ss_row_id' => 1 ], __METHOD__
);
1032 if ( $row === false ) {
1033 $this->output( "data is missing! rebuilding...\n" );
1034 } elseif ( isset( $row->site_stats
) && $row->ss_total_pages
== -1 ) {
1035 $this->output( "missing ss_total_pages, rebuilding...\n" );
1037 $this->output( "done.\n" );
1041 SiteStatsInit
::doAllAndCommit( $this->db
);
1044 # Common updater functions
1047 * Sets the number of active users in the site_stats table
1049 protected function doActiveUsersInit() {
1050 $activeUsers = $this->db
->selectField( 'site_stats', 'ss_active_users', '', __METHOD__
);
1051 if ( $activeUsers == -1 ) {
1052 $activeUsers = $this->db
->selectField( 'recentchanges',
1053 'COUNT( DISTINCT rc_user_text )',
1054 [ 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ], __METHOD__
1056 $this->db
->update( 'site_stats',
1057 [ 'ss_active_users' => intval( $activeUsers ) ],
1058 [ 'ss_row_id' => 1 ], __METHOD__
, [ 'LIMIT' => 1 ]
1061 $this->output( "...ss_active_users user count set...\n" );
1065 * Populates the log_user_text field in the logging table
1067 protected function doLogUsertextPopulation() {
1068 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
1070 "Populating log_user_text field, printing progress markers. For large\n" .
1071 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1072 "maintenance/populateLogUsertext.php.\n"
1075 $task = $this->maintenance
->runChild( PopulateLogUsertext
::class );
1077 $this->output( "done.\n" );
1082 * Migrate log params to new table and index for searching
1084 protected function doLogSearchPopulation() {
1085 if ( !$this->updateRowExists( 'populate log_search' ) ) {
1087 "Populating log_search table, printing progress markers. For large\n" .
1088 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1089 "maintenance/populateLogSearch.php.\n" );
1091 $task = $this->maintenance
->runChild( PopulateLogSearch
::class );
1093 $this->output( "done.\n" );
1098 * Updates the timestamps in the transcache table
1101 protected function doUpdateTranscacheField() {
1102 if ( $this->updateRowExists( 'convert transcache field' ) ) {
1103 $this->output( "...transcache tc_time already converted.\n" );
1108 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
1109 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
1113 * Update CategoryLinks collation
1115 protected function doCollationUpdate() {
1116 global $wgCategoryCollation;
1117 if ( $this->db
->fieldExists( 'categorylinks', 'cl_collation', __METHOD__
) ) {
1118 if ( $this->db
->selectField(
1121 'cl_collation != ' . $this->db
->addQuotes( $wgCategoryCollation ),
1125 $this->output( "...collations up-to-date.\n" );
1130 $this->output( "Updating category collations..." );
1131 $task = $this->maintenance
->runChild( UpdateCollation
::class );
1133 $this->output( "...done.\n" );
1138 * Migrates user options from the user table blob to user_properties
1140 protected function doMigrateUserOptions() {
1141 if ( $this->db
->tableExists( 'user_properties' ) ) {
1142 $cl = $this->maintenance
->runChild( ConvertUserOptions
::class, 'convertUserOptions.php' );
1144 $this->output( "done.\n" );
1149 * Enable profiling table when it's turned on
1151 protected function doEnableProfiling() {
1154 if ( !$this->doTable( 'profiling' ) ) {
1158 $profileToDb = false;
1159 if ( isset( $wgProfiler['output'] ) ) {
1160 $out = $wgProfiler['output'];
1161 if ( $out === 'db' ) {
1162 $profileToDb = true;
1163 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
1164 $profileToDb = true;
1168 if ( $profileToDb && !$this->db
->tableExists( 'profiling', __METHOD__
) ) {
1169 $this->applyPatch( 'patch-profiling.sql', false, 'Add profiling table' );
1174 * Rebuilds the localisation cache
1176 protected function rebuildLocalisationCache() {
1178 * @var $cl RebuildLocalisationCache
1180 $cl = $this->maintenance
->runChild(
1181 RebuildLocalisationCache
::class, 'rebuildLocalisationCache.php'
1183 $this->output( "Rebuilding localisation cache...\n" );
1186 $this->output( "done.\n" );
1190 * Turns off content handler fields during parts of the upgrade
1191 * where they aren't available.
1193 protected function disableContentHandlerUseDB() {
1194 global $wgContentHandlerUseDB;
1196 if ( $wgContentHandlerUseDB ) {
1197 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1198 $this->holdContentHandlerUseDB
= $wgContentHandlerUseDB;
1199 $wgContentHandlerUseDB = false;
1204 * Turns content handler fields back on.
1206 protected function enableContentHandlerUseDB() {
1207 global $wgContentHandlerUseDB;
1209 if ( $this->holdContentHandlerUseDB
) {
1210 $this->output( "Content Handler DB fields should be usable now.\n" );
1211 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB
;
1216 * Migrate comments to the new 'comment' table
1219 protected function migrateComments() {
1220 global $wgCommentTableSchemaMigrationStage;
1221 if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_NEW
&&
1222 !$this->updateRowExists( 'MigrateComments' )
1225 "Migrating comments to the 'comments' table, printing progress markers. For large\n" .
1226 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1227 "maintenance/migrateComments.php.\n"
1229 $task = $this->maintenance
->runChild( MigrateComments
::class, 'migrateComments.php' );
1230 $ok = $task->execute();
1231 $this->output( $ok ?
"done.\n" : "errors were encountered.\n" );
1236 * Migrate actors to the new 'actor' table
1239 protected function migrateActors() {
1240 global $wgActorTableSchemaMigrationStage;
1241 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_NEW
&&
1242 !$this->updateRowExists( 'MigrateActors' )
1245 "Migrating actors to the 'actor' table, printing progress markers. For large\n" .
1246 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1247 "maintenance/migrateActors.php.\n"
1249 $task = $this->maintenance
->runChild( 'MigrateActors', 'migrateActors.php' );
1250 $ok = $task->execute();
1251 $this->output( $ok ?
"done.\n" : "errors were encountered.\n" );
1256 * Migrate ar_text to modern storage
1259 protected function migrateArchiveText() {
1260 $this->output( "Migrating archive ar_text to modern storage.\n" );
1261 $task = $this->maintenance
->runChild( MigrateArchiveText
::class, 'migrateArchiveText.php' );
1263 $this->output( "done.\n" );
1267 * Populate ar_rev_id, then make it not nullable
1270 protected function populateArchiveRevId() {
1271 $info = $this->db
->fieldInfo( 'archive', 'ar_rev_id', __METHOD__
);
1273 throw new MWException( 'Missing ar_rev_id field of archive table. Should not happen.' );
1275 if ( $info->isNullable() ) {
1276 $this->output( "Populating ar_rev_id.\n" );
1277 $task = $this->maintenance
->runChild( 'PopulateArchiveRevId', 'populateArchiveRevId.php' );
1278 if ( $task->execute() ) {
1279 $this->applyPatch( 'patch-ar_rev_id-not-null.sql', false,
1280 'Making ar_rev_id not nullable' );