MediaWiki  REL1_22
LanguageSr.php
Go to the documentation of this file.
00001 <?php
00024 require_once __DIR__ . '/../LanguageConverter.php';
00025 
00035 class SrConverter extends LanguageConverter {
00036     public $mToLatin = array(
00037         'а' => 'a', 'б' => 'b',  'в' => 'v', 'г' => 'g',  'д' => 'd',
00038         'ђ' => 'đ', 'е' => 'e',  'ж' => 'ž', 'з' => 'z',  'и' => 'i',
00039         'ј' => 'j', 'к' => 'k',  'л' => 'l', 'љ' => 'lj', 'м' => 'm',
00040         'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p',  'р' => 'r',
00041         'с' => 's', 'т' => 't',  'ћ' => 'ć', 'у' => 'u',  'ф' => 'f',
00042         'х' => 'h', 'ц' => 'c',  'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
00043 
00044         'А' => 'A', 'Б' => 'B',  'В' => 'V', 'Г' => 'G',  'Д' => 'D',
00045         'Ђ' => 'Đ', 'Е' => 'E',  'Ж' => 'Ž', 'З' => 'Z',  'И' => 'I',
00046         'Ј' => 'J', 'К' => 'K',  'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
00047         'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P',  'Р' => 'R',
00048         'С' => 'S', 'Т' => 'T',  'Ћ' => 'Ć', 'У' => 'U',  'Ф' => 'F',
00049         'Х' => 'H', 'Ц' => 'C',  'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
00050     );
00051 
00052     public $mToCyrillics = array(
00053         'a' => 'а', 'b'  => 'б', 'c' => 'ц', 'č' => 'ч', 'ć'  => 'ћ',
00054         'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f'  => 'ф',
00055         'g' => 'г', 'h'  => 'х', 'i' => 'и', 'j' => 'ј', 'k'  => 'к',
00056         'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
00057         'o' => 'о', 'p'  => 'п', 'r' => 'р', 's' => 'с', 'š'  => 'ш',
00058         't' => 'т', 'u'  => 'у', 'v' => 'в', 'z' => 'з', 'ž'  => 'ж',
00059 
00060         'A' => 'А', 'B'  => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć'  => 'Ћ',
00061         'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F'  => 'Ф',
00062         'G' => 'Г', 'H'  => 'Х', 'I' => 'И', 'J' => 'Ј', 'K'  => 'К',
00063         'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
00064         'O' => 'О', 'P'  => 'П', 'R' => 'Р', 'S' => 'С', 'Š'  => 'Ш',
00065         'T' => 'Т', 'U'  => 'У', 'V' => 'В', 'Z' => 'З', 'Ž'  => 'Ж',
00066 
00067         'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
00068         'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
00069         'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
00070     );
00071 
00072     function loadDefaultTables() {
00073         $this->mTables = array(
00074             'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
00075             'sr-el' => new ReplacementArray( $this->mToLatin ),
00076             'sr' => new ReplacementArray()
00077         );
00078     }
00079 
00091     function parseManualRule( $rule, $flags = array() ) {
00092         if ( in_array( 'T', $flags ) ) {
00093             return parent::parseManualRule( $rule, $flags );
00094         }
00095 
00096         $carray = array();
00097         // otherwise ignore all formatting
00098         foreach ( $this->mVariants as $v ) {
00099             $carray[$v] = $rule;
00100         }
00101 
00102         return $carray;
00103     }
00104 
00115     function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
00116         // check for user namespace
00117         if ( is_object( $nt ) ) {
00118             $ns = $nt->getNamespace();
00119             if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
00120                 return;
00121             }
00122         }
00123 
00124         $oldlink = $link;
00125         parent::findVariantLink( $link, $nt, $ignoreOtherCond );
00126         if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
00127             $link = $oldlink;
00128         }
00129     }
00130 
00140     function autoConvert( $text, $toVariant = false ) {
00141         global $wgTitle;
00142         if ( is_object( $wgTitle ) && $wgTitle->getNamespace() == NS_FILE ) {
00143             $imagename = $wgTitle->getNsText();
00144             if ( preg_match( "/^$imagename:/", $text ) ) {
00145                 return $text;
00146             }
00147         }
00148         return parent::autoConvert( $text, $toVariant );
00149     }
00150 
00161     function translate( $text, $toVariant ) {
00162         $breaks = '[^\w\x80-\xff]';
00163 
00164         // regexp for roman numbers
00165         $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
00166 
00167         $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
00168             . $roman . '$|' . $breaks . $roman . $breaks . '/';
00169 
00170         $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
00171 
00172         $m = array_shift( $matches );
00173         $this->loadTables();
00174         if ( !isset( $this->mTables[$toVariant] ) ) {
00175             throw new MWException( "Broken variant table: "
00176                 . implode( ',', array_keys( $this->mTables ) ) );
00177         }
00178         $ret = $this->mTables[$toVariant]->replace( $m[0] );
00179         $mstart = $m[1] + strlen( $m[0] );
00180         foreach ( $matches as $m ) {
00181             $ret .= substr( $text, $mstart, $m[1] -$mstart );
00182             $ret .= parent::translate( $m[0], $toVariant );
00183             $mstart = $m[1] + strlen( $m[0] );
00184         }
00185 
00186         return $ret;
00187     }
00188 
00200     public function guessVariant( $text, $variant ) {
00201         $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
00202         $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
00203 
00204         if ( $variant == 'sr-ec' ) {
00205             return $numCyrillic > $numLatin;
00206         } elseif ( $variant == 'sr-el' ) {
00207             return $numLatin > $numCyrillic;
00208         } else {
00209             return false;
00210         }
00211 
00212     }
00213 
00214 }
00215 
00221 class LanguageSr extends Language {
00222     function __construct() {
00223         global $wgHooks;
00224 
00225         parent::__construct();
00226 
00227         $variants = array( 'sr', 'sr-ec', 'sr-el' );
00228         $variantfallbacks = array(
00229             'sr' => 'sr-ec',
00230             'sr-ec' => 'sr',
00231             'sr-el' => 'sr',
00232         );
00233 
00234         $flags = array(
00235             'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
00236             'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
00237         );
00238         $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
00239         $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
00240     }
00241 }