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