wiki.techinc.nl/includes/language/LanguageFactory.php
Timo Tijhof 0ffe341629 language: Add missing @ingroup, subgroup "Languages" and ungroup files
== Ungroup file blocks

Remove `@ingroup` from `@file` blocks and keep only the class block.

This matches similar changes previously applied to API, Skins, Profile,
and ResourceLoader.

This helps make the API documentation easier to navigate.
E.g. Modules -> Language in the sidebar of
<https://doc.wikimedia.org/mediawiki-core/master/php/> as well as
<https://doc.wikimedia.org/mediawiki-core/master/php/group__Language.html>

These are currently cluttered with tons of duplicate entries for files
and classes both. We only need to group files that aren't also
documented as a class (e.g. message files, entry points, other scripts
or files that we mainly consider a data file). This has the helpful
side-effect that we don't encourage duplication of the class
description (or worse, place useful docs only in the file block), and
makes the class files consistently start with a mentally ignorable
block. Basically, unless there's something other than a class, don't
describe or group the file itself.

== Missing group

Various classes in this subtree were missing the `Language` group,
or were using different group from before T225756.

== Subgroup

For ease of navigation, move Converter subclasses to a group called
"Languages", which for documentation purposes is a subgroup of
"Language". The next commit does the same for Messages* files,
and Language subclasses (done separately for ease of review).

Change-Id: I301f471f86ba2dee924fece29a16dc3c20b5bebe
2022-06-28 17:12:46 -07:00

225 lines
6.7 KiB
PHP

<?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;
use Config;
use Language;
use LanguageConverter;
use LocalisationCache;
use MapCacheLRU;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\MainConfigNames;
use MWException;
use NamespaceInfo;
/**
* 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 NamespaceInfo */
private $namespaceInfo;
/** @var LocalisationCache */
private $localisationCache;
/** @var LanguageNameUtils */
private $langNameUtils;
/** @var LanguageFallback */
private $langFallback;
/** @var LanguageConverterFactory */
private $langConverterFactory;
/** @var HookContainer */
private $hookContainer;
/** @var MapCacheLRU */
private $langObjCache;
/** @var Config */
private $config;
/** @var array */
private $parentLangCache = [];
/**
* @internal For use by ServiceWiring
*/
public const CONSTRUCTOR_OPTIONS = [
MainConfigNames::DummyLanguageCodes,
];
/** How many distinct Language objects to retain at most in memory (T40439). */
private const LANG_CACHE_SIZE = 10;
/**
* @param ServiceOptions $options
* @param NamespaceInfo $namespaceInfo
* @param LocalisationCache $localisationCache
* @param LanguageNameUtils $langNameUtils
* @param LanguageFallback $langFallback
* @param LanguageConverterFactory $langConverterFactory
* @param HookContainer $hookContainer
* @param Config $config
*/
public function __construct(
ServiceOptions $options,
NamespaceInfo $namespaceInfo,
LocalisationCache $localisationCache,
LanguageNameUtils $langNameUtils,
LanguageFallback $langFallback,
LanguageConverterFactory $langConverterFactory,
HookContainer $hookContainer,
Config $config
) {
// 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
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->options = $options;
$this->namespaceInfo = $namespaceInfo;
$this->localisationCache = $localisationCache;
$this->langNameUtils = $langNameUtils;
$this->langFallback = $langFallback;
$this->langConverterFactory = $langConverterFactory;
$this->hookContainer = $hookContainer;
$this->langObjCache = new MapCacheLRU( self::LANG_CACHE_SIZE );
$this->config = $config;
}
/**
* Get a cached or new language object for a given 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
*/
public function getLanguage( $code ): Language {
$code = $this->options->get( MainConfigNames::DummyLanguageCodes )[$code] ?? $code;
return $this->langObjCache->getWithSetCallback(
$code,
function () use ( $code ) {
return $this->newFromCode( $code );
}
);
}
/**
* 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 if the language code or fallback sequence is invalid
* @return Language
*/
private function newFromCode( $code, $fallback = false ): Language {
if ( !$this->langNameUtils->isValidCode( $code ) ) {
throw new MWException( "Invalid language code \"$code\"" );
}
$constructorArgs = [
$code,
$this->namespaceInfo,
$this->localisationCache,
$this->langNameUtils,
$this->langFallback,
$this->langConverterFactory,
$this->hookContainer,
$this->config
];
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 ) ) {
if ( !$this->langNameUtils->isValidBuiltInCode( $code ) ) {
$this->parentLangCache[$code] = null;
return null;
}
foreach ( LanguageConverter::$languagesWithVariants as $mainCode ) {
$lang = $this->getLanguage( $mainCode );
$converter = $this->langConverterFactory->getLanguageConverter( $lang );
if ( $converter->hasVariant( $code ) ) {
$this->parentLangCache[$code] = $lang;
return $lang;
}
}
$this->parentLangCache[$code] = null;
}
return $this->parentLangCache[$code];
}
}