Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / languages / classes / LanguageCrh.php
1 <?php
2 /**
3 * Crimean Tatar (Qırımtatarca) specific code.
4 *
5 * Adapted from https://crh.wikipedia.org/wiki/Qullan%C4%B1c%C4%B1:Don_Alessandro/Translit
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Language
24 */
25
26 /**
27 * Crimean Tatar (Qırımtatarca) converter routines
28 *
29 * @ingroup Language
30 */
31 class CrhConverter extends LanguageConverter {
32 // Defines working character ranges
33 const WORD_BEGINS = '\r\s\"\'\(\)\-<>\[\]\/.,:;!?';
34 const WORD_ENDS = '\r\s\"\'\(\)\-<>\[\]\/.,:;!?';
35
36 // Cyrillic
37 const C_UC = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'; # Crimean Tatar Cyrillic uppercase
38 const C_LC = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'; # Crimean Tatar Cyrillic lowercase
39 const C_CONS_UC = 'БВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ'; # Crimean Tatar Cyrillic + CÑ uppercase consonants
40 const C_CONS_LC = 'бвгджзйклмнпрстфхцчшщcñ'; # Crimean Tatar Cyrillic + CÑ lowercase consonants
41 const C_M_CONS = 'бгкмпшcБГКМПШC'; # Crimean Tatar Cyrillic M-type consonants
42
43 # Crimean Tatar Cyrillic + CÑ consonants
44 const C_CONS = 'бвгджзйклмнпрстфхцчшщcñБВГДЖЗЙКЛМНПРСТФХЦЧШЩCÑ';
45
46 // Latin
47 const L_UC = 'AÂBCÇDEFGĞHIİJKLMNÑOÖPQRSŞTUÜVYZ'; # Crimean Tatar Latin uppercase
48 const L_LC = 'aâbcçdefgğhıijklmnñoöpqrsştuüvyz'; # Crimean Tatar Latin lowercase
49 const L_N_CONS_UC = 'ÇNRSTZ'; # Crimean Tatar Latin N-type upper case consonants
50 const L_N_CONS_LC = 'çnrstz'; # Crimean Tatar Latin N-type lower case consonants
51 const L_N_CONS = 'çnrstzÇNRSTZ'; # Crimean Tatar Latin N-type consonants
52 const L_M_CONS = 'bcgkmpşBCGKMPŞ'; # Crimean Tatar Latin M-type consonants
53 const L_CONS_UC = 'BCÇDFGHJKLMNÑPRSŞTVZ'; # Crimean Tatar Latin uppercase consonants
54 const L_CONS_LC = 'bcçdfghjklmnñprsştvz'; # Crimean Tatar Latin lowercase consonants
55 const L_CONS = 'bcçdfghjklmnñprsştvzBCÇDFGHJKLMNÑPRSŞTVZ'; # Crimean Tatar Latin consonants
56 const L_VOW_UC = 'AÂEIİOÖUÜ'; # Crimean Tatar Latin uppercase vowels
57 const L_VOW = 'aâeıioöuüAÂEIİOÖUÜ'; # Crimean Tatar Latin vowels
58 const L_F_UC = 'EİÖÜ'; # Crimean Tatar Latin uppercase front vowels
59 const L_F = 'eiöüEİÖÜ'; # Crimean Tatar Latin front vowels
60
61 public $mCyrillicToLatin = [
62
63 ## these are independent of location in the word, but have
64 ## to go first so other transforms don't bleed them
65 'гъ' => 'ğ', 'Гъ' => 'Ğ', 'ГЪ' => 'Ğ',
66 'къ' => 'q', 'Къ' => 'Q', 'КЪ' => 'Q',
67 'нъ' => 'ñ', 'Нъ' => 'Ñ', 'НЪ' => 'Ñ',
68 'дж' => 'c', 'Дж' => 'C', 'ДЖ' => 'C',
69
70 'А' => 'A', 'а' => 'a', 'Б' => 'B', 'б' => 'b',
71 'В' => 'V', 'в' => 'v', 'Г' => 'G', 'г' => 'g',
72 'Д' => 'D', 'д' => 'd', 'Ж' => 'J', 'ж' => 'j',
73 'З' => 'Z', 'з' => 'z', 'И' => 'İ', 'и' => 'i',
74 'Й' => 'Y', 'й' => 'y', 'К' => 'K', 'к' => 'k',
75 'Л' => 'L', 'л' => 'l', 'М' => 'M', 'м' => 'm',
76 'Н' => 'N', 'н' => 'n', 'П' => 'P', 'п' => 'p',
77 'Р' => 'R', 'р' => 'r', 'С' => 'S', 'с' => 's',
78 'Т' => 'T', 'т' => 't', 'Ф' => 'F', 'ф' => 'f',
79 'Х' => 'H', 'х' => 'h', 'Ч' => 'Ç', 'ч' => 'ç',
80 'Ш' => 'Ş', 'ш' => 'ş', 'Ы' => 'I', 'ы' => 'ı',
81 'Э' => 'E', 'э' => 'e', 'Е' => 'E', 'е' => 'e',
82 'Я' => 'Â', 'я' => 'â', 'У' => 'U', 'у' => 'u',
83 'О' => 'O', 'о' => 'o',
84
85 'Ё' => 'Yo', 'ё' => 'yo', 'Ю' => 'Yu', 'ю' => 'yu',
86 'Ц' => 'Ts', 'ц' => 'ts', 'Щ' => 'Şç', 'щ' => 'şç',
87 'Ь' => '', 'ь' => '', 'Ъ' => '', 'ъ' => '',
88
89 ];
90
91 public $mLatinToCyrillic = [
92 'Â' => 'Я', 'â' => 'я', 'B' => 'Б', 'b' => 'б',
93 'Ç' => 'Ч', 'ç' => 'ч', 'D' => 'Д', 'd' => 'д',
94 'F' => 'Ф', 'f' => 'ф', 'G' => 'Г', 'g' => 'г',
95 'H' => 'Х', 'h' => 'х', 'I' => 'Ы', 'ı' => 'ы',
96 'İ' => 'И', 'i' => 'и', 'J' => 'Ж', 'j' => 'ж',
97 'K' => 'К', 'k' => 'к', 'L' => 'Л', 'l' => 'л',
98 'M' => 'М', 'm' => 'м', 'N' => 'Н', 'n' => 'н',
99 'O' => 'О', 'o' => 'о', 'P' => 'П', 'p' => 'п',
100 'R' => 'Р', 'r' => 'р', 'S' => 'С', 's' => 'с',
101 'Ş' => 'Ш', 'ş' => 'ш', 'T' => 'Т', 't' => 'т',
102 'V' => 'В', 'v' => 'в', 'Z' => 'З', 'z' => 'з',
103
104 'ya' => 'я', 'Ya' => 'Я', 'YA' => 'Я',
105 'ye' => 'е', 'YE' => 'Е', 'Ye' => 'Е',
106
107 // hack, hack, hack
108 'A' => 'А', 'a' => 'а', 'E' => 'Е', 'e' => 'е',
109 'Ö' => 'О', 'ö' => 'о', 'U' => 'У', 'u' => 'у',
110 'Ü' => 'У', 'ü' => 'у', 'Y' => 'Й', 'y' => 'й',
111
112 'C' => 'Дж', 'c' => 'дж', 'Ğ' => 'Гъ', 'ğ' => 'гъ',
113 'Ñ' => 'Нъ', 'ñ' => 'нъ', 'Q' => 'Къ', 'q' => 'къ',
114
115 ];
116
117 public $mExceptions = [];
118 public $mCyrl2LatnPatterns = [];
119 public $mLatn2CyrlPatterns = [];
120 public $mCyrlCleanUpRegexes = [];
121
122 public $mExceptionsLoaded = false;
123
124 function loadDefaultTables() {
125 $this->mTables = [
126 'crh-latn' => new ReplacementArray( $this->mCyrillicToLatin ),
127 'crh-cyrl' => new ReplacementArray( $this->mLatinToCyrillic ),
128 'crh' => new ReplacementArray()
129 ];
130 }
131
132 function postLoadTables() {
133 $this->loadExceptions();
134 }
135
136 function loadExceptions() {
137 if ( $this->mExceptionsLoaded ) {
138 return;
139 }
140
141 $this->mExceptionsLoaded = true;
142 $crhExceptions = new MediaWiki\Languages\Data\CrhExceptions();
143 list( $this->mExceptions, $this->mCyrl2LatnPatterns, $this->mLatn2CyrlPatterns,
144 $this->mCyrlCleanUpRegexes ) = $crhExceptions->loadExceptions( self::L_LC . self::C_LC,
145 self::L_UC . self::C_UC );
146 }
147
148 /**
149 * A function wrapper:
150 * - if there is no selected variant, leave the link
151 * names as they were
152 * - do not try to find variants for usernames
153 *
154 * @param string &$link
155 * @param Title &$nt
156 * @param bool $ignoreOtherCond
157 */
158 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
159 // check for user namespace
160 if ( is_object( $nt ) ) {
161 $ns = $nt->getNamespace();
162 if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
163 return;
164 }
165 }
166
167 $oldlink = $link;
168 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
169 if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
170 $link = $oldlink;
171 }
172 }
173
174 /**
175 * It translates text into variant, specials:
176 * - ommiting roman numbers
177 *
178 * @param string $text
179 * @param bool $toVariant
180 *
181 * @throws MWException
182 * @return string
183 */
184 function translate( $text, $toVariant ) {
185 $letters = '';
186 switch ( $toVariant ) {
187 case 'crh-cyrl':
188 $letters = self::L_UC . self::L_LC . "\'";
189 break;
190 case 'crh-latn':
191 $letters = self::C_UC . self::C_LC . "";
192 break;
193 default:
194 return $text;
195 break;
196 }
197
198 if ( !$this->mTablesLoaded ) {
199 $this->loadTables();
200 }
201
202 if ( !isset( $this->mTables[$toVariant] ) ) {
203 throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
204 }
205
206 // check for roman numbers like VII, XIX...
207 $roman = '/^M{0,3}(C[DM]|D{0,1}C{0,3})(X[LC]|L{0,1}X{0,3})(I[VX]|V{0,1}I{0,3})$/u';
208
209 # match any sub-string of the relevant letters and convert it
210 $matches = preg_split( '/(\b|^)[^' . $letters . ']+(\b|$)/u',
211 $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
212 $mstart = 0;
213 $ret = '';
214 foreach ( $matches as $m ) {
215 # copy over the non-matching bit
216 $ret .= substr( $text, $mstart, $m[1] - $mstart );
217 # skip certain classes of strings
218
219 if ( array_key_exists( $m[0], $this->mExceptions ) ) {
220 # if it's an exception, just copy down the right answer
221 $ret .= $this->mExceptions[$m[0]];
222 } elseif ( ! $m[0] || # empty strings
223 preg_match( $roman, $m[0] ) || # roman numerals
224 preg_match( '/[^' . $letters . ']/', $m[0] ) # mixed orthography
225 ) {
226 $ret .= $m[0];
227 } else {
228 # convert according to the rules
229 $token = $this->regsConverter( $m[0], $toVariant );
230 $ret .= parent::translate( $token, $toVariant );
231 }
232 $mstart = $m[1] + strlen( $m[0] );
233 }
234
235 # pick up stray quote marks
236 switch ( $toVariant ) {
237 case 'crh-cyrl':
238 $ret = strtr( $ret, [ '“' => '«', '”' => '»', ] );
239 $ret = $this->regsConverter( $ret, 'cyrl-cleanup' );
240 break;
241 case 'crh-latn':
242 $ret = strtr( $ret, [ '«' => '"', '»' => '"', ] );
243 break;
244 }
245
246 return $ret;
247 }
248
249 private function regsConverter( $text, $toVariant ) {
250 if ( $text == '' ) return $text;
251
252 $pat = [];
253 $rep = [];
254 switch ( $toVariant ) {
255 case 'crh-latn':
256 foreach ( $this->mCyrl2LatnPatterns as $pat => $rep ) {
257 $text = preg_replace( $pat, $rep, $text );
258 }
259 return $text;
260 case 'crh-cyrl':
261 foreach ( $this->mLatn2CyrlPatterns as $pat => $rep ) {
262 $text = preg_replace( $pat, $rep, $text );
263 }
264 return $text;
265 case 'cyrl-cleanup':
266 foreach ( $this->mCyrlCleanUpRegexes as $pat => $rep ) {
267 $text = preg_replace( $pat, $rep, $text );
268 }
269 return $text;
270 default:
271 return $text;
272 }
273 }
274
275 }
276
277 /**
278 * Crimean Tatar (Qırımtatarca)
279 *
280 * @ingroup Language
281 */
282 class LanguageCrh extends Language {
283
284 function __construct() {
285 parent::__construct();
286
287 $variants = [ 'crh', 'crh-cyrl', 'crh-latn' ];
288 $variantfallbacks = [
289 'crh' => 'crh-latn',
290 'crh-cyrl' => 'crh-latn',
291 'crh-latn' => 'crh-cyrl',
292 ];
293
294 $this->mConverter = new CrhConverter( $this, 'crh', $variants, $variantfallbacks );
295 }
296 }