MediaWiki  REL1_22
LanguageKk.php
Go to the documentation of this file.
00001 <?php
00024 require_once __DIR__ . '/../LanguageConverter.php';
00025 require_once __DIR__ . '/LanguageKk_cyrl.php';
00026 
00027 define( 'KK_C_UC', 'АӘБВГҒДЕЁЖЗИЙКҚЛМНҢОӨПРСТУҰҮФХҺЦЧШЩЪЫІЬЭЮЯ' ); # Kazakh Cyrillic uppercase
00028 define( 'KK_C_LC', 'аәбвгғдеёжзийкқлмнңоөпрстуұүфхһцчшщъыіьэюя' ); # Kazakh Cyrillic lowercase
00029 define( 'KK_L_UC', 'AÄBCÇDEÉFGĞHIİÏJKLMNÑOÖPQRSŞTUÜVWXYÝZ' ); # Kazakh Latin uppercase
00030 define( 'KK_L_LC', 'aäbcçdeéfgğhıiïjklmnñoöpqrsştuüvwxyýz' ); # Kazakh Latin lowercase
00031 // define( 'KK_A', 'ٴابپتجحدرزسشعفقكلمنڭەوۇۋۆىيچھ' ); # Kazakh Arabic
00032 define( 'H_HAMZA', 'ٴ' ); # U+0674 ARABIC LETTER HIGH HAMZA
00033 // define( 'ZWNJ', '‌' ); # U+200C ZERO WIDTH NON-JOINER
00034 
00040 class KkConverter extends LanguageConverter {
00041 
00042     protected $mCyrl2Latn, $mLatn2Cyrl, $mCyLa2Arab;
00043 
00051     function __construct( $langobj, $maincode,
00052                                 $variants = array(),
00053                                 $variantfallbacks = array(),
00054                                 $flags = array() ) {
00055         parent::__construct( $langobj, $maincode,
00056             $variants, $variantfallbacks, $flags );
00057 
00058         // No point delaying this since they're in code.
00059         // Waiting until loadDefaultTables() means they never get loaded
00060         // when the tables themselves are loaded from cache.
00061         $this->loadRegs();
00062     }
00063 
00064     function loadDefaultTables() {
00065         // require __DIR__."/../../includes/KkConversion.php";
00066         // Placeholder for future implementing. Remove variables declarations
00067         // after generating KkConversion.php
00068         $kk2Cyrl = array();
00069         $kk2Latn = array();
00070         $kk2Arab = array();
00071         $kk2KZ = array();
00072         $kk2TR = array();
00073         $kk2CN = array();
00074 
00075         $this->mTables = array(
00076             'kk-cyrl' => new ReplacementArray( $kk2Cyrl ),
00077             'kk-latn' => new ReplacementArray( $kk2Latn ),
00078             'kk-arab' => new ReplacementArray( $kk2Arab ),
00079             'kk-kz' => new ReplacementArray( array_merge( $kk2Cyrl, $kk2KZ ) ),
00080             'kk-tr' => new ReplacementArray( array_merge( $kk2Latn, $kk2TR ) ),
00081             'kk-cn' => new ReplacementArray( array_merge( $kk2Arab, $kk2CN ) ),
00082             'kk' => new ReplacementArray()
00083         );
00084     }
00085 
00086     function postLoadTables() {
00087         $this->mTables['kk-kz']->merge( $this->mTables['kk-cyrl'] );
00088         $this->mTables['kk-tr']->merge( $this->mTables['kk-latn'] );
00089         $this->mTables['kk-cn']->merge( $this->mTables['kk-arab'] );
00090     }
00091 
00092     function loadRegs() {
00093 
00094         $this->mCyrl2Latn = array(
00095             # # Punctuation
00096             '/№/u' => 'No.',
00097             # # Е after vowels
00098             '/([АӘЕЁИОӨҰҮЭЮЯЪЬ])Е/u' => '$1YE',
00099             '/([АӘЕЁИОӨҰҮЭЮЯЪЬ])е/ui' => '$1ye',
00100             # # leading ЁЮЯЩ
00101             '/^Ё([' . KK_C_UC . ']|$)/u' => 'YO$1', '/^Ё([' . KK_C_LC . ']|$)/u' => 'Yo$1',
00102             '/^Ю([' . KK_C_UC . ']|$)/u' => 'YU$1', '/^Ю([' . KK_C_LC . ']|$)/u' => 'Yu$1',
00103             '/^Я([' . KK_C_UC . ']|$)/u' => 'YA$1', '/^Я([' . KK_C_LC . ']|$)/u' => 'Ya$1',
00104             '/^Щ([' . KK_C_UC . ']|$)/u' => 'ŞÇ$1', '/^Щ([' . KK_C_LC . ']|$)/u' => 'Şç$1',
00105             # # other ЁЮЯ
00106             '/Ё/u' => 'YO', '/ё/u' => 'yo',
00107             '/Ю/u' => 'YU', '/ю/u' => 'yu',
00108             '/Я/u' => 'YA', '/я/u' => 'ya',
00109             '/Щ/u' => 'ŞÇ', '/щ/u' => 'şç',
00110             # # soft and hard signs
00111             '/[ъЪ]/u' => 'ʺ', '/[ьЬ]/u' => 'ʹ',
00112             # # other characters
00113             '/А/u' => 'A', '/а/u' => 'a', '/Ә/u' => 'Ä', '/ә/u' => 'ä',
00114             '/Б/u' => 'B', '/б/u' => 'b', '/В/u' => 'V', '/в/u' => 'v',
00115             '/Г/u' => 'G', '/г/u' => 'g', '/Ғ/u' => 'Ğ', '/ғ/u' => 'ğ',
00116             '/Д/u' => 'D', '/д/u' => 'd', '/Е/u' => 'E', '/е/u' => 'e',
00117             '/Ж/u' => 'J', '/ж/u' => 'j', '/З/u' => 'Z', '/з/u' => 'z',
00118             '/И/u' => 'Ï', '/и/u' => 'ï', '/Й/u' => 'Ý', '/й/u' => 'ý',
00119             '/К/u' => 'K', '/к/u' => 'k', '/Қ/u' => 'Q', '/қ/u' => 'q',
00120             '/Л/u' => 'L', '/л/u' => 'l', '/М/u' => 'M', '/м/u' => 'm',
00121             '/Н/u' => 'N', '/н/u' => 'n', '/Ң/u' => 'Ñ', '/ң/u' => 'ñ',
00122             '/О/u' => 'O', '/о/u' => 'o', '/Ө/u' => 'Ö', '/ө/u' => 'ö',
00123             '/П/u' => 'P', '/п/u' => 'p', '/Р/u' => 'R', '/р/u' => 'r',
00124             '/С/u' => 'S', '/с/u' => 's', '/Т/u' => 'T', '/т/u' => 't',
00125             '/У/u' => 'W', '/у/u' => 'w', '/Ұ/u' => 'U', '/ұ/u' => 'u',
00126             '/Ү/u' => 'Ü', '/ү/u' => 'ü', '/Ф/u' => 'F', '/ф/u' => 'f',
00127             '/Х/u' => 'X', '/х/u' => 'x', '/Һ/u' => 'H', '/һ/u' => 'h',
00128             '/Ц/u' => 'C', '/ц/u' => 'c', '/Ч/u' => 'Ç', '/ч/u' => 'ç',
00129             '/Ш/u' => 'Ş', '/ш/u' => 'ş', '/Ы/u' => 'I', '/ы/u' => 'ı',
00130             '/І/u' => 'İ', '/і/u' => 'i', '/Э/u' => 'É', '/э/u' => 'é',
00131         );
00132 
00133         $this->mLatn2Cyrl = array(
00134             # # Punctuation
00135             '/#|No\./' => '№',
00136             # # Şç
00137             '/ŞÇʹ/u' => 'ЩЬ', '/Şçʹ/u' => 'Щь',
00138             '/Ş[Çç]/u' => 'Щ', '/şç/u' => 'щ',
00139             # # soft and hard signs
00140             '/([' . KK_L_UC . '])ʺ([' . KK_L_UC . '])/u' => '$1Ъ$2',
00141             '/ʺ([' . KK_L_LC . '])/u' => 'ъ$1',
00142             '/([' . KK_L_UC . '])ʹ([' . KK_L_UC . '])/u' => '$1Ь$2',
00143             '/ʹ([' . KK_L_LC . '])/u' => 'ь$1',
00144             '/ʺ/u' => 'ъ',
00145             '/ʹ/u' => 'ь',
00146             # # Ye Yo Yu Ya.
00147             '/Y[Ee]/u' => 'Е', '/ye/u' => 'е',
00148             '/Y[Oo]/u' => 'Ё', '/yo/u' => 'ё',
00149             '/Y[UWuw]/u' => 'Ю', '/y[uw]/u' => 'ю',
00150             '/Y[Aa]/u' => 'Я', '/ya/u' => 'я',
00151             # # other characters
00152             '/A/u' => 'А', '/a/u' => 'а', '/Ä/u' => 'Ә', '/ä/u' => 'ә',
00153             '/B/u' => 'Б', '/b/u' => 'б', '/C/u' => 'Ц', '/c/u' => 'ц',
00154             '/Ç/u' => 'Ч', '/ç/u' => 'ч', '/D/u' => 'Д', '/d/u' => 'д',
00155             '/E/u' => 'Е', '/e/u' => 'е', '/É/u' => 'Э', '/é/u' => 'э',
00156             '/F/u' => 'Ф', '/f/u' => 'ф', '/G/u' => 'Г', '/g/u' => 'г',
00157             '/Ğ/u' => 'Ғ', '/ğ/u' => 'ғ', '/H/u' => 'Һ', '/h/u' => 'һ',
00158             '/I/u' => 'Ы', '/ı/u' => 'ы', '/İ/u' => 'І', '/i/u' => 'і',
00159             '/Ï/u' => 'И', '/ï/u' => 'и', '/J/u' => 'Ж', '/j/u' => 'ж',
00160             '/K/u' => 'К', '/k/u' => 'к', '/L/u' => 'Л', '/l/u' => 'л',
00161             '/M/u' => 'М', '/m/u' => 'м', '/N/u' => 'Н', '/n/u' => 'н',
00162             '/Ñ/u' => 'Ң', '/ñ/u' => 'ң', '/O/u' => 'О', '/o/u' => 'о',
00163             '/Ö/u' => 'Ө', '/ö/u' => 'ө', '/P/u' => 'П', '/p/u' => 'п',
00164             '/Q/u' => 'Қ', '/q/u' => 'қ', '/R/u' => 'Р', '/r/u' => 'р',
00165             '/S/u' => 'С', '/s/u' => 'с', '/Ş/u' => 'Ш', '/ş/u' => 'ш',
00166             '/T/u' => 'Т', '/t/u' => 'т', '/U/u' => 'Ұ', '/u/u' => 'ұ',
00167             '/Ü/u' => 'Ү', '/ü/u' => 'ү', '/V/u' => 'В', '/v/u' => 'в',
00168             '/W/u' => 'У', '/w/u' => 'у', '/Ý/u' => 'Й', '/ý/u' => 'й',
00169             '/X/u' => 'Х', '/x/u' => 'х', '/Z/u' => 'З', '/z/u' => 'з',
00170         );
00171 
00172         $this->mCyLa2Arab = array(
00173             # # Punctuation -> Arabic
00174             '/#|№|No\./u' => '؀', # &#x0600;
00175             '/\,/' => '،', # &#x060C;
00176             '/;/' => '؛', # &#x061B;
00177             '/\?/' => '؟', # &#x061F;
00178             '/%/' => '٪', # &#x066A;
00179             '/\*/' => '٭', # &#x066D;
00180             # # Digits -> Arabic
00181             '/0/' => '۰', # &#x06F0;
00182             '/1/' => '۱', # &#x06F1;
00183             '/2/' => '۲', # &#x06F2;
00184             '/3/' => '۳', # &#x06F3;
00185             '/4/' => '۴', # &#x06F4;
00186             '/5/' => '۵', # &#x06F5;
00187             '/6/' => '۶', # &#x06F6;
00188             '/7/' => '۷', # &#x06F7;
00189             '/8/' => '۸', # &#x06F8;
00190             '/9/' => '۹', # &#x06F9;
00191             # # Cyrillic -> Arabic
00192             '/Аллаһ/ui' => 'ﷲ',
00193             '/([АӘЕЁИОӨҰҮЭЮЯЪЬ])е/ui' => '$1يە',
00194             '/[еэ]/ui' => 'ە', '/[ъь]/ui' => '',
00195             '/[аә]/ui' => 'ا', '/[оө]/ui' => 'و', '/[ұү]/ui' => 'ۇ', '/[ыі]/ui' => 'ى',
00196             '/[и]/ui' => 'ىي', '/ё/ui' => 'يو', '/ю/ui' => 'يۋ', '/я/ui' => 'يا', '/[й]/ui' => 'ي',
00197             '/ц/ui' => 'تس', '/щ/ui' => 'شش',
00198             '/һ/ui' => 'ح', '/ч/ui' => 'تش',
00199             # '/һ/ui' => 'ھ', '/ч/ui' => 'چ',
00200             '/б/ui' => 'ب', '/в/ui' => 'ۆ', '/г/ui' => 'گ', '/ғ/ui' => 'ع',
00201             '/д/ui' => 'د', '/ж/ui' => 'ج', '/з/ui' => 'ز', '/к/ui' => 'ك',
00202             '/қ/ui' => 'ق', '/л/ui' => 'ل', '/м/ui' => 'م', '/н/ui' => 'ن',
00203             '/ң/ui' => 'ڭ', '/п/ui' => 'پ', '/р/ui' => 'ر', '/с/ui' => 'س',
00204             '/т/ui' => 'ت', '/у/ui' => 'ۋ', '/ф/ui' => 'ف', '/х/ui' => 'ح',
00205             '/ш/ui' => 'ش',
00206             # # Latin -> Arabic // commented for now...
00207             /*'/Allah/ui' => 'ﷲ',
00208             '/[eé]/ui' => 'ە', '/[yý]/ui' => 'ي', '/[ʺʹ]/ui' => '',
00209             '/[aä]/ui' => 'ا', '/[oö]/ui' => 'و', '/[uü]/ui' => 'ۇ',
00210             '/[ï]/ui' => 'ىي', '/[ıIiİ]/u' => 'ى',
00211             '/c/ui' => 'تس',
00212             '/ç/ui' => 'تش', '/h/ui' => 'ح',
00213             #'/ç/ui' => 'چ', '/h/ui' => 'ھ',
00214             '/b/ui' => 'ب','/d/ui' => 'د',
00215             '/f/ui' => 'ف', '/g/ui' => 'گ', '/ğ/ui' => 'ع',
00216             '/j/ui' => 'ج', '/k/ui' => 'ك', '/l/ui' => 'ل', '/m/ui' => 'م',
00217             '/n/ui' => 'ن', '/ñ/ui' => 'ڭ', '/p/ui' => 'پ', '/q/ui' => 'ق',
00218             '/r/ui' => 'ر', '/s/ui' => 'س', '/ş/ui' => 'ش', '/t/ui' => 'ت',
00219             '/v/ui' => 'ۆ', '/w/ui' => 'ۋ', '/x/ui' => 'ح', '/z/ui' => 'ز',*/
00220         );
00221     }
00222 
00234     function parseManualRule( $rule, $flags = array() ) {
00235         if ( in_array( 'T', $flags ) ) {
00236             return parent::parseManualRule( $rule, $flags );
00237         }
00238 
00239         $carray = array();
00240         // otherwise ignore all formatting
00241         foreach ( $this->mVariants as $v ) {
00242             $carray[$v] = $rule;
00243         }
00244 
00245         return $carray;
00246     }
00247 
00258     function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
00259         // check for user namespace
00260         if ( is_object( $nt ) ) {
00261             $ns = $nt->getNamespace();
00262             if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
00263                 return;
00264             }
00265         }
00266 
00267         $oldlink = $link;
00268         parent::findVariantLink( $link, $nt, $ignoreOtherCond );
00269         if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
00270             $link = $oldlink;
00271         }
00272     }
00273 
00283     function autoConvert( $text, $toVariant = false ) {
00284         global $wgTitle;
00285         if ( is_object( $wgTitle ) && $wgTitle->getNamespace() == NS_FILE ) {
00286             $imagename = $wgTitle->getNsText();
00287             if ( preg_match( "/^$imagename:/", $text ) ) {
00288                 return $text;
00289             }
00290         }
00291         return parent::autoConvert( $text, $toVariant );
00292     }
00293 
00302     function translate( $text, $toVariant ) {
00303         global $wgLanguageCode;
00304         $text = parent::translate( $text, $toVariant );
00305 
00306         switch ( $toVariant ) {
00307             case 'kk-cyrl':
00308             case 'kk-kz':
00309                 $letters = KK_L_UC . KK_L_LC . 'ʺʹ#0123456789';
00310                 $wgLanguageCode = 'kk';
00311                 break;
00312             case 'kk-latn':
00313             case 'kk-tr':
00314                 $letters = KK_C_UC . KK_C_LC . '№0123456789';
00315                 $wgLanguageCode = 'kk-Latn';
00316                 break;
00317             case 'kk-arab':
00318             case 'kk-cn':
00319                 $letters = KK_C_UC . KK_C_LC . /*KK_L_UC.KK_L_LC.'ʺʹ'.*/',;\?%\*№0123456789';
00320                 $wgLanguageCode = 'kk-Arab';
00321                 break;
00322             default:
00323                 $wgLanguageCode = 'kk';
00324                 return $text;
00325         }
00326         // disable conversion variables like $1, $2...
00327         $varsfix = '\$[0-9]';
00328 
00329         $matches = preg_split( '/' . $varsfix . '[^' . $letters . ']+/u', $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
00330         $mstart = 0;
00331         $ret = '';
00332         foreach ( $matches as $m ) {
00333             $ret .= substr( $text, $mstart, $m[1] -$mstart );
00334             $ret .= $this->regsConverter( $m[0], $toVariant );
00335             $mstart = $m[1] + strlen( $m[0] );
00336         }
00337         return $ret;
00338     }
00339 
00345     function regsConverter( $text, $toVariant ) {
00346         if ( $text == '' ) {
00347             return $text;
00348         }
00349 
00350         switch ( $toVariant ) {
00351             case 'kk-arab':
00352             case 'kk-cn':
00353                 $letters = KK_C_LC . KK_C_UC/*.KK_L_LC.KK_L_UC*/;
00354                 $front = 'әөүіӘӨҮІ'/*.'äöüiÄÖÜİ'*/;
00355                 $excludes = 'еэгғкқЕЭГҒКҚ'/*.'eégğkqEÉGĞKQ'*/;
00356                 // split text to words
00357                 $matches = preg_split( '/[\b\s\-\.:]+/', $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
00358                 $mstart = 0;
00359                 $ret = '';
00360                 foreach ( $matches as $m ) {
00361                     $ret .= substr( $text, $mstart, $m[1] - $mstart );
00362                     // is matched the word to front vowels?
00363                     // exclude a words matched to е, э, г, к, к, қ,
00364                     // them should be without hamza
00365                     if ( preg_match( '/[' . $front . ']/u', $m[0] ) && !preg_match( '/[' . $excludes . ']/u', $m[0] ) ) {
00366                         $ret .= preg_replace( '/[' . $letters . ']+/u', H_HAMZA . '$0', $m[0] );
00367                     } else {
00368                         $ret .= $m[0];
00369                     }
00370                     $mstart = $m[1] + strlen( $m[0] );
00371                 }
00372                 $text =& $ret;
00373                 foreach ( $this->mCyLa2Arab as $pat => $rep ) {
00374                     $text = preg_replace( $pat, $rep, $text );
00375                 }
00376                 return $text;
00377                 break;
00378             case 'kk-latn':
00379             case 'kk-tr':
00380                 foreach ( $this->mCyrl2Latn as $pat => $rep ) {
00381                     $text = preg_replace( $pat, $rep, $text );
00382                 }
00383                 return $text;
00384                 break;
00385             case 'kk-cyrl':
00386             case 'kk-kz':
00387                 foreach ( $this->mLatn2Cyrl as $pat => $rep ) {
00388                     $text = preg_replace( $pat, $rep, $text );
00389                 }
00390                 return $text;
00391                 break;
00392             default:
00393                 return $text;
00394         }
00395     }
00396 
00401     function convertCategoryKey( $key ) {
00402         return $this->autoConvert( $key, 'kk' );
00403     }
00404 
00405 }
00406 
00413 class LanguageKk extends LanguageKk_cyrl {
00414 
00415     function __construct() {
00416         global $wgHooks;
00417         parent::__construct();
00418 
00419         $variants = array( 'kk', 'kk-cyrl', 'kk-latn', 'kk-arab', 'kk-kz', 'kk-tr', 'kk-cn' );
00420         $variantfallbacks = array(
00421             'kk' => 'kk-cyrl',
00422             'kk-cyrl' => 'kk',
00423             'kk-latn' => 'kk',
00424             'kk-arab' => 'kk',
00425             'kk-kz' => 'kk-cyrl',
00426             'kk-tr' => 'kk-latn',
00427             'kk-cn' => 'kk-arab'
00428         );
00429 
00430         $this->mConverter = new KkConverter( $this, 'kk', $variants, $variantfallbacks );
00431 
00432         $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
00433     }
00434 
00442     function ucfirst( $string ) {
00443         $variant = $this->getPreferredVariant();
00444         if ( ( $variant == 'kk-latn' || $variant == 'kk-tr' ) && $string[0] == 'i' ) {
00445             $string = 'İ' . substr( $string, 1 );
00446         } else {
00447             $string = parent::ucfirst( $string );
00448         }
00449         return $string;
00450     }
00451 
00459     function lcfirst( $string ) {
00460         $variant = $this->getPreferredVariant();
00461         if ( ( $variant == 'kk-latn' || $variant == 'kk-tr' ) && $string[0] == 'I' ) {
00462             $string = 'ı' . substr( $string, 1 );
00463         } else {
00464             $string = parent::lcfirst( $string );
00465         }
00466         return $string;
00467     }
00468 
00474     function convertGrammar( $word, $case ) {
00475         wfProfileIn( __METHOD__ );
00476 
00477         $variant = $this->getPreferredVariant();
00478         switch ( $variant ) {
00479             case 'kk-arab':
00480             case 'kk-cn':
00481                 $word = parent::convertGrammarKk_arab( $word, $case );
00482                 break;
00483             case 'kk-latn':
00484             case 'kk-tr':
00485                 $word = parent::convertGrammarKk_latn( $word, $case );
00486                 break;
00487             case 'kk-cyrl':
00488             case 'kk-kz':
00489             case 'kk':
00490             default:
00491                 $word = parent::convertGrammarKk_cyrl( $word, $case );
00492         }
00493 
00494         wfProfileOut( __METHOD__ );
00495         return $word;
00496     }
00497 }