MediaWiki  master
LanguageConverterFactory.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Languages;
22 
23 use BanConverter;
24 use CrhConverter;
25 use EnConverter;
26 use GanConverter;
28 use IuConverter;
29 use KkConverter;
30 use KuConverter;
31 use Language;
33 use ShConverter;
34 use ShiConverter;
35 use SrConverter;
36 use TgConverter;
37 use TlyConverter;
39 use UzConverter;
40 use Wikimedia\ObjectFactory\ObjectFactory;
41 use ZhConverter;
42 
50 
51  private $cache = [];
55  private $converterList = [
56  'ban' => [
57  'class' => BanConverter::class,
58  ],
59  'crh' => [
60  'class' => CrhConverter::class,
61  ],
62  'gan' => [
63  'class' => GanConverter::class,
64  ],
65  'iu' => [
66  'class' => IuConverter::class,
67  ],
68  'kk' => [
69  'class' => KkConverter::class,
70  ],
71  'ku' => [
72  'class' => KuConverter::class,
73  ],
74  'shi' => [
75  'class' => ShiConverter::class,
76  ],
77  'sh' => [
78  'class' => ShConverter::class,
79  ],
80  'sr' => [
81  'class' => SrConverter::class,
82  ],
83  'tg' => [
84  'class' => TgConverter::class,
85  ],
86  'tly' => [
87  'class' => TlyConverter::class,
88  ],
89  'uz' => [
90  'class' => UzConverter::class,
91  ],
92  'zh' => [
93  'class' => ZhConverter::class,
94  ],
95  ];
96 
97  private const DEFAULT_CONVERTER = [
98  'class' => TrivialLanguageConverter::class,
99  'services' => [
100  'TitleFormatter',
101  ]
102  ];
103 
104  private const EN_CONVERTER = [
105  'class' => EnConverter::class,
106  ];
107 
109  private $objectFactory;
110 
114  private $isConversionDisabled;
115 
119  private $isTitleConversionDisabled;
120 
124  private $defaultLanguage;
125 
136  public function __construct(
137  ObjectFactory $objectFactory,
138  $usePigLatinVariant, $isConversionDisabled, $isTitleConversionDisabled,
139  callable $defaultLanguage
140  ) {
141  $this->objectFactory = $objectFactory;
142  if ( $usePigLatinVariant ) {
143  $this->converterList['en'] = self::EN_CONVERTER;
144  }
145  $this->isConversionDisabled = $isConversionDisabled;
146  $this->isTitleConversionDisabled = $isTitleConversionDisabled;
147  $this->defaultLanguage = $defaultLanguage;
148  }
149 
156  private function instantiateConverter( $lang ): ILanguageConverter {
157  $code = mb_strtolower( $lang->getCode() );
158  $spec = $this->converterList[$code] ?? self::DEFAULT_CONVERTER;
159  // ObjectFactory::createObject accepts an array, not just a callable (phan bug)
160  // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey,PhanTypeInvalidCallableArraySize
161  return $this->objectFactory->createObject(
162  $spec,
163  [
164  'assertClass' => ILanguageConverter::class,
165  'extraArgs' => [ $lang ],
166  ]
167  );
168  }
169 
179  public function getLanguageConverter( $language = null ): ILanguageConverter {
180  $lang = $language ?? ( $this->defaultLanguage )();
181  if ( isset( $this->cache[$lang->getCode()] ) ) {
182  return $this->cache[$lang->getCode()];
183  }
184  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
185  $converter = $this->instantiateConverter( $lang );
186  $this->cache[$lang->getCode()] = $converter;
187  return $converter;
188  }
189 
194  public function isConversionDisabled() {
195  return $this->isConversionDisabled;
196  }
197 
203  public function isTitleConversionDisabled() {
204  wfDeprecated( __METHOD__, '1.36' );
205  return $this->isTitleConversionDisabled;
206  }
207 
212  public function isLinkConversionDisabled() {
213  return $this->isConversionDisabled || $this->isTitleConversionDisabled;
214  }
215 }
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Balinese specific code.
Crimean Tatar (Qırımtatarca) converter routines.
English specific converter routines.
Definition: EnConverter.php:26
Gan Chinese specific code.
Inuktitut specific code.
Definition: IuConverter.php:35
Kazakh (Қазақша) converter routines.
Definition: KkConverter.php:34
Kurdish converter routines.
Definition: KuConverter.php:26
Base class for language-specific code.
Definition: Language.php:56
An interface for creating language converters.
isTitleConversionDisabled()
Whether to disable language variant conversion for titles.
__construct(ObjectFactory $objectFactory, $usePigLatinVariant, $isConversionDisabled, $isTitleConversionDisabled, callable $defaultLanguage)
isLinkConversionDisabled()
Whether to disable language variant conversion for links.
isConversionDisabled()
Whether to disable language variant conversion.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Stub object for the user language.
Converts Serbo-Croatian from Latin script to Cyrillic script.
Definition: ShConverter.php:26
Shilha specific code.
Serbian (Српски / Srpski) specific code.
Definition: SrConverter.php:32
Tajik (Тоҷикӣ) specific code, converting Tajiki to Latin orthography.
Definition: TgConverter.php:26
Talysh specific code.
A trivial language converter.
Uzbek specific code.
Definition: UzConverter.php:26
Chinese converter routine.
Definition: ZhConverter.php:26
The shared interface for all language converters.
if(!isset( $args[0])) $lang