Merge "Whitelist the <wbr> element."
[mediawiki.git] / languages / classes / LanguageSr.php
blobe8811ac7a1ad0486e57448c859a7270c61f6ffbc
1 <?php
2 /**
3 * Serbian (Српски / Srpski) specific code.
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.
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.
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
20 * @file
21 * @ingroup Language
24 require_once __DIR__ . '/../LanguageConverter.php';
25 require_once __DIR__ . '/LanguageSr_ec.php';
26 require_once __DIR__ . '/LanguageSr_el.php';
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.
35 * @ingroup Language
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ž', 'ш' => 'š',
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ž', 'Ш' => 'Š',
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' => 'з', 'ž' => 'ж',
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' => 'З', 'Ž' => 'Ж',
69 'DŽ' => 'Џ', 'd!ž' => 'дж', 'D!ž' => 'Дж', 'D!Ž' => 'ДЖ',
70 'Lj' => 'Љ', 'l!j' => 'лј', 'L!j' => 'Лј', 'L!J' => 'ЛЈ',
71 'Nj' => 'Њ', 'n!j' => 'нј', 'N!j' => 'Нј', 'N!J' => 'НЈ'
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()
82 /**
83 * rules should be defined as -{ekavian | iyekavian-} -or-
84 * -{code:text | code:text | ...}-
86 * update: delete all rule parsing because it's not used
87 * currently, and just produces a couple of bugs
89 * @param $rule string
90 * @param $flags array
91 * @return array
93 function parseManualRule( $rule, $flags = array() ) {
94 if ( in_array( 'T', $flags ) ) {
95 return parent::parseManualRule( $rule, $flags );
98 $carray = array();
99 // otherwise ignore all formatting
100 foreach ( $this->mVariants as $v ) {
101 $carray[$v] = $rule;
104 return $carray;
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
113 * @param $link string
114 * @param $nt Title
115 * @param $ignoreOtherCond bool
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;
126 $oldlink = $link;
127 parent::findVariantLink( $link, $nt, $ignoreOtherCond );
128 if ( $this->getPreferredVariant() == $this->mMainLanguageCode ) {
129 $link = $oldlink;
134 * An ugly function wrapper for parsing Image titles
135 * (to prevent image name conversion)
137 * @param $text string
138 * @param $toVariant bool
140 * @return string
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;
150 return parent::autoConvert( $text, $toVariant );
154 * It translates text into variant, specials:
155 * - ommiting roman numbers
157 * @param $text string
158 * @param $toVariant string
160 * @throws MWException
161 * @return string
163 function translate( $text, $toVariant ) {
164 $breaks = '[^\w\x80-\xff]';
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})';
169 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks . $roman . '$|' . $breaks . $roman . $breaks . '/';
171 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
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 ) ) );
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] );
186 return $ret;
190 * Guess if a text is written in Cyrillic or Latin.
191 * Overrides LanguageConverter::guessVariant()
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
197 * @author Nikola Smolenski <smolensk@eunet.rs>
198 * @since 1.19
200 public function guessVariant( $text, $variant ) {
201 $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
202 $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
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;
217 * Serbian (Српски / Srpski)
219 * @ingroup Language
221 class LanguageSr extends LanguageSr_ec {
222 function __construct() {
223 global $wgHooks;
225 parent::__construct();
227 $variants = array( 'sr', 'sr-ec', 'sr-el' );
228 $variantfallbacks = array(
229 'sr' => 'sr-ec',
230 'sr-ec' => 'sr',
231 'sr-el' => 'sr',
234 $flags = array(
235 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
236 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
238 $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
239 $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
243 * @param $count int
244 * @param $forms array
246 * @return string
248 function convertPlural( $count, $forms ) {
249 if ( !count( $forms ) ) {
250 return '';
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];
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 );
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];