Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.83% |
23 / 24 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
LanguageConverterFactory | |
95.83% |
23 / 24 |
|
80.00% |
4 / 5 |
8 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
instantiateConverter | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
getLanguageConverter | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
isConversionDisabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isLinkConversionDisabled | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Languages; |
22 | |
23 | use BanConverter; |
24 | use CrhConverter; |
25 | use EnConverter; |
26 | use GanConverter; |
27 | use IuConverter; |
28 | use KuConverter; |
29 | use MediaWiki\Config\ServiceOptions; |
30 | use MediaWiki\Language\ILanguageConverter; |
31 | use MediaWiki\Language\Language; |
32 | use MediaWiki\MainConfigNames; |
33 | use MediaWiki\StubObject\StubUserLang; |
34 | use MniConverter; |
35 | use ShConverter; |
36 | use ShiConverter; |
37 | use SrConverter; |
38 | use TgConverter; |
39 | use TlyConverter; |
40 | use TrivialLanguageConverter; |
41 | use UzConverter; |
42 | use Wikimedia\ObjectFactory\ObjectFactory; |
43 | use WuuConverter; |
44 | use ZghConverter; |
45 | use ZhConverter; |
46 | |
47 | /** |
48 | * An interface for creating language converters. |
49 | * |
50 | * @since 1.35 |
51 | * @ingroup Language |
52 | */ |
53 | class LanguageConverterFactory { |
54 | |
55 | private $cache = []; |
56 | /** |
57 | * @var array |
58 | */ |
59 | private $converterList = [ |
60 | 'ban' => [ |
61 | 'class' => BanConverter::class, |
62 | ], |
63 | 'crh' => [ |
64 | 'class' => CrhConverter::class, |
65 | ], |
66 | 'gan' => [ |
67 | 'class' => GanConverter::class, |
68 | ], |
69 | 'iu' => [ |
70 | 'class' => IuConverter::class, |
71 | ], |
72 | 'ku' => [ |
73 | 'class' => KuConverter::class, |
74 | ], |
75 | 'mni' => [ |
76 | 'class' => MniConverter::class, |
77 | ], |
78 | 'shi' => [ |
79 | 'class' => ShiConverter::class, |
80 | ], |
81 | 'sh' => [ |
82 | 'class' => ShConverter::class, |
83 | ], |
84 | 'sr' => [ |
85 | 'class' => SrConverter::class, |
86 | ], |
87 | 'tg' => [ |
88 | 'class' => TgConverter::class, |
89 | ], |
90 | 'tly' => [ |
91 | 'class' => TlyConverter::class, |
92 | ], |
93 | 'uz' => [ |
94 | 'class' => UzConverter::class, |
95 | ], |
96 | 'wuu' => [ |
97 | 'class' => WuuConverter::class, |
98 | ], |
99 | 'zgh' => [ |
100 | 'class' => ZghConverter::class, |
101 | ], |
102 | 'zh' => [ |
103 | 'class' => ZhConverter::class, |
104 | ], |
105 | ]; |
106 | |
107 | private const DEFAULT_CONVERTER = [ |
108 | 'class' => TrivialLanguageConverter::class, |
109 | 'services' => [ |
110 | 'TitleFormatter', |
111 | ] |
112 | ]; |
113 | |
114 | private const EN_CONVERTER = [ |
115 | 'class' => EnConverter::class, |
116 | ]; |
117 | |
118 | /** |
119 | * @internal For use by ServiceWiring |
120 | */ |
121 | public const CONSTRUCTOR_OPTIONS = [ |
122 | MainConfigNames::UsePigLatinVariant, |
123 | MainConfigNames::DisableLangConversion, |
124 | MainConfigNames::DisableTitleConversion, |
125 | ]; |
126 | |
127 | private ServiceOptions $options; |
128 | private ObjectFactory $objectFactory; |
129 | |
130 | /** |
131 | * @var callable callback of "() : Language" |
132 | */ |
133 | private $defaultLanguage; |
134 | |
135 | /** |
136 | * @param ServiceOptions $options |
137 | * @param ObjectFactory $objectFactory |
138 | * @param callable $defaultLanguage callback of "() : Language", should return |
139 | * default language. Used in getLanguageConverter when $language is null. |
140 | * |
141 | * @internal Should be called from MediaWikiServices only. |
142 | */ |
143 | public function __construct( |
144 | ServiceOptions $options, |
145 | ObjectFactory $objectFactory, |
146 | callable $defaultLanguage |
147 | ) { |
148 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
149 | $this->options = $options; |
150 | $this->objectFactory = $objectFactory; |
151 | if ( $options->get( MainConfigNames::UsePigLatinVariant ) ) { |
152 | $this->converterList['en'] = self::EN_CONVERTER; |
153 | } |
154 | $this->defaultLanguage = $defaultLanguage; |
155 | } |
156 | |
157 | /** |
158 | * Returns Converter instance for a given language object |
159 | * |
160 | * @param Language|StubUserLang $lang |
161 | * @return ILanguageConverter |
162 | */ |
163 | private function instantiateConverter( $lang ): ILanguageConverter { |
164 | $code = mb_strtolower( $lang->getCode() ); |
165 | $spec = $this->converterList[$code] ?? self::DEFAULT_CONVERTER; |
166 | // ObjectFactory::createObject accepts an array, not just a callable (phan bug) |
167 | // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey, PhanTypeInvalidCallableArraySize |
168 | return $this->objectFactory->createObject( |
169 | $spec, |
170 | [ |
171 | 'assertClass' => ILanguageConverter::class, |
172 | 'extraArgs' => [ $lang ], |
173 | ] |