2018-08-07 13:17:16 +00:00
|
|
|
<?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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Languages;
|
|
|
|
|
|
2021-05-12 07:38:49 +00:00
|
|
|
use Config;
|
2018-08-07 13:17:16 +00:00
|
|
|
use Language;
|
|
|
|
|
use LanguageConverter;
|
|
|
|
|
use LocalisationCache;
|
2021-07-16 17:38:02 +00:00
|
|
|
use MapCacheLRU;
|
2018-08-07 13:17:16 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2018-08-07 13:17:16 +00:00
|
|
|
use MWException;
|
2022-03-11 22:54:37 +00:00
|
|
|
use NamespaceInfo;
|
2018-08-07 13:17:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internationalisation code
|
2020-02-12 02:09:33 +00:00
|
|
|
* See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more information.
|
|
|
|
|
*
|
2018-08-07 13:17:16 +00:00
|
|
|
* @ingroup Language
|
|
|
|
|
* @since 1.35
|
|
|
|
|
*/
|
|
|
|
|
class LanguageFactory {
|
|
|
|
|
/** @var ServiceOptions */
|
|
|
|
|
private $options;
|
|
|
|
|
|
2022-03-11 22:54:37 +00:00
|
|
|
/** @var NamespaceInfo */
|
|
|
|
|
private $namespaceInfo;
|
|
|
|
|
|
2018-08-07 13:17:16 +00:00
|
|
|
/** @var LocalisationCache */
|
|
|
|
|
private $localisationCache;
|
|
|
|
|
|
|
|
|
|
/** @var LanguageNameUtils */
|
|
|
|
|
private $langNameUtils;
|
|
|
|
|
|
|
|
|
|
/** @var LanguageFallback */
|
|
|
|
|
private $langFallback;
|
|
|
|
|
|
2020-01-23 18:39:23 +00:00
|
|
|
/** @var LanguageConverterFactory */
|
|
|
|
|
private $langConverterFactory;
|
|
|
|
|
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
/** @var HookContainer */
|
|
|
|
|
private $hookContainer;
|
|
|
|
|
|
2021-07-31 21:42:00 +00:00
|
|
|
/** @var MapCacheLRU */
|
|
|
|
|
private $langObjCache;
|
2018-08-07 13:17:16 +00:00
|
|
|
|
2021-05-12 07:38:49 +00:00
|
|
|
/** @var Config */
|
|
|
|
|
private $config;
|
|
|
|
|
|
2018-08-07 13:17:16 +00:00
|
|
|
/** @var array */
|
|
|
|
|
private $parentLangCache = [];
|
|
|
|
|
|
|
|
|
|
/**
|
2019-10-25 08:07:22 +00:00
|
|
|
* @internal For use by ServiceWiring
|
2018-08-07 13:17:16 +00:00
|
|
|
*/
|
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
2022-04-26 15:48:03 +00:00
|
|
|
MainConfigNames::DummyLanguageCodes,
|
2018-08-07 13:17:16 +00:00
|
|
|
];
|
|
|
|
|
|
2021-07-31 21:42:00 +00:00
|
|
|
/** How many distinct Language objects to retain at most in memory (T40439). */
|
|
|
|
|
private const LANG_CACHE_SIZE = 10;
|
|
|
|
|
|
2018-08-07 13:17:16 +00:00
|
|
|
/**
|
|
|
|
|
* @param ServiceOptions $options
|
2022-03-11 22:54:37 +00:00
|
|
|
* @param NamespaceInfo $namespaceInfo
|
2018-08-07 13:17:16 +00:00
|
|
|
* @param LocalisationCache $localisationCache
|
|
|
|
|
* @param LanguageNameUtils $langNameUtils
|
|
|
|
|
* @param LanguageFallback $langFallback
|
2020-01-23 18:39:23 +00:00
|
|
|
* @param LanguageConverterFactory $langConverterFactory
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
* @param HookContainer $hookContainer
|
2021-05-12 07:38:49 +00:00
|
|
|
* @param Config $config
|
2018-08-07 13:17:16 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
ServiceOptions $options,
|
2022-03-11 22:54:37 +00:00
|
|
|
NamespaceInfo $namespaceInfo,
|
2018-08-07 13:17:16 +00:00
|
|
|
LocalisationCache $localisationCache,
|
|
|
|
|
LanguageNameUtils $langNameUtils,
|
2020-01-23 18:39:23 +00:00
|
|
|
LanguageFallback $langFallback,
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
LanguageConverterFactory $langConverterFactory,
|
2021-05-12 07:38:49 +00:00
|
|
|
HookContainer $hookContainer,
|
|
|
|
|
Config $config
|
2018-08-07 13:17:16 +00:00
|
|
|
) {
|
2021-05-12 07:38:49 +00:00
|
|
|
// We have both ServiceOptions and a Config object because
|
|
|
|
|
// the Language class hasn't (yet) been updated to use ServiceOptions
|
|
|
|
|
// and for now gets a full Config
|
2018-08-07 13:17:16 +00:00
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
|
|
|
|
|
|
$this->options = $options;
|
2022-03-11 22:54:37 +00:00
|
|
|
$this->namespaceInfo = $namespaceInfo;
|
2018-08-07 13:17:16 +00:00
|
|
|
$this->localisationCache = $localisationCache;
|
|
|
|
|
$this->langNameUtils = $langNameUtils;
|
|
|
|
|
$this->langFallback = $langFallback;
|
2020-01-23 18:39:23 +00:00
|
|
|
$this->langConverterFactory = $langConverterFactory;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookContainer = $hookContainer;
|
2021-07-31 21:42:00 +00:00
|
|
|
$this->langObjCache = new MapCacheLRU( self::LANG_CACHE_SIZE );
|
2021-05-12 07:38:49 +00:00
|
|
|
$this->config = $config;
|
2018-08-07 13:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cached or new language object for a given language code
|
2022-08-19 12:15:40 +00:00
|
|
|
* with normalization of the language code.
|
2018-08-07 13:17:16 +00:00
|
|
|
* @param string $code
|
2019-11-25 13:03:54 +00:00
|
|
|
* @throws MWException if the language code contains dangerous characters, e.g. HTML special
|
|
|
|
|
* characters or characters illegal in MediaWiki titles.
|
2018-08-07 13:17:16 +00:00
|
|
|
* @return Language
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getLanguage( $code ): Language {
|
2022-04-26 15:48:03 +00:00
|
|
|
$code = $this->options->get( MainConfigNames::DummyLanguageCodes )[$code] ?? $code;
|
2022-08-19 12:15:40 +00:00
|
|
|
return $this->getRawLanguage( $code );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cached or new language object for a given language code
|
|
|
|
|
* without normalization of the language code.
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @throws MWException if the language code contains dangerous characters, e.g. HTML special
|
|
|
|
|
* characters or characters illegal in MediaWiki titles.
|
|
|
|
|
* @return Language
|
|
|
|
|
* @since 1.39
|
|
|
|
|
*/
|
|
|
|
|
public function getRawLanguage( $code ): Language {
|
2021-10-21 14:11:56 +00:00
|
|
|
return $this->langObjCache->getWithSetCallback(
|
|
|
|
|
$code,
|
|
|
|
|
function () use ( $code ) {
|
|
|
|
|
return $this->newFromCode( $code );
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-08-07 13:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a language object for a given language code
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @param bool $fallback Whether we're going through language fallback chain
|
2019-11-25 13:03:54 +00:00
|
|
|
* @throws MWException if the language code or fallback sequence is invalid
|
2018-08-07 13:17:16 +00:00
|
|
|
* @return Language
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
private function newFromCode( $code, $fallback = false ): Language {
|
2018-08-07 13:17:16 +00:00
|
|
|
if ( !$this->langNameUtils->isValidCode( $code ) ) {
|
|
|
|
|
throw new MWException( "Invalid language code \"$code\"" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$constructorArgs = [
|
|
|
|
|
$code,
|
2022-03-11 22:54:37 +00:00
|
|
|
$this->namespaceInfo,
|
2018-08-07 13:17:16 +00:00
|
|
|
$this->localisationCache,
|
|
|
|
|
$this->langNameUtils,
|
2020-01-23 18:39:23 +00:00
|
|
|
$this->langFallback,
|
|
|
|
|
$this->langConverterFactory,
|
2021-05-12 07:38:49 +00:00
|
|
|
$this->hookContainer,
|
|
|
|
|
$this->config
|
2018-08-07 13:17:16 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
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 ) ) {
|
2021-12-05 06:43:20 +00:00
|
|
|
if ( !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
|
2018-08-07 13:17:16 +00:00
|
|
|
$this->parentLangCache[$code] = null;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-12-05 06:43:20 +00:00
|
|
|
foreach ( LanguageConverter::$languagesWithVariants as $mainCode ) {
|
|
|
|
|
$lang = $this->getLanguage( $mainCode );
|
|
|
|
|
$converter = $this->langConverterFactory->getLanguageConverter( $lang );
|
|
|
|
|
if ( $converter->hasVariant( $code ) ) {
|
|
|
|
|
$this->parentLangCache[$code] = $lang;
|
|
|
|
|
return $lang;
|
|
|
|
|
}
|
2018-08-07 13:17:16 +00:00
|
|
|
}
|
2021-12-05 06:43:20 +00:00
|
|
|
$this->parentLangCache[$code] = null;
|
2018-08-07 13:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->parentLangCache[$code];
|
|
|
|
|
}
|
|
|
|
|
}
|