Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.50% |
21 / 24 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
WikiFarmSettingsLoader | |
87.50% |
21 / 24 |
|
33.33% |
1 / 3 |
10.20 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
loadWikiFarmSettings | |
90.91% |
20 / 22 |
|
0.00% |
0 / 1 |
7.04 | |||
getWikiNameConstant | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Settings; |
4 | |
5 | use MediaWiki\MainConfigNames; |
6 | |
7 | /** |
8 | * Utility for loading site-specific settings in a multi-tenancy ("wiki farm" or "wiki family") |
9 | * environment. See <https://www.mediawiki.org/wiki/Manual:Wiki_family>. |
10 | * |
11 | * This class is designed to be used before the initialization of MediaWiki is complete. |
12 | * |
13 | * @unstable |
14 | */ |
15 | class WikiFarmSettingsLoader { |
16 | |
17 | private SettingsBuilder $settingsBuilder; |
18 | |
19 | /** |
20 | * @param SettingsBuilder $settingsBuilder |
21 | */ |
22 | public function __construct( SettingsBuilder $settingsBuilder ) { |
23 | $this->settingsBuilder = $settingsBuilder; |
24 | } |
25 | |
26 | /** |
27 | * Loads any site-specific settings in a multi-tenant (wiki-farm) |
28 | * environment. The settings file is expected to be found in the |
29 | * directory identified by the WikiFarmSettingsDirectory config |
30 | * variable. If WikiFarmSettingsDirectory is not set, wiki-farm |
31 | * mode is disabled, and no site-specific settings will be loaded. |
32 | * |
33 | * The name of the site-specific settings file is determined using |
34 | * the MW_WIKI_NAME environment variable. The file extension is |
35 | * given by WikiFarmSettingsExtension and defaults to "yaml". |
36 | * |
37 | * @unstable |
38 | */ |
39 | public function loadWikiFarmSettings() { |
40 | $config = $this->settingsBuilder->getConfig(); |
41 | |
42 | $farmDir = $config->get( MainConfigNames::WikiFarmSettingsDirectory ); |
43 | $farmExt = $config->get( MainConfigNames::WikiFarmSettingsExtension ); |
44 | |
45 | if ( !$farmDir ) { |
46 | return; |
47 | } |
48 | |
49 | $site = null; |
50 | $wikiName = $this->getWikiNameConstant(); |
51 | if ( $wikiName !== null ) { |
52 | // The MW_WIKI_NAME constant is used to control the target wiki when running CLI scripts. |
53 | // Maintenance.php sets it to the value of the --wiki option. |
54 | $site = $wikiName; |
55 | } elseif ( isset( $_SERVER['MW_WIKI_NAME'] ) ) { |
56 | // The MW_WIKI_NAME environment variable is used to set the target wiki |
57 | // via web server configuration, e.g. using Apache's SetEnv directive. |
58 | // For maintenance scripts, it may be set as an environment variable, |
59 | // or by using the --wiki option. |
60 | $site = $_SERVER['MW_WIKI_NAME']; |
61 | } elseif ( isset( $_SERVER['WIKI_NAME'] ) ) { |
62 | // In 1.38, experimental support for wiki farms was added using the |
63 | // "WIKI_NAME" server variable. This has been changed to "MW_WIKI_NAME" |
64 | // in 1.39. |
65 | $site = $_SERVER['WIKI_NAME']; |
66 | |
67 | // NOTE: We can't use wfDeprecatedMsg here, MediaWiki hasn't been initialized yet. |
68 | trigger_error( |
69 | 'The WIKI_NAME server variable has been deprecated since 1.39, ' . |
70 | 'use MW_WIKI_NAME instead.' |
71 | ); |
72 | } |
73 | |
74 | if ( !$site ) { |
75 | return; |
76 | } |
77 | |
78 | $path = "$farmDir/$site.$farmExt"; |
79 | if ( $this->settingsBuilder->fileExists( $path ) ) { |
80 | $this->settingsBuilder->loadFile( $path ); |
81 | } |
82 | } |
83 | |
84 | /** |
85 | * Access MW_WIKI_NAME in a way that can be overridden by tests |
86 | * |
87 | * @return string|null |
88 | */ |
89 | protected function getWikiNameConstant() { |
90 | return defined( 'MW_WIKI_NAME' ) ? MW_WIKI_NAME : null; |
91 | } |
92 | |
93 | } |