]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - languages/classes/LanguageSr.php
MediaWiki 1.15.0-scripts
[autoinstalls/mediawiki.git] / languages / classes / LanguageSr.php
1 <?php
2
3 require_once( dirname(__FILE__).'/../LanguageConverter.php' );
4 require_once( dirname(__FILE__).'/LanguageSr_ec.php' );
5 require_once( dirname(__FILE__).'/LanguageSr_el.php' );
6
7 /**
8  * There are two levels of conversion for Serbian: the script level
9  * (Cyrillics <-> Latin), and the variant level (ekavian
10  * <->iyekavian). The two are orthogonal. So we really only need two
11  * dictionaries: one for Cyrillics and Latin, and one for ekavian and
12  * iyekavian.
13  *
14  * @ingroup Language
15  */
16 class SrConverter extends LanguageConverter {
17         var $mToLatin = array(
18                 'а' => 'a', 'б' => 'b',  'в' => 'v', 'г' => 'g',  'д' => 'd',
19                 'ђ' => 'đ', 'е' => 'e',  'ж' => 'ž', 'з' => 'z',  'и' => 'i',
20                 'ј' => 'j', 'к' => 'k',  'л' => 'l', 'љ' => 'lj', 'м' => 'm',
21                 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p',  'р' => 'r',
22                 'с' => 's', 'т' => 't',  'ћ' => 'ć', 'у' => 'u',  'ф' => 'f',
23                 'х' => 'h', 'ц' => 'c',  'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
24
25                 'А' => 'A', 'Б' => 'B',  'В' => 'V', 'Г' => 'G',  'Д' => 'D',
26                 'Ђ' => 'Đ', 'Е' => 'E',  'Ж' => 'Ž', 'З' => 'Z',  'И' => 'I',
27                 'Ј' => 'J', 'К' => 'K',  'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
28                 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P',  'Р' => 'R',
29                 'С' => 'S', 'Т' => 'T',  'Ћ' => 'Ć', 'У' => 'U',  'Ф' => 'F',
30                 'Х' => 'H', 'Ц' => 'C',  'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
31         );
32
33         var $mToCyrillics = array(
34                 'a' => 'а', 'b'  => 'б', 'c' => 'ц', 'č' => 'ч', 'ć'  => 'ћ',
35                 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f'  => 'ф',
36                 'g' => 'г', 'h'  => 'х', 'i' => 'и', 'j' => 'ј', 'k'  => 'к',
37                 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
38                 'o' => 'о', 'p'  => 'п', 'r' => 'р', 's' => 'с', 'š'  => 'ш',
39                 't' => 'т', 'u'  => 'у', 'v' => 'в', 'z' => 'з', 'ž'  => 'ж',
40
41                 'A' => 'А', 'B'  => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć'  => 'Ћ',
42                 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F'  => 'Ф',
43                 'G' => 'Г', 'H'  => 'Х', 'I' => 'И', 'J' => 'Ј', 'K'  => 'К',
44                 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
45                 'O' => 'О', 'P'  => 'П', 'R' => 'Р', 'S' => 'С', 'Š'  => 'Ш',
46                 'T' => 'Т', 'U'  => 'У', 'V' => 'В', 'Z' => 'З', 'Ž'  => 'Ж',
47
48                 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž'=> 'Дж', 'D!Ž'=> 'ДЖ',
49                 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j'=> 'Лј', 'L!J'=> 'ЛЈ',
50                 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j'=> 'Нј', 'N!J'=> 'НЈ'
51         );
52
53         function loadDefaultTables() {
54                 $this->mTables = array(
55                         'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
56                         'sr-el' => new ReplacementArray( $this->mToLatin),
57                         'sr'    => new ReplacementArray()
58                 );
59         }
60
61         /* rules should be defined as -{ekavian | iyekavian-} -or-
62                 -{code:text | code:text | ...}-
63                 update: delete all rule parsing because it's not used
64                         currently, and just produces a couple of bugs
65         */
66         function parseManualRule($rule, $flags=array()) {
67                 if(in_array('T',$flags)){
68                         return parent::parseManualRule($rule, $flags);
69                 }
70
71                 // otherwise ignore all formatting
72                 foreach($this->mVariants as $v) {
73                         $carray[$v] = $rule;
74                 }
75
76                 return $carray;
77         }
78
79         // Do not convert content on talk pages
80         function parserConvert( $text, &$parser ){
81                 if(is_object($parser->getTitle() ) && $parser->getTitle()->isTalkPage())
82                         $this->mDoContentConvert=false;
83                 else
84                         $this->mDoContentConvert=true;
85
86                 return parent::parserConvert($text, $parser );
87         }
88
89         /*
90          * A function wrapper:
91          *   - if there is no selected variant, leave the link
92          *     names as they were
93          *   - do not try to find variants for usernames
94          */
95         function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
96                 // check for user namespace
97                 if(is_object($nt)){
98                         $ns = $nt->getNamespace();
99                         if($ns==NS_USER || $ns==NS_USER_TALK)
100                                 return;
101                 }
102
103                 $oldlink=$link;
104                 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
105                 if($this->getPreferredVariant()==$this->mMainLanguageCode)
106                         $link=$oldlink;
107         }
108
109         /*
110          * We want our external link captions to be converted in variants,
111          * so we return the original text instead -{$text}-, except for URLs
112          */
113         function markNoConversion($text, $noParse=false) {
114                 if($noParse || preg_match("/^https?:\/\/|ftp:\/\/|irc:\/\//",$text))
115                         return parent::markNoConversion($text);
116                 return $text;
117         }
118
119         /*
120          * An ugly function wrapper for parsing Image titles
121          * (to prevent image name conversion)
122          */
123         function autoConvert($text, $toVariant=false) {
124                 global $wgTitle;
125                 if(is_object($wgTitle) && $wgTitle->getNameSpace()==NS_FILE){
126                         $imagename = $wgTitle->getNsText();
127                         if(preg_match("/^$imagename:/",$text)) return $text;
128                 }
129                 return parent::autoConvert($text,$toVariant);
130         }
131
132         /**
133          *  It translates text into variant, specials:
134          *    - ommiting roman numbers
135          */
136         function translate($text, $toVariant){
137                 $breaks = '[^\w\x80-\xff]';
138
139                 // regexp for roman numbers
140                 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
141
142                 $reg = '/^'.$roman.'$|^'.$roman.$breaks.'|'.$breaks.$roman.'$|'.$breaks.$roman.$breaks.'/';
143
144                 $matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE);
145
146                 $m = array_shift($matches);
147                 if( !isset( $this->mTables[$toVariant] ) ) {
148                         throw new MWException( "Broken variant table: " . implode( ',', array_keys( $this->mTables ) ) );
149                 }
150                 $ret = $this->mTables[$toVariant]->replace( $m[0] );
151                 $mstart = $m[1]+strlen($m[0]);
152                 foreach($matches as $m) {
153                         $ret .= substr($text, $mstart, $m[1]-$mstart);
154                         $ret .= parent::translate($m[0], $toVariant);
155                         $mstart = $m[1] + strlen($m[0]);
156                 }
157
158                 return $ret;
159         }
160 }
161
162 /**
163  * @ingroup Language
164  */
165 class LanguageSr extends LanguageSr_ec {
166         function __construct() {
167                 global $wgHooks;
168
169                 parent::__construct();
170
171                 $variants = array('sr', 'sr-ec', 'sr-el');
172                 $variantfallbacks = array(
173                         'sr'    => 'sr-ec',
174                         'sr-ec' => 'sr',
175                         'sr-el' => 'sr',
176                 );
177
178                 $marker = array();//don't mess with these, leave them as they are
179                 $flags = array(
180                         'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
181                         'W' => 'W', 'реч'   => 'W', 'reč'   => 'W', 'ријеч' => 'W', 'riječ' => 'W'
182                 );
183                 $this->mConverter = new SrConverter($this, 'sr', $variants, $variantfallbacks, $marker, $flags);
184                 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
185         }
186 }