Index: trunk/phase3/languages/LanguageConverter.php |
— | — | @@ -9,27 +9,28 @@ |
10 | 10 | */ |
11 | 11 | |
12 | 12 | /** |
13 | | - * base class for language convert |
| 13 | + * Base class for language conversion. |
14 | 14 | * @ingroup Language |
15 | 15 | * |
16 | 16 | * @author Zhengzhu Feng <zhengzhu@gmail.com> |
17 | 17 | * @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>, PhiLiP <philip.npc@gmail.com> |
18 | 18 | */ |
19 | 19 | class LanguageConverter { |
20 | | - var $mPreferredVariant=''; |
| 20 | + var $mPreferredVariant = ''; |
21 | 21 | var $mMainLanguageCode; |
22 | 22 | var $mVariants, $mVariantFallbacks, $mVariantNames; |
23 | 23 | var $mTablesLoaded = false; |
24 | 24 | var $mTables; |
25 | 25 | var $mNamespaceTables; |
26 | | - var $mDoTitleConvert=true, $mDoContentConvert=true; |
27 | | - var $mManualLevel; // 'bidirectional' 'unidirectional' 'disable' for each variants |
| 26 | + var $mDoTitleConvert = true, $mDoContentConvert = true; |
| 27 | + // 'bidirectional' 'unidirectional' 'disable' for each variant |
| 28 | + var $mManualLevel; |
28 | 29 | var $mTitleFromFlag = false; |
29 | 30 | var $mCacheKey; |
30 | 31 | var $mLangObj; |
31 | 32 | var $mMarkup; |
32 | 33 | var $mFlags; |
33 | | - var $mDescCodeSep = ':',$mDescVarSep = ';'; |
| 34 | + var $mDescCodeSep = ':', $mDescVarSep = ';'; |
34 | 35 | var $mUcfirst = false; |
35 | 36 | var $mTitleOriginal = ''; |
36 | 37 | var $mTitleDisplay = ''; |
— | — | @@ -39,18 +40,20 @@ |
40 | 41 | /** |
41 | 42 | * Constructor |
42 | 43 | * |
| 44 | + * @param $langobj The Language Object |
43 | 45 | * @param string $maincode the main language code of this language |
44 | 46 | * @param array $variants the supported variants of this language |
45 | 47 | * @param array $variantfallback the fallback language of each variant |
46 | 48 | * @param array $markup array defining the markup used for manual conversion |
47 | | - * @param array $flags array defining the custom strings that maps to the flags |
| 49 | + * @param array $flags array defining the custom strings that maps to the |
| 50 | + * flags |
48 | 51 | * @param array $manualLevel limit for supported variants |
49 | 52 | * @public |
50 | 53 | */ |
51 | 54 | function __construct( $langobj, $maincode, |
52 | | - $variants=array(), |
53 | | - $variantfallbacks=array(), |
54 | | - $markup=array(), |
| 55 | + $variants = array(), |
| 56 | + $variantfallbacks = array(), |
| 57 | + $markup = array(), |
55 | 58 | $flags = array(), |
56 | 59 | $manualLevel = array() ) { |
57 | 60 | $this->mLangObj = $langobj; |
— | — | @@ -58,41 +61,45 @@ |
59 | 62 | |
60 | 63 | global $wgDisabledVariants; |
61 | 64 | $this->mVariants = array(); |
62 | | - foreach( $variants as $variant ) { |
63 | | - if( !in_array( $variant, $wgDisabledVariants ) ) |
| 65 | + foreach ( $variants as $variant ) { |
| 66 | + if ( !in_array( $variant, $wgDisabledVariants ) ) { |
64 | 67 | $this->mVariants[] = $variant; |
| 68 | + } |
65 | 69 | } |
66 | 70 | $this->mVariantFallbacks = $variantfallbacks; |
67 | 71 | global $wgLanguageNames; |
68 | 72 | $this->mVariantNames = $wgLanguageNames; |
69 | 73 | $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode ); |
70 | 74 | $m = array( |
71 | | - 'begin'=>'-{', |
72 | | - 'flagsep'=>'|', |
73 | | - 'unidsep'=>'=>', //for unidirectional conversion |
74 | | - 'codesep'=>':', |
75 | | - 'varsep'=>';', |
76 | | - 'end'=>'}-' |
| 75 | + 'begin' => '-{', |
| 76 | + 'flagsep' => '|', |
| 77 | + 'unidsep' => '=>', // for unidirectional conversion |
| 78 | + 'codesep' => ':', |
| 79 | + 'varsep' => ';', |
| 80 | + 'end' => '}-' |
77 | 81 | ); |
78 | | - $this->mMarkup = array_merge($m, $markup); |
79 | | - $f = array( |
| 82 | + $this->mMarkup = array_merge( $m, $markup ); |
| 83 | + $f = array( |
80 | 84 | // 'S' show converted text |
81 | 85 | // '+' add rules for alltext |
82 | 86 | // 'E' the gave flags is error |
83 | 87 | // these flags above are reserved for program |
84 | | - 'A'=>'A', // add rule for convert code (all text convert) |
85 | | - 'T'=>'T', // title convert |
86 | | - 'R'=>'R', // raw content |
87 | | - 'D'=>'D', // convert description (subclass implement) |
88 | | - '-'=>'-', // remove convert (not implement) |
89 | | - 'H'=>'H', // add rule for convert code (but no display in placed code ) |
90 | | - 'N'=>'N' // current variant name |
| 88 | + 'A' => 'A', // add rule for convert code (all text convert) |
| 89 | + 'T' => 'T', // title convert |
| 90 | + 'R' => 'R', // raw content |
| 91 | + 'D' => 'D', // convert description (subclass implement) |
| 92 | + '-' => '-', // remove convert (not implement) |
| 93 | + 'H' => 'H', // add rule for convert code |
| 94 | + // (but no display in placed code ) |
| 95 | + 'N' => 'N' // current variant name |
91 | 96 | ); |
92 | | - $this->mFlags = array_merge($f, $flags); |
93 | | - foreach( $this->mVariants as $v) { |
94 | | - $this->mManualLevel[$v]=array_key_exists($v,$manualLevel) |
95 | | - ?$manualLevel[$v] |
96 | | - :'bidirectional'; |
| 97 | + $this->mFlags = array_merge( $f, $flags ); |
| 98 | + foreach ( $this->mVariants as $v ) { |
| 99 | + if ( array_key_exists( $v, $manualLevel ) ) { |
| 100 | + $this->mManualLevel[$v] = $manualLevel[$v]; |
| 101 | + } else { |
| 102 | + $this->mManualLevel[$v] = 'bidirectional'; |
| 103 | + } |
97 | 104 | $this->mNamespaceTables[$v] = array(); |
98 | 105 | $this->mFlags[$v] = $v; |
99 | 106 | } |
— | — | @@ -106,127 +113,128 @@ |
107 | 114 | } |
108 | 115 | |
109 | 116 | /** |
110 | | - * in case some variant is not defined in the markup, we need |
111 | | - * to have some fallback. for example, in zh, normally people |
| 117 | + * In case some variant is not defined in the markup, we need |
| 118 | + * to have some fallback. For example, in zh, normally people |
112 | 119 | * will define zh-hans and zh-hant, but less so for zh-sg or zh-hk. |
113 | 120 | * when zh-sg is preferred but not defined, we will pick zh-hans |
114 | | - * in this case. right now this is only used by zh. |
| 121 | + * in this case. Right now this is only used by zh. |
115 | 122 | * |
116 | | - * @param string $v the language code of the variant |
117 | | - * @return string array the code of the fallback language or false if there is no fallback |
| 123 | + * @param string $v The language code of the variant |
| 124 | + * @return string array The code of the fallback language or false if there |
| 125 | + * is no fallback |
118 | 126 | * @public |
119 | 127 | */ |
120 | | - function getVariantFallbacks($v) { |
121 | | - if( isset( $this->mVariantFallbacks[$v] ) ) { |
| 128 | + function getVariantFallbacks( $v ) { |
| 129 | + if ( isset( $this->mVariantFallbacks[$v] ) ) { |
122 | 130 | return $this->mVariantFallbacks[$v]; |
123 | 131 | } |
124 | 132 | return $this->mMainLanguageCode; |
125 | 133 | } |
126 | 134 | |
127 | 135 | /** |
128 | | - * get preferred language variants. |
| 136 | + * Get preferred language variants. |
129 | 137 | * @param boolean $fromUser Get it from $wgUser's preferences |
130 | 138 | * @param boolean $fromHeader Get it from Accept-Language |
131 | 139 | * @return string the preferred language code |
132 | 140 | * @public |
133 | 141 | */ |
134 | 142 | function getPreferredVariant( $fromUser = true, $fromHeader = false ) { |
135 | | - global $wgUser, $wgRequest, $wgVariantArticlePath, $wgDefaultLanguageVariant, $wgOut; |
| 143 | + global $wgUser, $wgRequest, $wgVariantArticlePath, |
| 144 | + $wgDefaultLanguageVariant, $wgOut; |
136 | 145 | |
137 | 146 | // bug 21974, don't return $this->mPreferredVariant if $fromUser = false |
138 | | - if( $fromUser && $this->mPreferredVariant ) |
| 147 | + if ( $fromUser && $this->mPreferredVariant ) { |
139 | 148 | return $this->mPreferredVariant; |
| 149 | + } |
140 | 150 | |
141 | | - // figure out user lang without constructing wgLang to avoid infinite recursion |
142 | | - if( $fromUser ) |
| 151 | + // figure out user lang without constructing wgLang to avoid |
| 152 | + // infinite recursion |
| 153 | + if ( $fromUser ) { |
143 | 154 | $defaultUserLang = $wgUser->getOption( 'language' ); |
144 | | - else |
| 155 | + } else { |
145 | 156 | $defaultUserLang = $this->mMainLanguageCode; |
| 157 | + } |
| 158 | + |
146 | 159 | $userLang = $wgRequest->getVal( 'uselang', $defaultUserLang ); |
147 | | - // see if interface language is same as content, if not, prevent conversion |
148 | | - if( ! in_array( $userLang, $this->mVariants ) ){ |
149 | | - $this->mPreferredVariant = $this->mMainLanguageCode; // no conversion |
| 160 | + // see if interface language is same as content, if not, prevent |
| 161 | + // conversion |
| 162 | + |
| 163 | + if ( ! in_array( $userLang, $this->mVariants ) ) { |
| 164 | + // no conversion |
| 165 | + $this->mPreferredVariant = $this->mMainLanguageCode; |
150 | 166 | return $this->mPreferredVariant; |
151 | 167 | } |
152 | 168 | |
153 | 169 | // see if the preference is set in the request |
154 | 170 | $req = $wgRequest->getText( 'variant' ); |
155 | | - if( in_array( $req, $this->mVariants ) ) { |
| 171 | + if ( in_array( $req, $this->mVariants ) ) { |
156 | 172 | $this->mPreferredVariant = $req; |
157 | | - return $req; |
| 173 | + return $this->mPreferredVariant; |
158 | 174 | } |
159 | 175 | |
160 | | - // check the syntax /code/ArticleTitle |
161 | | - if($wgVariantArticlePath!=false && isset($_SERVER['SCRIPT_NAME'])){ |
162 | | - // Note: SCRIPT_NAME probably won't hold the correct value if PHP is run as CGI |
163 | | - // (it will hold path to php.cgi binary), and might not exist on some very old PHP installations |
164 | | - $scriptBase = basename( $_SERVER['SCRIPT_NAME'] ); |
165 | | - if(in_array($scriptBase,$this->mVariants)){ |
166 | | - $this->mPreferredVariant = $scriptBase; |
167 | | - return $this->mPreferredVariant; |
168 | | - } |
169 | | - } |
170 | | - |
171 | 176 | // get language variant preference from logged in users |
172 | 177 | // Don't call this on stub objects because that causes infinite |
173 | 178 | // recursion during initialisation |
174 | | - if( $fromUser && $wgUser->isLoggedIn() ) { |
175 | | - $this->mPreferredVariant = $wgUser->getOption('variant'); |
| 179 | + if ( $fromUser && $wgUser->isLoggedIn() ) { |
| 180 | + $this->mPreferredVariant = $wgUser->getOption( 'variant' ); |
176 | 181 | return $this->mPreferredVariant; |
177 | 182 | } |
178 | 183 | |
179 | 184 | // see if default variant is globaly set |
180 | | - if($wgDefaultLanguageVariant != false && in_array( $wgDefaultLanguageVariant, $this->mVariants )){ |
| 185 | + if ( $wgDefaultLanguageVariant != false |
| 186 | + && in_array( $wgDefaultLanguageVariant, $this->mVariants ) ) { |
181 | 187 | $this->mPreferredVariant = $wgDefaultLanguageVariant; |
182 | 188 | return $this->mPreferredVariant; |
183 | 189 | } |
184 | 190 | |
185 | | - if( !$this->mPreferredVariant ) { |
| 191 | + if ( !$this->mPreferredVariant ) { |
186 | 192 | // see if some supported language variant is set in the |
187 | 193 | // http header, but we don't set the mPreferredVariant |
188 | 194 | // variable in case this is called before the user's |
189 | 195 | // preference is loaded |
190 | | - if( $fromHeader && array_key_exists( 'HTTP_ACCEPT_LANGUAGE', $_SERVER ) ) { |
191 | | - $acceptLanguage = strtolower( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ); |
| 196 | + |
| 197 | + $acceptLanguage = $wgRequest->getHeader( 'Accept-Language' ); |
| 198 | + if ( $fromHeader && $acceptLanguage ) { |
192 | 199 | // explode by comma |
193 | | - $result = explode(',', $acceptLanguage); |
| 200 | + $result = explode( ',', strtolower( $acceptLanguage ) ); |
194 | 201 | |
195 | 202 | $languages = array(); |
196 | 203 | |
197 | | - foreach( $result as $elem ) { |
| 204 | + foreach ( $result as $elem ) { |
198 | 205 | // if $elem likes 'zh-cn;q=0.9' |
199 | | - if(($posi = strpos( $elem, ';' )) !== false ) { |
| 206 | + if ( ( $posi = strpos( $elem, ';' ) ) !== false ) { |
200 | 207 | // get the real language code likes 'zh-cn' |
201 | 208 | $languages[] = substr( $elem, 0, $posi ); |
202 | | - } |
203 | | - else { |
| 209 | + } else { |
204 | 210 | $languages[] = $elem; |
205 | 211 | } |
206 | 212 | } |
207 | 213 | |
208 | 214 | $fallback_languages = array(); |
209 | | - foreach( $languages as $language ) { |
| 215 | + foreach ( $languages as $language ) { |
210 | 216 | // strip whitespace |
211 | 217 | $language = trim( $language ); |
212 | | - if( in_array( $language, $this->mVariants ) ) { |
| 218 | + if ( in_array( $language, $this->mVariants ) ) { |
213 | 219 | return $language; |
214 | | - } |
215 | | - else { |
| 220 | + } else { |
216 | 221 | // To see if there are fallbacks of current language. |
217 | 222 | // We record these fallback variants, and process |
218 | 223 | // them later. |
219 | 224 | $fallbacks = $this->getVariantFallbacks( $language ); |
220 | | - if( is_string( $fallbacks ) ) |
| 225 | + if ( is_string( $fallbacks ) ) { |
221 | 226 | $fallback_languages[] = $fallbacks; |
222 | | - elseif( is_array( $fallbacks ) ) |
223 | | - $fallback_languages = array_merge( $fallback_languages, $fallbacks ); |
| 227 | + } elseif ( is_array( $fallbacks ) ) { |
| 228 | + $fallback_languages = |
| 229 | + array_merge( $fallback_languages, |
| 230 | + $fallbacks ); |
| 231 | + } |
224 | 232 | } |
225 | 233 | } |
226 | 234 | |
227 | 235 | // process fallback languages now |
228 | 236 | $fallback_languages = array_unique( $fallback_languages ); |
229 | | - foreach( $fallback_languages as $language ) { |
230 | | - if( in_array( $language, $this->mVariants ) ) { |
| 237 | + foreach ( $fallback_languages as $language ) { |
| 238 | + if ( in_array( $language, $this->mVariants ) ) { |
231 | 239 | return $language; |
232 | 240 | } |
233 | 241 | } |
— | — | @@ -236,9 +244,9 @@ |
237 | 245 | } |
238 | 246 | |
239 | 247 | /** |
240 | | - * caption convert, base on preg_replace_callback |
| 248 | + * Caption convert, base on preg_replace_callback. |
241 | 249 | * |
242 | | - * to convert text in "title" or "alt", like '<img alt="text" ... ' |
| 250 | + * To convert text in "title" or "alt", like '<img alt="text" ... ' |
243 | 251 | * or '<span title="text" ... ' |
244 | 252 | * |
245 | 253 | * @return string like ' alt="yyyy"' or ' title="yyyy"' |
— | — | @@ -249,31 +257,35 @@ |
250 | 258 | $title = $matches[1]; |
251 | 259 | $text = $matches[2]; |
252 | 260 | // we convert captions except URL |
253 | | - if( !strpos( $text, '://' ) ) |
254 | | - $text = $this->translate($text, $toVariant); |
| 261 | + if ( !strpos( $text, '://' ) ) { |
| 262 | + $text = $this->translate( $text, $toVariant ); |
| 263 | + } |
255 | 264 | return " $title=\"$text\""; |
256 | 265 | } |
257 | 266 | |
258 | 267 | /** |
259 | | - * dictionary-based conversion |
| 268 | + * Dictionary-based conversion. |
260 | 269 | * |
261 | 270 | * @param string $text the text to be converted |
262 | 271 | * @param string $toVariant the target language code |
263 | 272 | * @return string the converted text |
264 | 273 | * @private |
265 | 274 | */ |
266 | | - function autoConvert($text, $toVariant=false) { |
267 | | - $fname="LanguageConverter::autoConvert"; |
| 275 | + function autoConvert( $text, $toVariant = false ) { |
| 276 | + $fname = 'LanguageConverter::autoConvert'; |
268 | 277 | |
269 | 278 | wfProfileIn( $fname ); |
270 | 279 | |
271 | | - if(!$this->mTablesLoaded) |
| 280 | + if ( !$this->mTablesLoaded ) { |
272 | 281 | $this->loadTables(); |
| 282 | + } |
273 | 283 | |
274 | | - if(!$toVariant) |
| 284 | + if ( !$toVariant ) { |
275 | 285 | $toVariant = $this->getPreferredVariant(); |
276 | | - if(!in_array($toVariant, $this->mVariants)) |
| 286 | + } |
| 287 | + if ( !in_array( $toVariant, $this->mVariants ) ) { |
277 | 288 | return $text; |
| 289 | + } |
278 | 290 | |
279 | 291 | /* we convert everything except: |
280 | 292 | 1. html markups (anything between < and >) |
— | — | @@ -281,10 +293,11 @@ |
282 | 294 | 3. place holders created by the parser |
283 | 295 | */ |
284 | 296 | global $wgParser; |
285 | | - if (isset($wgParser) && $wgParser->UniqPrefix()!=''){ |
| 297 | + if ( isset( $wgParser ) && $wgParser->UniqPrefix() != '' ) { |
286 | 298 | $marker = '|' . $wgParser->UniqPrefix() . '[\-a-zA-Z0-9]+'; |
287 | | - } else |
288 | | - $marker = ""; |
| 299 | + } else { |
| 300 | + $marker = ''; |
| 301 | + } |
289 | 302 | |
290 | 303 | // this one is needed when the text is inside an html markup |
291 | 304 | $htmlfix = '|<[^>]+$|^[^<>]*>'; |
— | — | @@ -296,35 +309,39 @@ |
297 | 310 | // disable conversion of <pre xxxx> ... </pre> |
298 | 311 | $prefix = '<pre.*?>.*?<\/pre>|'; |
299 | 312 | |
300 | | - $reg = '/'.$codefix . $scriptfix . $prefix . '<[^>]+>|&[a-zA-Z#][a-z0-9]+;' . $marker . $htmlfix . '/s'; |
| 313 | + $reg = '/' . $codefix . $scriptfix . $prefix . |
| 314 | + '<[^>]+>|&[a-zA-Z#][a-z0-9]+;' . $marker . $htmlfix . '/s'; |
301 | 315 | |
302 | | - $matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE); |
| 316 | + $matches = preg_split( $reg, $text, - 1, PREG_SPLIT_OFFSET_CAPTURE ); |
303 | 317 | |
304 | | - $m = array_shift($matches); |
| 318 | + $m = array_shift( $matches ); |
305 | 319 | |
306 | | - $ret = $this->translate($m[0], $toVariant); |
307 | | - $mstart = $m[1]+strlen($m[0]); |
| 320 | + $ret = $this->translate( $m[0], $toVariant ); |
| 321 | + $mstart = $m[1] + strlen( $m[0] ); |
308 | 322 | |
309 | | - // enable convertsion of '<img alt="xxxx" ... ' or '<span title="xxxx" ... ' |
310 | | - $captionpattern = '/\s(title|alt)\s*=\s*"([\s\S]*?)"/'; |
| 323 | + // enable convertsion of '<img alt="xxxx" ... ' |
| 324 | + // or '<span title="xxxx" ... ' |
| 325 | + $captionpattern = '/\s(title|alt)\s*=\s*"([\s\S]*?)"/'; |
311 | 326 | |
312 | 327 | $trtext = ''; |
313 | 328 | $trtextmark = "\0"; |
314 | 329 | $notrtext = array(); |
315 | | - foreach($matches as $m) { |
316 | | - $mark = substr($text, $mstart, $m[1]-$mstart); |
317 | | - $mark = preg_replace_callback($captionpattern, array(&$this, 'captionConvert'), $mark); |
| 330 | + foreach ( $matches as $m ) { |
| 331 | + $mark = substr( $text, $mstart, $m[1] - $mstart ); |
| 332 | + $mark = preg_replace_callback( $captionpattern, |
| 333 | + array( &$this, 'captionConvert' ), |
| 334 | + $mark ); |
318 | 335 | // Let's convert the trtext only once, |
319 | 336 | // it would give us more performance improvement |
320 | 337 | $notrtext[] = $mark; |
321 | 338 | $trtext .= $m[0] . $trtextmark; |
322 | | - $mstart = $m[1] + strlen($m[0]); |
| 339 | + $mstart = $m[1] + strlen( $m[0] ); |
323 | 340 | } |
324 | 341 | $notrtext[] = ''; |
325 | 342 | $trtext = $this->translate( $trtext, $toVariant ); |
326 | 343 | $trtext = StringUtils::explode( $trtextmark, $trtext ); |
327 | | - foreach( $trtext as $t ) { |
328 | | - $ret .= array_shift($notrtext); |
| 344 | + foreach ( $trtext as $t ) { |
| 345 | + $ret .= array_shift( $notrtext ); |
329 | 346 | $ret .= $t; |
330 | 347 | } |
331 | 348 | wfProfileOut( $fname ); |
— | — | @@ -332,8 +349,9 @@ |
333 | 350 | } |
334 | 351 | |
335 | 352 | /** |
336 | | - * Translate a string to a variant |
337 | | - * Doesn't process markup or do any of that other stuff, for that use convert() |
| 353 | + * Translate a string to a variant. |
| 354 | + * Doesn't process markup or do any of that other stuff, for that use |
| 355 | + * convert(). |
338 | 356 | * |
339 | 357 | * @param string $text Text to convert |
340 | 358 | * @param string $variant Variant language code |
— | — | @@ -344,31 +362,33 @@ |
345 | 363 | wfProfileIn( __METHOD__ ); |
346 | 364 | // If $text is empty or only includes spaces, do nothing |
347 | 365 | // Otherwise translate it |
348 | | - if( trim($text) ) { |
349 | | - if( !$this->mTablesLoaded ) |
| 366 | + if ( trim( $text ) ) { |
| 367 | + if ( !$this->mTablesLoaded ) { |
350 | 368 | $this->loadTables(); |
351 | | - $text = $this->mTables[$variant]->replace( $text ); |
| 369 | + } |
| 370 | + $text = $this->mTables[$variant]->replace( $text ); |
352 | 371 | } |
353 | 372 | wfProfileOut( __METHOD__ ); |
354 | 373 | return $text; |
355 | 374 | } |
356 | 375 | |
357 | 376 | /** |
358 | | - * convert text to all supported variants |
| 377 | + * Convert text to all supported variants. |
359 | 378 | * |
360 | 379 | * @param string $text the text to be converted |
361 | 380 | * @return array of string |
362 | 381 | * @public |
363 | 382 | */ |
364 | | - function autoConvertToAllVariants($text) { |
365 | | - $fname="LanguageConverter::autoConvertToAllVariants"; |
| 383 | + function autoConvertToAllVariants( $text ) { |
| 384 | + $fname = 'LanguageConverter::autoConvertToAllVariants'; |
366 | 385 | wfProfileIn( $fname ); |
367 | | - if( !$this->mTablesLoaded ) |
| 386 | + if ( !$this->mTablesLoaded ) { |
368 | 387 | $this->loadTables(); |
| 388 | + } |
369 | 389 | |
370 | 390 | $ret = array(); |
371 | | - foreach($this->mVariants as $variant) { |
372 | | - $ret[$variant] = $this->translate($text, $variant); |
| 391 | + foreach ( $this->mVariants as $variant ) { |
| 392 | + $ret[$variant] = $this->translate( $text, $variant ); |
373 | 393 | } |
374 | 394 | |
375 | 395 | wfProfileOut( $fname ); |
— | — | @@ -376,30 +396,34 @@ |
377 | 397 | } |
378 | 398 | |
379 | 399 | /** |
380 | | - * convert link text to all supported variants |
| 400 | + * Convert link text to all supported variants. |
381 | 401 | * |
382 | 402 | * @param string $text the text to be converted |
383 | 403 | * @return array of string |
384 | 404 | * @public |
385 | 405 | */ |
386 | | - function convertLinkToAllVariants($text) { |
387 | | - if( !$this->mTablesLoaded ) |
| 406 | + function convertLinkToAllVariants( $text ) { |
| 407 | + if ( !$this->mTablesLoaded ) { |
388 | 408 | $this->loadTables(); |
| 409 | + } |
389 | 410 | |
390 | 411 | $ret = array(); |
391 | | - $tarray = explode($this->mMarkup['begin'], $text); |
392 | | - $tfirst = array_shift($tarray); |
| 412 | + $tarray = explode( $this->mMarkup['begin'], $text ); |
| 413 | + $tfirst = array_shift( $tarray ); |
393 | 414 | |
394 | | - foreach($this->mVariants as $variant) |
395 | | - $ret[$variant] = $this->translate($tfirst,$variant); |
| 415 | + foreach ( $this->mVariants as $variant ) { |
| 416 | + $ret[$variant] = $this->translate( $tfirst, $variant ); |
| 417 | + } |
396 | 418 | |
397 | | - foreach($tarray as $txt) { |
398 | | - $marked = explode($this->mMarkup['end'], $txt, 2); |
| 419 | + foreach ( $tarray as $txt ) { |
| 420 | + $marked = explode( $this->mMarkup['end'], $txt, 2 ); |
399 | 421 | |
400 | | - foreach($this->mVariants as $variant){ |
401 | | - $ret[$variant] .= $this->mMarkup['begin'].$marked[0].$this->mMarkup['end']; |
402 | | - if(array_key_exists(1, $marked)) |
403 | | - $ret[$variant] .= $this->translate($marked[1],$variant); |
| 422 | + foreach ( $this->mVariants as $variant ) { |
| 423 | + $ret[$variant] .= $this->mMarkup['begin'] . $marked[0] . |
| 424 | + $this->mMarkup['end']; |
| 425 | + if ( array_key_exists( 1, $marked ) ) { |
| 426 | + $ret[$variant] .= $this->translate( $marked[1], $variant ); |
| 427 | + } |
404 | 428 | } |
405 | 429 | |
406 | 430 | } |
— | — | @@ -408,52 +432,57 @@ |
409 | 433 | } |
410 | 434 | |
411 | 435 | /** |
412 | | - * prepare manual conversion table |
| 436 | + * Prepare manual conversion table. |
413 | 437 | * @private |
414 | 438 | */ |
415 | | - function applyManualConv( $convRule ){ |
416 | | - // use syntax -{T|zh:TitleZh;zh-tw:TitleTw}- for custom conversion in title |
| 439 | + function applyManualConv( $convRule ) { |
| 440 | + // use syntax -{T|zh:TitleZh;zh-tw:TitleTw}- for custom |
| 441 | + // conversion in title |
417 | 442 | $title = $convRule->getTitle(); |
418 | | - if( $title ){ |
| 443 | + if ( $title ) { |
419 | 444 | $this->mTitleFromFlag = true; |
420 | | - $this->mTitleDisplay = $title; |
| 445 | + $this->mTitleDisplay = $title; |
421 | 446 | } |
422 | 447 | |
423 | | - //apply manual conversion table to global table |
| 448 | + // apply manual conversion table to global table |
424 | 449 | $convTable = $convRule->getConvTable(); |
425 | 450 | $action = $convRule->getRulesAction(); |
426 | | - foreach( $convTable as $variant => $pair ) { |
427 | | - if( !in_array( $variant, $this->mVariants ) )continue; |
428 | | - if( $action == 'add' ) { |
429 | | - foreach( $pair as $from => $to ) { |
| 451 | + foreach ( $convTable as $variant => $pair ) { |
| 452 | + if ( !in_array( $variant, $this->mVariants ) ) { |
| 453 | + continue; |
| 454 | + } |
| 455 | + |
| 456 | + if ( $action == 'add' ) { |
| 457 | + foreach ( $pair as $from => $to ) { |
430 | 458 | // to ensure that $from and $to not be left blank |
431 | 459 | // so $this->translate() could always return a string |
432 | | - if ( $from || $to ) |
| 460 | + if ( $from || $to ) { |
433 | 461 | // more efficient than array_merge(), about 2.5 times. |
434 | 462 | $this->mTables[$variant]->setPair( $from, $to ); |
| 463 | + } |
435 | 464 | } |
436 | | - } |
437 | | - elseif ( $action == 'remove' ) { |
| 465 | + } elseif ( $action == 'remove' ) { |
438 | 466 | $this->mTables[$variant]->removeArray( $pair ); |
439 | 467 | } |
440 | 468 | } |
441 | 469 | } |
442 | 470 | |
443 | 471 | /** |
444 | | - * Convert text using a parser object for context |
| 472 | + * Convert text using a parser object for context. |
445 | 473 | * @public |
446 | 474 | */ |
447 | 475 | function parserConvert( $text, &$parser ) { |
448 | 476 | global $wgDisableLangConversion; |
449 | 477 | /* don't do anything if this is the conversion table */ |
450 | | - if ( $parser->getTitle()->getNamespace() == NS_MEDIAWIKI && |
451 | | - strpos($parser->mTitle->getText(), "Conversiontable") !== false ) |
452 | | - { |
| 478 | + if ( $parser->getTitle()->getNamespace() == NS_MEDIAWIKI |
| 479 | + && strpos( $parser->mTitle->getText(), |
| 480 | + 'Conversiontable' ) !== false ) { |
453 | 481 | return $text; |
454 | 482 | } |
455 | 483 | |
456 | | - if ( $wgDisableLangConversion ) |
| 484 | + if ( $wgDisableLangConversion ) { |
457 | 485 | return $text; |
| 486 | + } |
458 | 487 | |
459 | 488 | $text = $this->convert( $text ); |
460 | 489 | |
— | — | @@ -464,18 +493,20 @@ |
465 | 494 | } |
466 | 495 | |
467 | 496 | /** |
468 | | - * convert namespace |
| 497 | + * Convert namespace. |
469 | 498 | * @param string $title the title included namespace |
470 | 499 | * @return array of string |
471 | 500 | * @private |
472 | 501 | */ |
473 | 502 | function convertNamespace( $title, $variant ) { |
474 | 503 | $splittitle = explode( ':', $title ); |
475 | | - if (count($splittitle) < 2) |
| 504 | + if ( count( $splittitle ) < 2 ) { |
476 | 505 | return $title; |
477 | | - if ( isset( $this->mNamespaceTables[$variant][$splittitle[0]] ) ) |
| 506 | + } |
| 507 | + if ( isset( $this->mNamespaceTables[$variant][$splittitle[0]] ) ) { |
478 | 508 | $splittitle[0] = $this->mNamespaceTables[$variant][$splittitle[0]]; |
479 | | - $ret = implode(':', $splittitle ); |
| 509 | + } |
| 510 | + $ret = implode( ':', $splittitle ); |
480 | 511 | return $ret; |
481 | 512 | } |
482 | 513 | |
— | — | @@ -484,7 +515,7 @@ |
485 | 516 | * store the default converted title to $this->mTitleDisplay. |
486 | 517 | * @private |
487 | 518 | */ |
488 | | - function preConvertTitle( $text, $variant ){ |
| 519 | + function preConvertTitle( $text, $variant ) { |
489 | 520 | $this->mTitleOriginal = $text; |
490 | 521 | |
491 | 522 | $text = $this->convertNamespace( $text, $variant ); |
— | — | @@ -492,34 +523,34 @@ |
493 | 524 | } |
494 | 525 | |
495 | 526 | /** |
496 | | - * convert title |
| 527 | + * Convert title. |
497 | 528 | * @private |
498 | 529 | */ |
499 | | - function convertTitle(){ |
| 530 | + function convertTitle() { |
500 | 531 | global $wgDisableTitleConversion, $wgUser, $wgRequest; |
501 | 532 | $isredir = $wgRequest->getText( 'redirect', 'yes' ); |
502 | 533 | $action = $wgRequest->getText( 'action' ); |
503 | 534 | $linkconvert = $wgRequest->getText( 'linkconvert', 'yes' ); |
504 | 535 | |
505 | 536 | // check for the global variable, __NOTC__ magic word, and user setting |
506 | | - if( $wgDisableTitleConversion || !$this->mDoTitleConvert || |
507 | | - $wgUser->getOption('noconvertlink') == 1 ) { |
| 537 | + if ( $wgDisableTitleConversion || !$this->mDoTitleConvert || |
| 538 | + $wgUser->getOption( 'noconvertlink' ) == 1 ) { |
508 | 539 | $this->mTitleDisplay = $this->mTitleOriginal; |
509 | | - } |
510 | | - |
511 | | - // check for GET params |
512 | | - elseif ( $isredir == 'no' || $action == 'edit' || $linkconvert == 'no' ) { |
| 540 | + } elseif ( $isredir == 'no' |
| 541 | + || $action == 'edit' |
| 542 | + || $linkconvert == 'no' ) { |
| 543 | + // check for GET params |
513 | 544 | $this->mTitleDisplay = $this->mTitleOriginal; |
514 | 545 | } |
515 | 546 | } |
516 | 547 | |
517 | 548 | /** |
518 | | - * convert text to different variants of a language. the automatic |
519 | | - * conversion is done in autoConvert(). here we parse the text |
| 549 | + * Convert text to different variants of a language. The automatic |
| 550 | + * conversion is done in autoConvert(). Here we parse the text |
520 | 551 | * marked with -{}-, which specifies special conversions of the |
521 | | - * text that can not be accomplished in autoConvert() |
| 552 | + * text that can not be accomplished in autoConvert(). |
522 | 553 | * |
523 | | - * syntax of the markup: |
| 554 | + * Syntax of the markup: |
524 | 555 | * -{code1:text1;code2:text2;...}- or |
525 | 556 | * -{flags|code1:text1;code2:text2;...}- or |
526 | 557 | * -{text}- in which case no conversion should take place for text |
— | — | @@ -532,17 +563,19 @@ |
533 | 564 | function convert( $text, $isTitle = false ) { |
534 | 565 | |
535 | 566 | $mw =& MagicWord::get( 'notitleconvert' ); |
536 | | - if( $mw->matchAndRemove( $text ) ) |
| 567 | + if ( $mw->matchAndRemove( $text ) ) { |
537 | 568 | $this->mDoTitleConvert = false; |
| 569 | + } |
538 | 570 | $mw =& MagicWord::get( 'nocontentconvert' ); |
539 | | - if( $mw->matchAndRemove( $text ) ) { |
| 571 | + if ( $mw->matchAndRemove( $text ) ) { |
540 | 572 | $this->mDoContentConvert = false; |
541 | 573 | } |
542 | 574 | |
543 | 575 | // no conversion if redirecting |
544 | 576 | $mw =& MagicWord::get( 'redirect' ); |
545 | | - if( $mw->matchStart( $text ) ) |
| 577 | + if ( $mw->matchStart( $text ) ) { |
546 | 578 | return $text; |
| 579 | + } |
547 | 580 | |
548 | 581 | $plang = $this->getPreferredVariant(); |
549 | 582 | |
— | — | @@ -559,75 +592,87 @@ |
560 | 593 | |
561 | 594 | $marked = explode( $this->mMarkup['begin'], $txt, 2 ); |
562 | 595 | |
563 | | - if( $this->mDoContentConvert ) |
564 | | - // Bug 19620: should convert a string immediately after a new rule added. |
| 596 | + if ( $this->mDoContentConvert ) { |
| 597 | + // Bug 19620: should convert a string immediately after a |
| 598 | + // new rule added. |
565 | 599 | $text .= $this->autoConvert( $marked[0], $plang ); |
566 | | - else |
| 600 | + } else { |
567 | 601 | $text .= $marked[0]; |
| 602 | + } |
568 | 603 | |
569 | 604 | if ( array_key_exists( 1, $marked ) ) { |
570 | | - $crule = new ConverterRule($marked[1], $this); |
| 605 | + $crule = new ConverterRule( $marked[1], $this ); |
571 | 606 | $crule->parse( $plang ); |
572 | 607 | $text .= $crule->getDisplay(); |
573 | 608 | $this->applyManualConv( $crule ); |
| 609 | + } else { |
| 610 | + $text .= $this->mMarkup['end']; |
574 | 611 | } |
575 | | - else |
576 | | - $text .= $this->mMarkup['end']; |
577 | | - |
578 | 612 | } |
579 | 613 | |
580 | 614 | // Remove the last delimiter (wasn't real) |
581 | | - $text = substr( $text, 0, -strlen( $this->mMarkup['end'] ) ); |
| 615 | + $text = substr( $text, 0, - strlen( $this->mMarkup['end'] ) ); |
582 | 616 | return $text; |
583 | 617 | } |
584 | 618 | |
585 | 619 | /** |
586 | | - * if a language supports multiple variants, it is |
| 620 | + * If a language supports multiple variants, it is |
587 | 621 | * possible that non-existing link in one variant |
588 | | - * actually exists in another variant. this function |
| 622 | + * actually exists in another variant. This function |
589 | 623 | * tries to find it. See e.g. LanguageZh.php |
590 | 624 | * |
591 | 625 | * @param string $link the name of the link |
592 | 626 | * @param mixed $nt the title object of the link |
593 | 627 | * @param boolean $ignoreOtherCond: to disable other conditions when |
594 | | - * we need to transclude a template or update a category's link |
| 628 | + * we need to transclude a template or update a category's link |
595 | 629 | * @return null the input parameters may be modified upon return |
596 | 630 | * @public |
597 | 631 | */ |
598 | 632 | function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) { |
599 | 633 | # If the article has already existed, there is no need to |
600 | 634 | # check it again, otherwise it may cause a fault. |
601 | | - if ( is_object( $nt ) && $nt->exists() ) |
| 635 | + if ( is_object( $nt ) && $nt->exists() ) { |
602 | 636 | return; |
| 637 | + } |
603 | 638 | |
604 | | - global $wgDisableLangConversion, $wgDisableTitleConversion, $wgRequest, $wgUser; |
| 639 | + global $wgDisableLangConversion, $wgDisableTitleConversion, $wgRequest, |
| 640 | + $wgUser; |
605 | 641 | $isredir = $wgRequest->getText( 'redirect', 'yes' ); |
606 | 642 | $action = $wgRequest->getText( 'action' ); |
607 | 643 | $linkconvert = $wgRequest->getText( 'linkconvert', 'yes' ); |
608 | | - $disableLinkConversion = $wgDisableLangConversion || $wgDisableTitleConversion; |
| 644 | + $disableLinkConversion = $wgDisableLangConversion |
| 645 | + || $wgDisableTitleConversion; |
609 | 646 | $linkBatch = new LinkBatch(); |
610 | 647 | |
611 | | - $ns=NS_MAIN; |
| 648 | + $ns = NS_MAIN; |
612 | 649 | |
613 | | - if ( $disableLinkConversion || ( !$ignoreOtherCond && ( $isredir == 'no' || $action == 'edit' |
614 | | - || $action == 'submit' || $linkconvert == 'no' || $wgUser->getOption('noconvertlink') == 1 ) ) ) |
| 650 | + if ( $disableLinkConversion || |
| 651 | + ( !$ignoreOtherCond && |
| 652 | + ( $isredir == 'no' |
| 653 | + || $action == 'edit' |
| 654 | + || $action == 'submit' |
| 655 | + || $linkconvert == 'no' |
| 656 | + || $wgUser->getOption( 'noconvertlink' ) == 1 ) ) ) { |
615 | 657 | return; |
| 658 | + } |
616 | 659 | |
617 | | - if ( is_object( $nt ) ) |
| 660 | + if ( is_object( $nt ) ) { |
618 | 661 | $ns = $nt->getNamespace(); |
| 662 | + } |
619 | 663 | |
620 | | - $variants = $this->autoConvertToAllVariants($link); |
621 | | - if($variants == false) //give up |
| 664 | + $variants = $this->autoConvertToAllVariants( $link ); |
| 665 | + if ( $variants == false ) { // give up |
622 | 666 | return; |
| 667 | + } |
623 | 668 | |
624 | 669 | $titles = array(); |
625 | 670 | |
626 | | - foreach( $variants as $v ) { |
627 | | - if($v != $link){ |
| 671 | + foreach ( $variants as $v ) { |
| 672 | + if ( $v != $link ) { |
628 | 673 | $varnt = Title::newFromText( $v, $ns ); |
629 | | - if(!is_null($varnt)){ |
630 | | - $linkBatch->addObj($varnt); |
631 | | - $titles[]=$varnt; |
| 674 | + if ( !is_null( $varnt ) ) { |
| 675 | + $linkBatch->addObj( $varnt ); |
| 676 | + $titles[] = $varnt; |
632 | 677 | } |
633 | 678 | } |
634 | 679 | } |
— | — | @@ -635,8 +680,8 @@ |
636 | 681 | // fetch all variants in single query |
637 | 682 | $linkBatch->execute(); |
638 | 683 | |
639 | | - foreach( $titles as $varnt ) { |
640 | | - if( $varnt->getArticleID() > 0 ) { |
| 684 | + foreach ( $titles as $varnt ) { |
| 685 | + if ( $varnt->getArticleID() > 0 ) { |
641 | 686 | $nt = $varnt; |
642 | 687 | $link = $varnt->getText(); |
643 | 688 | break; |
— | — | @@ -645,7 +690,7 @@ |
646 | 691 | } |
647 | 692 | |
648 | 693 | /** |
649 | | - * returns language specific hash options |
| 694 | + * Returns language specific hash options. |
650 | 695 | * |
651 | 696 | * @public |
652 | 697 | */ |
— | — | @@ -655,7 +700,7 @@ |
656 | 701 | } |
657 | 702 | |
658 | 703 | /** |
659 | | - * get title text as defined in the body of the article text |
| 704 | + * Get title text as defined in the body of the article text. |
660 | 705 | * |
661 | 706 | * @public |
662 | 707 | */ |
— | — | @@ -664,103 +709,109 @@ |
665 | 710 | } |
666 | 711 | |
667 | 712 | /** |
668 | | - * Load default conversion tables |
669 | | - * This method must be implemented in derived class |
| 713 | + * Load default conversion tables. |
| 714 | + * This method must be implemented in derived class. |
670 | 715 | * |
671 | 716 | * @private |
672 | 717 | */ |
673 | 718 | function loadDefaultTables() { |
674 | | - $name = get_class($this); |
675 | | - wfDie("Must implement loadDefaultTables() method in class $name"); |
| 719 | + $name = get_class( $this ); |
| 720 | + wfDie( "Must implement loadDefaultTables() method in class $name" ); |
676 | 721 | } |
677 | 722 | |
678 | 723 | /** |
679 | | - * load conversion tables either from the cache or the disk |
| 724 | + * Load conversion tables either from the cache or the disk. |
680 | 725 | * @private |
681 | 726 | */ |
682 | | - function loadTables($fromcache=true) { |
| 727 | + function loadTables( $fromcache = true ) { |
683 | 728 | global $wgMemc; |
684 | | - if( $this->mTablesLoaded ) |
| 729 | + if ( $this->mTablesLoaded ) { |
685 | 730 | return; |
| 731 | + } |
686 | 732 | wfProfileIn( __METHOD__ ); |
687 | 733 | $this->mTablesLoaded = true; |
688 | 734 | $this->mTables = false; |
689 | | - if($fromcache) { |
690 | | - wfProfileIn( __METHOD__.'-cache' ); |
| 735 | + if ( $fromcache ) { |
| 736 | + wfProfileIn( __METHOD__ . '-cache' ); |
691 | 737 | $this->mTables = $wgMemc->get( $this->mCacheKey ); |
692 | | - wfProfileOut( __METHOD__.'-cache' ); |
| 738 | + wfProfileOut( __METHOD__ . '-cache' ); |
693 | 739 | } |
694 | | - if ( !$this->mTables || !isset( $this->mTables[self::CACHE_VERSION_KEY] ) ) { |
695 | | - wfProfileIn( __METHOD__.'-recache' ); |
| 740 | + if ( !$this->mTables |
| 741 | + || !isset( $this->mTables[self::CACHE_VERSION_KEY] ) ) { |
| 742 | + wfProfileIn( __METHOD__ . '-recache' ); |
696 | 743 | // not in cache, or we need a fresh reload. |
697 | 744 | // we will first load the default tables |
698 | 745 | // then update them using things in MediaWiki:Zhconversiontable/* |
699 | 746 | $this->loadDefaultTables(); |
700 | | - foreach($this->mVariants as $var) { |
701 | | - $cached = $this->parseCachedTable($var); |
702 | | - $this->mTables[$var]->mergeArray($cached); |
| 747 | + foreach ( $this->mVariants as $var ) { |
| 748 | + $cached = $this->parseCachedTable( $var ); |
| 749 | + $this->mTables[$var]->mergeArray( $cached ); |
703 | 750 | } |
704 | 751 | |
705 | 752 | $this->postLoadTables(); |
706 | 753 | $this->mTables[self::CACHE_VERSION_KEY] = true; |
707 | 754 | |
708 | | - $wgMemc->set($this->mCacheKey, $this->mTables, 43200); |
709 | | - wfProfileOut( __METHOD__.'-recache' ); |
| 755 | + $wgMemc->set( $this->mCacheKey, $this->mTables, 43200 ); |
| 756 | + wfProfileOut( __METHOD__ . '-recache' ); |
710 | 757 | } |
711 | 758 | wfProfileOut( __METHOD__ ); |
712 | 759 | } |
713 | 760 | |
714 | 761 | /** |
715 | | - * Hook for post processig after conversion tables are loaded |
| 762 | + * Hook for post processig after conversion tables are loaded. |
716 | 763 | * |
717 | 764 | */ |
718 | | - function postLoadTables() {} |
| 765 | + function postLoadTables() { } |
719 | 766 | |
720 | 767 | /** |
721 | | - * Reload the conversion tables |
| 768 | + * Reload the conversion tables. |
722 | 769 | * |
723 | 770 | * @private |
724 | 771 | */ |
725 | 772 | function reloadTables() { |
726 | | - if($this->mTables) |
727 | | - unset($this->mTables); |
| 773 | + if ( $this->mTables ) { |
| 774 | + unset( $this->mTables ); |
| 775 | + } |
728 | 776 | $this->mTablesLoaded = false; |
729 | | - $this->loadTables(false); |
| 777 | + $this->loadTables( false ); |
730 | 778 | } |
731 | 779 | |
732 | 780 | |
733 | 781 | /** |
734 | | - * parse the conversion table stored in the cache |
| 782 | + * Parse the conversion table stored in the cache. |
735 | 783 | * |
736 | | - * the tables should be in blocks of the following form: |
| 784 | + * The tables should be in blocks of the following form: |
737 | 785 | * -{ |
738 | 786 | * word => word ; |
739 | 787 | * word => word ; |
740 | 788 | * ... |
741 | 789 | * }- |
742 | 790 | * |
743 | | - * to make the tables more manageable, subpages are allowed |
744 | | - * and will be parsed recursively if $recursive=true |
| 791 | + * To make the tables more manageable, subpages are allowed |
| 792 | + * and will be parsed recursively if $recursive == true. |
745 | 793 | * |
746 | 794 | */ |
747 | | - function parseCachedTable($code, $subpage='', $recursive=true) { |
| 795 | + function parseCachedTable( $code, $subpage = '', $recursive = true ) { |
748 | 796 | global $wgMessageCache; |
749 | 797 | static $parsed = array(); |
750 | 798 | |
751 | | - if(!is_object($wgMessageCache)) |
| 799 | + if ( !is_object( $wgMessageCache ) ) { |
752 | 800 | return array(); |
| 801 | + } |
753 | 802 | |
754 | | - $key = 'Conversiontable/'.$code; |
755 | | - if($subpage) |
| 803 | + $key = 'Conversiontable/' . $code; |
| 804 | + if ( $subpage ) { |
756 | 805 | $key .= '/' . $subpage; |
757 | | - |
758 | | - if(array_key_exists($key, $parsed)) |
| 806 | + } |
| 807 | + if ( array_key_exists( $key, $parsed ) ) { |
759 | 808 | return array(); |
| 809 | + } |
760 | 810 | |
761 | 811 | if ( strpos( $code, '/' ) === false ) { |
762 | 812 | $txt = $wgMessageCache->get( 'Conversiontable', true, $code ); |
763 | 813 | } else { |
764 | | - $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Conversiontable/$code" ); |
| 814 | + $title = Title::makeTitleSafe( NS_MEDIAWIKI, |
| 815 | + "Conversiontable/$code" ); |
765 | 816 | if ( $title && $title->exists() ) { |
766 | 817 | $article = new Article( $title ); |
767 | 818 | $txt = $article->getContents(); |
— | — | @@ -771,57 +822,61 @@ |
772 | 823 | |
773 | 824 | // get all subpage links of the form |
774 | 825 | // [[MediaWiki:conversiontable/zh-xx/...|...]] |
775 | | - $linkhead = $this->mLangObj->getNsText(NS_MEDIAWIKI) . ':Conversiontable'; |
776 | | - $subs = explode('[[', $txt); |
| 826 | + $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) . |
| 827 | + ':Conversiontable'; |
| 828 | + $subs = explode( '[[', $txt ); |
777 | 829 | $sublinks = array(); |
778 | | - foreach( $subs as $sub ) { |
779 | | - $link = explode(']]', $sub, 2); |
780 | | - if(count($link) != 2) |
| 830 | + foreach ( $subs as $sub ) { |
| 831 | + $link = explode( ']]', $sub, 2 ); |
| 832 | + if ( count( $link ) != 2 ) { |
781 | 833 | continue; |
782 | | - $b = explode('|', $link[0]); |
783 | | - $b = explode('/', trim($b[0]), 3); |
784 | | - if(count($b)==3) |
| 834 | + } |
| 835 | + $b = explode( '|', $link[0] ); |
| 836 | + $b = explode( '/', trim( $b[0] ), 3 ); |
| 837 | + if ( count( $b ) == 3 ) { |
785 | 838 | $sublink = $b[2]; |
786 | | - else |
| 839 | + } else { |
787 | 840 | $sublink = ''; |
| 841 | + } |
788 | 842 | |
789 | | - if($b[0] == $linkhead && $b[1] == $code) { |
| 843 | + if ( $b[0] == $linkhead && $b[1] == $code ) { |
790 | 844 | $sublinks[] = $sublink; |
791 | 845 | } |
792 | 846 | } |
793 | 847 | |
794 | 848 | |
795 | 849 | // parse the mappings in this page |
796 | | - $blocks = explode($this->mMarkup['begin'], $txt); |
797 | | - array_shift($blocks); |
| 850 | + $blocks = explode( $this->mMarkup['begin'], $txt ); |
| 851 | + array_shift( $blocks ); |
798 | 852 | $ret = array(); |
799 | | - foreach($blocks as $block) { |
800 | | - $mappings = explode($this->mMarkup['end'], $block, 2); |
801 | | - $stripped = str_replace(array("'", '"', '*','#'), '', $mappings[0]); |
| 853 | + foreach ( $blocks as $block ) { |
| 854 | + $mappings = explode( $this->mMarkup['end'], $block, 2 ); |
| 855 | + $stripped = str_replace( array( "'", '"', '*', '#' ), '', |
| 856 | + $mappings[0] ); |
802 | 857 | $table = explode( ';', $stripped ); |
803 | | - foreach( $table as $t ) { |
| 858 | + foreach ( $table as $t ) { |
804 | 859 | $m = explode( '=>', $t ); |
805 | | - if( count( $m ) != 2) |
| 860 | + if ( count( $m ) != 2 ) |
806 | 861 | continue; |
807 | 862 | // trim any trailling comments starting with '//' |
808 | | - $tt = explode('//', $m[1], 2); |
809 | | - $ret[trim($m[0])] = trim($tt[0]); |
| 863 | + $tt = explode( '//', $m[1], 2 ); |
| 864 | + $ret[trim( $m[0] )] = trim( $tt[0] ); |
810 | 865 | } |
811 | 866 | } |
812 | 867 | $parsed[$key] = true; |
813 | 868 | |
814 | 869 | |
815 | 870 | // recursively parse the subpages |
816 | | - if($recursive) { |
817 | | - foreach($sublinks as $link) { |
818 | | - $s = $this->parseCachedTable($code, $link, $recursive); |
819 | | - $ret = array_merge($ret, $s); |
| 871 | + if ( $recursive ) { |
| 872 | + foreach ( $sublinks as $link ) { |
| 873 | + $s = $this->parseCachedTable( $code, $link, $recursive ); |
| 874 | + $ret = array_merge( $ret, $s ); |
820 | 875 | } |
821 | 876 | } |
822 | 877 | |
823 | | - if ($this->mUcfirst) { |
824 | | - foreach ($ret as $k => $v) { |
825 | | - $ret[Language::ucfirst($k)] = Language::ucfirst($v); |
| 878 | + if ( $this->mUcfirst ) { |
| 879 | + foreach ( $ret as $k => $v ) { |
| 880 | + $ret[Language::ucfirst( $k )] = Language::ucfirst( $v ); |
826 | 881 | } |
827 | 882 | } |
828 | 883 | return $ret; |
— | — | @@ -829,42 +884,45 @@ |
830 | 885 | |
831 | 886 | /** |
832 | 887 | * Enclose a string with the "no conversion" tag. This is used by |
833 | | - * various functions in the Parser |
| 888 | + * various functions in the Parser. |
834 | 889 | * |
835 | 890 | * @param string $text text to be tagged for no conversion |
836 | 891 | * @return string the tagged text |
837 | 892 | * @public |
838 | 893 | */ |
839 | | - function markNoConversion($text, $noParse=false) { |
| 894 | + function markNoConversion( $text, $noParse = false ) { |
840 | 895 | # don't mark if already marked |
841 | | - if(strpos($text, $this->mMarkup['begin']) || |
842 | | - strpos($text, $this->mMarkup['end'])) |
| 896 | + if ( strpos( $text, $this->mMarkup['begin'] ) |
| 897 | + || strpos( $text, $this->mMarkup['end'] ) ) { |
843 | 898 | return $text; |
| 899 | + } |
844 | 900 | |
845 | | - $ret = $this->mMarkup['begin'] .'R|'. $text . $this->mMarkup['end']; |
| 901 | + $ret = $this->mMarkup['begin'] . 'R|' . $text . $this->mMarkup['end']; |
846 | 902 | return $ret; |
847 | 903 | } |
848 | 904 | |
849 | 905 | /** |
850 | | - * convert the sorting key for category links. this should make different |
851 | | - * keys that are variants of each other map to the same key |
| 906 | + * Convert the sorting key for category links. This should make different |
| 907 | + * keys that are variants of each other map to the same key. |
852 | 908 | */ |
853 | 909 | function convertCategoryKey( $key ) { |
854 | 910 | return $key; |
855 | 911 | } |
| 912 | + |
856 | 913 | /** |
857 | | - * hook to refresh the cache of conversion tables when |
858 | | - * MediaWiki:conversiontable* is updated |
| 914 | + * Hook to refresh the cache of conversion tables when |
| 915 | + * MediaWiki:conversiontable* is updated. |
859 | 916 | * @private |
860 | 917 | */ |
861 | | - function OnArticleSaveComplete($article, $user, $text, $summary, $isminor, $iswatch, $section, $flags, $revision) { |
| 918 | + function OnArticleSaveComplete( $article, $user, $text, $summary, $isminor, |
| 919 | + $iswatch, $section, $flags, $revision ) { |
862 | 920 | $titleobj = $article->getTitle(); |
863 | | - if($titleobj->getNamespace() == NS_MEDIAWIKI) { |
| 921 | + if ( $titleobj->getNamespace() == NS_MEDIAWIKI ) { |
864 | 922 | $title = $titleobj->getDBkey(); |
865 | | - $t = explode('/', $title, 3); |
866 | | - $c = count($t); |
867 | | - if( $c > 1 && $t[0] == 'Conversiontable' ) { |
868 | | - if(in_array($t[1], $this->mVariants)) { |
| 923 | + $t = explode( '/', $title, 3 ); |
| 924 | + $c = count( $t ); |
| 925 | + if ( $c > 1 && $t[0] == 'Conversiontable' ) { |
| 926 | + if ( in_array( $t[1], $this->mVariants ) ) { |
869 | 927 | $this->reloadTables(); |
870 | 928 | } |
871 | 929 | } |
— | — | @@ -873,21 +931,21 @@ |
874 | 932 | } |
875 | 933 | |
876 | 934 | /** |
877 | | - * Armour rendered math against conversion |
878 | | - * Wrap math into rawoutput -{R| math }- syntax |
| 935 | + * Armour rendered math against conversion. |
| 936 | + * Wrap math into rawoutput -{R| math }- syntax. |
879 | 937 | * @public |
880 | 938 | */ |
881 | | - function armourMath($text){ |
882 | | - // we need to convert '-{' and '}-' to '-{' and '}-' |
883 | | - // to avoid a unwanted '}-' appeared after the math-image. |
884 | | - $text = strtr( $text, array('-{' => '-{', '}-' => '}-') ); |
| 939 | + function armourMath( $text ) { |
| 940 | + // we need to convert '-{' and '}-' to '-{' and '}-' |
| 941 | + // to avoid a unwanted '}-' appeared after the math-image. |
| 942 | + $text = strtr( $text, array( '-{' => '-{', '}-' => '}-' ) ); |
885 | 943 | $ret = $this->mMarkup['begin'] . 'R|' . $text . $this->mMarkup['end']; |
886 | 944 | return $ret; |
887 | 945 | } |
888 | 946 | } |
889 | 947 | |
890 | 948 | /** |
891 | | - * parser for rules of language conversion , parse rules in -{ }- tag |
| 949 | + * Parser for rules of language conversion , parse rules in -{ }- tag. |
892 | 950 | * @ingroup Language |
893 | 951 | * @author fdcn <fdcn64@gmail.com>, PhiLiP <philip.npc@gmail.com> |
894 | 952 | */ |
— | — | @@ -911,26 +969,30 @@ |
912 | 970 | * @param object $converter a LanguageConverter object |
913 | 971 | * @access public |
914 | 972 | */ |
915 | | - function __construct( $text, $converter ){ |
| 973 | + function __construct( $text, $converter ) { |
916 | 974 | $this->mText = $text; |
917 | 975 | $this->mConverter = $converter; |
918 | | - foreach( $converter->mVariants as $v ){ |
| 976 | + foreach ( $converter->mVariants as $v ) { |
919 | 977 | $this->mConvTable[$v] = array(); |
920 | 978 | } |
921 | 979 | } |
922 | 980 | |
923 | 981 | /** |
924 | | - * check if variants array in convert array |
| 982 | + * Check if variants array in convert array. |
925 | 983 | * |
926 | 984 | * @param string $variant Variant language code |
927 | 985 | * @return string Translated text |
928 | 986 | * @public |
929 | 987 | */ |
930 | | - function getTextInBidtable( $variants ){ |
931 | | - if( is_string( $variants ) ){ $variants = array( $variants ); } |
932 | | - if( !is_array( $variants ) ) return false; |
933 | | - foreach( $variants as $variant ){ |
934 | | - if( array_key_exists( $variant, $this->mBidtable ) ){ |
| 988 | + function getTextInBidtable( $variants ) { |
| 989 | + if ( is_string( $variants ) ) { |
| 990 | + $variants = array( $variants ); |
| 991 | + } |
| 992 | + if ( !is_array( $variants ) ) { |
| 993 | + return false; |
| 994 | + } |
| 995 | + foreach ( $variants as $variant ) { |
| 996 | + if ( array_key_exists( $variant, $this->mBidtable ) ) { |
935 | 997 | return $this->mBidtable[$variant]; |
936 | 998 | } |
937 | 999 | } |
— | — | @@ -941,9 +1003,9 @@ |
942 | 1004 | * Parse flags with syntax -{FLAG| ... }- |
943 | 1005 | * @private |
944 | 1006 | */ |
945 | | - function parseFlags(){ |
| 1007 | + function parseFlags() { |
946 | 1008 | $text = $this->mText; |
947 | | - if(strlen($text) < 2 ) { |
| 1009 | + if ( strlen( $text ) < 2 ) { |
948 | 1010 | $this->mFlags = array( 'R' ); |
949 | 1011 | $this->mRules = $text; |
950 | 1012 | return; |
— | — | @@ -954,60 +1016,68 @@ |
955 | 1017 | $validFlags = $this->mConverter->mFlags; |
956 | 1018 | $variants = $this->mConverter->mVariants; |
957 | 1019 | |
958 | | - $tt = explode($markup['flagsep'], $text, 2); |
959 | | - if(count($tt) == 2) { |
960 | | - $f = explode($markup['varsep'], $tt[0]); |
961 | | - foreach($f as $ff) { |
962 | | - $ff = trim($ff); |
963 | | - if(array_key_exists($ff, $validFlags) && |
964 | | - !in_array($validFlags[$ff], $flags)) |
| 1020 | + $tt = explode( $markup['flagsep'], $text, 2 ); |
| 1021 | + if ( count( $tt ) == 2 ) { |
| 1022 | + $f = explode( $markup['varsep'], $tt[0] ); |
| 1023 | + foreach ( $f as $ff ) { |
| 1024 | + $ff = trim( $ff ); |
| 1025 | + if ( array_key_exists( $ff, $validFlags ) |
| 1026 | + && !in_array( $validFlags[$ff], $flags ) ) { |
965 | 1027 | $flags[] = $validFlags[$ff]; |
| 1028 | + } |
966 | 1029 | } |
967 | 1030 | $rules = $tt[1]; |
968 | 1031 | } else { |
969 | 1032 | $rules = $text; |
970 | 1033 | } |
971 | 1034 | |
972 | | - //check flags |
973 | | - if( in_array('R',$flags) ){ |
974 | | - $flags = array('R');// remove other flags |
975 | | - } elseif ( in_array('N',$flags) ){ |
976 | | - $flags = array('N');// remove other flags |
977 | | - } elseif ( in_array('-',$flags) ){ |
978 | | - $flags = array('-');// remove other flags |
979 | | - } elseif (count($flags)==1 && $flags[0]=='T'){ |
980 | | - $flags[]='H'; |
981 | | - } elseif ( in_array('H',$flags) ){ |
| 1035 | + // check flags |
| 1036 | + if ( in_array( 'R', $flags ) ) { |
| 1037 | + $flags = array( 'R' );// remove other flags |
| 1038 | + } elseif ( in_array( 'N', $flags ) ) { |
| 1039 | + $flags = array( 'N' );// remove other flags |
| 1040 | + } elseif ( in_array( '-', $flags ) ) { |
| 1041 | + $flags = array( '-' );// remove other flags |
| 1042 | + } elseif ( count( $flags ) == 1 && $flags[0] == 'T' ) { |
| 1043 | + $flags[] = 'H'; |
| 1044 | + } elseif ( in_array( 'H', $flags ) ) { |
982 | 1045 | // replace A flag, and remove other flags except T |
983 | | - $temp=array('+','H'); |
984 | | - if(in_array('T',$flags)) $temp[] = 'T'; |
985 | | - if(in_array('D',$flags)) $temp[] = 'D'; |
| 1046 | + $temp = array( '+', 'H' ); |
| 1047 | + if ( in_array( 'T', $flags ) ) { |
| 1048 | + $temp[] = 'T'; |
| 1049 | + } |
| 1050 | + if ( in_array( 'D', $flags ) ) { |
| 1051 | + $temp[] = 'D'; |
| 1052 | + } |
986 | 1053 | $flags = $temp; |
987 | 1054 | } else { |
988 | | - if ( in_array('A',$flags) ) { |
989 | | - $flags[]='+'; |
990 | | - $flags[]='S'; |
| 1055 | + if ( in_array( 'A', $flags ) ) { |
| 1056 | + $flags[] = '+'; |
| 1057 | + $flags[] = 'S'; |
991 | 1058 | } |
992 | | - if ( in_array('D',$flags) ) |
993 | | - $flags=array_diff($flags,array('S')); |
| 1059 | + if ( in_array( 'D', $flags ) ) { |
| 1060 | + $flags = array_diff( $flags, array( 'S' ) ); |
| 1061 | + } |
994 | 1062 | $flags_temp = array(); |
995 | | - foreach ($variants as $variant) { |
| 1063 | + foreach ( $variants as $variant ) { |
996 | 1064 | // try to find flags like "zh-hans", "zh-hant" |
997 | 1065 | // allow syntaxes like "-{zh-hans;zh-hant|XXXX}-" |
998 | | - if ( in_array($variant, $flags) ) |
| 1066 | + if ( in_array( $variant, $flags ) ) |
999 | 1067 | $flags_temp[] = $variant; |
1000 | 1068 | } |
1001 | | - if ( count($flags_temp) !== 0 ) |
| 1069 | + if ( count( $flags_temp ) !== 0 ) { |
1002 | 1070 | $flags = $flags_temp; |
| 1071 | + } |
1003 | 1072 | } |
1004 | | - if ( count($flags) == 0 ) |
1005 | | - $flags = array('S'); |
1006 | | - $this->mRules=$rules; |
1007 | | - $this->mFlags=$flags; |
| 1073 | + if ( count( $flags ) == 0 ) { |
| 1074 | + $flags = array( 'S' ); |
| 1075 | + } |
| 1076 | + $this->mRules = $rules; |
| 1077 | + $this->mFlags = $flags; |
1008 | 1078 | } |
1009 | 1079 | |
1010 | 1080 | /** |
1011 | | - * generate conversion table |
| 1081 | + * Generate conversion table. |
1012 | 1082 | * @private |
1013 | 1083 | */ |
1014 | 1084 | function parseRules() { |
— | — | @@ -1021,44 +1091,52 @@ |
1022 | 1092 | // varsep_pattern for preg_split: |
1023 | 1093 | // text should be splited by ";" only if a valid variant |
1024 | 1094 | // name exist after the markup, for example: |
1025 | | - // -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:<span style="font-size:120%;">yyy</span>;}- |
| 1095 | + // -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:\ |
| 1096 | + // <span style="font-size:120%;">yyy</span>;}- |
1026 | 1097 | // we should split it as: |
1027 | 1098 | // array( |
1028 | | - // [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>' |
1029 | | - // [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>' |
1030 | | - // [2] => '' |
1031 | | - // ) |
| 1099 | + // [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>' |
| 1100 | + // [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>' |
| 1101 | + // [2] => '' |
| 1102 | + // ) |
1032 | 1103 | $varsep_pattern = '/' . $markup['varsep'] . '\s*' . '(?='; |
1033 | | - foreach( $variants as $variant ) { |
1034 | | - $varsep_pattern .= $variant . '\s*' . $markup['codesep'] . '|'; // zh-hans:xxx;zh-hant:yyy |
| 1104 | + foreach ( $variants as $variant ) { |
| 1105 | + // zh-hans:xxx;zh-hant:yyy |
| 1106 | + $varsep_pattern .= $variant . '\s*' . $markup['codesep'] . '|'; |
| 1107 | + // xxx=>zh-hans:yyy; xxx=>zh-hant:zzz |
1035 | 1108 | $varsep_pattern .= '[^;]*?' . $markup['unidsep'] . '\s*' . $variant |
1036 | | - . '\s*' . $markup['codesep'] . '|'; // xxx=>zh-hans:yyy; xxx=>zh-hant:zzz |
| 1109 | + . '\s*' . $markup['codesep'] . '|'; |
1037 | 1110 | } |
1038 | 1111 | $varsep_pattern .= '\s*$)/'; |
1039 | 1112 | |
1040 | | - $choice = preg_split($varsep_pattern, $rules); |
| 1113 | + $choice = preg_split( $varsep_pattern, $rules ); |
1041 | 1114 | |
1042 | | - foreach( $choice as $c ) { |
1043 | | - $v = explode($markup['codesep'], $c, 2); |
1044 | | - if( count($v) != 2 ) |
1045 | | - continue;// syntax error, skip |
1046 | | - $to = trim($v[1]); |
1047 | | - $v = trim($v[0]); |
1048 | | - $u = explode($markup['unidsep'], $v, 2); |
| 1115 | + foreach ( $choice as $c ) { |
| 1116 | + $v = explode( $markup['codesep'], $c, 2 ); |
| 1117 | + if ( count( $v ) != 2 ) { |
| 1118 | + // syntax error, skip |
| 1119 | + continue; |
| 1120 | + } |
| 1121 | + $to = trim( $v[1] ); |
| 1122 | + $v = trim( $v[0] ); |
| 1123 | + $u = explode( $markup['unidsep'], $v, 2 ); |
1049 | 1124 | // if $to is empty, strtr() could return a wrong result |
1050 | | - if( count($u) == 1 && $to && in_array( $v, $variants ) ) { |
| 1125 | + if ( count( $u ) == 1 && $to && in_array( $v, $variants ) ) { |
1051 | 1126 | $bidtable[$v] = $to; |
1052 | | - } else if(count($u) == 2){ |
1053 | | - $from = trim($u[0]); |
1054 | | - $v = trim($u[1]); |
1055 | | - if( array_key_exists( $v, $unidtable ) && !is_array( $unidtable[$v] ) |
1056 | | - && $to && in_array( $v, $variants ) ) |
1057 | | - $unidtable[$v] = array( $from=>$to ); |
1058 | | - elseif ( $to && in_array( $v, $variants ) ) |
| 1127 | + } elseif ( count( $u ) == 2 ) { |
| 1128 | + $from = trim( $u[0] ); |
| 1129 | + $v = trim( $u[1] ); |
| 1130 | + if ( array_key_exists( $v, $unidtable ) |
| 1131 | + && !is_array( $unidtable[$v] ) |
| 1132 | + && $to |
| 1133 | + && in_array( $v, $variants ) ) { |
| 1134 | + $unidtable[$v] = array( $from => $to ); |
| 1135 | + } elseif ( $to && in_array( $v, $variants ) ) { |
1059 | 1136 | $unidtable[$v][$from] = $to; |
| 1137 | + } |
1060 | 1138 | } |
1061 | 1139 | // syntax error, pass |
1062 | | - if ( !array_key_exists( $v, $this->mConverter->mVariantNames ) ){ |
| 1140 | + if ( !array_key_exists( $v, $this->mConverter->mVariantNames ) ) { |
1063 | 1141 | $bidtable = array(); |
1064 | 1142 | $unidtable = array(); |
1065 | 1143 | break; |
— | — | @@ -1071,111 +1149,121 @@ |
1072 | 1150 | /** |
1073 | 1151 | * @private |
1074 | 1152 | */ |
1075 | | - function getRulesDesc(){ |
| 1153 | + function getRulesDesc() { |
1076 | 1154 | $codesep = $this->mConverter->mDescCodeSep; |
1077 | 1155 | $varsep = $this->mConverter->mDescVarSep; |
1078 | | - $text=''; |
1079 | | - foreach($this->mBidtable as $k => $v) |
1080 | | - $text .= $this->mConverter->mVariantNames[$k]."$codesep$v$varsep"; |
1081 | | - foreach($this->mUnidtable as $k => $a) |
1082 | | - foreach($a as $from=>$to) |
1083 | | - $text.=$from.'⇒'.$this->mConverter->mVariantNames[$k]."$codesep$to$varsep"; |
| 1156 | + $text = ''; |
| 1157 | + foreach ( $this->mBidtable as $k => $v ) { |
| 1158 | + $text .= $this->mConverter->mVariantNames[$k] . "$codesep$v$varsep"; |
| 1159 | + } |
| 1160 | + foreach ( $this->mUnidtable as $k => $a ) { |
| 1161 | + foreach ( $a as $from => $to ) { |
| 1162 | + $text .= $from . '⇒' . $this->mConverter->mVariantNames[$k] . |
| 1163 | + "$codesep$to$varsep"; |
| 1164 | + } |
| 1165 | + } |
1084 | 1166 | return $text; |
1085 | 1167 | } |
1086 | 1168 | |
1087 | 1169 | /** |
1088 | | - * Parse rules conversion |
| 1170 | + * Parse rules conversion. |
1089 | 1171 | * @private |
1090 | 1172 | */ |
1091 | | - function getRuleConvertedStr( $variant, $doConvert ){ |
| 1173 | + function getRuleConvertedStr( $variant, $doConvert ) { |
1092 | 1174 | $bidtable = $this->mBidtable; |
1093 | 1175 | $unidtable = $this->mUnidtable; |
1094 | 1176 | |
1095 | | - if( count($bidtable) + count($unidtable) == 0 ){ |
| 1177 | + if ( count( $bidtable ) + count( $unidtable ) == 0 ) { |
1096 | 1178 | return $this->mRules; |
1097 | | - } elseif ( $doConvert ){// the text converted |
| 1179 | + } elseif ( $doConvert ) { // the text converted |
1098 | 1180 | // display current variant in bidirectional array |
1099 | | - $disp = $this->getTextInBidtable($variant); |
| 1181 | + $disp = $this->getTextInBidtable( $variant ); |
1100 | 1182 | // or display current variant in fallbacks |
1101 | | - if(!$disp) |
| 1183 | + if ( !$disp ) { |
1102 | 1184 | $disp = $this->getTextInBidtable( |
1103 | | - $this->mConverter->getVariantFallbacks($variant)); |
| 1185 | + $this->mConverter->getVariantFallbacks( $variant ) ); |
| 1186 | + } |
1104 | 1187 | // or display current variant in unidirectional array |
1105 | | - if(!$disp && array_key_exists($variant,$unidtable)){ |
1106 | | - $disp = array_values($unidtable[$variant]); |
| 1188 | + if ( !$disp && array_key_exists( $variant, $unidtable ) ) { |
| 1189 | + $disp = array_values( $unidtable[$variant] ); |
1107 | 1190 | $disp = $disp[0]; |
1108 | 1191 | } |
1109 | 1192 | // or display frist text under disable manual convert |
1110 | | - if(!$disp && $this->mConverter->mManualLevel[$variant]=='disable') { |
1111 | | - if(count($bidtable)>0){ |
1112 | | - $disp = array_values($bidtable); |
| 1193 | + if ( !$disp |
| 1194 | + && $this->mConverter->mManualLevel[$variant] == 'disable' ) { |
| 1195 | + if ( count( $bidtable ) > 0 ) { |
| 1196 | + $disp = array_values( $bidtable ); |
1113 | 1197 | $disp = $disp[0]; |
1114 | 1198 | } else { |
1115 | | - $disp = array_values($unidtable); |
1116 | | - $disp = array_values($disp[0]); |
| 1199 | + $disp = array_values( $unidtable ); |
| 1200 | + $disp = array_values( $disp[0] ); |
1117 | 1201 | $disp = $disp[0]; |
1118 | 1202 | } |
1119 | 1203 | } |
1120 | 1204 | return $disp; |
1121 | | - } else {// no convert |
| 1205 | + } else { // no convert |
1122 | 1206 | return $this->mRules; |
1123 | 1207 | } |
1124 | 1208 | } |
1125 | 1209 | |
1126 | 1210 | /** |
1127 | | - * generate conversion table for all text |
| 1211 | + * Generate conversion table for all text. |
1128 | 1212 | * @private |
1129 | 1213 | */ |
1130 | | - function generateConvTable(){ |
| 1214 | + function generateConvTable() { |
1131 | 1215 | $flags = $this->mFlags; |
1132 | 1216 | $bidtable = $this->mBidtable; |
1133 | 1217 | $unidtable = $this->mUnidtable; |
1134 | 1218 | $manLevel = $this->mConverter->mManualLevel; |
1135 | 1219 | |
1136 | | - $vmarked=array(); |
1137 | | - foreach($this->mConverter->mVariants as $v) { |
| 1220 | + $vmarked = array(); |
| 1221 | + foreach ( $this->mConverter->mVariants as $v ) { |
1138 | 1222 | /* for bidirectional array |
1139 | 1223 | fill in the missing variants, if any, |
1140 | 1224 | with fallbacks */ |
1141 | | - if(!array_key_exists($v, $bidtable)) { |
1142 | | - $variantFallbacks = $this->mConverter->getVariantFallbacks($v); |
1143 | | - $vf = $this->getTextInBidtable($variantFallbacks); |
1144 | | - if($vf) $bidtable[$v] = $vf; |
| 1225 | + if ( !array_key_exists( $v, $bidtable ) ) { |
| 1226 | + $variantFallbacks = |
| 1227 | + $this->mConverter->getVariantFallbacks( $v ); |
| 1228 | + $vf = $this->getTextInBidtable( $variantFallbacks ); |
| 1229 | + if ( $vf ) { |
| 1230 | + $bidtable[$v] = $vf; |
| 1231 | + } |
1145 | 1232 | } |
1146 | 1233 | |
1147 | | - if(array_key_exists($v,$bidtable)){ |
1148 | | - foreach($vmarked as $vo){ |
| 1234 | + if ( array_key_exists( $v, $bidtable ) ) { |
| 1235 | + foreach ( $vmarked as $vo ) { |
1149 | 1236 | // use syntax: -{A|zh:WordZh;zh-tw:WordTw}- |
1150 | | - // or -{H|zh:WordZh;zh-tw:WordTw}- or -{-|zh:WordZh;zh-tw:WordTw}- |
| 1237 | + // or -{H|zh:WordZh;zh-tw:WordTw}- |
| 1238 | + // or -{-|zh:WordZh;zh-tw:WordTw}- |
1151 | 1239 | // to introduce a custom mapping between |
1152 | 1240 | // words WordZh and WordTw in the whole text |
1153 | | - if($manLevel[$v]=='bidirectional'){ |
1154 | | - $this->mConvTable[$v][$bidtable[$vo]]=$bidtable[$v]; |
| 1241 | + if ( $manLevel[$v] == 'bidirectional' ) { |
| 1242 | + $this->mConvTable[$v][$bidtable[$vo]] = $bidtable[$v]; |
1155 | 1243 | } |
1156 | | - if($manLevel[$vo]=='bidirectional'){ |
1157 | | - $this->mConvTable[$vo][$bidtable[$v]]=$bidtable[$vo]; |
| 1244 | + if ( $manLevel[$vo] == 'bidirectional' ) { |
| 1245 | + $this->mConvTable[$vo][$bidtable[$v]] = $bidtable[$vo]; |
1158 | 1246 | } |
1159 | 1247 | } |
1160 | | - $vmarked[]=$v; |
| 1248 | + $vmarked[] = $v; |
1161 | 1249 | } |
1162 | | - /*for unidirectional array |
1163 | | - fill to convert tables */ |
1164 | | - $allow_unid = $manLevel[$v]=='bidirectional' |
1165 | | - || $manLevel[$v]=='unidirectional'; |
1166 | | - if( $allow_unid && array_key_exists( $v, $unidtable ) ){ |
| 1250 | + /*for unidirectional array fill to convert tables */ |
| 1251 | + if ( ( $manLevel[$v] == 'bidirectional' |
| 1252 | + || $manLevel[$v] == 'unidirectional' ) |
| 1253 | + && array_key_exists( $v, $unidtable ) ) { |
1167 | 1254 | $ct = $this->mConvTable[$v]; |
1168 | | - $this->mConvTable[$v] = array_merge($ct, $unidtable[$v]); |
| 1255 | + $this->mConvTable[$v] = array_merge( $ct, $unidtable[$v] ); |
1169 | 1256 | } |
1170 | 1257 | } |
1171 | 1258 | } |
1172 | 1259 | |
1173 | 1260 | /** |
1174 | | - * Parse rules and flags |
| 1261 | + * Parse rules and flags. |
1175 | 1262 | * @public |
1176 | 1263 | */ |
1177 | | - function parse($variant){ |
1178 | | - if(!$variant) |
| 1264 | + function parse( $variant = NULL ) { |
| 1265 | + if ( !$variant ) { |
1179 | 1266 | $variant = $this->mConverter->getPreferredVariant(); |
| 1267 | + } |
1180 | 1268 | |
1181 | 1269 | $variants = $this->mConverter->mVariants; |
1182 | 1270 | $this->parseFlags(); |
— | — | @@ -1183,70 +1271,82 @@ |
1184 | 1272 | |
1185 | 1273 | // convert to specified variant |
1186 | 1274 | // syntax: -{zh-hans;zh-hant[;...]|<text to convert>}- |
1187 | | - if( count( array_diff( $flags, $variants ) ) == 0 and count( $flags ) != 0 ) { |
1188 | | - if ( in_array( $variant, $flags ) ) // check if current variant in flags |
| 1275 | + if ( count( array_diff( $flags, $variants ) ) == 0 |
| 1276 | + and count( $flags ) != 0 ) { |
| 1277 | + // check if current variant in flags |
| 1278 | + if ( in_array( $variant, $flags ) ) { |
1189 | 1279 | // then convert <text to convert> to current language |
1190 | | - $this->mRules = $this->mConverter->autoConvert( $this->mRules, $variant ); |
1191 | | - else { // if current variant no in flags, |
| 1280 | + $this->mRules = $this->mConverter->autoConvert( $this->mRules, |
| 1281 | + $variant ); |
| 1282 | + } else { // if current variant no in flags, |
1192 | 1283 | // then we check its fallback variants. |
1193 | | - $variantFallbacks = $this->mConverter->getVariantFallbacks($variant); |
| 1284 | + $variantFallbacks = |
| 1285 | + $this->mConverter->getVariantFallbacks( $variant ); |
1194 | 1286 | foreach ( $variantFallbacks as $variantFallback ) { |
1195 | 1287 | // if current variant's fallback exist in flags |
1196 | 1288 | if ( in_array( $variantFallback, $flags ) ) { |
1197 | 1289 | // then convert <text to convert> to fallback language |
1198 | | - $this->mRules = $this->mConverter->autoConvert( $this->mRules, $variantFallback ); |
| 1290 | + $this->mRules = |
| 1291 | + $this->mConverter->autoConvert( $this->mRules, |
| 1292 | + $variantFallback ); |
1199 | 1293 | break; |
1200 | 1294 | } |
1201 | 1295 | } |
1202 | 1296 | } |
1203 | | - $this->mFlags = $flags = array('R'); |
| 1297 | + $this->mFlags = $flags = array( 'R' ); |
1204 | 1298 | } |
1205 | 1299 | |
1206 | | - if( !in_array( 'R', $flags ) || !in_array( 'N', $flags ) ) { |
| 1300 | + if ( !in_array( 'R', $flags ) || !in_array( 'N', $flags ) ) { |
1207 | 1301 | // decode => HTML entities modified by Sanitizer::removeHTMLtags |
1208 | | - $this->mRules = str_replace('=>','=>',$this->mRules); |
| 1302 | + $this->mRules = str_replace( '=>', '=>', $this->mRules ); |
1209 | 1303 | |
1210 | 1304 | $this->parseRules(); |
1211 | 1305 | } |
1212 | 1306 | $rules = $this->mRules; |
1213 | 1307 | |
1214 | | - if( count( $this->mBidtable ) == 0 && count( $this->mUnidtable ) == 0 ){ |
1215 | | - if(in_array('+',$flags) || in_array('-',$flags)) |
| 1308 | + if ( count( $this->mBidtable ) == 0 |
| 1309 | + && count( $this->mUnidtable ) == 0 ) { |
| 1310 | + if ( in_array( '+', $flags ) || in_array( '-', $flags ) ) { |
1216 | 1311 | // fill all variants if text in -{A/H/-|text} without rules |
1217 | | - foreach($this->mConverter->mVariants as $v) |
| 1312 | + foreach ( $this->mConverter->mVariants as $v ) { |
1218 | 1313 | $this->mBidtable[$v] = $rules; |
1219 | | - elseif (!in_array('N',$flags) && !in_array('T',$flags) ) |
1220 | | - $this->mFlags = $flags = array('R'); |
| 1314 | + } |
| 1315 | + } elseif ( !in_array( 'N', $flags ) && !in_array( 'T', $flags ) ) { |
| 1316 | + $this->mFlags = $flags = array( 'R' ); |
| 1317 | + } |
1221 | 1318 | } |
1222 | 1319 | |
1223 | | - if( in_array('R',$flags) ) { |
| 1320 | + if ( in_array( 'R', $flags ) ) { |
1224 | 1321 | // if we don't do content convert, still strip the -{}- tags |
1225 | 1322 | $this->mRuleDisplay = $rules; |
1226 | | - } elseif ( in_array('N',$flags) ){ |
| 1323 | + } elseif ( in_array( 'N', $flags ) ) { |
1227 | 1324 | // proces N flag: output current variant name |
1228 | | - $this->mRuleDisplay = $this->mConverter->mVariantNames[trim($rules)]; |
1229 | | - } elseif ( in_array('D',$flags) ){ |
| 1325 | + $this->mRuleDisplay = |
| 1326 | + $this->mConverter->mVariantNames[ trim( $rules ) ]; |
| 1327 | + } elseif ( in_array( 'D', $flags ) ) { |
1230 | 1328 | // proces D flag: output rules description |
1231 | 1329 | $this->mRuleDisplay = $this->getRulesDesc(); |
1232 | | - } elseif ( in_array('H',$flags) || in_array('-',$flags) ) { |
| 1330 | + } elseif ( in_array( 'H', $flags ) || in_array( '-', $flags ) ) { |
1233 | 1331 | // proces H,- flag or T only: output nothing |
1234 | 1332 | $this->mRuleDisplay = ''; |
1235 | | - } elseif ( in_array('S',$flags) ){ |
1236 | | - $this->mRuleDisplay = $this->getRuleConvertedStr($variant, |
1237 | | - $this->mConverter->mDoContentConvert); |
| 1333 | + } elseif ( in_array( 'S', $flags ) ) { |
| 1334 | + $this->mRuleDisplay = $this->getRuleConvertedStr( $variant, |
| 1335 | + $this->mConverter->mDoContentConvert ); |
1238 | 1336 | } else { |
1239 | | - $this->mRuleDisplay= $this->mManualCodeError; |
| 1337 | + $this->mRuleDisplay = $this->mManualCodeError; |
1240 | 1338 | } |
1241 | 1339 | // proces T flag |
1242 | | - if ( in_array('T',$flags) ) { |
1243 | | - $this->mRuleTitle = $this->getRuleConvertedStr($variant, |
1244 | | - $this->mConverter->mDoTitleConvert); |
| 1340 | + if ( in_array( 'T', $flags ) ) { |
| 1341 | + $this->mRuleTitle = $this->getRuleConvertedStr( $variant, |
| 1342 | + $this->mConverter->mDoTitleConvert ); |
1245 | 1343 | } |
1246 | 1344 | |
1247 | | - if (in_array('-', $flags)) |
1248 | | - $this->mRulesAction='remove'; |
1249 | | - if (in_array('+', $flags)) |
1250 | | - $this->mRulesAction='add'; |
| 1345 | + if ( in_array( '-', $flags ) ) { |
| 1346 | + $this->mRulesAction = 'remove'; |
| 1347 | + } |
| 1348 | + if ( in_array( '+', $flags ) ) { |
| 1349 | + $this->mRulesAction = 'add'; |
| 1350 | + } |
1251 | 1351 | |
1252 | 1352 | $this->generateConvTable(); |
1253 | 1353 | } |
— | — | @@ -1254,54 +1354,56 @@ |
1255 | 1355 | /** |
1256 | 1356 | * @public |
1257 | 1357 | */ |
1258 | | - function hasRules(){ |
| 1358 | + function hasRules() { |
1259 | 1359 | // TODO: |
1260 | 1360 | } |
1261 | 1361 | |
1262 | 1362 | /** |
1263 | | - * get display text on markup -{...}- |
| 1363 | + * Get display text on markup -{...}- |
1264 | 1364 | * @public |
1265 | 1365 | */ |
1266 | | - function getDisplay(){ |
| 1366 | + function getDisplay() { |
1267 | 1367 | return $this->mRuleDisplay; |
1268 | 1368 | } |
| 1369 | + |
1269 | 1370 | /** |
1270 | | - * get converted title |
| 1371 | + * Get converted title. |
1271 | 1372 | * @public |
1272 | 1373 | */ |
1273 | | - function getTitle(){ |
| 1374 | + function getTitle() { |
1274 | 1375 | return $this->mRuleTitle; |
1275 | 1376 | } |
1276 | 1377 | |
1277 | 1378 | /** |
1278 | | - * return how deal with conversion rules |
| 1379 | + * Return how deal with conversion rules. |
1279 | 1380 | * @public |
1280 | 1381 | */ |
1281 | | - function getRulesAction(){ |
| 1382 | + function getRulesAction() { |
1282 | 1383 | return $this->mRulesAction; |
1283 | 1384 | } |
1284 | 1385 | |
1285 | 1386 | /** |
1286 | | - * get conversion table ( bidirectional and unidirectional conversion table ) |
| 1387 | + * Get conversion table. ( bidirectional and unidirectional |
| 1388 | + * conversion table ) |
1287 | 1389 | * @public |
1288 | 1390 | */ |
1289 | | - function getConvTable(){ |
| 1391 | + function getConvTable() { |
1290 | 1392 | return $this->mConvTable; |
1291 | 1393 | } |
1292 | 1394 | |
1293 | 1395 | /** |
1294 | | - * get conversion rules string |
| 1396 | + * Get conversion rules string. |
1295 | 1397 | * @public |
1296 | 1398 | */ |
1297 | | - function getRules(){ |
| 1399 | + function getRules() { |
1298 | 1400 | return $this->mRules; |
1299 | 1401 | } |
1300 | 1402 | |
1301 | 1403 | /** |
1302 | | - * get conversion flags |
| 1404 | + * Get conversion flags. |
1303 | 1405 | * @public |
1304 | 1406 | */ |
1305 | | - function getFlags(){ |
| 1407 | + function getFlags() { |
1306 | 1408 | return $this->mFlags; |
1307 | 1409 | } |
1308 | 1410 | } |