phpcs: More require/include is not a function
[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 require_once __DIR__ . '/LanguageSr_ec.php';
26 require_once __DIR__ . '/LanguageSr_el.php';
27
28 /**
29 * There are two levels of conversion for Serbian: the script level
30 * (Cyrillics <-> Latin), and the variant level (ekavian
31 * <->iyekavian). The two are orthogonal. So we really only need two
32 * dictionaries: one for Cyrillics and Latin, and one for ekavian and
33 * iyekavian.
34 *
35 * @ingroup Language
36 */
37 class SrConverter extends LanguageConverter {
38 public $mToLatin = array(
39 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
40 'ђ' => 'đ', 'е' => 'e', 'ж' => 'ž', 'з' => 'z', 'и' => 'i',
41 'ј' => 'j', 'к' => 'k', 'л' => 'l', 'љ' => 'lj', 'м' => 'm',
42 'н' => 'n', 'њ' => 'nj', 'о' => 'o', 'п' => 'p', 'р' => 'r',
43 'с' => 's', 'т' => 't', 'ћ' => 'ć', 'у' => 'u', 'ф' => 'f',
44 'х' => 'h', 'ц' => 'c', 'ч' => 'č', 'џ' => 'dž', 'ш' => 'š',
45
46 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
47 'Ђ' => 'Đ', 'Е' => 'E', 'Ж' => 'Ž', 'З' => 'Z', 'И' => 'I',
48 'Ј' => 'J', 'К' => 'K', 'Л' => 'L', 'Љ' => 'Lj', 'М' => 'M',
49 'Н' => 'N', 'Њ' => 'Nj', 'О' => 'O', 'П' => 'P', 'Р' => 'R',
50 'С' => 'S', 'Т' => 'T', 'Ћ' => 'Ć', 'У' => 'U', 'Ф' => 'F',
51 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Č', 'Џ' => 'Dž', 'Ш' => 'Š',
52 );
53
54 public $mToCyrillics = array(
55 'a' => 'а', 'b' => 'б', 'c' => 'ц', 'č' => 'ч', 'ć' => 'ћ',
56 'd' => 'д', 'dž' => 'џ', 'đ' => 'ђ', 'e' => 'е', 'f' => 'ф',
57 'g' => 'г', 'h' => 'х', 'i' => 'и', 'j' => 'ј', 'k' => 'к',
58 'l' => 'л', 'lj' => 'љ', 'm' => 'м', 'n' => 'н', 'nj' => 'њ',
59 'o' => 'о', 'p' => 'п', 'r' => 'р', 's' => 'с', 'š' => 'ш',
60 't' => 'т', 'u' => 'у', 'v' => 'в', 'z' => 'з', 'ž' => 'ж',
61
62 'A' => 'А', 'B' => 'Б', 'C' => 'Ц', 'Č' => 'Ч', 'Ć' => 'Ћ',
63 'D' => 'Д', 'Dž' => 'Џ', 'Đ' => 'Ђ', 'E' => 'Е', 'F' => 'Ф',
64 'G' => 'Г', 'H' => 'Х', 'I' => 'И', 'J' => 'Ј', 'K' => 'К',
65 'L' => 'Л', 'LJ' => 'Љ', 'M' => 'М', 'N' => 'Н', 'NJ' => 'Њ',
66 'O' => 'О', 'P' => 'П', 'R' => 'Р', 'S' => 'С', 'Š' => 'Ш',
67 'T' => 'Т', 'U' => 'У', 'V' => 'В', 'Z' => 'З', 'Ž' => 'Ж',
68
69 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
70 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
71 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
72 );
73
74 function loadDefaultTables() {
75 $this->mTables = array(
76 'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
77 'sr-el' => new ReplacementArray( $this->mToLatin ),
78 'sr' => new ReplacementArray()
79 );
80 }
81
82 /**
83 * rules should be defined as -{ekavian | iyekavian-} -or-
84 * -{code:text | code:text | ...}-
85 *
86 * update: delete all rule parsing because it's not used
87 * currently, and just produces a couple of bugs
88 *
89 * @param $rule string
90 * @param $flags array
91 * @return array
92 */
93 function parseManualRule( $rule, $flags = array() ) {
94 if ( in_array( 'T', $flags ) ) {
95 return parent::parseManualRule( $rule, $flags );
96 }
97
98 $carray = array();
99 // otherwise ignore all formatting
100 foreach ( $this->mVariants as $v ) {
101 $carray[$v] = $rule;
102 }
103
104 return $carray;
105 }
106
107 /**
108 * A function wrapper:
109 * - if there is no selected variant, leave the link
110 * names as they were
111 * - do not try to find variants for usernames
112 *
113 * @param $link string
114 * @param $nt Title
115 * @param $ignoreOtherCond bool
116 */
117 function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
118 // check for user namespace
119 if ( is_object( $nt ) ) {
120 $ns = $nt->getNamespace();
121 if ( $ns == NS_USER || $ns == NS_USER_TALK ) {
122 return;
123 }
124 }
125
126 $oldlink = $link;
127 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
128 if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
129 $link = $oldlink;
130 }
131 }
132
133 /**
134 * An ugly function wrapper for parsing Image titles
135 * (to prevent image name conversion)
136 *
137 * @param $text string
138 * @param $toVariant bool
139 *
140 * @return string
141 */
142 function autoConvert( $text, $toVariant = false ) {
143 global $wgTitle;
144 if ( is_object( $wgTitle ) && $wgTitle->getNamespace() == NS_FILE ) {
145 $imagename = $wgTitle->getNsText();
146 if ( preg_match( "/^$imagename:/", $text ) ) {
147 return $text;
148 }
149 }
150 return parent::autoConvert( $text, $toVariant );
151 }
152
153 /**
154 * It translates text into variant, specials:
155 * - ommiting roman numbers
156 *
157 * @param $text string
158 * @param $toVariant string
159 *
160 * @throws MWException
161 * @return string
162 */
163 function translate( $text, $toVariant ) {
164 $breaks = '[^\w\x80-\xff]';
165
166 // regexp for roman numbers
167 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
168
169 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks . $roman . '$|' . $breaks . $roman . $breaks . '/';
170
171 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
172
173 $m = array_shift( $matches );
174 $this->loadTables();
175 if ( !isset( $this->mTables[$toVariant] ) ) {
176 throw new MWException( "Broken variant table: " . 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 (boolean) ( $numCyrillic > $numLatin );
206 } elseif ( $variant == 'sr-el' ) {
207 return (boolean) ( $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 LanguageSr_ec {
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
242 /**
243 * @param $count int
244 * @param $forms array
245 *
246 * @return string
247 */
248 function convertPlural( $count, $forms ) {
249 if ( !count( $forms ) ) {
250 return '';
251 }
252
253 // If the actual number is not mentioned in the expression, then just two forms are enough:
254 // singular for $count == 1
255 // plural for $count != 1
256 // For example, "This user belongs to {{PLURAL:$1|one group|several groups}}."
257 if ( count( $forms ) === 2 ) {
258 return $count == 1 ? $forms[0] : $forms[1];
259 }
260
261 // @todo FIXME: CLDR defines 4 plural forms. Form with decimals missing.
262 // See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html#ru
263 $forms = $this->preConvertPlural( $forms, 3 );
264
265 if ( $count > 10 && floor( ( $count % 100 ) / 10 ) == 1 ) {
266 return $forms[2];
267 } else {
268 switch ( $count % 10 ) {
269 case 1: return $forms[0];
270 case 2:
271 case 3:
272 case 4: return $forms[1];
273 default: return $forms[2];
274 }
275 }
276 }
277 }