Merge "Add pluggable talk page poster and use it for mediawiki.feedback"
[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 string $rule
88 * @param array $flags
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 string &$link
112 * @param Title &$nt
113 * @param bool $ignoreOtherCond
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 * It translates text into variant, specials:
133 * - ommiting roman numbers
134 *
135 * @param string $text
136 * @param string $toVariant
137 *
138 * @throws MWException
139 * @return string
140 */
141 function translate( $text, $toVariant ) {
142 $breaks = '[^\w\x80-\xff]';
143
144 // regexp for roman numbers
145 $roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';
146
147 $reg = '/^' . $roman . '$|^' . $roman . $breaks . '|' . $breaks
148 . $roman . '$|' . $breaks . $roman . $breaks . '/';
149
150 $matches = preg_split( $reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
151
152 $m = array_shift( $matches );
153 $this->loadTables();
154 if ( !isset( $this->mTables[$toVariant] ) ) {
155 throw new MWException( "Broken variant table: "
156 . implode( ',', array_keys( $this->mTables ) ) );
157 }
158 $ret = $this->mTables[$toVariant]->replace( $m[0] );
159 $mstart = $m[1] + strlen( $m[0] );
160 foreach ( $matches as $m ) {
161 $ret .= substr( $text, $mstart, $m[1] -$mstart );
162 $ret .= parent::translate( $m[0], $toVariant );
163 $mstart = $m[1] + strlen( $m[0] );
164 }
165
166 return $ret;
167 }
168
169 /**
170 * Guess if a text is written in Cyrillic or Latin.
171 * Overrides LanguageConverter::guessVariant()
172 *
173 * @param string $text The text to be checked
174 * @param string $variant Language code of the variant to be checked for
175 * @return bool True if $text appears to be written in $variant
176 *
177 * @author Nikola Smolenski <smolensk@eunet.rs>
178 * @since 1.19
179 */
180 public function guessVariant( $text, $variant ) {
181 $numCyrillic = preg_match_all( "/[шђчћжШЂЧЋЖ]/u", $text, $dummy );
182 $numLatin = preg_match_all( "/[šđč枊ĐČĆŽ]/u", $text, $dummy );
183
184 if ( $variant == 'sr-ec' ) {
185 return $numCyrillic > $numLatin;
186 } elseif ( $variant == 'sr-el' ) {
187 return $numLatin > $numCyrillic;
188 } else {
189 return false;
190 }
191
192 }
193
194 }
195
196 /**
197 * Serbian (Српски / Srpski)
198 *
199 * @ingroup Language
200 */
201 class LanguageSr extends Language {
202 function __construct() {
203 global $wgHooks;
204
205 parent::__construct();
206
207 $variants = array( 'sr', 'sr-ec', 'sr-el' );
208 $variantfallbacks = array(
209 'sr' => 'sr-ec',
210 'sr-ec' => 'sr',
211 'sr-el' => 'sr',
212 );
213
214 $flags = array(
215 'S' => 'S', 'писмо' => 'S', 'pismo' => 'S',
216 'W' => 'W', 'реч' => 'W', 'reč' => 'W', 'ријеч' => 'W', 'riječ' => 'W'
217 );
218 $this->mConverter = new SrConverter( $this, 'sr', $variants, $variantfallbacks, $flags );
219 $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
220 }
221 }