wiki.techinc.nl/includes/BootstrapHelperFunctions.php
daniel dcef1674a5 Allow main settings file to be selected via env variable.
This allows a file other than LocalSettings.php to be used as the primary
settings file by setting the MW_CONFIG_FILE environment variable.
This also allows the primary settings file to use YAML or JSON format.

Using static configuration files should be the default in the future.
However, YAML files in the document root could easily be exposed to the
public. Better not to encourage that, and require them to be enabled
explicitly and loaded from a different place.

Bug: T294750
Change-Id: I7747f83481cb05a6d05f819be652259951183819
2022-02-06 21:13:00 +01:00

51 lines
1.6 KiB
PHP

<?php
/**
* Functions that need to be available during bootstrapping.
* Code in this file cannot expect MediaWiki to have been initialized.
* @file
*/
/**
* Detect the path to the primary settings file that is
* to be loaded by Setup.php and define MW_CONFIG_FILE.
*
* 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.
*
* If the settings file has the .yaml or .json extension, it is expected
* to 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
*
* @since 1.38
*/
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";
}
define( 'MW_CONFIG_FILE', $configFile );
return $configFile;
}