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