Setup.php: clarify the use of $IP.

The global variable $IP has been replaced by the MW_INSTALL_PATH
constant. Clarify the continued use of $IP on Setup.php.

Change-Id: I157abfd9049fb8382da53005a084ab86f47e8d8a
This commit is contained in:
daniel 2022-04-27 21:33:22 +02:00
parent 3f431f265d
commit bedd996fe6
9 changed files with 53 additions and 24 deletions

View file

@ -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;
}

View file

@ -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" ) ) {

View file

@ -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)

View file

@ -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

View file

@ -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 */

View file

@ -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'];

View file

@ -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 . " "

View file

@ -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;

View file

@ -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() {