Debian Codegraph @

includes/language/LanguageFactory.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Languages;
22
23 use InvalidArgumentException;
24 use LocalisationCache;
25 use LogicException;
26 use MapCacheLRU;
27 use MediaWiki\Config\Config;
28 use MediaWiki\Config\ServiceOptions;
29 use MediaWiki\HookContainer\HookContainer;
30 use MediaWiki\Language\Language;
31 use MediaWiki\Language\LanguageCode;
32 use MediaWiki\Language\LanguageConverter;
33 use MediaWiki\MainConfigNames;
34 use MediaWiki\Title\NamespaceInfo;
35 use Wikimedia\Bcp47Code\Bcp47Code;
36
37 /**
38 * Internationalisation code
39 * See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more information.
40 *
41 * @ingroup Language
42 * @since 1.35
43 */
44 class LanguageFactory {
45 /** @var ServiceOptions */
46 private $options;
47
48 /** @var NamespaceInfo */
49 private $namespaceInfo;
50
51 /** @var LocalisationCache */
52 private $localisationCache;
53
54 /** @var LanguageNameUtils */
55 private $langNameUtils;
56
57 /** @var LanguageFallback */
58 private $langFallback;
59
60 /** @var LanguageConverterFactory */
61 private $langConverterFactory;
62
63 /** @var HookContainer */
64 private $hookContainer;
65
66 /** @var MapCacheLRU */
67 private $langObjCache;
68
69 /** @var Config */
70 private $config;
71
72 /** @var array */
73 private $parentLangCache = [];
74
75 /**
76 * @internal For use by ServiceWiring
77 */
78 public const CONSTRUCTOR_OPTIONS = [
79 MainConfigNames::DummyLanguageCodes,
80 ];
81
82 /** How many distinct Language objects to retain at most in memory (T40439). */
83 private const LANG_CACHE_SIZE = 10;
84
85 /**
86 * @param ServiceOptions $options
87 * @param NamespaceInfo $namespaceInfo
88 * @param LocalisationCache $localisationCache
89 * @param LanguageNameUtils $langNameUtils
90 * @param LanguageFallback $langFallback
91 * @param LanguageConverterFactory $langConverterFactory
92 * @param HookContainer $hookContainer
93 * @param Config $config
94 */
95 public function __construct(
96 ServiceOptions $options,
97 NamespaceInfo $namespaceInfo,
98 LocalisationCache $localisationCache,
99 LanguageNameUtils $langNameUtils,
100 LanguageFallback $langFallback,
101 LanguageConverterFactory $langConverterFactory,
102 HookContainer $hookContainer,
103 Config $config
104 ) {
105 // We have both ServiceOptions and a Config object because
106 // the Language class hasn't (yet) been updated to use ServiceOptions
107 // and for now gets a full Config
108 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
109
110 $this->options = $options;
111 $this->namespaceInfo = $namespaceInfo;
112 $this->localisationCache = $localisationCache;
113 $this->langNameUtils = $langNameUtils;
114 $this->langFallback = $langFallback;
115 $this->langConverterFactory = $langConverterFactory;
116 $this->hookContainer = $hookContainer;
117 $this->langObjCache = new MapCacheLRU( self::LANG_CACHE_SIZE );
118 $this->config = $config;
119 }
120
121 /**
122 * Get a cached or new language object for a given language code
123 * with normalization of the language code.
124 *
125 * If the language code comes from user input, check
126 * LanguageNameUtils::isValidCode() before calling this method.
127 *
128 * The language code is presumed to be a MediaWiki-internal code,
129 * unless you pass a Bcp47Code opaque object, in which case it is
130 * presumed to be a standard BCP-47 code. (There are, regrettably,
131 * some ambiguous codes where this makes a difference.)
132 *
133 * As the Language class itself implements Bcp47Code, this method is an efficient
134 * and safe downcast if you pass in a Language object.
135 *
136 * @param string|Bcp47Code $code
137 * @return Language
138 */
139 public function getLanguage( $code ): Language {
140 if ( $code instanceof Language ) {
141 return $code;
142 }
143 if ( $code instanceof Bcp47Code ) {
144 // Any compatibility remapping of valid BCP-47 codes would be done
145 // inside ::bcp47ToInternal, not here.
146 $code = LanguageCode::bcp47ToInternal( $code );
147 } else {
148 // Perform various deprecated and compatibility mappings of
149 // internal codes.
150 $code = $this->options->get( MainConfigNames::DummyLanguageCodes )[$code] ?? $code;
151 }
152 return $this->getRawLanguage( $code );
153 }
154
155 public function getLanguageCode( string $code ): LanguageCode {
156 $code = $this->options->get( MainConfigNames::DummyLanguageCodes )[$code] ?? $code;
157 if ( !$this->langNameUtils->isValidCode( $code ) ) {
158 throw new InvalidArgumentException( "Invalid language code \"$code\"" );
159 }
160 return new LanguageCode( $code );
161 }
162
163 /**
164 * Get a cached or new language object for a given language code
165 * without normalization of the language code.
166 *
167 * If the language code comes from user input, check LanguageNameUtils::isValidCode()
168 * before calling this method.
169 *
170 * @param string $code
171 * @return Language
172 * @since 1.39
173 */
174 public function getRawLanguage( $code ): Language {
175 return $this->langObjCache->getWithSetCallback(
176 $code,
177 function () use ( $code ) {
178 return $this->newFromCode( $code );
179 }
180 );
181 }
182
183 /**
184 * Create a language object for a given language code.
185 *
186 * @param string $code
187 * @param bool $fallback Whether we're going through the language fallback chain
188 * @return Language
189 */
190 private function newFromCode( $code, $fallback = false ): Language {
191 if ( !$this->langNameUtils->isValidCode( $code ) ) {
192 throw new InvalidArgumentException( "Invalid language code \"$code\"" );
193 }
194
195 $constructorArgs = [
196 $code,
197 $this->namespaceInfo,
198 $this->localisationCache,
199 $this->langNameUtils,
200 $this->langFallback,
201 $this->langConverterFactory,
202 $this->hookContainer,
203 $this->config
204 ];
205
206 if ( !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
207 // It's not possible to customise this code with class files, so
208 // just return a Language object. This is to support uselang= hacks.
209 return new Language( ...$constructorArgs );
210 }
211
212 // Check if there is a language class for the code
213 $class = $this->classFromCode( $code, $fallback );
214 // LanguageCode does not inherit Language
215 if ( class_exists( $class ) && is_a( $class, 'Language', true ) ) {
216 return new $class( ...$constructorArgs );
217 }
218
219 // Keep trying the fallback list until we find an existing class
220 $fallbacks = $this->langFallback->getAll( $code );
221 foreach ( $fallbacks as $fallbackCode ) {
222 $class = $this->classFromCode( $fallbackCode );
223 if ( class_exists( $class ) ) {
224 // TODO allow additional dependencies to be injected for subclasses somehow
225 return new $class( ...$constructorArgs );
226 }
227 }
228
229 throw new LogicException( "Invalid fallback sequence for language '$code'" );
230 }
231
232 /**
233 * @param string $code
234 * @param bool $fallback Whether we're going through the language fallback chain
235 * @return string Name of the language class
236 */
237 private function classFromCode( $code, $fallback = true ) {
238 if ( $fallback && $code == 'en' ) {
239 return 'Language';
240 } else {
241 return 'Language' . str_replace( '-', '_', ucfirst( $code ) );
242 }
243 }
244
245 /**
246 * Get the "parent" language which has a converter to convert a "compatible" language
247 * (in another variant) to this language (eg., zh for zh-cn, but not en for en-gb).
248 *
249 * @note This method does not contain the deprecated and compatibility
250 * mappings of Language::getLanguage(string).
251 *
252 * @param string|Bcp47Code $code The language to convert to; can be an
253 * internal MediaWiki language code or a Bcp47Code object (which includes
254 * Language, which implements Bcp47Code).
255 * @return Language|null A base language which has a converter to the given
256 * language, or null if none exists.
257 * @since 1.22
258 */
259 public function getParentLanguage( $code ) {
260 if ( $code instanceof Language ) {
261 $code = $code->getCode();
262 } elseif ( $code instanceof Bcp47Code ) {
263 $code = LanguageCode::bcp47ToInternal( $code );
264 }
265 // $code is now a mediawiki internal code string.
266 // We deliberately use array_key_exists() instead of isset() because we cache null.
267 if ( !array_key_exists( $code, $this->parentLangCache ) ) {
268 if ( !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
269 $this->parentLangCache[$code] = null;
270 return null;
271 }
272 foreach ( LanguageConverter::$languagesWithVariants as $mainCode ) {
273 $lang = $this->getLanguage( $mainCode );
274 $converter = $this->langConverterFactory->getLanguageConverter( $lang );
275 if ( $converter->hasVariant( $code ) ) {
276 $this->parentLangCache[$code] = $lang;
277 return $lang;
278 }
279 }
280 $this->parentLangCache[$code] = null;
281 }
282
283 return $this->parentLangCache[$code];
284 }
285 }