installer: Simplify wfDetectLocalSettingsFile and document gotchass

Follows-up I7747f83481cb05a6d.

While at it:

* Avoid confusing preg_match, use strpos or str_contains instead.
  If this is not the same, then I have proven my point that it is
  unclear (but also picked the wrong alternative).

* Use silence operator (at) instead of AtEase. Also note that in the
  previous change this was incorrectly expanded to silence errors
  in wfDetectLocalSettingsFile, which I assume was unintentional
  and among the reasons we avoid it in new code (T253461).

Change-Id: I0dda95d3c0f21c872e43697acba192d6a19fb0d5
This commit is contained in:
Timo Tijhof 2022-03-01 17:07:49 +00:00
parent 57afe4d257
commit a3406011f6
2 changed files with 41 additions and 42 deletions

View file

@ -6,44 +6,40 @@
*/
/**
* Detect the path to the primary settings file that is
* to be loaded by Setup.php and define MW_CONFIG_FILE.
* Decide and remember where to load LocalSettings from.
*
* The primary settings file would traditionally be
* LocalSettings.php in the installation root, but can be
* a different file specified via the MW_CONFIG_FILE constant
* or the environment variable of the same name.
* This is used by Setup.php and will (if not already) store the result
* in the MW_CONFIG_FILE constant.
*
* If the settings file has the .yaml or .json extension, it is expected
* to use the format described on
* The primary settings file is traditionally LocalSettings.php under the %MediaWiki
* installation path, but can also be placed differently and specified via the
* MW_CONFIG_FILE constant (from an entrypoint wrapper) or via a `MW_CONFIG_FILE`
* environment variable (from the web server).
*
* Experimental: The settings file can use the `.yaml` or `.json` extension, which
* must use the format described on
* https://www.mediawiki.org/wiki/Manual:YAML_settings_file_format.
*
* @internal
*
* @param string $installationPath The installation's base path, typically global $IP.
*
* @return string The path to the primary settings file
*
* @internal Only for use by Setup.php and Installer.
* @since 1.38
* @param string $installationPath The installation's base path, typically global $IP.
* @return string The path to the settings file
*/
function wfDetectLocalSettingsFile( string $installationPath ): string {
if ( defined( 'MW_CONFIG_FILE' ) ) {
return MW_CONFIG_FILE;
}
$configFile = getenv( 'MW_CONFIG_FILE' );
if ( $configFile !== false ) {
if ( !preg_match( '@[\\\\/]@', $configFile ) ) {
$configFile = "$installationPath/$configFile";
}
} else {
// NOTE: 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.
// In order to use LocalSettings.yaml and LocalSettings.json, they
// will have to be referenced explicitly by MW_CONFIG_FILE.
$configFile = "$installationPath/LocalSettings.php";
// 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.
// In order to use LocalSettings.yaml and LocalSettings.json, they
// will have to be referenced explicitly by MW_CONFIG_FILE.
$configFile = getenv( 'MW_CONFIG_FILE' ) ?: "LocalSettings.php";
// Can't use str_contains because for maintenance scripts (update.php, install.php),
// this is called *before* Setup.php and vendor (polyfill-php80) are included.
if ( strpos( $configFile, '/' ) === false ) {
$configFile = "$installationPath/$configFile";
}
define( 'MW_CONFIG_FILE', $configFile );

View file

@ -653,21 +653,26 @@ abstract class Installer {
// phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables
global $wgExtensionDirectory, $wgStyleDirectory;
AtEase::suppressWarnings();
wfDetectLocalSettingsFile( $IP ); // defines MW_CONFIG_FILE
$_lsExists = file_exists( MW_CONFIG_FILE );
AtEase::restoreWarnings();
// This will also define MW_CONFIG_FILE
$lsFile = wfDetectLocalSettingsFile( $IP );
// phpcs:ignore Generic.PHP.NoSilencedErrors
$lsExists = @file_exists( $lsFile );
if ( !$_lsExists ) {
if ( !$lsExists ) {
return false;
}
if ( !str_ends_with( MW_CONFIG_FILE, '.php' ) ) {
if ( !str_ends_with( $lsFile, '.php' ) ) {
throw new Exception(
'The installer cannot yet handle non-php settings files: ' . MW_CONFIG_FILE . '. ' .
'The installer cannot yet handle non-php settings files: ' . $lsFile . '. ' .
'Use maintenance/update.php to update an existing installation.'
);
}
unset( $lsExists );
require "$IP/includes/DefaultSettings.php";
$wgExtensionDirectory = "$IP/extensions";
$wgStyleDirectory = "$IP/skins";
// NOTE: To support YAML settings files, this needs to start using SettingsBuilder.
// However, as of 1.38, YAML settings files are still experimental and
@ -675,14 +680,12 @@ abstract class Installer {
// the existing settings file is not PHP. The updater should still work though.
// NOTE: When adding support for YAML settings file, all references to LocalSettings.php
// in localisation messages need to be replaced.
unset( $_lsExists );
require "$IP/includes/DefaultSettings.php";
$wgExtensionDirectory = "$IP/extensions";
$wgStyleDirectory = "$IP/skins";
require MW_CONFIG_FILE;
// NOTE: This assumes simple variable assignments. More complex setups may involve
// settings coming from sub-required and/or functions that assign globals
// directly. This is fine here because this isn't used as the "real" include.
// It is only used for reading out a small set of variables that the installer
// validates and/or displays.
require $lsFile;
return get_defined_vars();
}