aboutsummaryrefslogtreecommitdiffstats
path: root/includes/Settings/WikiFarmSettingsLoader.php
blob: 4a6120c9ad496114e33f35c1a606b7f349feb126 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php

namespace MediaWiki\Settings;

use MediaWiki\MainConfigNames;

/**
 * Utility for loading site-specific settings in a multi-tenancy ("wiki farm" or "wiki family")
 * environment. See <https://www.mediawiki.org/wiki/Manual:Wiki_family>.
 *
 * This class is designed to be used before the initialization of MediaWiki is complete.
 *
 * @unstable
 */
class WikiFarmSettingsLoader {

	private SettingsBuilder $settingsBuilder;

	/**
	 * @param SettingsBuilder $settingsBuilder
	 */
	public function __construct( SettingsBuilder $settingsBuilder ) {
		$this->settingsBuilder = $settingsBuilder;
	}

	/**
	 * Loads any site-specific settings in a multi-tenant (wiki-farm)
	 * environment. The settings file is expected to be found in the
	 * directory identified by the WikiFarmSettingsDirectory config
	 * variable. If WikiFarmSettingsDirectory is not set, wiki-farm
	 * mode is disabled, and no site-specific settings will be loaded.
	 *
	 * The name of the site-specific settings file is determined using
	 * the MW_WIKI_NAME environment variable. The file extension is
	 * given by WikiFarmSettingsExtension and defaults to "yaml".
	 *
	 * @unstable
	 */
	public function loadWikiFarmSettings() {
		$config = $this->settingsBuilder->getConfig();

		$farmDir = $config->get( MainConfigNames::WikiFarmSettingsDirectory );
		$farmExt = $config->get( MainConfigNames::WikiFarmSettingsExtension );

		if ( !$farmDir ) {
			return;
		}

		$site = null;
		$wikiName = $this->getWikiNameConstant();
		if ( $wikiName !== null ) {
			// The MW_WIKI_NAME constant is used to control the target wiki when running CLI scripts.
			// Maintenance.php sets it to the value of the --wiki option.
			$site = $wikiName;
		} elseif ( isset( $_SERVER['MW_WIKI_NAME'] ) ) {
			// The MW_WIKI_NAME environment variable is used to set the target wiki
			// via web server configuration, e.g. using Apache's SetEnv directive.
			// For maintenance scripts, it may be set as an environment variable,
			// or by using the --wiki option.
			$site = $_SERVER['MW_WIKI_NAME'];
		} elseif ( isset( $_SERVER['WIKI_NAME'] ) ) {
			// In 1.38, experimental support for wiki farms was added using the
			// "WIKI_NAME" server variable. This has been changed to "MW_WIKI_NAME"
			// in 1.39.
			$site = $_SERVER['WIKI_NAME'];

			// NOTE: We can't use wfDeprecatedMsg here, MediaWiki hasn't been initialized yet.
			trigger_error(
				'The WIKI_NAME server variable has been deprecated since 1.39, ' .
					'use MW_WIKI_NAME instead.'
			);
		}

		if ( !$site ) {
			return;
		}

		$path = "$farmDir/$site.$farmExt";
		if ( $this->settingsBuilder->fileExists( $path ) ) {
			$this->settingsBuilder->loadFile( $path );
		}
	}

	/**
	 * Access MW_WIKI_NAME in a way that can be overridden by tests
	 *
	 * @return string|null
	 */
	protected function getWikiNameConstant() {
		return defined( 'MW_WIKI_NAME' ) ? MW_WIKI_NAME : null;
	}

}