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