MediaWiki  REL1_19
LanguageSr.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once( dirname( __FILE__ ) . '/../LanguageConverter.php' );
00004 require_once( dirname( __FILE__ ) . '/LanguageSr_ec.php' );
00005 require_once( dirname( __FILE__ ) . '/LanguageSr_el.php' );
00006 
00016 class SrConverter extends LanguageConverter {
00017         var $mToLatin = array(
00018                 'а' => 'a', 'б' => 'b',  'в' => 'v', 'г' => 'g',  'д' => 'd',
00019                 'ђ' => 'đ', 'е' => 'e',  'ж' => 'ž', 'з' => 'z',  'и' => 'i',
00020                 'ј' => 'j', 'к' => 'k',  'л' => 'l', 'љ' => 'lj', 'м' => 'm',
00021                 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p',  'р' => 'r',
00022                 'с' => 's', 'т' => 't',  'ћ' => 'ć', 'у' => 'u',  'ф' => 'f',
00023                 'х' => 'h', 'ц' => 'c',  'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
00024 
00025                 'А' => 'A', 'Б' => 'B',  'В' => 'V', 'Г' => 'G',  'Д' => 'D',
00026                 'Ђ' => 'Đ', 'Е' => 'E',  'Ж' => 'Ž', 'З' => 'Z',  'И' => 'I',
00027                 'Ј' => 'J', 'К' => 'K',  'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
00028                 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P',  'Р' => 'R',
00029                 'С' => 'S', 'Т' => 'T',  'Ћ' => 'Ć', 'У' => 'U',  'Ф' => 'F',
00030                 'Х' => 'H', 'Ц' => 'C',  'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
00031         );
00032 
00033         var $mToCyrillics = array(
00034                 'a' => 'а', 'b'  => 'б', 'c' => 'ц', 'č' => 'ч', 'ć'  => 'ћ',
00035                 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f'  => 'ф',
00036                 'g' => 'г', 'h'  => 'х', 'i' => 'и', 'j' => 'ј', 'k'  => 'к',
00037                 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
00038                 'o' => 'о', 'p'  => 'п', 'r' => 'р', 's' => 'с', 'š'  => 'ш',
00039                 't' => 'т', 'u'  => 'у', 'v' => 'в', 'z' => 'з', 'ž'  => 'ж',
00040 
00041                 'A' => 'А', 'B'  => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć'  => 'Ћ',
00042                 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F'  => 'Ф',
00043                 'G' => 'Г', 'H'  => 'Х', 'I' => 'И', 'J' => 'Ј', 'K'  => 'К',
00044                 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
00045                 'O' => 'О', 'P'  => 'П', 'R' => 'Р', 'S' => 'С', 'Š'  => 'Ш',
00046                 'T' => 'Т', 'U'  => 'У', 'V' => 'В', 'Z' => 'З', 'Ž'  => 'Ж',
00047 
00048                 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
00049                 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
00050                 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
00051         );
00052 
00053         function loadDefaultTables() {
00054                 $this->mTables = array(
00055                         'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
00056                         'sr-el' => new ReplacementArray( $this->mToLatin ),
00057                         'sr'    => new ReplacementArray()
00058                 );
00059         }
00060 
00072         function parseManualRule( $rule, $flags = array() ) {
00073                 if ( in_array( 'T', $flags ) ) {
00074                         return parent::parseManualRule( $rule, $flags );
00075                 }
00076 
00077                 $carray = array();
00078                 // otherwise ignore all formatting
00079                 foreach ( $this->mVariants as $v ) {
00080                         $carray[$v] = $rule;
00081                 }
00082 
00083                 return $carray;
00084         }
00085 
00096         function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
00097                 // check for user namespace
00098                 if ( is_object( $nt ) ) {
00099                         $ns = $nt->getNamespace();
00100                         if ( $ns == NS_USER || $ns == NS_USER_TALK )
00101                                 return;
00102                 }
00103 
00104                 $oldlink = $link;
00105                 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
00106                 if ( $this->getPreferredVariant() == $this->mMainLanguageCode )
00107                         $link = $oldlink;
00108         }
00109 
00119         function markNoConversion( $text, $noParse = false ) {
00120                 if ( $noParse || preg_match( "/^https?:\/\/|ftp:\/\/|irc:\/\//", $text ) )
00121                         return parent::markNoConversion( $text );
00122                 return $text;
00123         }
00124 
00134         function autoConvert( $text, $toVariant = false ) {
00135                 global $wgTitle;
00136                 if ( is_object( $wgTitle ) && $wgTitle->getNameSpace() == NS_FILE ) {
00137                         $imagename = $wgTitle->getNsText();
00138                         if ( preg_match( "/^$imagename:/", $text ) ) return $text;
00139                 }
00140                 return parent::autoConvert( $text, $toVariant );
00141         }
00142 
00152         function translate( $text, $toVariant ) {
00153                 $breaks = '[^\w\x80-\xff]';
00154 
00155                 // regexp for roman numbers
00156                 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
00157 
00158                 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks . $roman . '$|' . $breaks . $roman . $breaks . '/';
00159 
00160                 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
00161 
00162                 $m = array_shift( $matches );
00163                 if ( !isset( $this->mTables[$toVariant] ) ) {
00164                         throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
00165                 }
00166                 $ret = $this->mTables[$toVariant]->replace( $m[0] );
00167                 $mstart = $m[1] + strlen( $m[0] );
00168                 foreach ( $matches as $m ) {
00169                         $ret .= substr( $text, $mstart, $m[1] -$mstart );
00170                         $ret .= parent::translate( $m[0], $toVariant );
00171                         $mstart = $m[1] + strlen( $m[0] );
00172                 }
00173 
00174                 return $ret;
00175         }
00176 
00188         public function guessVariant( $text, $variant ) {
00189                 $numCyrillic = preg_match_all("/[шђчћжШЂЧЋЖ]/u", $text, $dummy);
00190                 $numLatin = preg_match_all("/[šđč枊ĐČĆŽ]/u", $text, $dummy);
00191 
00192                 if( $variant == 'sr-ec' ) {
00193                         return (boolean) ($numCyrillic > $numLatin);
00194                 } elseif( $variant == 'sr-el' ) {
00195                         return (boolean) ($numLatin > $numCyrillic);
00196                 } else {
00197                         return false;
00198                 }
00199 
00200         }
00201 
00202 }
00203 
00209 class LanguageSr extends LanguageSr_ec {
00210         function __construct() {
00211                 global $wgHooks;
00212 
00213                 parent::__construct();
00214 
00215                 $variants = array( 'sr', 'sr-ec', 'sr-el' );
00216                 $variantfallbacks = array(
00217                         'sr'    => 'sr-ec',
00218                         'sr-ec' => 'sr',
00219                         'sr-el' => 'sr',
00220                 );
00221 
00222                 $flags = array(
00223                         'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
00224                         'W' => 'W', 'реч'   => 'W', 'reč'   => 'W', 'ријеч' => 'W', 'riječ' => 'W'
00225                 );
00226                 $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
00227                 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
00228         }
00229 
00236         function convertPlural( $count, $forms ) {
00237                 if ( !count( $forms ) ) {
00238                         return '';
00239                 }
00240 
00241                 // If the actual number is not mentioned in the expression, then just two forms are enough:
00242                 // singular for $count == 1
00243                 // plural   for $count != 1
00244                 // For example, "This user belongs to {{PLURAL:$1|one group|several groups}}."
00245                 if ( count( $forms ) === 2 ) {
00246                         return $count == 1 ? $forms[0] : $forms[1];
00247                 }
00248 
00249                 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
00250                 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
00251                 $forms = $this->preConvertPlural( $forms, 3 );
00252 
00253                 if ( $count > 10 && floor( ( $count % 100 ) / 10 ) == 1 ) {
00254                         return $forms[2];
00255                 } else {
00256                         switch ( $count % 10 ) {
00257                                 case 1:  return $forms[0];
00258                                 case 2:
00259                                 case 3:
00260                                 case 4:  return $forms[1];
00261                                 default: return $forms[2];
00262                         }
00263                 }
00264         }
00265 }