2020-01-23 18:39:23 +00:00
|
|
|
<?php
|
2020-02-10 14:08:01 +00:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2020-01-23 18:39:23 +00:00
|
|
|
|
|
|
|
|
namespace MediaWiki\Languages;
|
|
|
|
|
|
2020-10-22 18:38:06 +00:00
|
|
|
use BanConverter;
|
2020-01-23 18:39:23 +00:00
|
|
|
use CrhConverter;
|
|
|
|
|
use EnConverter;
|
|
|
|
|
use GanConverter;
|
|
|
|
|
use IuConverter;
|
|
|
|
|
use KuConverter;
|
2023-12-08 17:41:34 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2024-08-08 09:39:26 +00:00
|
|
|
use MediaWiki\Language\ILanguageConverter;
|
|
|
|
|
use MediaWiki\Language\Language;
|
2023-12-08 17:41:34 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-10-25 16:58:49 +00:00
|
|
|
use MediaWiki\StubObject\StubUserLang;
|
2024-05-04 11:46:02 +00:00
|
|
|
use MniConverter;
|
2021-10-05 21:55:15 +00:00
|
|
|
use ShConverter;
|
2020-01-23 18:39:23 +00:00
|
|
|
use ShiConverter;
|
|
|
|
|
use SrConverter;
|
|
|
|
|
use TgConverter;
|
2020-07-27 12:21:43 +00:00
|
|
|
use TlyConverter;
|
2020-01-23 18:39:23 +00:00
|
|
|
use TrivialLanguageConverter;
|
|
|
|
|
use UzConverter;
|
2022-03-09 22:16:22 +00:00
|
|
|
use Wikimedia\ObjectFactory\ObjectFactory;
|
2014-07-26 14:55:14 +00:00
|
|
|
use WuuConverter;
|
2023-08-30 09:21:23 +00:00
|
|
|
use ZghConverter;
|
2020-01-23 18:39:23 +00:00
|
|
|
use ZhConverter;
|
|
|
|
|
|
2020-02-10 14:08:01 +00:00
|
|
|
/**
|
2020-02-12 02:09:33 +00:00
|
|
|
* An interface for creating language converters.
|
2020-02-10 14:08:01 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.35
|
|
|
|
|
* @ingroup Language
|
|
|
|
|
*/
|
2020-01-23 18:39:23 +00:00
|
|
|
class LanguageConverterFactory {
|
|
|
|
|
|
2024-09-14 09:56:07 +00:00
|
|
|
/** @var ILanguageConverter[] */
|
2020-01-23 18:39:23 +00:00
|
|
|
private $cache = [];
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-10-23 21:33:01 +00:00
|
|
|
private $converterList = [
|
|
|
|
|
'ban' => [
|
|
|
|
|
'class' => BanConverter::class,
|
|
|
|
|
],
|
|
|
|
|
'crh' => [
|
|
|
|
|
'class' => CrhConverter::class,
|
|
|
|
|
],
|
|
|
|
|
'gan' => [
|
|
|
|
|
'class' => GanConverter::class,
|
|
|
|
|
],
|
|
|
|
|
'iu' => [
|
|
|
|
|
'class' => IuConverter::class,
|
|
|
|
|
],
|
|
|
|
|
'ku' => [
|
|
|
|
|
'class' => KuConverter::class,
|
|
|
|
|
],
|
2024-05-04 11:46:02 +00:00
|
|
|
'mni' => [
|
|
|
|
|
'class' => MniConverter::class,
|
|
|
|
|
],
|
2021-10-23 21:33:01 +00:00
|
|
|
'shi' => [
|
|
|
|
|
'class' => ShiConverter::class,
|
|
|
|
|
],
|
2021-10-05 21:55:15 +00:00
|
|
|
'sh' => [
|
|
|
|
|
'class' => ShConverter::class,
|
|
|
|
|
],
|
2021-10-23 21:33:01 +00:00
|
|
|
'sr' => [
|
|
|
|
|
'class' => SrConverter::class,
|
|
|
|
|
],
|
|
|
|
|
'tg' => [
|
|
|
|
|
'class' => TgConverter::class,
|
|
|
|
|
],
|
|
|
|
|
'tly' => [
|
|
|
|
|
'class' => TlyConverter::class,
|
|
|
|
|
],
|
|
|
|
|
'uz' => [
|
|
|
|
|
'class' => UzConverter::class,
|
|
|
|
|
],
|
2014-07-26 14:55:14 +00:00
|
|
|
'wuu' => [
|
|
|
|
|
'class' => WuuConverter::class,
|
|
|
|
|
],
|
2023-08-30 09:21:23 +00:00
|
|
|
'zgh' => [
|
|
|
|
|
'class' => ZghConverter::class,
|
|
|
|
|
],
|
2021-10-23 21:33:01 +00:00
|
|
|
'zh' => [
|
|
|
|
|
'class' => ZhConverter::class,
|
|
|
|
|
],
|
2020-01-23 18:39:23 +00:00
|
|
|
];
|
|
|
|
|
|
2021-10-23 21:33:01 +00:00
|
|
|
private const DEFAULT_CONVERTER = [
|
|
|
|
|
'class' => TrivialLanguageConverter::class,
|
|
|
|
|
'services' => [
|
|
|
|
|
'TitleFormatter',
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
private const EN_CONVERTER = [
|
|
|
|
|
'class' => EnConverter::class,
|
|
|
|
|
];
|
|
|
|
|
|
2020-11-25 04:35:49 +00:00
|
|
|
/**
|
2023-12-08 17:41:34 +00:00
|
|
|
* @internal For use by ServiceWiring
|
2020-11-25 04:35:49 +00:00
|
|
|
*/
|
2023-12-08 17:41:34 +00:00
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
|
MainConfigNames::UsePigLatinVariant,
|
|
|
|
|
MainConfigNames::DisableLangConversion,
|
|
|
|
|
MainConfigNames::DisableTitleConversion,
|
|
|
|
|
];
|
2020-11-25 04:35:49 +00:00
|
|
|
|
2023-12-08 17:41:34 +00:00
|
|
|
private ServiceOptions $options;
|
|
|
|
|
private ObjectFactory $objectFactory;
|
2020-11-25 04:35:49 +00:00
|
|
|
|
2020-01-23 18:39:23 +00:00
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* @var callable callback of "() : Language"
|
2020-01-23 18:39:23 +00:00
|
|
|
*/
|
|
|
|
|
private $defaultLanguage;
|
|
|
|
|
|
|
|
|
|
/**
|
2023-12-08 17:41:34 +00:00
|
|
|
* @param ServiceOptions $options
|
2021-10-23 21:33:01 +00:00
|
|
|
* @param ObjectFactory $objectFactory
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param callable $defaultLanguage callback of "() : Language", should return
|
|
|
|
|
* default language. Used in getLanguageConverter when $language is null.
|
2020-02-10 14:08:01 +00:00
|
|
|
*
|
|
|
|
|
* @internal Should be called from MediaWikiServices only.
|
2020-01-23 18:39:23 +00:00
|
|
|
*/
|
2020-11-25 04:35:49 +00:00
|
|
|
public function __construct(
|
2023-12-08 17:41:34 +00:00
|
|
|
ServiceOptions $options,
|
2021-10-23 21:33:01 +00:00
|
|
|
ObjectFactory $objectFactory,
|
2020-11-25 04:35:49 +00:00
|
|
|
callable $defaultLanguage
|
|
|
|
|
) {
|
2023-12-08 17:41:34 +00:00
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
|
$this->options = $options;
|
2021-10-23 21:33:01 +00:00
|
|
|
$this->objectFactory = $objectFactory;
|
2023-12-08 17:41:34 +00:00
|
|
|
if ( $options->get( MainConfigNames::UsePigLatinVariant ) ) {
|
2021-10-23 21:33:01 +00:00
|
|
|
$this->converterList['en'] = self::EN_CONVERTER;
|
2020-01-23 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
$this->defaultLanguage = $defaultLanguage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-02 12:56:25 +00:00
|
|
|
* Returns Converter instance for a given language object
|
2020-01-23 18:39:23 +00:00
|
|
|
*
|
2023-10-02 12:56:25 +00:00
|
|
|
* @param Language|StubUserLang $lang
|
2021-10-23 21:33:01 +00:00
|
|
|
* @return ILanguageConverter
|
2020-01-23 18:39:23 +00:00
|
|
|
*/
|
2022-02-25 05:53:44 +00:00
|
|
|
private function instantiateConverter( $lang ): ILanguageConverter {
|
2021-10-23 21:33:01 +00:00
|
|
|
$code = mb_strtolower( $lang->getCode() );
|
|
|
|
|
$spec = $this->converterList[$code] ?? self::DEFAULT_CONVERTER;
|
|
|
|
|
// ObjectFactory::createObject accepts an array, not just a callable (phan bug)
|
2023-10-02 12:56:25 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeInvalidCallableArrayKey, PhanTypeInvalidCallableArraySize
|
2021-10-23 21:33:01 +00:00
|
|
|
return $this->objectFactory->createObject(
|
|
|
|
|
$spec,
|
|
|
|
|
[
|
|
|
|
|
'assertClass' => ILanguageConverter::class,
|
|
|
|
|
'extraArgs' => [ $lang ],
|
|
|
|
|
]
|
|
|
|
|
);
|
2020-01-23 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide a LanguageConverter for given language
|
|
|
|
|
*
|
2022-02-25 06:54:27 +00:00
|
|
|
* @param Language|StubUserLang|null $language for which a LanguageConverter should be provided.
|
2023-10-02 12:56:25 +00:00
|
|
|
* If it is null, then the LanguageConverter provided for current content language as returned
|
2021-08-23 08:56:24 +00:00
|
|
|
* by the callback provided to the constructor.
|
2020-01-23 18:39:23 +00:00
|
|
|
*
|
|
|
|
|
* @return ILanguageConverter
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getLanguageConverter( $language = null ): ILanguageConverter {
|
2020-01-23 18:39:23 +00:00
|
|
|
$lang = $language ?? ( $this->defaultLanguage )();
|
|
|
|
|
if ( isset( $this->cache[$lang->getCode()] ) ) {
|
|
|
|
|
return $this->cache[$lang->getCode()];
|
|
|
|
|
}
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
|
2021-10-23 21:33:01 +00:00
|
|
|
$converter = $this->instantiateConverter( $lang );
|
2020-01-23 18:39:23 +00:00
|
|
|
$this->cache[$lang->getCode()] = $converter;
|
|
|
|
|
return $converter;
|
|
|
|
|
}
|
2020-11-25 04:35:49 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether to disable language variant conversion.
|
2023-10-02 12:56:25 +00:00
|
|
|
*
|
2020-11-25 04:35:49 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isConversionDisabled() {
|
2023-12-08 17:41:34 +00:00
|
|
|
return $this->options->get( MainConfigNames::DisableLangConversion );
|
2020-11-25 04:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether to disable language variant conversion for links.
|
2023-10-02 12:56:25 +00:00
|
|
|
*
|
2020-11-25 04:35:49 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isLinkConversionDisabled() {
|
2023-12-08 17:41:34 +00:00
|
|
|
return $this->options->get( MainConfigNames::DisableLangConversion ) ||
|
|
|
|
|
// Note that this configuration option is misnamed.
|
|
|
|
|
$this->options->get( MainConfigNames::DisableTitleConversion );
|
2020-11-25 04:35:49 +00:00
|
|
|
}
|
2020-01-23 18:39:23 +00:00
|
|
|
}
|