diff --git a/includes/BootstrapHelperFunctions.php b/includes/BootstrapHelperFunctions.php index c9c93e14de2..c8dc6f940b9 100644 --- a/includes/BootstrapHelperFunctions.php +++ b/includes/BootstrapHelperFunctions.php @@ -22,14 +22,20 @@ * * @internal Only for use by Setup.php and Installer. * @since 1.38 - * @param string $installationPath The installation's base path, typically global $IP. + * @param string|null $installationPath The installation's base path, + * as returned by wfDetectInstallPath(). + * * @return string The path to the settings file */ -function wfDetectLocalSettingsFile( string $installationPath ): string { +function wfDetectLocalSettingsFile( ?string $installationPath = null ): string { if ( defined( 'MW_CONFIG_FILE' ) ) { return MW_CONFIG_FILE; } + if ( $installationPath === null ) { + $installationPath = wfDetectInstallPath(); + } + // We could look for LocalSettings.yaml and LocalSettings.json, // and use them if they exist. But having them in a web accessible // place is dangerous, so better not to encourage that. @@ -45,3 +51,28 @@ function wfDetectLocalSettingsFile( string $installationPath ): string { define( 'MW_CONFIG_FILE', $configFile ); return $configFile; } + +/** + * Decide and remember where mediawiki is installed. + * + * This is used by Setup.php and will (if not already) store the result + * in the MW_INSTALL_PATH constant. + * + * The install path is detected based on the location of this file, + * but can be overwritten using the MW_INSTALL_PATH environment variable. + * + * @internal Only for use by Setup.php and Installer. + * @since 1.39 + * @return string The path to the mediawiki installation + */ +function wfDetectInstallPath(): string { + if ( !defined( 'MW_INSTALL_PATH' ) ) { + $IP = getenv( 'MW_INSTALL_PATH' ); + if ( $IP === false ) { + $IP = dirname( __DIR__ ); + } + define( 'MW_INSTALL_PATH', $IP ); + } + + return MW_INSTALL_PATH; +} diff --git a/includes/Setup.php b/includes/Setup.php index 562b1279805..8802569ac46 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -94,12 +94,13 @@ if ( !defined( 'MW_ENTRY_POINT' ) ) { define( 'MW_ENTRY_POINT', 'unknown' ); } -if ( !defined( 'MW_INSTALL_PATH' ) ) { - define( 'MW_INSTALL_PATH', $IP ); -} else { - // enforce consistency - $IP = MW_INSTALL_PATH; -} +// The $IP variable is defined for use inside this file and by LocalSettings.php. +// It is made available as a global variable for backwards compatibility, +// but application logic should use the BaseDirectory config setting +// or the MW_INSTALL_PATH constant. +// The BaseDirectory setting is later defined to have the same value as MW_INSTALL_PATH. +global $IP; +$IP = $IP = wfDetectInstallPath(); /** * Pre-config setup: Before loading LocalSettings.php @@ -108,7 +109,6 @@ if ( !defined( 'MW_INSTALL_PATH' ) ) { */ require_once "$IP/includes/AutoLoader.php"; require_once "$IP/includes/Defines.php"; -require_once "$IP/includes/BootstrapHelperFunctions.php"; // Load composer's autoloader if present if ( is_readable( "$IP/vendor/autoload.php" ) ) { diff --git a/includes/WebStart.php b/includes/WebStart.php index c2832404702..81871c8d1f5 100644 --- a/includes/WebStart.php +++ b/includes/WebStart.php @@ -47,12 +47,6 @@ header( 'X-Content-Type-Options: nosniff' ); # its purpose. define( 'MEDIAWIKI', true ); -# Full path to the installation directory. -$IP = getenv( 'MW_INSTALL_PATH' ); -if ( $IP === false ) { - $IP = dirname( __DIR__ ); -} - /** * @param SettingsBuilder $settings * @@ -66,7 +60,10 @@ function wfWebStartNoLocalSettings( SettingsBuilder $settings ) { die(); } -require_once "$IP/includes/BootstrapHelperFunctions.php"; +require_once __DIR__ . "/BootstrapHelperFunctions.php"; + +# Full path to the installation directory. +$IP = wfDetectInstallPath(); // If no LocalSettings file exists, try to display an error page // (use a callback because it depends on TemplateParser) diff --git a/includes/installer/Installer.php b/includes/installer/Installer.php index 115d9ac835f..db25ae4950c 100644 --- a/includes/installer/Installer.php +++ b/includes/installer/Installer.php @@ -646,7 +646,7 @@ abstract class Installer { * @return array|false */ public static function getExistingLocalSettings() { - global $IP; + $IP = wfDetectInstallPath(); // You might be wondering why this is here. Well if you don't do this // then some poorly-formed extensions try to call their own classes diff --git a/maintenance/doMaintenance.php b/maintenance/doMaintenance.php index 66c21dfd57e..0398055eedb 100644 --- a/maintenance/doMaintenance.php +++ b/maintenance/doMaintenance.php @@ -49,8 +49,7 @@ if ( !$maintClass || !class_exists( $maintClass ) ) { // Define the MediaWiki entrypoint define( 'MEDIAWIKI', true ); -// This environment variable is ensured present by Maintenance.php. -$IP = getenv( 'MW_INSTALL_PATH' ); +$IP = wfDetectInstallPath(); // Get an object to start us off /** @var Maintenance $maintenance */ diff --git a/maintenance/includes/Maintenance.php b/maintenance/includes/Maintenance.php index 6b6dbf7136e..4cb638bc4d0 100644 --- a/maintenance/includes/Maintenance.php +++ b/maintenance/includes/Maintenance.php @@ -1291,7 +1291,7 @@ abstract class Maintenance { * @return string */ public function loadSettings() { - global $wgCommandLineMode, $IP; + global $wgCommandLineMode; if ( isset( $this->mOptions['conf'] ) ) { // Define the constant instead of directly setting $settingsFile @@ -1299,7 +1299,7 @@ abstract class Maintenance { // MW_CONFIG_FILE if it is defined. define( 'MW_CONFIG_FILE', $this->mOptions['conf'] ); } - $settingsFile = wfDetectLocalSettingsFile( $IP ); + $settingsFile = wfDetectLocalSettingsFile(); if ( isset( $this->mOptions['wiki'] ) ) { $wikiName = $this->mOptions['wiki']; diff --git a/tests/phpunit/MediaWikiIntegrationTestCase.php b/tests/phpunit/MediaWikiIntegrationTestCase.php index 8532badd9bc..cce1a4fa6ea 100644 --- a/tests/phpunit/MediaWikiIntegrationTestCase.php +++ b/tests/phpunit/MediaWikiIntegrationTestCase.php @@ -186,8 +186,7 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase { * @beforeClass */ final public static function mediaWikiSetUpBeforeClass(): void { - global $IP; - $settingsFile = wfDetectLocalSettingsFile( $IP ); + $settingsFile = wfDetectLocalSettingsFile(); if ( !is_file( $settingsFile ) ) { echo "The file $settingsFile could not be found. " . "Test case " . static::class . " extends " . self::class . " " diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php index 98285c89a97..e38527a83d8 100644 --- a/tests/phpunit/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -38,8 +38,9 @@ require_once "$IP/tests/common/TestSetup.php"; // We don't use a settings file here but some code still assumes that one exists TestSetup::requireOnceInGlobalScope( "$IP/includes/BootstrapHelperFunctions.php" ); + +$IP = wfDetectInstallPath(); // ensure MW_INSTALL_PATH is defined wfDetectLocalSettingsFile( $IP ); -define( 'MW_INSTALL_PATH', $IP ); // these variables must be defined before setup runs $GLOBALS['IP'] = $IP; diff --git a/tests/phpunit/phpunit.php b/tests/phpunit/phpunit.php index 633af45c98e..059a1a2b1de 100644 --- a/tests/phpunit/phpunit.php +++ b/tests/phpunit/phpunit.php @@ -133,6 +133,8 @@ $wrapper = new PHPUnitMaintClass(); $wrapper->setup(); require_once "$IP/includes/BootstrapHelperFunctions.php"; + +$IP = wfDetectInstallPath(); // ensure MW_INSTALL_PATH is defined wfDetectLocalSettingsFile( $IP ); function wfPHPUnitSetup() {