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 WuuConverter;
42 use YueConverter;
43 use ZhConverter;
44 
52 
53  private $cache = [];
57  private $converterList = [
58  'ban' => [
59  'class' => BanConverter::class,
60  ],
61  'crh' => [
62  'class' => CrhConverter::class,
63  ],
64  'gan' => [
65  'class' => GanConverter::class,
66  ],
67  'iu' => [
68  'class' => IuConverter::class,
69  ],
70  'kk' => [
71  'class' => KkConverter::class,
72  ],
73  'ku' => [
74  'class' => KuConverter::class,
75  ],
76  'shi' => [
77  'class' => ShiConverter::class,
78  ],
79  'sh' => [
80  'class' => ShConverter::class,
81  ],
82  'sr' => [
83  'class' => SrConverter::class,
84  ],
85  'tg' => [
86  'class' => TgConverter::class,
87  ],
88  'tly' => [
89  'class' => TlyConverter::class,
90  ],
91  'uz' => [
92  'class' => UzConverter::class,
93  ],
94  'wuu' => [
95  'class' => WuuConverter::class,
96  ],
97  'yue' => [
98  'class' => YueConverter::class,
99  ],
100  'zh' => [
101  'class' => ZhConverter::class,
102  ],
103  ];
104 
105  private const DEFAULT_CONVERTER = [
106  'class' => TrivialLanguageConverter::class,
107  'services' => [
108  'TitleFormatter',
109  ]
110  ];
111 
112  private const EN_CONVERTER = [
113  'class' => EnConverter::class,
114  ];
115 
117  private $objectFactory;
118 
122  private $isConversionDisabled;
123 
127  private $isTitleConversionDisabled;
128 
132  private $defaultLanguage;
133 
144  public function __construct(
145  ObjectFactory $objectFactory,
146  $usePigLatinVariant, $isConversionDisabled, $isTitleConversionDisabled,
147  callable $defaultLanguage
148  ) {
149  $this->objectFactory = $objectFactory;
150  if ( $usePigLatinVariant ) {
151  $this->converterList['en'] = self::EN_CONVERTER;
152  }
153  $this->isConversionDisabled = $isConversionDisabled;
154  $this->isTitleConversionDisabled = $isTitleConversionDisabled;
155  $this->defaultLanguage = $defaultLanguage;
156  }
157 
164  private function instantiateConverter( $lang ): ILanguageConverter {
165  $code = mb_strtolower( $lang->getCode() );
166  $spec = $this->converterList[$code] ?? self::DEFAULT_CONVERTER;
167  // ObjectFactory::createObject accepts an array, not just a callable (phan bug)
168  // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey,PhanTypeInvalidCallableArraySize
169  return $this->objectFactory->createObject(
170  $spec,
171  [
172  'assertClass' => ILanguageConverter::class,
173  'extraArgs' => [ $lang ],
174  ]
175  );
176  }
177 
187  public function getLanguageConverter( $language = null ): ILanguageConverter {
188  $lang = $language ?? ( $this->defaultLanguage )();
189  if ( isset( $this->cache[$lang->getCode()] ) ) {
190  return $this->cache[$lang->getCode()];
191  }
192  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
193  $converter = $this->instantiateConverter( $lang );
194  $this->cache[$lang->getCode()] = $converter;
195  return $converter;
196  }
197 
202  public function isConversionDisabled() {
203  return $this->isConversionDisabled;
204  }
205 
211  public function isTitleConversionDisabled() {
212  wfDeprecated( __METHOD__, '1.36' );
213  return $this->isTitleConversionDisabled;
214  }
215 
220  public function isLinkConversionDisabled() {
221  return $this->isConversionDisabled || $this->isTitleConversionDisabled;
222  }
223 }
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:57
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
Wu language specific code.
Cantonese converter routine.
Chinese converter routine.
Definition: ZhConverter.php:26
The shared interface for all language converters.
if(!isset( $args[0])) $lang