Merge "Remove "or other" option for stubtreshold"
[lhc/web/wiklou.git] / languages / classes / LanguageSr.php
1 <?php
2 /**
3 * Serbian (Српски / Srpski) specific code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Language
22 */
23
24 require_once __DIR__ . '/../LanguageConverter.php';
25
26 /**
27 * There are two levels of conversion for Serbian: the script level
28 * (Cyrillics <-> Latin), and the variant level (ekavian
29 * <->iyekavian). The two are orthogonal. So we really only need two
30 * dictionaries: one for Cyrillics and Latin, and one for ekavian and
31 * iyekavian.
32 *
33 * @ingroup Language
34 */
35 class SrConverter extends LanguageConverter {
36 public $mToLatin = array(
37 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
38 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
39 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
40 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
41 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
42 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
43
44 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
45 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
46 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
47 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
48 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
49 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
50 );
51
52 public $mToCyrillics = array(
53 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
54 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
55 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
56 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
57 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
58 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
59
60 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
61 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
62 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
63 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
64 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
65 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
66
67 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
68 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
69 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
70 );
71
72 function loadDefaultTables() {
73 $this->mTables = array(
74 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
75 'sr-el' => new ReplacementArray( $this->mToLatin ),
76 'sr' => new ReplacementArray()
77 );
78 }
79
80 /**
81 * rules should be defined as -{ekavian | iyekavian-} -or-
82 * -{code:text | code:text | ...}-
83 *
84 * update: delete all rule parsing because it's not used
85 * currently, and just produces a couple of bugs
86 *
87 * @param $rule string
88 * @param $flags array
89 * @return array
90 */
91 function parseManualRule( $rule, $flags = array() ) {
92 if ( in_array( 'T', $flags ) ) {
93 return parent::parseManualRule( $rule, $flags );
94 }
95
96 $carray = array();
97 // otherwise ignore all formatting
98 foreach ( $this->mVariants as $v ) {
99 $carray[$v] = $rule;
100 }
101
102 return $carray;
103 }
104
105 /**
106 * A function wrapper:
107 * - if there is no selected variant, leave the link
108 * names as they were
109 * - do not try to find variants for usernames
110 *
111 * @param $link string
112 * @param $nt Title
113 * @param $ignoreOtherCond bool
114 */
115 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
116 // check for user namespace
117 if ( is_object( $nt ) ) {
118 $ns = $nt->getNamespace();
119 if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
120 return;
121 }
122 }
123
124 $oldlink = $link;
125 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
126 if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
127 $link = $oldlink;
128 }
129 }
130
131 /**
132 * An ugly function wrapper for parsing Image titles
133 * (to prevent image name conversion)
134 *
135 * @param $text string
136 * @param $toVariant bool
137 *
138 * @return string
139 */
140 function autoConvert( $text, $toVariant = false ) {
141 global $wgTitle;
142 if ( is_object( $wgTitle ) && $wgTitle->getNamespace() == NS_FILE ) {
143 $imagename = $wgTitle->getNsText();
144 if ( preg_match( "/^$imagename:/", $text ) ) {
145 return $text;
146 }
147 }
148 return parent::autoConvert( $text, $toVariant );
149 }
150
151 /**
152 * It translates text into variant, specials:
153 * - ommiting roman numbers
154 *
155 * @param $text string
156 * @param $toVariant string
157 *
158 * @throws MWException
159 * @return string
160 */
161 function translate( $text, $toVariant ) {
162 $breaks = '[^\w\x80-\xff]';
163
164 // regexp for roman numbers
165 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
166
167 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
168 . $roman . '$|' . $breaks . $roman . $breaks . '/';
169
170 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
171
172 $m = array_shift( $matches );
173 $this->loadTables();
174 if ( !isset( $this->mTables[$toVariant] ) ) {
175 throw new MWException( "Broken variant table: "
176 . implode( ',', array_keys( $this->mTables ) ) );
177 }
178 $ret = $this->mTables[$toVariant]->replace( $m[0] );
179 $mstart = $m[1] + strlen( $m[0] );
180 foreach ( $matches as $m ) {
181 $ret .= substr( $text, $mstart, $m[1] -$mstart );
182 $ret .= parent::translate( $m[0], $toVariant );
183 $mstart = $m[1] + strlen( $m[0] );
184 }
185
186 return $ret;
187 }
188
189 /**
190 * Guess if a text is written in Cyrillic or Latin.
191 * Overrides LanguageConverter::guessVariant()
192 *
193 * @param string $text The text to be checked
194 * @param string $variant Language code of the variant to be checked for
195 * @return bool true if $text appears to be written in $variant
196 *
197 * @author Nikola Smolenski <smolensk@eunet.rs>
198 * @since 1.19
199 */
200 public function guessVariant( $text, $variant ) {
201 $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
202 $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
203
204 if ( $variant == 'sr-ec' ) {
205 return $numCyrillic > $numLatin;
206 } elseif ( $variant == 'sr-el' ) {
207 return $numLatin > $numCyrillic;
208 } else {
209 return false;
210 }
211
212 }
213
214 }
215
216 /**
217 * Serbian (Српски / Srpski)
218 *
219 * @ingroup Language
220 */
221 class LanguageSr extends Language {
222 function __construct() {
223 global $wgHooks;
224
225 parent::__construct();
226
227 $variants = array( 'sr', 'sr-ec', 'sr-el' );
228 $variantfallbacks = array(
229 'sr' => 'sr-ec',
230 'sr-ec' => 'sr',
231 'sr-el' => 'sr',
232 );
233
234 $flags = array(
235 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
236 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
237 );
238 $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
239 $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
240 }
241 }