Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.25% |
52 / 61 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
CrhConverter | |
85.25% |
52 / 61 |
|
50.00% |
4 / 8 |
25.85 | |
0.00% |
0 / 1 |
getMainCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLanguageVariants | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getVariantsFallbacks | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
loadDefaultTables | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
loadExceptions | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
translate | |
95.45% |
21 / 22 |
|
0.00% |
0 / 1 |
9 | |||
regsConverter | |
88.89% |
16 / 18 |
|
0.00% |
0 / 1 |
8.09 |
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 | use MediaWiki\Language\Language; |
22 | use MediaWiki\Languages\Data\CrhExceptions; |
23 | use MediaWiki\StubObject\StubUserLang; |
24 | |
25 | /** |
26 | * Crimean Tatar (Qırımtatarca) converter routines. |
27 | * |
28 | * Adapted from https://crh.wikipedia.org/wiki/Qullan%C4%B1c%C4%B1:Don_Alessandro/Translit |
29 | * |
30 | * @ingroup Languages |
31 | */ |
32 | class CrhConverter extends LanguageConverterSpecific { |
33 | // Defines working character ranges |
34 | |
35 | // Cyrillic |
36 | # Crimean Tatar Cyrillic uppercase |
37 | public const C_UC = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'; |
38 | # Crimean Tatar Cyrillic lowercase |
39 | public const C_LC = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'; |
40 | # Crimean Tatar Cyrillic + CÑ uppercase consonants |
41 | public const C_CONS_UC = 'БВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ'; |
42 | # Crimean Tatar Cyrillic + CÑ lowercase consonants |
43 | public const C_CONS_LC = 'бвгджзйклмнпрстфхцчшщcñ'; |
44 | # Crimean Tatar Cyrillic M-type consonants |
45 | public const C_M_CONS = 'бгкмшcБГКМШC'; |
46 | |
47 | // Crimean Tatar Cyrillic + CÑ consonants |
48 | public const C_CONS = 'бвгджзйклмнпрстфхцчшщcñБВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ'; |
49 | |
50 | // Latin |
51 | # Crimean Tatar Latin uppercase |
52 | public const L_UC = 'AÂBCÇDEFGĞHIİJKLMNÑOÖPQRSŞTUÜVYZ'; |
53 | # Crimean Tatar Latin lowercase |
54 | public const L_LC = 'aâbcçdefgğhıijklmnñoöpqrsştuüvyz'; |
55 | # Crimean Tatar Latin N-type upper case consonants |
56 | public const L_N_CONS_UC = 'ÇNRSTZ'; |
57 | # Crimean Tatar Latin N-type lower case consonants |
58 | public const L_N_CONS_LC = 'çnrstz'; |
59 | # Crimean Tatar Latin N-type consonants |
60 | public const L_N_CONS = 'çnrstzÇNRSTZ'; |
61 | # Crimean Tatar Latin M-type consonants |
62 | public const L_M_CONS = 'bcgkmpşBCGKMPŞ'; |
63 | # Crimean Tatar Latin uppercase consonants |
64 | public const L_CONS_UC = 'BCÇDFGĞHJKLMNÑPQRSŞTVZ'; |
65 | # Crimean Tatar Latin lowercase consonants |
66 | public const L_CONS_LC = 'bcçdfgğhjklmnñpqrsştvz'; |
67 | # Crimean Tatar Latin consonants |
68 | public const L_CONS = 'bcçdfgğhjklmnñpqrsştvzBCÇDFGĞHJKLMNÑPQRSŞTVZ'; |
69 | # Crimean Tatar Latin uppercase vowels |
70 | public const L_VOW_UC = 'AÂEIİOÖUÜ'; |
71 | # Crimean Tatar Latin vowels |
72 | public const L_VOW = 'aâeıioöuüAÂEIİOÖUÜ'; |
73 | # Crimean Tatar Latin uppercase front vowels |
74 | public const L_F_UC = 'EİÖÜ'; |
75 | # Crimean Tatar Latin front vowels |
76 | public const L_F = 'eiöüEİÖÜ'; |
77 | |
78 | public function getMainCode(): string { |
79 | return 'crh'; |
80 | } |
81 | |
82 | public function getLanguageVariants(): array { |
83 | return [ 'crh', 'crh-cyrl', 'crh-latn' ]; |
84 | } |
85 | |
86 | public function getVariantsFallbacks(): array { |
87 | return [ |
88 | 'crh' => 'crh-latn', |
89 | 'crh-cyrl' => 'crh-latn', |
90 | 'crh-latn' => 'crh-cyrl', |
91 | ]; |
92 | } |
93 | |
94 | /** |
95 | * @param Language|StubUserLang $langobj |
96 | */ |
97 | public function __construct( $langobj ) { |
98 | parent::__construct( $langobj ); |
99 | |
100 | // No point delaying this since they're in code. |
101 | // Waiting until loadDefaultTables() means they never get loaded |
102 | // when the tables themselves are loaded from the cache. |
103 | $this->loadExceptions(); |
104 | } |
105 | |
106 | public const CYRILLIC_TO_LATIN = [ |
107 | ## these are independent of location in the word, but have |
108 | ## to go first so other transforms don't bleed them |
109 | 'гъ' => 'ğ', 'Гъ' => 'Ğ', 'ГЪ' => 'Ğ', |
110 | 'къ' => 'q', 'Къ' => 'Q', 'КЪ' => 'Q', |
111 | 'нъ' => 'ñ', 'Нъ' => 'Ñ', 'НЪ' => 'Ñ', |
112 | 'дж' => 'c', 'Дж' => 'C', 'ДЖ' => 'C', |
113 | |
114 | 'А' => 'A', 'а' => 'a', 'Б' => 'B', 'б' => 'b', |
115 | 'В' => 'V', 'в' => 'v', 'Г' => 'G', 'г' => 'g', |
116 | 'Д' => 'D', 'д' => 'd', 'Ж' => 'J', 'ж' => 'j', |
117 | 'З' => 'Z', 'з' => 'z', 'И' => 'İ', 'и' => 'i', |
118 | 'Й' => 'Y', 'й' => 'y', 'К' => 'K', 'к' => 'k', |
119 | 'Л' => 'L', 'л' => 'l', 'М' => 'M', 'м' => 'm', |
120 | 'Н' => 'N', 'н' => 'n', 'П' => 'P', 'п' => 'p', |
121 | 'Р' => 'R', 'р' => 'r', 'С' => 'S', 'с' => 's', |
122 | 'Т' => 'T', 'т' => 't', 'Ф' => 'F', 'ф' => 'f', |
123 | 'Х' => 'H', 'х' => 'h', 'Ч' => 'Ç', 'ч' => 'ç', |
124 | 'Ш' => 'Ş', 'ш' => 'ş', 'Ы' => 'I', 'ы' => 'ı', |
125 | 'Э' => 'E', 'э' => 'e', 'Е' => 'E', 'е' => 'e', |
126 | 'Я' => 'Â', 'я' => 'â', 'У' => 'U', 'у' => 'u', |
127 | 'О' => 'O', 'о' => 'o', |
128 | |
129 | 'Ё' => 'Yo', 'ё' => 'yo', 'Ю' => 'Yu', 'ю' => 'yu', |
130 | 'Ц' => 'Ts', 'ц' => 'ts', 'Щ' => 'Şç', 'щ' => 'şç', |
131 | 'Ь' => '', 'ь' => '', 'Ъ' => '', 'ъ' => '', |
132 | ]; |
133 | |
134 | public const LATIN_TO_CYRILLIC = [ |
135 | 'Â' => 'Я', 'â' => 'я', 'B' => 'Б', 'b' => 'б', |
136 | 'Ç' => 'Ч', 'ç' => 'ч', 'D' => 'Д', 'd' => 'д', |
137 | 'F' => 'Ф', 'f' => 'ф', 'G' => 'Г', 'g' => 'г', |
138 | 'H' => 'Х', 'h' => 'х', 'I' => 'Ы', 'ı' => 'ы', |
139 | 'İ' => 'И', 'i' => 'и', 'J' => 'Ж', 'j' => 'ж', |
140 | 'K' => 'К', 'k' => 'к', 'L' => 'Л', 'l' => 'л', |
141 | 'M' => 'М', 'm' => 'м', 'N' => 'Н', 'n' => 'н', |
142 | 'O' => 'О', 'o' => 'о', 'P' => 'П', 'p' => 'п', |
143 | 'R' => 'Р', 'r' => 'р', 'S' => 'С', 's' => 'с', |
144 | 'Ş' => 'Ш', 'ş' => 'ш', 'T' => 'Т', 't' => 'т', |
145 | 'V' => 'В', 'v' => 'в', 'Z' => 'З', 'z' => 'з', |
146 | |
147 | 'ya' => 'я', 'Ya' => 'Я', 'YA' => 'Я', |
148 | 'ye' => 'е', 'YE' => 'Е', 'Ye' => 'Е', |
149 | |
150 | // hack, hack, hack |
151 | 'A' => 'А', 'a' => 'а', 'E' => 'Е', 'e' => 'е', |
152 | 'Ö' => 'Ё', 'ö' => 'ё', 'U' => 'У', 'u' => 'у', |
153 | 'Ü' => 'Ю', 'ü' => 'ю', 'Y' => 'Й', 'y' => 'й', |
154 | 'C' => 'Дж', 'c' => 'дж', 'Ğ' => 'Гъ', 'ğ' => 'гъ', |
155 | 'Ñ' => 'Нъ', 'ñ' => 'нъ', 'Q' => 'Къ', 'q' => 'къ', |
156 | ]; |
157 | |
158 | /** @var string[] */ |
159 | private array $mCyrl2LatnExceptions = []; |
160 | /** @var string[] */ |
161 | private array $mLatn2CyrlExceptions = []; |
162 | |
163 | /** @var string[] */ |
164 | private array $mCyrl2LatnPatterns = []; |
165 | /** @var string[] */ |
166 | private array $mLatn2CyrlPatterns = []; |
167 | |
168 | /** @var string[] */ |
169 | private array $mCyrlCleanUpRegexes = []; |
170 | |
171 | private bool $mExceptionsLoaded = false; |
172 | |
173 | /** |
174 | * @inheritDoc |
175 | */ |
176 | protected function loadDefaultTables(): array { |
177 | return [ |
178 | 'crh-latn' => new ReplacementArray( self::CYRILLIC_TO_LATIN ), |
179 | 'crh-cyrl' => new ReplacementArray( self::LATIN_TO_CYRILLIC ), |
180 | 'crh' => new ReplacementArray() |
181 | ]; |
182 | } |
183 | |
184 | private function loadExceptions() { |
185 | if ( $this->mExceptionsLoaded ) { |
186 | return; |
187 | } |
188 | |
189 | $this->mExceptionsLoaded = true; |
190 | $crhExceptions = new CrhExceptions(); |
191 | [ $this->mCyrl2LatnExceptions, $this->mLatn2CyrlExceptions, |
192 | $this->mCyrl2LatnPatterns, $this->mLatn2CyrlPatterns, $this->mCyrlCleanUpRegexes ] = |
193 | $crhExceptions->loadExceptions( self::L_LC . self::C_LC, self::L_UC . self::C_UC ); |
194 | } |
195 | |
196 | /** |
197 | * It translates text into variant, specials: |
198 | * - omitting roman numbers |
199 | * |
200 | * @param string $text |
201 | * @param string $toVariant |
202 | * @return string |
203 | */ |
204 | public function translate( $text, $toVariant ) { |
205 | switch ( $toVariant ) { |
206 | case 'crh-cyrl': |
207 | case 'crh-latn': |
208 | break; |
209 | default: |
210 | return $text; |
211 | } |
212 | |
213 | $this->loadTables(); |
214 | |
215 | if ( !isset( $this->mTables[$toVariant] ) ) { |
216 | throw new LogicException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) ); |
217 | } |
218 | |
219 | switch ( $toVariant ) { |
220 | case 'crh-cyrl': |
221 | /* Check for roman numbers like VII, XIX... |
222 | * Only need to split on Roman numerals when converting to Cyrillic |
223 | * Lookahead assertion ensures $roman doesn't match the empty string, and |
224 | * non-period after the first "Roman" character allows initials to be converted |
225 | */ |
226 | $roman = '(?=[MDCLXVI]([^.]|$))M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})'; |
227 | |
228 | $breaks = '([^\w\x80-\xff])'; |
229 | |
230 | // allow for multiple Roman numerals in a row; rare but it happens |
231 | $romanRegex = '/^' . $roman . '$|^(' . $roman . $breaks . ')+|(' . $breaks . $roman . ')+$|' . |
232 | $breaks . '(' . $roman . $breaks . ')+/'; |
233 | |
234 | $matches = preg_split( $romanRegex, $text, -1, PREG_SPLIT_OFFSET_CAPTURE ); |
235 | $mstart = 0; |
236 | $ret = ''; |
237 | foreach ( $matches as $m ) { |
238 | // copy over Roman numerals |
239 | $ret .= substr( $text, $mstart, (int)$m[1] - $mstart ); |
240 | |
241 | // process everything else |
242 | if ( $m[0] !== '' ) { |
243 | $ret .= $this->regsConverter( $m[0], $toVariant ); |
244 | } |
245 | |
246 | $mstart = (int)$m[1] + strlen( $m[0] ); |
247 | } |
248 | |
249 | return $ret; |
250 | |
251 | default: |
252 | // Just process the whole string in one go |
253 | return $this->regsConverter( $text, $toVariant ); |
254 | } |
255 | } |
256 | |
257 | private function regsConverter( $text, $toVariant ) { |
258 | if ( $text == '' ) { |
259 | return $text; |
260 | } |
261 | |
262 | switch ( $toVariant ) { |
263 | case 'crh-latn': |
264 | $text = strtr( $text, $this->mCyrl2LatnExceptions ); |
265 | foreach ( $this->mCyrl2LatnPatterns as $pat => $rep ) { |
266 | $text = preg_replace( $pat, $rep, $text ); |
267 | } |
268 | $text = parent::translate( $text, $toVariant ); |
269 | return strtr( $text, [ '«' => '"', '»' => '"', ] ); |
270 | |
271 | case 'crh-cyrl': |
272 | $text = strtr( $text, $this->mLatn2CyrlExceptions ); |
273 | foreach ( $this->mLatn2CyrlPatterns as $pat => $rep ) { |
274 | $text = preg_replace( $pat, $rep, $text ); |
275 | } |
276 | $text = parent::translate( $text, $toVariant ); |
277 | $text = strtr( $text, [ '“' => '«', '”' => '»', ] ); |
278 | foreach ( $this->mCyrlCleanUpRegexes as $pat => $rep ) { |
279 | $text = preg_replace( $pat, $rep, $text ); |
280 | } |
281 | return $text; |
282 | |
283 | default: |
284 | return $text; |
285 | } |
286 | } |
287 | } |