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
24 require_once __DIR__
. '/../../maintenance/Maintenance.php';
27 * Class for handling database updates. Roughly based off of updaters.inc, with
28 * a few improvements :)
33 abstract class DatabaseUpdater
{
34 protected static $updateCounter = 0;
37 * Array of updates to perform on the database
41 protected $updates = [];
44 * Array of updates that were skipped
48 protected $updatesSkipped = [];
51 * List of extension-provided database updates
54 protected $extensionUpdates = [];
57 * Handle to the database subclass
63 protected $shared = false;
66 * @var string[] Scripts to run after database update
67 * Should be a subclass of LoggedUpdateMaintenance
69 protected $postDatabaseUpdateMaintenance = [
70 DeleteDefaultMessages
::class,
71 PopulateRevisionLength
::class,
72 PopulateRevisionSha1
::class,
73 PopulateImageSha1
::class,
74 FixExtLinksProtocolRelative
::class,
75 PopulateFilearchiveSha1
::class,
76 PopulateBacklinkNamespace
::class,
77 FixDefaultJsonContentPages
::class,
81 * File handle for SQL output.
85 protected $fileHandle = null;
88 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
92 protected $skipSchema = false;
95 * Hold the value of $wgContentHandlerUseDB during the upgrade.
97 protected $holdContentHandlerUseDB = true;
102 * @param DatabaseBase $db To perform updates on
103 * @param bool $shared Whether to perform updates on shared tables
104 * @param Maintenance $maintenance Maintenance object which created us
106 protected function __construct( DatabaseBase
&$db, $shared, Maintenance
$maintenance = null ) {
108 $this->db
->setFlag( DBO_DDLMODE
); // For Oracle's handling of schema files
109 $this->shared
= $shared;
110 if ( $maintenance ) {
111 $this->maintenance
= $maintenance;
112 $this->fileHandle
= $maintenance->fileHandle
;
114 $this->maintenance
= new FakeMaintenance
;
116 $this->maintenance
->setDB( $db );
117 $this->initOldGlobals();
118 $this->loadExtensions();
119 Hooks
::run( 'LoadExtensionSchemaUpdates', [ $this ] );
123 * Initialize all of the old globals. One day this should all become
124 * something much nicer
126 private function initOldGlobals() {
127 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
128 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
130 # For extensions only, should be populated via hooks
131 # $wgDBtype should be checked to specifiy the proper file
132 $wgExtNewTables = []; // table, dir
133 $wgExtNewFields = []; // table, column, dir
134 $wgExtPGNewFields = []; // table, column, column attributes; for PostgreSQL
135 $wgExtPGAlteredFields = []; // table, column, new type, conversion method; for PostgreSQL
136 $wgExtNewIndexes = []; // table, index, dir
137 $wgExtModifiedFields = []; // table, index, dir
141 * Loads LocalSettings.php, if needed, and initialises everything needed for
142 * LoadExtensionSchemaUpdates hook.
144 private function loadExtensions() {
145 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
146 return; // already loaded
148 $vars = Installer
::getExistingLocalSettings();
150 $registry = ExtensionRegistry
::getInstance();
151 $queue = $registry->getQueue();
152 // Don't accidentally load extensions in the future
153 $registry->clearQueue();
155 // This will automatically add "AutoloadClasses" to $wgAutoloadClasses
156 $data = $registry->readFromQueue( $queue );
157 $hooks = [ 'wgHooks' => [ 'LoadExtensionSchemaUpdates' => [] ] ];
158 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
159 $hooks = $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'];
161 if ( $vars && isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
162 $hooks = array_merge_recursive( $hooks, $vars['wgHooks']['LoadExtensionSchemaUpdates'] );
164 global $wgHooks, $wgAutoloadClasses;
165 $wgHooks['LoadExtensionSchemaUpdates'] = $hooks;
166 if ( $vars && isset( $vars['wgAutoloadClasses'] ) ) {
167 $wgAutoloadClasses +
= $vars['wgAutoloadClasses'];
172 * @param DatabaseBase $db
173 * @param bool $shared
174 * @param Maintenance $maintenance
176 * @throws MWException
177 * @return DatabaseUpdater
179 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
180 $type = $db->getType();
181 if ( in_array( $type, Installer
::getDBTypes() ) ) {
182 $class = ucfirst( $type ) . 'Updater';
184 return new $class( $db, $shared, $maintenance );
186 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
191 * Get a database connection to run updates
193 * @return DatabaseBase
195 public function getDB() {
200 * Output some text. If we're running from web, escape the text first.
202 * @param string $str Text to output
204 public function output( $str ) {
205 if ( $this->maintenance
->isQuiet() ) {
208 global $wgCommandLineMode;
209 if ( !$wgCommandLineMode ) {
210 $str = htmlspecialchars( $str );
217 * Add a new update coming from an extension. This should be called by
218 * extensions while executing the LoadExtensionSchemaUpdates hook.
222 * @param array $update The update to run. Format is the following:
223 * first item is the callback function, it also can be a
224 * simple string with the name of a function in this class,
225 * following elements are parameters to the function.
226 * Note that callback functions will receive this object as
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)
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 ];
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 ];
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 ];
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
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 ];
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
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
[] = [
325 $skipBothIndexExistWarning,
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 ];
346 * @param string $tableName
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
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
371 protected function getExtensionUpdates() {
372 return $this->extensionUpdates
;
380 public function getPostDatabaseUpdateMaintenance() {
381 return $this->postDatabaseUpdateMaintenance
;
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];
397 $origParams = $funcList[2];
398 call_user_func_array( $func, $arg );
400 $this->updatesSkipped
[] = $origParams;
407 * @param array $what What updates to perform
409 public function doUpdates( $what = [ 'core', 'extensions', 'stats' ] ) {
412 $what = array_flip( $what );
413 $this->skipSchema
= isset( $what['noschema'] ) ||
$this->fileHandle
!== null;
414 if ( isset( $what['core'] ) ) {
415 $this->runUpdates( $this->getCoreUpdateList(), false );
417 if ( isset( $what['extensions'] ) ) {
418 $this->runUpdates( $this->getOldGlobalUpdates(), false );
419 $this->runUpdates( $this->getExtensionUpdates(), true );
422 if ( isset( $what['stats'] ) ) {
426 $this->setAppliedUpdates( $wgVersion, $this->updates
);
428 if ( $this->fileHandle
) {
429 $this->skipSchema
= false;
430 $this->writeSchemaUpdateFile();
431 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped
);
436 * Helper function for doUpdates()
438 * @param array $updates Array of updates to run
439 * @param bool $passSelf Whether to pass this object we calling external functions
441 private function runUpdates( array $updates, $passSelf ) {
443 $updatesSkipped = [];
444 foreach ( $updates as $params ) {
445 $origParams = $params;
446 $func = array_shift( $params );
447 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
448 $func = [ $this, $func ];
449 } elseif ( $passSelf ) {
450 array_unshift( $params, $this );
452 $ret = call_user_func_array( $func, $params );
454 if ( $ret !== false ) {
455 $updatesDone[] = $origParams;
456 wfGetLBFactory()->waitForReplication();
458 $updatesSkipped[] = [ $func, $params, $origParams ];
461 $this->updatesSkipped
= array_merge( $this->updatesSkipped
, $updatesSkipped );
462 $this->updates
= array_merge( $this->updates
, $updatesDone );
466 * @param string $version
467 * @param array $updates
469 protected function setAppliedUpdates( $version, $updates = [] ) {
470 $this->db
->clearFlag( DBO_DDLMODE
);
471 if ( !$this->canUseNewUpdatelog() ) {
474 $key = "updatelist-$version-" . time() . self
::$updateCounter;
475 self
::$updateCounter++
;
476 $this->db
->insert( 'updatelog',
477 [ 'ul_key' => $key, 'ul_value' => serialize( $updates ) ],
479 $this->db
->setFlag( DBO_DDLMODE
);
483 * Helper function: check if the given key is present in the updatelog table.
484 * Obviously, only use this for updates that occur after the updatelog table was
486 * @param string $key Name of the key to check for
489 public function updateRowExists( $key ) {
490 $row = $this->db
->selectRow(
494 [ 'ul_key' => $key ],
502 * Helper function: Add a key to the updatelog table
503 * Obviously, only use this for updates that occur after the updatelog table was
505 * @param string $key Name of key to insert
506 * @param string $val [optional] Value to insert along with the key
508 public function insertUpdateRow( $key, $val = null ) {
509 $this->db
->clearFlag( DBO_DDLMODE
);
510 $values = [ 'ul_key' => $key ];
511 if ( $val && $this->canUseNewUpdatelog() ) {
512 $values['ul_value'] = $val;
514 $this->db
->insert( 'updatelog', $values, __METHOD__
, 'IGNORE' );
515 $this->db
->setFlag( DBO_DDLMODE
);
519 * Updatelog was changed in 1.17 to have a ul_value column so we can record
520 * more information about what kind of updates we've done (that's what this
521 * class does). Pre-1.17 wikis won't have this column, and really old wikis
522 * might not even have updatelog at all
526 protected function canUseNewUpdatelog() {
527 return $this->db
->tableExists( 'updatelog', __METHOD__
) &&
528 $this->db
->fieldExists( 'updatelog', 'ul_value', __METHOD__
);
532 * Returns whether updates should be executed on the database table $name.
533 * Updates will be prevented if the table is a shared table and it is not
534 * specified to run updates on shared tables.
536 * @param string $name Table name
539 protected function doTable( $name ) {
540 global $wgSharedDB, $wgSharedTables;
542 // Don't bother to check $wgSharedTables if there isn't a shared database
543 // or the user actually also wants to do updates on the shared database.
544 if ( $wgSharedDB === null ||
$this->shared
) {
548 if ( in_array( $name, $wgSharedTables ) ) {
549 $this->output( "...skipping update to shared table $name.\n" );
557 * Before 1.17, we used to handle updates via stuff like
558 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
559 * of this in 1.17 but we want to remain back-compatible for a while. So
560 * load up these old global-based things into our update list.
564 protected function getOldGlobalUpdates() {
565 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
570 foreach ( $wgExtNewTables as $tableRecord ) {
572 'addTable', $tableRecord[0], $tableRecord[1], true
576 foreach ( $wgExtNewFields as $fieldRecord ) {
578 'addField', $fieldRecord[0], $fieldRecord[1],
579 $fieldRecord[2], true
583 foreach ( $wgExtNewIndexes as $fieldRecord ) {
585 'addIndex', $fieldRecord[0], $fieldRecord[1],
586 $fieldRecord[2], true
590 foreach ( $wgExtModifiedFields as $fieldRecord ) {
592 'modifyField', $fieldRecord[0], $fieldRecord[1],
593 $fieldRecord[2], true
601 * Get an array of updates to perform on the database. Should return a
602 * multi-dimensional array. The main key is the MediaWiki version (1.12,
603 * 1.13...) with the values being arrays of updates, identical to how
604 * updaters.inc did it (for now)
608 abstract protected function getCoreUpdateList();
611 * Append an SQL fragment to the open file handle.
613 * @param string $filename File name to open
615 public function copyFile( $filename ) {
616 $this->db
->sourceFile( $filename, false, false, false,
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" );
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" );
658 $this->output( "$msg ..." );
660 if ( !$isFullPath ) {
661 $path = $this->db
->patchPath( $path );
663 if ( $this->fileHandle
!== null ) {
664 $this->copyFile( $path );
666 $this->db
->sourceFile( $path );
668 $this->output( "done.\n" );
674 * Add a new table to the database
676 * @param string $name Name of the new table
677 * @param string $patch Path to the patch file
678 * @param bool $fullpath Whether to treat $patch path as a relative or not
679 * @return bool False if this was skipped because schema changes are skipped
681 protected function addTable( $name, $patch, $fullpath = false ) {
682 if ( !$this->doTable( $name ) ) {
686 if ( $this->db
->tableExists( $name, __METHOD__
) ) {
687 $this->output( "...$name table already exists.\n" );
689 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
696 * Add a new field to an existing table
698 * @param string $table Name of the table to modify
699 * @param string $field Name of the new field
700 * @param string $patch Path to the patch file
701 * @param bool $fullpath Whether to treat $patch path as a relative or not
702 * @return bool False if this was skipped because schema changes are skipped
704 protected function addField( $table, $field, $patch, $fullpath = false ) {
705 if ( !$this->doTable( $table ) ) {
709 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
710 $this->output( "...$table table does not exist, skipping new field patch.\n" );
711 } elseif ( $this->db
->fieldExists( $table, $field, __METHOD__
) ) {
712 $this->output( "...have $field field in $table table.\n" );
714 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
721 * Add a new index to an existing table
723 * @param string $table Name of the table to modify
724 * @param string $index Name of the new index
725 * @param string $patch Path to the patch file
726 * @param bool $fullpath Whether to treat $patch path as a relative or not
727 * @return bool False if this was skipped because schema changes are skipped
729 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
730 if ( !$this->doTable( $table ) ) {
734 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
735 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
736 } elseif ( $this->db
->indexExists( $table, $index, __METHOD__
) ) {
737 $this->output( "...index $index already set on $table table.\n" );
739 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
746 * Drop a field from an existing table
748 * @param string $table Name of the table to modify
749 * @param string $field Name of the old field
750 * @param string $patch Path to the patch file
751 * @param bool $fullpath Whether to treat $patch path as a relative or not
752 * @return bool False if this was skipped because schema changes are skipped
754 protected function dropField( $table, $field, $patch, $fullpath = false ) {
755 if ( !$this->doTable( $table ) ) {
759 if ( $this->db
->fieldExists( $table, $field, __METHOD__
) ) {
760 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
762 $this->output( "...$table table does not contain $field field.\n" );
769 * Drop an index from an existing table
771 * @param string $table Name of the table to modify
772 * @param string $index Name of the index
773 * @param string $patch Path to the patch file
774 * @param bool $fullpath Whether to treat $patch path as a relative or not
775 * @return bool False if this was skipped because schema changes are skipped
777 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
778 if ( !$this->doTable( $table ) ) {
782 if ( $this->db
->indexExists( $table, $index, __METHOD__
) ) {
783 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
785 $this->output( "...$index key doesn't exist.\n" );
792 * Rename an index from an existing table
794 * @param string $table Name of the table to modify
795 * @param string $oldIndex Old name of the index
796 * @param string $newIndex New name of the index
797 * @param bool $skipBothIndexExistWarning Whether to warn if both the
798 * old and the new indexes exist.
799 * @param string $patch Path to the patch file
800 * @param bool $fullpath Whether to treat $patch path as a relative or not
801 * @return bool False if this was skipped because schema changes are skipped
803 protected function renameIndex( $table, $oldIndex, $newIndex,
804 $skipBothIndexExistWarning, $patch, $fullpath = false
806 if ( !$this->doTable( $table ) ) {
810 // First requirement: the table must exist
811 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
812 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
817 // Second requirement: the new index must be missing
818 if ( $this->db
->indexExists( $table, $newIndex, __METHOD__
) ) {
819 $this->output( "...index $newIndex already set on $table table.\n" );
820 if ( !$skipBothIndexExistWarning &&
821 $this->db
->indexExists( $table, $oldIndex, __METHOD__
)
823 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
824 "been renamed into $newIndex (which also exists).\n" .
825 " $oldIndex should be manually removed if not needed anymore.\n" );
831 // Third requirement: the old index must exist
832 if ( !$this->db
->indexExists( $table, $oldIndex, __METHOD__
) ) {
833 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
838 // Requirements have been satisfied, patch can be applied
839 return $this->applyPatch(
842 "Renaming index $oldIndex into $newIndex to table $table"
847 * If the specified table exists, drop it, or execute the
848 * patch if one is provided.
852 * @param string $table Table to drop.
853 * @param string|bool $patch String of patch file that will drop the table. Default: false.
854 * @param bool $fullpath Whether $patch is a full path. Default: false.
855 * @return bool False if this was skipped because schema changes are skipped
857 public function dropTable( $table, $patch = false, $fullpath = false ) {
858 if ( !$this->doTable( $table ) ) {
862 if ( $this->db
->tableExists( $table, __METHOD__
) ) {
863 $msg = "Dropping table $table";
865 if ( $patch === false ) {
866 $this->output( "$msg ..." );
867 $this->db
->dropTable( $table, __METHOD__
);
868 $this->output( "done.\n" );
870 return $this->applyPatch( $patch, $fullpath, $msg );
873 $this->output( "...$table doesn't exist.\n" );
880 * Modify an existing field
882 * @param string $table Name of the table to which the field belongs
883 * @param string $field Name of the field to modify
884 * @param string $patch Path to the patch file
885 * @param bool $fullpath Whether to treat $patch path as a relative or not
886 * @return bool False if this was skipped because schema changes are skipped
888 public function modifyField( $table, $field, $patch, $fullpath = false ) {
889 if ( !$this->doTable( $table ) ) {
893 $updateKey = "$table-$field-$patch";
894 if ( !$this->db
->tableExists( $table, __METHOD__
) ) {
895 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
896 } elseif ( !$this->db
->fieldExists( $table, $field, __METHOD__
) ) {
897 $this->output( "...$field field does not exist in $table table, " .
898 "skipping modify field patch.\n" );
899 } elseif ( $this->updateRowExists( $updateKey ) ) {
900 $this->output( "...$field in table $table already modified by patch $patch.\n" );
902 $this->insertUpdateRow( $updateKey );
904 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
911 * Set any .htaccess files or equivilent for storage repos
913 * Some zones (e.g. "temp") used to be public and may have been initialized as such
915 public function setFileAccess() {
916 $repo = RepoGroup
::singleton()->getLocalRepo();
917 $zonePath = $repo->getZonePath( 'temp' );
918 if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
919 // If the directory was never made, then it will have the right ACLs when it is made
920 $status = $repo->getBackend()->secure( [
925 if ( $status->isOK() ) {
926 $this->output( "Set the local repo temp zone container to be private.\n" );
928 $this->output( "Failed to set the local repo temp zone container to be private.\n" );
934 * Purge the objectcache table
936 public function purgeCache() {
937 global $wgLocalisationCacheConf;
938 # We can't guarantee that the user will be able to use TRUNCATE,
939 # but we know that DELETE is available to us
940 $this->output( "Purging caches..." );
941 $this->db
->delete( 'objectcache', '*', __METHOD__
);
942 if ( $wgLocalisationCacheConf['manualRecache'] ) {
943 $this->rebuildLocalisationCache();
945 $blobStore = new MessageBlobStore();
947 $this->db
->delete( 'module_deps', '*', __METHOD__
);
948 $this->output( "done.\n" );
952 * Check the site_stats table is not properly populated.
954 protected function checkStats() {
955 $this->output( "...site_stats is populated..." );
956 $row = $this->db
->selectRow( 'site_stats', '*', [ 'ss_row_id' => 1 ], __METHOD__
);
957 if ( $row === false ) {
958 $this->output( "data is missing! rebuilding...\n" );
959 } elseif ( isset( $row->site_stats
) && $row->ss_total_pages
== -1 ) {
960 $this->output( "missing ss_total_pages, rebuilding...\n" );
962 $this->output( "done.\n" );
966 SiteStatsInit
::doAllAndCommit( $this->db
);
969 # Common updater functions
972 * Sets the number of active users in the site_stats table
974 protected function doActiveUsersInit() {
975 $activeUsers = $this->db
->selectField( 'site_stats', 'ss_active_users', false, __METHOD__
);
976 if ( $activeUsers == -1 ) {
977 $activeUsers = $this->db
->selectField( 'recentchanges',
978 'COUNT( DISTINCT rc_user_text )',
979 [ 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ], __METHOD__
981 $this->db
->update( 'site_stats',
982 [ 'ss_active_users' => intval( $activeUsers ) ],
983 [ 'ss_row_id' => 1 ], __METHOD__
, [ 'LIMIT' => 1 ]
986 $this->output( "...ss_active_users user count set...\n" );
990 * Populates the log_user_text field in the logging table
992 protected function doLogUsertextPopulation() {
993 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
995 "Populating log_user_text field, printing progress markers. For large\n" .
996 "databases, you may want to hit Ctrl-C and do this manually with\n" .
997 "maintenance/populateLogUsertext.php.\n"
1000 $task = $this->maintenance
->runChild( 'PopulateLogUsertext' );
1002 $this->output( "done.\n" );
1007 * Migrate log params to new table and index for searching
1009 protected function doLogSearchPopulation() {
1010 if ( !$this->updateRowExists( 'populate log_search' ) ) {
1012 "Populating log_search table, printing progress markers. For large\n" .
1013 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1014 "maintenance/populateLogSearch.php.\n" );
1016 $task = $this->maintenance
->runChild( 'PopulateLogSearch' );
1018 $this->output( "done.\n" );
1023 * Updates the timestamps in the transcache table
1026 protected function doUpdateTranscacheField() {
1027 if ( $this->updateRowExists( 'convert transcache field' ) ) {
1028 $this->output( "...transcache tc_time already converted.\n" );
1033 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
1034 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
1038 * Update CategoryLinks collation
1040 protected function doCollationUpdate() {
1041 global $wgCategoryCollation;
1042 if ( $this->db
->fieldExists( 'categorylinks', 'cl_collation', __METHOD__
) ) {
1043 if ( $this->db
->selectField(
1046 'cl_collation != ' . $this->db
->addQuotes( $wgCategoryCollation ),
1050 $this->output( "...collations up-to-date.\n" );
1055 $this->output( "Updating category collations..." );
1056 $task = $this->maintenance
->runChild( 'UpdateCollation' );
1058 $this->output( "...done.\n" );
1063 * Migrates user options from the user table blob to user_properties
1065 protected function doMigrateUserOptions() {
1066 if ( $this->db
->tableExists( 'user_properties' ) ) {
1067 $cl = $this->maintenance
->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
1069 $this->output( "done.\n" );
1074 * Enable profiling table when it's turned on
1076 protected function doEnableProfiling() {
1079 if ( !$this->doTable( 'profiling' ) ) {
1083 $profileToDb = false;
1084 if ( isset( $wgProfiler['output'] ) ) {
1085 $out = $wgProfiler['output'];
1086 if ( $out === 'db' ) {
1087 $profileToDb = true;
1088 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
1089 $profileToDb = true;
1093 if ( $profileToDb && !$this->db
->tableExists( 'profiling', __METHOD__
) ) {
1094 $this->applyPatch( 'patch-profiling.sql', false, 'Add profiling table' );
1099 * Rebuilds the localisation cache
1101 protected function rebuildLocalisationCache() {
1103 * @var $cl RebuildLocalisationCache
1105 $cl = $this->maintenance
->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
1106 $this->output( "Rebuilding localisation cache...\n" );
1109 $this->output( "done.\n" );
1113 * Turns off content handler fields during parts of the upgrade
1114 * where they aren't available.
1116 protected function disableContentHandlerUseDB() {
1117 global $wgContentHandlerUseDB;
1119 if ( $wgContentHandlerUseDB ) {
1120 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1121 $this->holdContentHandlerUseDB
= $wgContentHandlerUseDB;
1122 $wgContentHandlerUseDB = false;
1127 * Turns content handler fields back on.
1129 protected function enableContentHandlerUseDB() {
1130 global $wgContentHandlerUseDB;
1132 if ( $this->holdContentHandlerUseDB
) {
1133 $this->output( "Content Handler DB fields should be usable now.\n" );
1134 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB
;