aboutsummaryrefslogtreecommitdiffstats
path: root/includes/language/LanguageFactory.php
blob: cd6c85e859342be15eacc0d07865b9cb0840cabf (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

/**
 * @defgroup Language Language
 */

namespace MediaWiki\Languages;

use Language;
use LanguageConverter;
use LocalisationCache;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\MediaWikiServices;
use MWException;

/**
 * Internationalisation code
 * See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more information.
 *
 * @ingroup Language
 * @since 1.35
 */
class LanguageFactory {
	/** @var ServiceOptions */
	private $options;

	/** @var LocalisationCache */
	private $localisationCache;

	/** @var LanguageNameUtils */
	private $langNameUtils;

	/** @var LanguageFallback */
	private $langFallback;

	/** @var LanguageConverterFactory */
	private $langConverterFactory;

	/** @var array */
	private $langObjCache = [];

	/** @var array */
	private $parentLangCache = [];

	/**
	 * @since 1.35
	 * @var array
	 */
	public const CONSTRUCTOR_OPTIONS = [
		'DummyLanguageCodes',
		'LangObjCacheSize',
	];

	/**
	 * @param ServiceOptions $options
	 * @param LocalisationCache $localisationCache
	 * @param LanguageNameUtils $langNameUtils
	 * @param LanguageFallback $langFallback
	 * @param LanguageConverterFactory $langConverterFactory
	 */
	public function __construct(
		ServiceOptions $options,
		LocalisationCache $localisationCache,
		LanguageNameUtils $langNameUtils,
		LanguageFallback $langFallback,
		LanguageConverterFactory $langConverterFactory
	) {
		$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );

		$this->options = $options;
		$this->localisationCache = $localisationCache;
		$this->langNameUtils = $langNameUtils;
		$this->langFallback = $langFallback;
		$this->langConverterFactory = $langConverterFactory;
	}

	/**
	 * Get a cached or new language object for a given language code
	 * @param string $code
	 * @throws MWException
	 * @return Language
	 */
	public function getLanguage( $code ) : Language {
		$code = $this->options->get( 'DummyLanguageCodes' )[$code] ?? $code;

		// This is horrible, horrible code, but is necessary to support Language::$mLangObjCache
		// per the deprecation policy. Kill with fire in 1.36!
		if (
			MediaWikiServices::hasInstance() &&
			$this === MediaWikiServices::getInstance()->getLanguageFactory()
		) {
			$this->langObjCache = Language::$mLangObjCache;
		}

		// Get the language object to process
		$langObj = $this->langObjCache[$code] ?? $this->newFromCode( $code );

		// Merge the language object in to get it up front in the cache
		$this->langObjCache = array_merge( [ $code => $langObj ], $this->langObjCache );
		// Get rid of the oldest ones in case we have an overflow
		$this->langObjCache =
			array_slice( $this->langObjCache, 0, $this->options->get( 'LangObjCacheSize' ), true );

		// As above, remove this in 1.36
		if (
			MediaWikiServices::hasInstance() &&
			$this === MediaWikiServices::getInstance()->getLanguageFactory()
		) {
			Language::$mLangObjCache = $this->langObjCache;
		}

		return $langObj;
	}

	/**
	 * Create a language object for a given language code
	 * @param string $code
	 * @param bool $fallback Whether we're going through language fallback chain
	 * @throws MWException
	 * @return Language
	 */
	private function newFromCode( $code, $fallback = false ) : Language {
		if ( !$this->langNameUtils->isValidCode( $code ) ) {
			throw new MWException( "Invalid language code \"$code\"" );
		}

		$constructorArgs = [
			$code,
			$this->localisationCache,
			$this->langNameUtils,
			$this->langFallback,
			$this->langConverterFactory,
		];

		if ( !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
			// It's not possible to customise this code with class files, so
			// just return a Language object. This is to support uselang= hacks.
			return new Language( ...$constructorArgs );
		}

		// Check if there is a language class for the code
		$class = $this->classFromCode( $code, $fallback );
		// LanguageCode does not inherit Language
		if ( class_exists( $class ) && is_a( $class, 'Language', true ) ) {
			return new $class( ...$constructorArgs );
		}

		// Keep trying the fallback list until we find an existing class
		$fallbacks = $this->langFallback->getAll( $code );
		foreach ( $fallbacks as $fallbackCode ) {
			$class = $this->classFromCode( $fallbackCode );
			if ( class_exists( $class ) ) {
				// TODO allow additional dependencies to be injected for subclasses somehow
				return new $class( ...$constructorArgs );
			}
		}

		throw new MWException( "Invalid fallback sequence for language '$code'" );
	}

	/**
	 * @param string $code
	 * @param bool $fallback Whether we're going through language fallback chain
	 * @return string Name of the language class
	 */
	private function classFromCode( $code, $fallback = true ) {
		if ( $fallback && $code == 'en' ) {
			return 'Language';
		} else {
			return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
		}
	}

	/**
	 * Get the "parent" language which has a converter to convert a "compatible" language
	 * (in another variant) to this language (eg. zh for zh-cn, but not en for en-gb).
	 *
	 * @param string $code
	 * @return Language|null
	 * @since 1.22
	 */
	public function getParentLanguage( $code ) {
		// We deliberately use array_key_exists() instead of isset() because we cache null.
		if ( !array_key_exists( $code, $this->parentLangCache ) ) {
			$codeBase = explode( '-', $code )[0];
			if ( !in_array( $codeBase, LanguageConverter::$languagesWithVariants ) ) {
				$this->parentLangCache[$code] = null;
				return null;
			}

			$lang = $this->getLanguage( $codeBase );
			$converter = $this->langConverterFactory->getLanguageConverter( $lang );
			if ( !$converter->hasVariant( $code ) ) {
				$this->parentLangCache[$code] = null;
				return null;
			}

			$this->parentLangCache[$code] = $lang;
		}

		return $this->parentLangCache[$code];
	}
}