* Restructured
[lhc/web/wiklou.git] / languages / LanguageSr.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Language
5 */
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 require_once( "LanguageConverter.php" );
15 require_once( "LanguageSr_ec.php" );
16 require_once( "LanguageSr_el.php" );
17 require_once( "LanguageSr_jc.php" );
18 require_once( "LanguageSr_jl.php" );
19
20 class SrConverter extends LanguageConverter {
21 var $mToLatin = array(
22 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
23 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
24 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
25 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
26 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
27 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
28
29 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
30 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
31 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
32 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
33 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
34 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
35 );
36
37 var $mToCyrillics = array(
38 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
39 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
40 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
41 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
42 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
43 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
44
45 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
46 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
47 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
48 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
49 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
50 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
51
52 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž'=> 'Дж', 'D!Ž'=> 'ДЖ',
53 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j'=> 'Лј', 'L!J'=> 'ЛЈ',
54 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j'=> 'Нј', 'N!J'=> 'НЈ'
55 );
56
57 function loadDefaultTables() {
58 $this->mTables = array();
59 $this->mTables['sr-ec'] = $this->mToCyrillics;
60 $this->mTables['sr-jc'] = $this->mToCyrillics;
61 $this->mTables['sr-el'] = $this->mToLatin;
62 $this->mTables['sr-jl'] = $this->mToLatin;
63 $this->mTables['sr'] = array();
64 }
65
66 /* rules should be defined as -{ekavian | iyekavian-} -or-
67 -{code:text | code:text | ...}-
68 */
69 function parseManualRule($rule, $flags) {
70
71 $echoices = preg_split("/(<[^>]+>)/",$rule,-1,PREG_SPLIT_DELIM_CAPTURE);
72 $choices = array();
73
74 // check if we did a split across an HTML tag
75 // if so, glue them back together
76
77 $ctold = '';
78 foreach($echoices as $ct){
79 if($ct=='');
80 else if(preg_match('/<[^>]+>/',$ct)){
81 $ctold.=$ct;
82 }
83 else{
84 $c = explode($this->mMarkup['varsep'],$ct);
85 if(count($c)>1){
86 $choices[]=$ctold.array_shift($c);
87 $ctold=array_pop($c);
88 $choices=array_merge($choices,$c);
89 }
90 else $ctold.=array_pop($c);
91 }
92 }
93 if($ctold!='') $choices[]=$ctold;
94
95 $carray = array();
96 if(sizeof($choices) == 1) {
97 if(in_array('W', $flags)) {
98 $carray['sr'] = $this->autoConvert($choices[0], 'sr-ec');
99 $carray['sr-ec'] = $this->autoConvert($choices[0], 'sr-ec');
100 $carray['sr-jc'] = $this->autoConvert($choices[0], 'sr-jc');
101 $carray['sr-el'] = $this->autoConvert($choices[0], 'sr-el');
102 $carray['sr-jl'] = $this->autoConvert($choices[0], 'sr-jl');
103 }
104 foreach($this->mVariants as $v) {
105 $carray[$v] = $choices[0];
106 }
107 return $carray;
108 }
109
110 /* detect which format is used, also trim the choices*/
111 $n=0;
112 foreach($choices as $c=>$t) {
113 if(strpos($t, $this->mMarkup['codesep']) !== false) { $n++; }
114 $choices[$c] = trim($t);
115 }
116 /* the -{code:text | ...}- format */
117 if($n == sizeof($choices)) {
118 foreach($choices as $c) {
119 list($code, $text) = explode($this->mMarkup['codesep'], $c);
120 $carray[trim($code)] = trim($text);
121 }
122 return $carray;
123 }
124
125 /* the two choice format -{choice1; choice2}-*/
126 if(sizeof($choices == 2) && $n==0) {
127 if(in_array('S', $flags)) {
128 // conversion between Cyrillics and Latin
129 $carray['sr'] = $carray['sr-ec'] =$carray['sr-jc'] = $choices[0];
130 $carray['sr-el'] =$carray['sr-jl'] = $choices[1];
131 }
132 else {
133 $carray['sr'] = $this->autoConvert($choices[0], 'sr-ec');
134 $carray['sr-ec'] = $this->autoConvert($choices[0], 'sr-ec');
135 $carray['sr-jc'] = $this->autoConvert($choices[1], 'sr-jc');
136 $carray['sr-el'] = $this->autoConvert($choices[0], 'sr-el');
137 $carray['sr-jl'] = $this->autoConvert($choices[1], 'sr-jl');
138 }
139 return $carray;
140 }
141 return $carray;
142 }
143
144 /*
145 * Override function from LanguageConvertor
146 * Additional checks:
147 * - There should be no conversion for Talk pages
148 */
149 function getPreferredVariant(){
150 global $wgTitle;
151 if($wgTitle!=NULL && $wgTitle->isTalkPage()){
152 return $this->mMainLanguageCode;
153 }
154 return parent::getPreferredVariant();
155 }
156
157
158 /*
159 * A function wrapper, if there is no selected variant,
160 * leave the link names as they were
161 */
162 function findVariantLink( &$link, &$nt ) {
163 $oldlink=$link;
164 parent::findVariantLink($link,$nt);
165 if($this->getPreferredVariant()==$this->mMainLanguageCode)
166 $link=$oldlink;
167 }
168
169
170 /*
171 * We want our external link captions to be converted in variants,
172 * so we return the original text instead -{$text}-, except for URLs
173 */
174 function markNoConversion($text) {
175 if(preg_match("/^https?:\/\/|ftp:\/\/|irc:\/\//",$text))
176 return parent::markNoConversion($text);
177 return $text;
178 }
179
180 /*
181 * An ugly function wrapper for parsing Image titles
182 * (to prevent image name conversion)
183 */
184 function autoConvert($text, $toVariant=false) {
185 global $wgTitle;
186 if($wgTitle->getNameSpace()==NS_IMAGE){
187 $imagename = $wgTitle->getNsText();
188 if(preg_match("/^$imagename:/",$text)) return $text;
189 }
190 return parent::autoConvert($text,$toVariant);
191 }
192
193
194 }
195
196 class LanguageSr extends LanguageSr_ec {
197 function __construct() {
198 global $wgHooks;
199 $variants = array('sr', 'sr-ec', 'sr-jc', 'sr-el', 'sr-jl');
200 $variantfallbacks = array(
201 'sr' => 'sr-ec',
202 'sr-ec' => 'sr-jc',
203 'sr-jc' => 'sr-ec',
204 'sr-el' => 'sr-jl',
205 'sr-jl' => 'sr-el'
206 );
207 $marker = array();//don't mess with these, leave them as they are
208 $flags = array(
209 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
210 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
211 );
212 $this->mConverter = new SrConverter($this, 'sr', $variants, $variantfallbacks, $marker, $flags);
213 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
214 }
215 function getVariantname( $code ) {
216 return wfMsg( "variantname-$code" );
217 }
218
219 function linkTrail() {
220 return "/^([abvgdđežzijklljmnnjoprstćufhcčdžšабвгдђежзијклљмнњопрстћуфхцчџш]+)(.*)$/usD";
221 }
222
223 }
224 ?>