2019-05-02 14:23:42 +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;
|
|
|
|
|
|
2023-06-09 22:25:07 +00:00
|
|
|
use InvalidArgumentException;
|
2019-05-02 14:23:42 +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;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2024-08-08 09:39:26 +00:00
|
|
|
use MediaWiki\Language\LanguageCode;
|
2022-04-25 15:19:41 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-09-18 15:00:50 +00:00
|
|
|
use MediaWiki\Title\MediaWikiTitleCodec;
|
2024-07-09 13:37:44 +00:00
|
|
|
use Wikimedia\ObjectCache\BagOStuff;
|
|
|
|
|
use Wikimedia\ObjectCache\HashBagOStuff;
|
2019-05-02 14:23:42 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A service that provides utilities to do with language names and codes.
|
|
|
|
|
*
|
2020-02-12 02:09:33 +00:00
|
|
|
* See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more information.
|
|
|
|
|
*
|
2019-05-02 14:23:42 +00:00
|
|
|
* @since 1.34
|
2020-02-12 02:09:33 +00:00
|
|
|
* @ingroup Language
|
2019-05-02 14:23:42 +00:00
|
|
|
*/
|
|
|
|
|
class LanguageNameUtils {
|
|
|
|
|
/**
|
|
|
|
|
* Return autonyms in getLanguageName(s).
|
|
|
|
|
*/
|
2020-05-12 22:37:45 +00:00
|
|
|
public const AUTONYMS = null;
|
2019-05-02 14:23:42 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return all known languages in getLanguageName(s).
|
|
|
|
|
*/
|
2020-05-12 22:37:45 +00:00
|
|
|
public const ALL = 'all';
|
2019-05-02 14:23:42 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return in getLanguageName(s) only the languages that are defined by MediaWiki.
|
|
|
|
|
*/
|
2020-05-12 22:37:45 +00:00
|
|
|
public const DEFINED = 'mw';
|
2019-05-02 14:23:42 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return in getLanguageName(s) only the languages for which we have at least some localisation.
|
|
|
|
|
*/
|
2020-05-12 22:37:45 +00:00
|
|
|
public const SUPPORTED = 'mwfile';
|
2019-05-02 14:23:42 +00:00
|
|
|
|
|
|
|
|
/** @var ServiceOptions */
|
|
|
|
|
private $options;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cache for language names
|
|
|
|
|
* @var HashBagOStuff|null
|
|
|
|
|
*/
|
|
|
|
|
private $languageNameCache;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cache for validity of language codes
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $validCodeCache = [];
|
|
|
|
|
|
2019-10-25 08:07:22 +00:00
|
|
|
/**
|
|
|
|
|
* @internal For use by ServiceWiring
|
|
|
|
|
*/
|
2019-10-10 23:56:18 +00:00
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
2022-04-25 15:19:41 +00:00
|
|
|
MainConfigNames::ExtraLanguageNames,
|
|
|
|
|
MainConfigNames::UsePigLatinVariant,
|
2023-07-12 15:44:30 +00:00
|
|
|
MainConfigNames::UseXssLanguage,
|
2019-05-02 14:23:42 +00:00
|
|
|
];
|
|
|
|
|
|
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 HookRunner */
|
|
|
|
|
private $hookRunner;
|
|
|
|
|
|
2019-05-02 14:23:42 +00:00
|
|
|
/**
|
|
|
|
|
* @param ServiceOptions $options
|
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
|
2019-05-02 14:23:42 +00:00
|
|
|
*/
|
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
|
|
|
public function __construct( ServiceOptions $options, HookContainer $hookContainer ) {
|
2019-10-10 23:56:18 +00:00
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
2019-05-02 14:23:42 +00:00
|
|
|
$this->options = $options;
|
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->hookRunner = new HookRunner( $hookContainer );
|
2019-05-02 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks whether any localisation is available for that language tag in MediaWiki
|
|
|
|
|
* (MessagesXx.php or xx.json exists).
|
|
|
|
|
*
|
|
|
|
|
* @param string $code Language tag (in lower case)
|
|
|
|
|
* @return bool Whether language is supported
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isSupportedLanguage( string $code ): bool {
|
2019-05-02 14:23:42 +00:00
|
|
|
if ( !$this->isValidBuiltInCode( $code ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $code === 'qqq' ) {
|
|
|
|
|
// Special code for internal use, not supported even though there is a qqq.json
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-01-26 16:54:24 +00:00
|
|
|
if (
|
|
|
|
|
$code === 'en-x-piglatin' &&
|
|
|
|
|
!$this->options->get( MainConfigNames::UsePigLatinVariant )
|
|
|
|
|
) {
|
|
|
|
|
// Suppress Pig Latin unless explicitly enabled.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-02 14:23:42 +00:00
|
|
|
|
|
|
|
|
return is_readable( $this->getMessagesFileName( $code ) ) ||
|
|
|
|
|
is_readable( $this->getJsonMessagesFileName( $code ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Returns true if a language code string is of a valid form, whether it exists.
|
|
|
|
|
* This includes codes which are used solely for customisation via the MediaWiki namespace.
|
2019-05-02 14:23:42 +00:00
|
|
|
*
|
|
|
|
|
* @param string $code
|
|
|
|
|
*
|
2023-10-02 12:56:25 +00:00
|
|
|
* @return bool False if the language code contains dangerous characters, e.g, HTML special
|
|
|
|
|
* characters or characters that are illegal in MediaWiki titles.
|
2019-05-02 14:23:42 +00:00
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isValidCode( string $code ): bool {
|
2019-05-02 14:23:42 +00:00
|
|
|
if ( !isset( $this->validCodeCache[$code] ) ) {
|
2023-10-02 12:56:25 +00:00
|
|
|
// People think language codes are HTML-safe, so enforce it. Ideally, we should only
|
2019-05-02 14:23:42 +00:00
|
|
|
// allow a-zA-Z0-9- but .+ and other chars are often used for {{int:}} hacks. See bugs
|
|
|
|
|
// T39564, T39587, T38938.
|
|
|
|
|
$this->validCodeCache[$code] =
|
|
|
|
|
// Protect against path traversal
|
|
|
|
|
strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code ) &&
|
2020-11-19 02:07:02 +00:00
|
|
|
!preg_match( MediaWikiTitleCodec::getTitleInvalidRegex(), $code ) &&
|
|
|
|
|
// libicu sets ULOC_FULLNAME_CAPACITY to 157; stay comfortably lower
|
|
|
|
|
strlen( $code ) <= 128;
|
2019-05-02 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
return $this->validCodeCache[$code];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if a language code is of a valid form for the purposes of internal customisation
|
|
|
|
|
* of MediaWiki, via Messages*.php or *.json.
|
|
|
|
|
*
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isValidBuiltInCode( string $code ): bool {
|
2024-09-05 21:04:34 +00:00
|
|
|
return (bool)preg_match( '/^[a-z0-9-]{2,128}$/', $code );
|
2019-05-02 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if a language code is an IETF tag known to MediaWiki.
|
|
|
|
|
*
|
|
|
|
|
* @param string $tag
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isKnownLanguageTag( string $tag ): bool {
|
2019-05-02 14:23:42 +00:00
|
|
|
// Quick escape for invalid input to avoid exceptions down the line when code tries to
|
|
|
|
|
// process tags which are not valid at all.
|
|
|
|
|
if ( !$this->isValidBuiltInCode( $tag ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:22:45 +00:00
|
|
|
if ( isset( Data\Names::NAMES[$tag] ) || $this->getLanguageName( $tag, $tag ) !== '' ) {
|
2019-05-02 14:23:42 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an array of language names, indexed by code.
|
2023-10-02 12:56:25 +00:00
|
|
|
*
|
2019-05-02 14:23:42 +00:00
|
|
|
* @param null|string $inLanguage Code of language in which to return the names
|
|
|
|
|
* Use self::AUTONYMS for autonyms (native names)
|
|
|
|
|
* @param string $include One of:
|
2023-10-02 12:56:25 +00:00
|
|
|
* self::ALL All available languages
|
|
|
|
|
* self::DEFINED Only if the language is defined in MediaWiki or wgExtraLanguageNames
|
2019-05-02 14:23:42 +00:00
|
|
|
* (default)
|
2023-10-02 12:56:25 +00:00
|
|
|
* self::SUPPORTED Only if the language is in self::DEFINED *and* has a message file
|
2019-05-02 14:23:42 +00:00
|
|
|
* @return array Language code => language name (sorted by key)
|
|
|
|
|
*/
|
|
|
|
|
public function getLanguageNames( $inLanguage = self::AUTONYMS, $include = self::DEFINED ) {
|
2023-02-23 09:13:01 +00:00
|
|
|
if ( $inLanguage !== self::AUTONYMS ) {
|
|
|
|
|
$inLanguage = LanguageCode::replaceDeprecatedCodes( LanguageCode::bcp47ToInternal( $inLanguage ) );
|
|
|
|
|
}
|
2019-05-02 14:23:42 +00:00
|
|
|
$cacheKey = $inLanguage === self::AUTONYMS ? 'null' : $inLanguage;
|
|
|
|
|
$cacheKey .= ":$include";
|
|
|
|
|
if ( !$this->languageNameCache ) {
|
|
|
|
|
$this->languageNameCache = new HashBagOStuff( [ 'maxKeys' => 20 ] );
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 14:11:56 +00:00
|
|
|
return $this->languageNameCache->getWithSetCallback(
|
|
|
|
|
$cacheKey,
|
|
|
|
|
BagOStuff::TTL_INDEFINITE,
|
|
|
|
|
function () use ( $inLanguage, $include ) {
|
|
|
|
|
return $this->getLanguageNamesUncached( $inLanguage, $include );
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-05-02 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Uncached helper for getLanguageNames.
|
|
|
|
|
*
|
2019-05-02 14:23:42 +00:00
|
|
|
* @param null|string $inLanguage As getLanguageNames
|
|
|
|
|
* @param string $include As getLanguageNames
|
|
|
|
|
* @return array Language code => language name (sorted by key)
|
|
|
|
|
*/
|
|
|
|
|
private function getLanguageNamesUncached( $inLanguage, $include ) {
|
|
|
|
|
// If passed an invalid language code to use, fallback to en
|
|
|
|
|
if ( $inLanguage !== self::AUTONYMS && !$this->isValidCode( $inLanguage ) ) {
|
|
|
|
|
$inLanguage = 'en';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$names = [];
|
|
|
|
|
|
|
|
|
|
if ( $inLanguage !== self::AUTONYMS ) {
|
|
|
|
|
# TODO: also include for self::AUTONYMS, when this code is more efficient
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
|
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->hookRunner->onLanguageGetTranslatedLanguageNames( $names, $inLanguage );
|
2019-05-02 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-17 18:22:45 +00:00
|
|
|
$mwNames = $this->options->get( MainConfigNames::ExtraLanguageNames ) + Data\Names::NAMES;
|
2023-01-26 16:54:24 +00:00
|
|
|
if ( !$this->options->get( MainConfigNames::UsePigLatinVariant ) ) {
|
|
|
|
|
// Suppress Pig Latin unless explicitly enabled.
|
|
|
|
|
unset( $mwNames['en-x-piglatin'] );
|
2019-05-02 14:23:42 +00:00
|
|
|
}
|
2023-07-12 15:44:30 +00:00
|
|
|
if ( $this->options->get( MainConfigNames::UseXssLanguage ) ) {
|
|
|
|
|
$mwNames['x-xss'] = 'fake xss language (see $wgUseXssLanguage)';
|
|
|
|
|
}
|
2019-05-02 14:23:42 +00:00
|
|
|
|
|
|
|
|
foreach ( $mwNames as $mwCode => $mwName ) {
|
|
|
|
|
# - Prefer own MediaWiki native name when not using the hook
|
|
|
|
|
# - For other names just add if not added through the hook
|
|
|
|
|
if ( $mwCode === $inLanguage || !isset( $names[$mwCode] ) ) {
|
|
|
|
|
$names[$mwCode] = $mwName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $include === self::ALL ) {
|
|
|
|
|
ksort( $names );
|
|
|
|
|
return $names;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$returnMw = [];
|
|
|
|
|
$coreCodes = array_keys( $mwNames );
|
|
|
|
|
foreach ( $coreCodes as $coreCode ) {
|
|
|
|
|
$returnMw[$coreCode] = $names[$coreCode];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $include === self::SUPPORTED ) {
|
|
|
|
|
$namesMwFile = [];
|
|
|
|
|
# We do this using a foreach over the codes instead of a directory loop so that messages
|
|
|
|
|
# files in extensions will work correctly.
|
|
|
|
|
foreach ( $returnMw as $code => $value ) {
|
|
|
|
|
if ( is_readable( $this->getMessagesFileName( $code ) ) ||
|
|
|
|
|
is_readable( $this->getJsonMessagesFileName( $code ) )
|
|
|
|
|
) {
|
|
|
|
|
$namesMwFile[$code] = $names[$code];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ksort( $namesMwFile );
|
|
|
|
|
return $namesMwFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ksort( $returnMw );
|
|
|
|
|
# self::DEFINED option; default if it's not one of the other two options
|
|
|
|
|
# (self::ALL/self::SUPPORTED)
|
|
|
|
|
return $returnMw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $code The code of the language for which to get the name
|
|
|
|
|
* @param null|string $inLanguage Code of language in which to return the name (self::AUTONYMS
|
|
|
|
|
* for autonyms)
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param string $include See getLanguageNames(), except this function defaults to self::ALL instead of
|
2019-05-02 14:23:42 +00:00
|
|
|
* self::DEFINED
|
|
|
|
|
* @return string Language name or empty
|
|
|
|
|
*/
|
|
|
|
|
public function getLanguageName( $code, $inLanguage = self::AUTONYMS, $include = self::ALL ) {
|
2023-02-23 09:13:01 +00:00
|
|
|
$code = LanguageCode::replaceDeprecatedCodes( LanguageCode::bcp47ToInternal( $code ) );
|
2019-05-02 14:23:42 +00:00
|
|
|
$array = $this->getLanguageNames( $inLanguage, $include );
|
|
|
|
|
return $array[$code] ?? '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Get the name of a file for a certain language code.
|
|
|
|
|
*
|
2019-05-02 14:23:42 +00:00
|
|
|
* @param string $prefix Prepend this to the filename
|
|
|
|
|
* @param string $code Language code
|
|
|
|
|
* @param string $suffix Append this to the filename
|
|
|
|
|
* @return string $prefix . $mangledCode . $suffix
|
|
|
|
|
*/
|
|
|
|
|
public function getFileName( $prefix, $code, $suffix = '.php' ) {
|
|
|
|
|
if ( !$this->isValidBuiltInCode( $code ) ) {
|
2023-06-09 22:25:07 +00:00
|
|
|
throw new InvalidArgumentException( "Invalid language code \"$code\"" );
|
2019-05-02 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $prefix . str_replace( '-', '_', ucfirst( $code ) ) . $suffix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getMessagesFileName( $code ) {
|
|
|
|
|
global $IP;
|
|
|
|
|
$file = $this->getFileName( "$IP/languages/messages/Messages", $code, '.php' );
|
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->hookRunner->onLanguage__getMessagesFileName( $code, $file );
|
2019-05-02 14:23:42 +00:00
|
|
|
return $file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getJsonMessagesFileName( $code ) {
|
|
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
if ( !$this->isValidBuiltInCode( $code ) ) {
|
2023-06-09 22:25:07 +00:00
|
|
|
throw new InvalidArgumentException( "Invalid language code \"$code\"" );
|
2019-05-02 14:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "$IP/languages/i18n/$code.json";
|
|
|
|
|
}
|
|
|
|
|
}
|