MediaWiki  master
CrhConverter.php
Go to the documentation of this file.
1 <?php
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 
80  public function __construct( $langobj ) {
81  $variants = [ 'crh', 'crh-cyrl', 'crh-latn' ];
82  $variantfallbacks = [
83  'crh' => 'crh-latn',
84  'crh-cyrl' => 'crh-latn',
85  'crh-latn' => 'crh-cyrl',
86  ];
87 
88  parent::__construct( $langobj, 'crh',
89  $variants, $variantfallbacks, [] );
90 
91  // No point delaying this since they're in code.
92  // Waiting until loadDefaultTables() means they never get loaded
93  // when the tables themselves are loaded from cache.
94  $this->loadExceptions();
95  }
96 
97  public $mCyrillicToLatin = [
98 
99  ## these are independent of location in the word, but have
100  ## to go first so other transforms don't bleed them
101  'гъ' => 'ğ', 'Гъ' => 'Ğ', 'ГЪ' => 'Ğ',
102  'къ' => 'q', 'Къ' => 'Q', 'КЪ' => 'Q',
103  'нъ' => 'ñ', 'Нъ' => 'Ñ', 'НЪ' => 'Ñ',
104  'дж' => 'c', 'Дж' => 'C', 'ДЖ' => 'C',
105 
106  'А' => 'A', 'а' => 'a', 'Б' => 'B', 'б' => 'b',
107  'В' => 'V', 'в' => 'v', 'Г' => 'G', 'г' => 'g',
108  'Д' => 'D', 'д' => 'd', 'Ж' => 'J', 'ж' => 'j',
109  'З' => 'Z', 'з' => 'z', 'И' => 'İ', 'и' => 'i',
110  'Й' => 'Y', 'й' => 'y', 'К' => 'K', 'к' => 'k',
111  'Л' => 'L', 'л' => 'l', 'М' => 'M', 'м' => 'm',
112  'Н' => 'N', 'н' => 'n', 'П' => 'P', 'п' => 'p',
113  'Р' => 'R', 'р' => 'r', 'С' => 'S', 'с' => 's',
114  'Т' => 'T', 'т' => 't', 'Ф' => 'F', 'ф' => 'f',
115  'Х' => 'H', 'х' => 'h', 'Ч' => 'Ç', 'ч' => 'ç',
116  'Ш' => 'Ş', 'ш' => 'ş', 'Ы' => 'I', 'ы' => 'ı',
117  'Э' => 'E', 'э' => 'e', 'Е' => 'E', 'е' => 'e',
118  'Я' => 'Â', 'я' => 'â', 'У' => 'U', 'у' => 'u',
119  'О' => 'O', 'о' => 'o',
120 
121  'Ё' => 'Yo', 'ё' => 'yo', 'Ю' => 'Yu', 'ю' => 'yu',
122  'Ц' => 'Ts', 'ц' => 'ts', 'Щ' => 'Şç', 'щ' => 'şç',
123  'Ь' => '', 'ь' => '', 'Ъ' => '', 'ъ' => '',
124 
125  ];
126 
127  public $mLatinToCyrillic = [
128  'Â' => 'Я', 'â' => 'я', 'B' => 'Б', 'b' => 'б',
129  'Ç' => 'Ч', 'ç' => 'ч', 'D' => 'Д', 'd' => 'д',
130  'F' => 'Ф', 'f' => 'ф', 'G' => 'Г', 'g' => 'г',
131  'H' => 'Х', 'h' => 'х', 'I' => 'Ы', 'ı' => 'ы',
132  'İ' => 'И', 'i' => 'и', 'J' => 'Ж', 'j' => 'ж',
133  'K' => 'К', 'k' => 'к', 'L' => 'Л', 'l' => 'л',
134  'M' => 'М', 'm' => 'м', 'N' => 'Н', 'n' => 'н',
135  'O' => 'О', 'o' => 'о', 'P' => 'П', 'p' => 'п',
136  'R' => 'Р', 'r' => 'р', 'S' => 'С', 's' => 'с',
137  'Ş' => 'Ш', 'ş' => 'ш', 'T' => 'Т', 't' => 'т',
138  'V' => 'В', 'v' => 'в', 'Z' => 'З', 'z' => 'з',
139 
140  'ya' => 'я', 'Ya' => 'Я', 'YA' => 'Я',
141  'ye' => 'е', 'YE' => 'Е', 'Ye' => 'Е',
142 
143  // hack, hack, hack
144  'A' => 'А', 'a' => 'а', 'E' => 'Е', 'e' => 'е',
145  'Ö' => 'Ё', 'ö' => 'ё', 'U' => 'У', 'u' => 'у',
146  'Ü' => 'Ю', 'ü' => 'ю', 'Y' => 'Й', 'y' => 'й',
147  'C' => 'Дж', 'c' => 'дж', 'Ğ' => 'Гъ', 'ğ' => 'гъ',
148  'Ñ' => 'Нъ', 'ñ' => 'нъ', 'Q' => 'Къ', 'q' => 'къ',
149 
150  ];
151 
154 
155  public $mCyrl2LatnPatterns = [];
156  public $mLatn2CyrlPatterns = [];
157 
158  public $mCyrlCleanUpRegexes = [];
159 
160  public $mExceptionsLoaded = false;
161 
162  protected function loadDefaultTables() {
163  $this->mTables = [
164  'crh-latn' => new ReplacementArray( $this->mCyrillicToLatin ),
165  'crh-cyrl' => new ReplacementArray( $this->mLatinToCyrillic ),
166  'crh' => new ReplacementArray()
167  ];
168  }
169 
170  private function loadExceptions() {
171  if ( $this->mExceptionsLoaded ) {
172  return;
173  }
174 
175  $this->mExceptionsLoaded = true;
176  $crhExceptions = new MediaWiki\Languages\Data\CrhExceptions();
177  list( $this->mCyrl2LatnExceptions, $this->mLatn2CyrlExceptions,
178  $this->mCyrl2LatnPatterns, $this->mLatn2CyrlPatterns, $this->mCyrlCleanUpRegexes ) =
179  $crhExceptions->loadExceptions( self::L_LC . self::C_LC, self::L_UC . self::C_UC );
180  }
181 
192  public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
193  // check for user namespace
194  if ( is_object( $nt ) ) {
195  $ns = $nt->getNamespace();
196  if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
197  return;
198  }
199  }
200 
201  $oldlink = $link;
202  parent::findVariantLink( $link, $nt, $ignoreOtherCond );
203  if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
204  $link = $oldlink;
205  }
206  }
207 
218  public function translate( $text, $toVariant ) {
219  switch ( $toVariant ) {
220  case 'crh-cyrl':
221  case 'crh-latn':
222  break;
223  default:
224  return $text;
225  }
226 
227  $this->loadTables();
228 
229  if ( !isset( $this->mTables[$toVariant] ) ) {
230  throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
231  }
232 
233  switch ( $toVariant ) {
234  case 'crh-cyrl':
235  /* Check for roman numbers like VII, XIX...
236  * Only need to split on Roman numerals when converting to Cyrillic
237  * Lookahead assertion ensures $roman doesn't match the empty string, and
238  * non-period after first "Roman" character allows initials to be converted
239  */
240  $roman = '(?=[MDCLXVI]([^.]|$))M{0,4}(C[DM]|D?C{0,3})(X[LC]|L?X{0,3})(I[VX]|V?I{0,3})';
241 
242  $breaks = '([^\w\x80-\xff])';
243 
244  // allow for multiple Roman numerals in a row; rare but it happens
245  $romanRegex = '/^' . $roman . '$|^(' . $roman . $breaks . ')+|(' . $breaks . $roman . ')+$|' .
246  $breaks . '(' . $roman . $breaks . ')+/';
247 
248  $matches = preg_split( $romanRegex, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
249  $mstart = 0;
250  $ret = '';
251  foreach ( $matches as $m ) {
252  // copy over Roman numerals
253  $ret .= substr( $text, $mstart, $m[1] - $mstart );
254 
255  // process everything else
256  if ( $m[0] !== '' ) {
257  $ret .= $this->regsConverter( $m[0], $toVariant );
258  }
259 
260  $mstart = $m[1] + strlen( $m[0] );
261  }
262 
263  return $ret;
264  default:
265  // Just process the whole string in one go
266  return $this->regsConverter( $text, $toVariant );
267  }
268  }
269 
270  private function regsConverter( $text, $toVariant ) {
271  if ( $text == '' ) return $text;
272 
273  $pat = [];
274  $rep = [];
275  switch ( $toVariant ) {
276  case 'crh-latn':
277  $text = strtr( $text, $this->mCyrl2LatnExceptions );
278  foreach ( $this->mCyrl2LatnPatterns as $pat => $rep ) {
279  $text = preg_replace( $pat, $rep, $text );
280  }
281  $text = parent::translate( $text, $toVariant );
282  $text = strtr( $text, [ '«' => '"', '»' => '"', ] );
283  return $text;
284  case 'crh-cyrl':
285  $text = strtr( $text, $this->mLatn2CyrlExceptions );
286  foreach ( $this->mLatn2CyrlPatterns as $pat => $rep ) {
287  $text = preg_replace( $pat, $rep, $text );
288  }
289  $text = parent::translate( $text, $toVariant );
290  $text = strtr( $text, [ '“' => '«', '”' => '»', ] );
291  foreach ( $this->mCyrlCleanUpRegexes as $pat => $rep ) {
292  $text = preg_replace( $pat, $rep, $text );
293  }
294  return $text;
295  default:
296  return $text;
297  }
298  }
299 
300 }
CrhConverter\$mCyrl2LatnExceptions
$mCyrl2LatnExceptions
Definition: CrhConverter.php:152
MediaWiki\Languages\Data\CrhExceptions
Definition: CrhExceptions.php:14
CrhConverter\L_N_CONS
const L_N_CONS
Definition: CrhConverter.php:59
CrhConverter\loadExceptions
loadExceptions()
Definition: CrhConverter.php:170
CrhConverter\C_M_CONS
const C_M_CONS
Definition: CrhConverter.php:44
CrhConverter\__construct
__construct( $langobj)
Definition: CrhConverter.php:80
CrhConverter\L_CONS_LC
const L_CONS_LC
Definition: CrhConverter.php:65
CrhConverter\$mLatinToCyrillic
$mLatinToCyrillic
Definition: CrhConverter.php:127
CrhConverter\loadDefaultTables
loadDefaultTables()
Load default conversion tables.
Definition: CrhConverter.php:162
CrhConverter\L_N_CONS_UC
const L_N_CONS_UC
Definition: CrhConverter.php:55
MWException
MediaWiki exception.
Definition: MWException.php:26
CrhConverter\L_CONS_UC
const L_CONS_UC
Definition: CrhConverter.php:63
CrhConverter\L_CONS
const L_CONS
Definition: CrhConverter.php:67
$matches
$matches
Definition: NoLocalSettings.php:24
CrhConverter\L_F_UC
const L_F_UC
Definition: CrhConverter.php:73
CrhConverter\L_VOW_UC
const L_VOW_UC
Definition: CrhConverter.php:69
CrhConverter\findVariantLink
findVariantLink(&$link, &$nt, $ignoreOtherCond=false)
A function wrapper:
Definition: CrhConverter.php:192
CrhConverter\regsConverter
regsConverter( $text, $toVariant)
Definition: CrhConverter.php:270
CrhConverter\C_CONS_UC
const C_CONS_UC
Definition: CrhConverter.php:40
CrhConverter\$mCyrillicToLatin
$mCyrillicToLatin
Definition: CrhConverter.php:97
CrhConverter\C_LC
const C_LC
Definition: CrhConverter.php:38
CrhConverter\$mCyrl2LatnPatterns
$mCyrl2LatnPatterns
Definition: CrhConverter.php:155
CrhConverter\$mLatn2CyrlExceptions
$mLatn2CyrlExceptions
Definition: CrhConverter.php:153
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
ReplacementArray
Wrapper around strtr() that holds replacements.
Definition: ReplacementArray.php:24
CrhConverter\L_VOW
const L_VOW
Definition: CrhConverter.php:71
CrhConverter\C_CONS_LC
const C_CONS_LC
Definition: CrhConverter.php:42
CrhConverter\C_CONS
const C_CONS
Definition: CrhConverter.php:47
LanguageConverter\getPreferredVariant
getPreferredVariant()
Get preferred language variant.
Definition: LanguageConverter.php:187
CrhConverter\L_N_CONS_LC
const L_N_CONS_LC
Definition: CrhConverter.php:57
CrhConverter
Crimean Tatar (Qırımtatarca) converter routines.
Definition: CrhConverter.php:31
LanguageConverter\loadTables
loadTables( $fromCache=true)
Load conversion tables either from the cache or the disk.
Definition: LanguageConverter.php:973
CrhConverter\L_LC
const L_LC
Definition: CrhConverter.php:53
CrhConverter\$mExceptionsLoaded
$mExceptionsLoaded
Definition: CrhConverter.php:160
CrhConverter\L_M_CONS
const L_M_CONS
Definition: CrhConverter.php:61
CrhConverter\translate
translate( $text, $toVariant)
It translates text into variant, specials:
Definition: CrhConverter.php:218
CrhConverter\$mCyrlCleanUpRegexes
$mCyrlCleanUpRegexes
Definition: CrhConverter.php:158
CrhConverter\$mLatn2CyrlPatterns
$mLatn2CyrlPatterns
Definition: CrhConverter.php:156
NS_USER
const NS_USER
Definition: Defines.php:71
CrhConverter\L_UC
const L_UC
Definition: CrhConverter.php:51
LanguageConverter
Base class for multi-variant language conversion.
Definition: LanguageConverter.php:35
CrhConverter\C_UC
const C_UC
Definition: CrhConverter.php:36
CrhConverter\L_F
const L_F
Definition: CrhConverter.php:75