Reset $mConvRuleTitle before every language conversion run
[lhc/web/wiklou.git] / languages / LanguageConverter.php
1 <?php
2 /**
3 * Contains the LanguageConverter class and ConverterRule class
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 /**
25 * Base class for language conversion.
26 * @ingroup Language
27 *
28 * @author Zhengzhu Feng <zhengzhu@gmail.com>
29 * @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>, PhiLiP <philip.npc@gmail.com>
30 */
31 class LanguageConverter {
32
33 /**
34 * languages supporting variants
35 * @since 1.20
36 * @var array
37 */
38 static public $languagesWithVariants = array(
39 'gan',
40 'iu',
41 'kk',
42 'ku',
43 'shi',
44 'sr',
45 'tg',
46 'uz',
47 'zh',
48 );
49
50 var $mMainLanguageCode;
51 var $mVariants, $mVariantFallbacks, $mVariantNames;
52 var $mTablesLoaded = false;
53 var $mTables;
54 // 'bidirectional' 'unidirectional' 'disable' for each variant
55 var $mManualLevel;
56
57 /**
58 * @var String: memcached key name
59 */
60 var $mCacheKey;
61
62 var $mLangObj;
63 var $mFlags;
64 var $mDescCodeSep = ':', $mDescVarSep = ';';
65 var $mUcfirst = false;
66 var $mConvRuleTitle = false;
67 var $mURLVariant;
68 var $mUserVariant;
69 var $mHeaderVariant;
70 var $mMaxDepth = 10;
71 var $mVarSeparatorPattern;
72
73 const CACHE_VERSION_KEY = 'VERSION 6';
74
75 /**
76 * Constructor
77 *
78 * @param $langobj Language: the Language Object
79 * @param $maincode String: the main language code of this language
80 * @param $variants Array: the supported variants of this language
81 * @param $variantfallbacks Array: the fallback language of each variant
82 * @param $flags Array: defining the custom strings that maps to the flags
83 * @param $manualLevel Array: limit for supported variants
84 */
85 public function __construct( $langobj, $maincode, $variants = array(),
86 $variantfallbacks = array(), $flags = array(),
87 $manualLevel = array() ) {
88 global $wgDisabledVariants;
89 $this->mLangObj = $langobj;
90 $this->mMainLanguageCode = $maincode;
91 $this->mVariants = array_diff( $variants, $wgDisabledVariants );
92 $this->mVariantFallbacks = $variantfallbacks;
93 $this->mVariantNames = Language::fetchLanguageNames();
94 $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode );
95 $defaultflags = array(
96 // 'S' show converted text
97 // '+' add rules for alltext
98 // 'E' the gave flags is error
99 // these flags above are reserved for program
100 'A' => 'A', // add rule for convert code (all text convert)
101 'T' => 'T', // title convert
102 'R' => 'R', // raw content
103 'D' => 'D', // convert description (subclass implement)
104 '-' => '-', // remove convert (not implement)
105 'H' => 'H', // add rule for convert code
106 // (but no display in placed code)
107 'N' => 'N' // current variant name
108 );
109 $this->mFlags = array_merge( $defaultflags, $flags );
110 foreach ( $this->mVariants as $v ) {
111 if ( array_key_exists( $v, $manualLevel ) ) {
112 $this->mManualLevel[$v] = $manualLevel[$v];
113 } else {
114 $this->mManualLevel[$v] = 'bidirectional';
115 }
116 $this->mFlags[$v] = $v;
117 }
118 }
119
120 /**
121 * Get all valid variants.
122 * Call this instead of using $this->mVariants directly.
123 *
124 * @return Array: contains all valid variants
125 */
126 public function getVariants() {
127 return $this->mVariants;
128 }
129
130 /**
131 * In case some variant is not defined in the markup, we need
132 * to have some fallback. For example, in zh, normally people
133 * will define zh-hans and zh-hant, but less so for zh-sg or zh-hk.
134 * when zh-sg is preferred but not defined, we will pick zh-hans
135 * in this case. Right now this is only used by zh.
136 *
137 * @param $variant String: the language code of the variant
138 * @return String|array: The code of the fallback language or the
139 * main code if there is no fallback
140 */
141 public function getVariantFallbacks( $variant ) {
142 if ( isset( $this->mVariantFallbacks[$variant] ) ) {
143 return $this->mVariantFallbacks[$variant];
144 }
145 return $this->mMainLanguageCode;
146 }
147
148 /**
149 * Get the title produced by the conversion rule.
150 * @return String: The converted title text
151 */
152 public function getConvRuleTitle() {
153 return $this->mConvRuleTitle;
154 }
155
156 /**
157 * Get preferred language variant.
158 * @return String: the preferred language code
159 */
160 public function getPreferredVariant() {
161 global $wgDefaultLanguageVariant, $wgUser;
162
163 $req = $this->getURLVariant();
164
165 if ( $wgUser->isLoggedIn() && !$req ) {
166 $req = $this->getUserVariant();
167 } elseif ( !$req ) {
168 $req = $this->getHeaderVariant();
169 }
170
171 if ( $wgDefaultLanguageVariant && !$req ) {
172 $req = $this->validateVariant( $wgDefaultLanguageVariant );
173 }
174
175 // This function, unlike the other get*Variant functions, is
176 // not memoized (i.e. there return value is not cached) since
177 // new information might appear during processing after this
178 // is first called.
179 if ( $this->validateVariant( $req ) ) {
180 return $req;
181 }
182 return $this->mMainLanguageCode;
183 }
184
185 /**
186 * Get default variant.
187 * This function would not be affected by user's settings or headers
188 * @return String: the default variant code
189 */
190 public function getDefaultVariant() {
191 global $wgDefaultLanguageVariant;
192
193 $req = $this->getURLVariant();
194
195 if ( $wgDefaultLanguageVariant && !$req ) {
196 $req = $this->validateVariant( $wgDefaultLanguageVariant );
197 }
198
199 if ( $req ) {
200 return $req;
201 }
202 return $this->mMainLanguageCode;
203 }
204
205 /**
206 * Validate the variant
207 * @param $variant String: the variant to validate
208 * @return Mixed: returns the variant if it is valid, null otherwise
209 */
210 public function validateVariant( $variant = null ) {
211 if ( $variant !== null && in_array( $variant, $this->mVariants ) ) {
212 return $variant;
213 }
214 return null;
215 }
216
217 /**
218 * Get the variant specified in the URL
219 *
220 * @return Mixed: variant if one found, false otherwise.
221 */
222 public function getURLVariant() {
223 global $wgRequest;
224
225 if ( $this->mURLVariant ) {
226 return $this->mURLVariant;
227 }
228
229 // see if the preference is set in the request
230 $ret = $wgRequest->getText( 'variant' );
231
232 if ( !$ret ) {
233 $ret = $wgRequest->getVal( 'uselang' );
234 }
235
236 return $this->mURLVariant = $this->validateVariant( $ret );
237 }
238
239 /**
240 * Determine if the user has a variant set.
241 *
242 * @return Mixed: variant if one found, false otherwise.
243 */
244 protected function getUserVariant() {
245 global $wgUser;
246
247 // memoizing this function wreaks havoc on parserTest.php
248 /*
249 if ( $this->mUserVariant ) {
250 return $this->mUserVariant;
251 }
252 */
253
254 // Get language variant preference from logged in users
255 // Don't call this on stub objects because that causes infinite
256 // recursion during initialisation
257 if ( $wgUser->isLoggedIn() ) {
258 $ret = $wgUser->getOption( 'variant' );
259 } else {
260 // figure out user lang without constructing wgLang to avoid
261 // infinite recursion
262 $ret = $wgUser->getOption( 'language' );
263 }
264
265 return $this->mUserVariant = $this->validateVariant( $ret );
266 }
267
268 /**
269 * Determine the language variant from the Accept-Language header.
270 *
271 * @return Mixed: variant if one found, false otherwise.
272 */
273 protected function getHeaderVariant() {
274 global $wgRequest;
275
276 if ( $this->mHeaderVariant ) {
277 return $this->mHeaderVariant;
278 }
279
280 // see if some supported language variant is set in the
281 // HTTP header.
282 $languages = array_keys( $wgRequest->getAcceptLang() );
283 if ( empty( $languages ) ) {
284 return null;
285 }
286
287 $fallbackLanguages = array();
288 foreach ( $languages as $language ) {
289 $this->mHeaderVariant = $this->validateVariant( $language );
290 if ( $this->mHeaderVariant ) {
291 break;
292 }
293
294 // To see if there are fallbacks of current language.
295 // We record these fallback variants, and process
296 // them later.
297 $fallbacks = $this->getVariantFallbacks( $language );
298 if ( is_string( $fallbacks ) && $fallbacks !== $this->mMainLanguageCode ) {
299 $fallbackLanguages[] = $fallbacks;
300 } elseif ( is_array( $fallbacks ) ) {
301 $fallbackLanguages =
302 array_merge( $fallbackLanguages, $fallbacks );
303 }
304 }
305
306 if ( !$this->mHeaderVariant ) {
307 // process fallback languages now
308 $fallback_languages = array_unique( $fallbackLanguages );
309 foreach ( $fallback_languages as $language ) {
310 $this->mHeaderVariant = $this->validateVariant( $language );
311 if ( $this->mHeaderVariant ) {
312 break;
313 }
314 }
315 }
316
317 return $this->mHeaderVariant;
318 }
319
320 /**
321 * Dictionary-based conversion.
322 * This function would not parse the conversion rules.
323 * If you want to parse rules, try to use convert() or
324 * convertTo().
325 *
326 * @param $text String the text to be converted
327 * @param $toVariant bool|string the target language code
328 * @return String the converted text
329 */
330 public function autoConvert( $text, $toVariant = false ) {
331 wfProfileIn( __METHOD__ );
332
333 $this->loadTables();
334
335 if ( !$toVariant ) {
336 $toVariant = $this->getPreferredVariant();
337 if ( !$toVariant ) {
338 wfProfileOut( __METHOD__ );
339 return $text;
340 }
341 }
342
343 if( $this->guessVariant( $text, $toVariant ) ) {
344 wfProfileOut( __METHOD__ );
345 return $text;
346 }
347
348 /* we convert everything except:
349 1. HTML markups (anything between < and >)
350 2. HTML entities
351 3. placeholders created by the parser
352 */
353 global $wgParser;
354 if ( isset( $wgParser ) && $wgParser->UniqPrefix() != '' ) {
355 $marker = '|' . $wgParser->UniqPrefix() . '[\-a-zA-Z0-9]+';
356 } else {
357 $marker = '';
358 }
359
360 // this one is needed when the text is inside an HTML markup
361 $htmlfix = '|<[^>]+$|^[^<>]*>';
362
363 // disable convert to variants between <code></code> tags
364 $codefix = '<code>.+?<\/code>|';
365 // disable convertsion of <script type="text/javascript"> ... </script>
366 $scriptfix = '<script.*?>.*?<\/script>|';
367 // disable conversion of <pre xxxx> ... </pre>
368 $prefix = '<pre.*?>.*?<\/pre>|';
369
370 $reg = '/' . $codefix . $scriptfix . $prefix .
371 '<[^>]+>|&[a-zA-Z#][a-z0-9]+;' . $marker . $htmlfix . '/s';
372 $startPos = 0;
373 $sourceBlob = '';
374 $literalBlob = '';
375
376 // Guard against delimiter nulls in the input
377 $text = str_replace( "\000", '', $text );
378
379 $markupMatches = null;
380 $elementMatches = null;
381 while ( $startPos < strlen( $text ) ) {
382 if ( preg_match( $reg, $text, $markupMatches, PREG_OFFSET_CAPTURE, $startPos ) ) {
383 $elementPos = $markupMatches[0][1];
384 $element = $markupMatches[0][0];
385 } else {
386 $elementPos = strlen( $text );
387 $element = '';
388 }
389
390 // Queue the part before the markup for translation in a batch
391 $sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) . "\000";
392
393 // Advance to the next position
394 $startPos = $elementPos + strlen( $element );
395
396 // Translate any alt or title attributes inside the matched element
397 if ( $element !== '' && preg_match( '/^(<[^>\s]*)\s([^>]*)(.*)$/', $element,
398 $elementMatches ) )
399 {
400 $attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] );
401 $changed = false;
402 foreach ( array( 'title', 'alt' ) as $attrName ) {
403 if ( !isset( $attrs[$attrName] ) ) {
404 continue;
405 }
406 $attr = $attrs[$attrName];
407 // Don't convert URLs
408 if ( !strpos( $attr, '://' ) ) {
409 $attr = $this->translate( $attr, $toVariant );
410 }
411
412 // Remove HTML tags to avoid disrupting the layout
413 $attr = preg_replace( '/<[^>]+>/', '', $attr );
414 if ( $attr !== $attrs[$attrName] ) {
415 $attrs[$attrName] = $attr;
416 $changed = true;
417 }
418 }
419 if ( $changed ) {
420 $element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
421 $elementMatches[3];
422 }
423 }
424 $literalBlob .= $element . "\000";
425 }
426
427 // Do the main translation batch
428 $translatedBlob = $this->translate( $sourceBlob, $toVariant );
429
430 // Put the output back together
431 $translatedIter = StringUtils::explode( "\000", $translatedBlob );
432 $literalIter = StringUtils::explode( "\000", $literalBlob );
433 $output = '';
434 while ( $translatedIter->valid() && $literalIter->valid() ) {
435 $output .= $translatedIter->current();
436 $output .= $literalIter->current();
437 $translatedIter->next();
438 $literalIter->next();
439 }
440
441 wfProfileOut( __METHOD__ );
442 return $output;
443 }
444
445 /**
446 * Translate a string to a variant.
447 * Doesn't parse rules or do any of that other stuff, for that use
448 * convert() or convertTo().
449 *
450 * @param $text String: text to convert
451 * @param $variant String: variant language code
452 * @return String: translated text
453 */
454 public function translate( $text, $variant ) {
455 wfProfileIn( __METHOD__ );
456 // If $text is empty or only includes spaces, do nothing
457 // Otherwise translate it
458 if ( trim( $text ) ) {
459 $this->loadTables();
460 $text = $this->mTables[$variant]->replace( $text );
461 }
462 wfProfileOut( __METHOD__ );
463 return $text;
464 }
465
466 /**
467 * Call translate() to convert text to all valid variants.
468 *
469 * @param $text String: the text to be converted
470 * @return Array: variant => converted text
471 */
472 public function autoConvertToAllVariants( $text ) {
473 wfProfileIn( __METHOD__ );
474 $this->loadTables();
475
476 $ret = array();
477 foreach ( $this->mVariants as $variant ) {
478 $ret[$variant] = $this->translate( $text, $variant );
479 }
480
481 wfProfileOut( __METHOD__ );
482 return $ret;
483 }
484
485 /**
486 * Convert link text to all valid variants.
487 * In the first, this function only convert text outside the
488 * "-{" "}-" markups. Since the "{" and "}" are not allowed in
489 * titles, the text will get all converted always.
490 * So I removed this feature and deprecated the function.
491 *
492 * @param $text String: the text to be converted
493 * @return Array: variant => converted text
494 * @deprecated since 1.17 Use autoConvertToAllVariants() instead
495 */
496 public function convertLinkToAllVariants( $text ) {
497 return $this->autoConvertToAllVariants( $text );
498 }
499
500 /**
501 * Apply manual conversion rules.
502 *
503 * @param $convRule ConverterRule Object of ConverterRule
504 */
505 protected function applyManualConv( $convRule ) {
506 // Use syntax -{T|zh-cn:TitleCN; zh-tw:TitleTw}- to custom
507 // title conversion.
508 // Bug 24072: $mConvRuleTitle was overwritten by other manual
509 // rule(s) not for title, this breaks the title conversion.
510 $newConvRuleTitle = $convRule->getTitle();
511 if ( $newConvRuleTitle ) {
512 // So I add an empty check for getTitle()
513 $this->mConvRuleTitle = $newConvRuleTitle;
514 }
515
516 // merge/remove manual conversion rules to/from global table
517 $convTable = $convRule->getConvTable();
518 $action = $convRule->getRulesAction();
519 foreach ( $convTable as $variant => $pair ) {
520 if ( !$this->validateVariant( $variant ) ) {
521 continue;
522 }
523
524 if ( $action == 'add' ) {
525 foreach ( $pair as $from => $to ) {
526 // to ensure that $from and $to not be left blank
527 // so $this->translate() could always return a string
528 if ( $from || $to ) {
529 // more efficient than array_merge(), about 2.5 times.
530 $this->mTables[$variant]->setPair( $from, $to );
531 }
532 }
533 } elseif ( $action == 'remove' ) {
534 $this->mTables[$variant]->removeArray( $pair );
535 }
536 }
537 }
538
539 /**
540 * Auto convert a Title object to a readable string in the
541 * preferred variant.
542 *
543 * @param $title Title a object of Title
544 * @return String: converted title text
545 */
546 public function convertTitle( $title ) {
547 $variant = $this->getPreferredVariant();
548 $index = $title->getNamespace();
549 if ( $index === NS_MAIN ) {
550 $text = '';
551 } else {
552 // first let's check if a message has given us a converted name
553 $nsConvMsg = wfMessage( 'conversion-ns' . $index )->inContentLanguage();
554 if ( $nsConvMsg->exists() ) {
555 $text = $nsConvMsg->plain();
556 } else {
557 // the message does not exist, try retrieve it from the current
558 // variant's namespace names.
559 $langObj = $this->mLangObj->factory( $variant );
560 $text = $langObj->getFormattedNsText( $index );
561 }
562 $text .= ':';
563 }
564 $text .= $title->getText();
565 $text = $this->translate( $text, $variant );
566 return $text;
567 }
568
569 /**
570 * Convert text to different variants of a language. The automatic
571 * conversion is done in autoConvert(). Here we parse the text
572 * marked with -{}-, which specifies special conversions of the
573 * text that can not be accomplished in autoConvert().
574 *
575 * Syntax of the markup:
576 * -{code1:text1;code2:text2;...}- or
577 * -{flags|code1:text1;code2:text2;...}- or
578 * -{text}- in which case no conversion should take place for text
579 *
580 * @param $text String: text to be converted
581 * @return String: converted text
582 */
583 public function convert( $text ) {
584 $variant = $this->getPreferredVariant();
585 return $this->convertTo( $text, $variant );
586 }
587
588 /**
589 * Same as convert() except a extra parameter to custom variant.
590 *
591 * @param $text String: text to be converted
592 * @param $variant String: the target variant code
593 * @return String: converted text
594 */
595 public function convertTo( $text, $variant ) {
596 global $wgDisableLangConversion;
597 if ( $wgDisableLangConversion ) {
598 return $text;
599 }
600 // Reset converter state for a new converter run.
601 $this->mConvRuleTitle = false;
602 return $this->recursiveConvertTopLevel( $text, $variant );
603 }
604
605 /**
606 * Recursively convert text on the outside. Allow to use nested
607 * markups to custom rules.
608 *
609 * @param $text String: text to be converted
610 * @param $variant String: the target variant code
611 * @param $depth Integer: depth of recursion
612 * @return String: converted text
613 */
614 protected function recursiveConvertTopLevel( $text, $variant, $depth = 0 ) {
615 $startPos = 0;
616 $out = '';
617 $length = strlen( $text );
618 $shouldConvert = !$this->guessVariant( $text, $variant );
619
620 while ( $startPos < $length ) {
621 $pos = strpos( $text, '-{', $startPos );
622
623 if ( $pos === false ) {
624 // No more markup, append final segment
625 $fragment = substr( $text, $startPos );
626 $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment;
627 return $out;
628 }
629
630 // Markup found
631 // Append initial segment
632 $fragment = substr( $text, $startPos, $pos - $startPos );
633 $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment;
634
635 // Advance position
636 $startPos = $pos;
637
638 // Do recursive conversion
639 $out .= $this->recursiveConvertRule( $text, $variant, $startPos, $depth + 1 );
640 }
641
642 return $out;
643 }
644
645 /**
646 * Recursively convert text on the inside.
647 *
648 * @param $text String: text to be converted
649 * @param $variant String: the target variant code
650 * @param $startPos int
651 * @param $depth Integer: depth of recursion
652 *
653 * @throws MWException
654 * @return String: converted text
655 */
656 protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) {
657 // Quick sanity check (no function calls)
658 if ( $text[$startPos] !== '-' || $text[$startPos + 1] !== '{' ) {
659 throw new MWException( __METHOD__ . ': invalid input string' );
660 }
661
662 $startPos += 2;
663 $inner = '';
664 $warningDone = false;
665 $length = strlen( $text );
666
667 while ( $startPos < $length ) {
668 $m = false;
669 preg_match( '/-\{|\}-/', $text, $m, PREG_OFFSET_CAPTURE, $startPos );
670 if ( !$m ) {
671 // Unclosed rule
672 break;
673 }
674
675 $token = $m[0][0];
676 $pos = $m[0][1];
677
678 // Markup found
679 // Append initial segment
680 $inner .= substr( $text, $startPos, $pos - $startPos );
681
682 // Advance position
683 $startPos = $pos;
684
685 switch ( $token ) {
686 case '-{':
687 // Check max depth
688 if ( $depth >= $this->mMaxDepth ) {
689 $inner .= '-{';
690 if ( !$warningDone ) {
691 $inner .= '<span class="error">' .
692 wfMessage( 'language-converter-depth-warning' )
693 ->numParams( $this->mMaxDepth )->inContentLanguage()->text() .
694 '</span>';
695 $warningDone = true;
696 }
697 $startPos += 2;
698 continue;
699 }
700 // Recursively parse another rule
701 $inner .= $this->recursiveConvertRule( $text, $variant, $startPos, $depth + 1 );
702 break;
703 case '}-':
704 // Apply the rule
705 $startPos += 2;
706 $rule = new ConverterRule( $inner, $this );
707 $rule->parse( $variant );
708 $this->applyManualConv( $rule );
709 return $rule->getDisplay();
710 default:
711 throw new MWException( __METHOD__ . ': invalid regex match' );
712 }
713 }
714
715 // Unclosed rule
716 if ( $startPos < $length ) {
717 $inner .= substr( $text, $startPos );
718 }
719 $startPos = $length;
720 return '-{' . $this->autoConvert( $inner, $variant );
721 }
722
723 /**
724 * If a language supports multiple variants, it is possible that
725 * non-existing link in one variant actually exists in another variant.
726 * This function tries to find it. See e.g. LanguageZh.php
727 *
728 * @param $link String: the name of the link
729 * @param $nt Mixed: the title object of the link
730 * @param $ignoreOtherCond Boolean: to disable other conditions when
731 * we need to transclude a template or update a category's link
732 * @return Null, the input parameters may be modified upon return
733 */
734 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
735 # If the article has already existed, there is no need to
736 # check it again, otherwise it may cause a fault.
737 if ( is_object( $nt ) && $nt->exists() ) {
738 return;
739 }
740
741 global $wgDisableLangConversion, $wgDisableTitleConversion, $wgRequest,
742 $wgUser;
743 $isredir = $wgRequest->getText( 'redirect', 'yes' );
744 $action = $wgRequest->getText( 'action' );
745 $linkconvert = $wgRequest->getText( 'linkconvert', 'yes' );
746 $disableLinkConversion = $wgDisableLangConversion
747 || $wgDisableTitleConversion;
748 $linkBatch = new LinkBatch();
749
750 $ns = NS_MAIN;
751
752 if ( $disableLinkConversion ||
753 ( !$ignoreOtherCond &&
754 ( $isredir == 'no'
755 || $action == 'edit'
756 || $action == 'submit'
757 || $linkconvert == 'no'
758 || $wgUser->getOption( 'noconvertlink' ) == 1 ) ) ) {
759 return;
760 }
761
762 if ( is_object( $nt ) ) {
763 $ns = $nt->getNamespace();
764 }
765
766 $variants = $this->autoConvertToAllVariants( $link );
767 if ( !$variants ) { // give up
768 return;
769 }
770
771 $titles = array();
772
773 foreach ( $variants as $v ) {
774 if ( $v != $link ) {
775 $varnt = Title::newFromText( $v, $ns );
776 if ( !is_null( $varnt ) ) {
777 $linkBatch->addObj( $varnt );
778 $titles[] = $varnt;
779 }
780 }
781 }
782
783 // fetch all variants in single query
784 $linkBatch->execute();
785
786 foreach ( $titles as $varnt ) {
787 if ( $varnt->getArticleID() > 0 ) {
788 $nt = $varnt;
789 $link = $varnt->getText();
790 break;
791 }
792 }
793 }
794
795 /**
796 * Returns language specific hash options.
797 *
798 * @return string
799 */
800 public function getExtraHashOptions() {
801 $variant = $this->getPreferredVariant();
802 return '!' . $variant;
803 }
804
805 /**
806 * Guess if a text is written in a variant. This should be implemented in subclasses.
807 *
808 * @param string $text the text to be checked
809 * @param string $variant language code of the variant to be checked for
810 * @return bool true if $text appears to be written in $variant, false if not
811 *
812 * @author Nikola Smolenski <smolensk@eunet.rs>
813 * @since 1.19
814 */
815 public function guessVariant($text, $variant) {
816 return false;
817 }
818
819 /**
820 * Load default conversion tables.
821 * This method must be implemented in derived class.
822 *
823 * @private
824 * @throws MWException
825 */
826 function loadDefaultTables() {
827 $name = get_class( $this );
828 throw new MWException( "Must implement loadDefaultTables() method in class $name" );
829 }
830
831 /**
832 * Load conversion tables either from the cache or the disk.
833 * @private
834 * @param $fromCache Boolean: load from memcached? Defaults to true.
835 */
836 function loadTables( $fromCache = true ) {
837 global $wgLangConvMemc;
838
839 if ( $this->mTablesLoaded ) {
840 return;
841 }
842
843 wfProfileIn( __METHOD__ );
844 $this->mTablesLoaded = true;
845 $this->mTables = false;
846 if ( $fromCache ) {
847 wfProfileIn( __METHOD__ . '-cache' );
848 $this->mTables = $wgLangConvMemc->get( $this->mCacheKey );
849 wfProfileOut( __METHOD__ . '-cache' );
850 }
851 if ( !$this->mTables
852 || !array_key_exists( self::CACHE_VERSION_KEY, $this->mTables ) ) {
853 wfProfileIn( __METHOD__ . '-recache' );
854 // not in cache, or we need a fresh reload.
855 // We will first load the default tables
856 // then update them using things in MediaWiki:Conversiontable/*
857 $this->loadDefaultTables();
858 foreach ( $this->mVariants as $var ) {
859 $cached = $this->parseCachedTable( $var );
860 $this->mTables[$var]->mergeArray( $cached );
861 }
862
863 $this->postLoadTables();
864 $this->mTables[self::CACHE_VERSION_KEY] = true;
865
866 $wgLangConvMemc->set( $this->mCacheKey, $this->mTables, 43200 );
867 wfProfileOut( __METHOD__ . '-recache' );
868 }
869 wfProfileOut( __METHOD__ );
870 }
871
872 /**
873 * Hook for post processing after conversion tables are loaded.
874 */
875 function postLoadTables() { }
876
877 /**
878 * Reload the conversion tables.
879 *
880 * @private
881 */
882 function reloadTables() {
883 if ( $this->mTables ) {
884 unset( $this->mTables );
885 }
886 $this->mTablesLoaded = false;
887 $this->loadTables( false );
888 }
889
890 /**
891 * Parse the conversion table stored in the cache.
892 *
893 * The tables should be in blocks of the following form:
894 * -{
895 * word => word ;
896 * word => word ;
897 * ...
898 * }-
899 *
900 * To make the tables more manageable, subpages are allowed
901 * and will be parsed recursively if $recursive == true.
902 *
903 * @param $code String: language code
904 * @param $subpage String: subpage name
905 * @param $recursive Boolean: parse subpages recursively? Defaults to true.
906 *
907 * @return array
908 */
909 function parseCachedTable( $code, $subpage = '', $recursive = true ) {
910 static $parsed = array();
911
912 $key = 'Conversiontable/' . $code;
913 if ( $subpage ) {
914 $key .= '/' . $subpage;
915 }
916 if ( array_key_exists( $key, $parsed ) ) {
917 return array();
918 }
919
920 $parsed[$key] = true;
921
922 if ( $subpage === '' ) {
923 $txt = MessageCache::singleton()->get( 'conversiontable', true, $code );
924 } else {
925 $txt = false;
926 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
927 if ( $title && $title->exists() ) {
928 $revision = Revision::newFromTitle( $title );
929 if ( $revision ) {
930 $txt = $revision->getRawText();
931 }
932 }
933 }
934
935 # Nothing to parse if there's no text
936 if ( $txt === false || $txt === null || $txt === '' ) {
937 return array();
938 }
939
940 // get all subpage links of the form
941 // [[MediaWiki:Conversiontable/zh-xx/...|...]]
942 $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
943 ':Conversiontable';
944 $subs = StringUtils::explode( '[[', $txt );
945 $sublinks = array();
946 foreach ( $subs as $sub ) {
947 $link = explode( ']]', $sub, 2 );
948 if ( count( $link ) != 2 ) {
949 continue;
950 }
951 $b = explode( '|', $link[0], 2 );
952 $b = explode( '/', trim( $b[0] ), 3 );
953 if ( count( $b ) == 3 ) {
954 $sublink = $b[2];
955 } else {
956 $sublink = '';
957 }
958
959 if ( $b[0] == $linkhead && $b[1] == $code ) {
960 $sublinks[] = $sublink;
961 }
962 }
963
964 // parse the mappings in this page
965 $blocks = StringUtils::explode( '-{', $txt );
966 $ret = array();
967 $first = true;
968 foreach ( $blocks as $block ) {
969 if ( $first ) {
970 // Skip the part before the first -{
971 $first = false;
972 continue;
973 }
974 $mappings = explode( '}-', $block, 2 );
975 $stripped = str_replace( array( "'", '"', '*', '#' ), '',
976 $mappings[0] );
977 $table = StringUtils::explode( ';', $stripped );
978 foreach ( $table as $t ) {
979 $m = explode( '=>', $t, 3 );
980 if ( count( $m ) != 2 ) {
981 continue;
982 }
983 // trim any trailling comments starting with '//'
984 $tt = explode( '//', $m[1], 2 );
985 $ret[trim( $m[0] )] = trim( $tt[0] );
986 }
987 }
988
989 // recursively parse the subpages
990 if ( $recursive ) {
991 foreach ( $sublinks as $link ) {
992 $s = $this->parseCachedTable( $code, $link, $recursive );
993 $ret = array_merge( $ret, $s );
994 }
995 }
996
997 if ( $this->mUcfirst ) {
998 foreach ( $ret as $k => $v ) {
999 $ret[$this->mLangObj->ucfirst( $k )] = $this->mLangObj->ucfirst( $v );
1000 }
1001 }
1002 return $ret;
1003 }
1004
1005 /**
1006 * Enclose a string with the "no conversion" tag. This is used by
1007 * various functions in the Parser.
1008 *
1009 * @param $text String: text to be tagged for no conversion
1010 * @param $noParse Boolean: unused
1011 * @return String: the tagged text
1012 */
1013 public function markNoConversion( $text, $noParse = false ) {
1014 # don't mark if already marked
1015 if ( strpos( $text, '-{' ) || strpos( $text, '}-' ) ) {
1016 return $text;
1017 }
1018
1019 $ret = "-{R|$text}-";
1020 return $ret;
1021 }
1022
1023 /**
1024 * Convert the sorting key for category links. This should make different
1025 * keys that are variants of each other map to the same key.
1026 *
1027 * @param $key string
1028 *
1029 * @return string
1030 */
1031 function convertCategoryKey( $key ) {
1032 return $key;
1033 }
1034
1035 /**
1036 * Hook to refresh the cache of conversion tables when
1037 * MediaWiki:Conversiontable* is updated.
1038 * @private
1039 *
1040 * @param $article Article object
1041 * @param $user Object: User object for the current user
1042 * @param $text String: article text (?)
1043 * @param $summary String: edit summary of the edit
1044 * @param $isMinor Boolean: was the edit marked as minor?
1045 * @param $isWatch Boolean: did the user watch this page or not?
1046 * @param $section
1047 * @param $flags int Bitfield
1048 * @param $revision Object: new Revision object or null
1049 * @return Boolean: true
1050 */
1051 function OnArticleSaveComplete( $article, $user, $text, $summary, $isMinor,
1052 $isWatch, $section, $flags, $revision ) {
1053 $titleobj = $article->getTitle();
1054 if ( $titleobj->getNamespace() == NS_MEDIAWIKI ) {
1055 $title = $titleobj->getDBkey();
1056 $t = explode( '/', $title, 3 );
1057 $c = count( $t );
1058 if ( $c > 1 && $t[0] == 'Conversiontable' ) {
1059 if ( $this->validateVariant( $t[1] ) ) {
1060 $this->reloadTables();
1061 }
1062 }
1063 }
1064 return true;
1065 }
1066
1067 /**
1068 * Armour rendered math against conversion.
1069 * Escape special chars in parsed math text. (in most cases are img elements)
1070 *
1071 * @param $text String: text to armour against conversion
1072 * @return String: armoured text where { and } have been converted to
1073 * &#123; and &#125;
1074 */
1075 public function armourMath( $text ) {
1076 // convert '-{' and '}-' to '-&#123;' and '&#125;-' to prevent
1077 // any unwanted markup appearing in the math image tag.
1078 $text = strtr( $text, array( '-{' => '-&#123;', '}-' => '&#125;-' ) );
1079 return $text;
1080 }
1081
1082 /**
1083 * Get the cached separator pattern for ConverterRule::parseRules()
1084 */
1085 function getVarSeparatorPattern() {
1086 if ( is_null( $this->mVarSeparatorPattern ) ) {
1087 // varsep_pattern for preg_split:
1088 // text should be splited by ";" only if a valid variant
1089 // name exist after the markup, for example:
1090 // -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:\
1091 // <span style="font-size:120%;">yyy</span>;}-
1092 // we should split it as:
1093 // array(
1094 // [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>'
1095 // [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>'
1096 // [2] => ''
1097 // )
1098 $pat = '/;\s*(?=';
1099 foreach ( $this->mVariants as $variant ) {
1100 // zh-hans:xxx;zh-hant:yyy
1101 $pat .= $variant . '\s*:|';
1102 // xxx=>zh-hans:yyy; xxx=>zh-hant:zzz
1103 $pat .= '[^;]*?=>\s*' . $variant . '\s*:|';
1104 }
1105 $pat .= '\s*$)/';
1106 $this->mVarSeparatorPattern = $pat;
1107 }
1108 return $this->mVarSeparatorPattern;
1109 }
1110 }
1111
1112 /**
1113 * Parser for rules of language conversion , parse rules in -{ }- tag.
1114 * @ingroup Language
1115 * @author fdcn <fdcn64@gmail.com>, PhiLiP <philip.npc@gmail.com>
1116 */
1117 class ConverterRule {
1118 var $mText; // original text in -{text}-
1119 var $mConverter; // LanguageConverter object
1120 var $mRuleDisplay = '';
1121 var $mRuleTitle = false;
1122 var $mRules = '';// string : the text of the rules
1123 var $mRulesAction = 'none';
1124 var $mFlags = array();
1125 var $mVariantFlags = array();
1126 var $mConvTable = array();
1127 var $mBidtable = array();// array of the translation in each variant
1128 var $mUnidtable = array();// array of the translation in each variant
1129
1130 /**
1131 * Constructor
1132 *
1133 * @param $text String: the text between -{ and }-
1134 * @param $converter LanguageConverter object
1135 */
1136 public function __construct( $text, $converter ) {
1137 $this->mText = $text;
1138 $this->mConverter = $converter;
1139 }
1140
1141 /**
1142 * Check if variants array in convert array.
1143 *
1144 * @param $variants Array or string: variant language code
1145 * @return String: translated text
1146 */
1147 public function getTextInBidtable( $variants ) {
1148 $variants = (array)$variants;
1149 if ( !$variants ) {
1150 return false;
1151 }
1152 foreach ( $variants as $variant ) {
1153 if ( isset( $this->mBidtable[$variant] ) ) {
1154 return $this->mBidtable[$variant];
1155 }
1156 }
1157 return false;
1158 }
1159
1160 /**
1161 * Parse flags with syntax -{FLAG| ... }-
1162 * @private
1163 */
1164 function parseFlags() {
1165 $text = $this->mText;
1166 $flags = array();
1167 $variantFlags = array();
1168
1169 $sepPos = strpos( $text, '|' );
1170 if ( $sepPos !== false ) {
1171 $validFlags = $this->mConverter->mFlags;
1172 $f = StringUtils::explode( ';', substr( $text, 0, $sepPos ) );
1173 foreach ( $f as $ff ) {
1174 $ff = trim( $ff );
1175 if ( isset( $validFlags[$ff] ) ) {
1176 $flags[$validFlags[$ff]] = true;
1177 }
1178 }
1179 $text = strval( substr( $text, $sepPos + 1 ) );
1180 }
1181
1182 if ( !$flags ) {
1183 $flags['S'] = true;
1184 } elseif ( isset( $flags['R'] ) ) {
1185 $flags = array( 'R' => true );// remove other flags
1186 } elseif ( isset( $flags['N'] ) ) {
1187 $flags = array( 'N' => true );// remove other flags
1188 } elseif ( isset( $flags['-'] ) ) {
1189 $flags = array( '-' => true );// remove other flags
1190 } elseif ( count( $flags ) == 1 && isset( $flags['T'] ) ) {
1191 $flags['H'] = true;
1192 } elseif ( isset( $flags['H'] ) ) {
1193 // replace A flag, and remove other flags except T
1194 $temp = array( '+' => true, 'H' => true );
1195 if ( isset( $flags['T'] ) ) {
1196 $temp['T'] = true;
1197 }
1198 if ( isset( $flags['D'] ) ) {
1199 $temp['D'] = true;
1200 }
1201 $flags = $temp;
1202 } else {
1203 if ( isset( $flags['A'] ) ) {
1204 $flags['+'] = true;
1205 $flags['S'] = true;
1206 }
1207 if ( isset( $flags['D'] ) ) {
1208 unset( $flags['S'] );
1209 }
1210 // try to find flags like "zh-hans", "zh-hant"
1211 // allow syntaxes like "-{zh-hans;zh-hant|XXXX}-"
1212 $variantFlags = array_intersect( array_keys( $flags ), $this->mConverter->mVariants );
1213 if ( $variantFlags ) {
1214 $variantFlags = array_flip( $variantFlags );
1215 $flags = array();
1216 }
1217 }
1218 $this->mVariantFlags = $variantFlags;
1219 $this->mRules = $text;
1220 $this->mFlags = $flags;
1221 }
1222
1223 /**
1224 * Generate conversion table.
1225 * @private
1226 */
1227 function parseRules() {
1228 $rules = $this->mRules;
1229 $bidtable = array();
1230 $unidtable = array();
1231 $variants = $this->mConverter->mVariants;
1232 $varsep_pattern = $this->mConverter->getVarSeparatorPattern();
1233
1234 $choice = preg_split( $varsep_pattern, $rules );
1235
1236 foreach ( $choice as $c ) {
1237 $v = explode( ':', $c, 2 );
1238 if ( count( $v ) != 2 ) {
1239 // syntax error, skip
1240 continue;
1241 }
1242 $to = trim( $v[1] );
1243 $v = trim( $v[0] );
1244 $u = explode( '=>', $v, 2 );
1245 // if $to is empty, strtr() could return a wrong result
1246 if ( count( $u ) == 1 && $to && in_array( $v, $variants ) ) {
1247 $bidtable[$v] = $to;
1248 } elseif ( count( $u ) == 2 ) {
1249 $from = trim( $u[0] );
1250 $v = trim( $u[1] );
1251 if ( array_key_exists( $v, $unidtable )
1252 && !is_array( $unidtable[$v] )
1253 && $to
1254 && in_array( $v, $variants ) ) {
1255 $unidtable[$v] = array( $from => $to );
1256 } elseif ( $to && in_array( $v, $variants ) ) {
1257 $unidtable[$v][$from] = $to;
1258 }
1259 }
1260 // syntax error, pass
1261 if ( !isset( $this->mConverter->mVariantNames[$v] ) ) {
1262 $bidtable = array();
1263 $unidtable = array();
1264 break;
1265 }
1266 }
1267 $this->mBidtable = $bidtable;
1268 $this->mUnidtable = $unidtable;
1269 }
1270
1271 /**
1272 * @private
1273 *
1274 * @return string
1275 */
1276 function getRulesDesc() {
1277 $codesep = $this->mConverter->mDescCodeSep;
1278 $varsep = $this->mConverter->mDescVarSep;
1279 $text = '';
1280 foreach ( $this->mBidtable as $k => $v ) {
1281 $text .= $this->mConverter->mVariantNames[$k] . "$codesep$v$varsep";
1282 }
1283 foreach ( $this->mUnidtable as $k => $a ) {
1284 foreach ( $a as $from => $to ) {
1285 $text .= $from . '⇒' . $this->mConverter->mVariantNames[$k] .
1286 "$codesep$to$varsep";
1287 }
1288 }
1289 return $text;
1290 }
1291
1292 /**
1293 * Parse rules conversion.
1294 * @private
1295 *
1296 * @param $variant
1297 *
1298 * @return string
1299 */
1300 function getRuleConvertedStr( $variant ) {
1301 $bidtable = $this->mBidtable;
1302 $unidtable = $this->mUnidtable;
1303
1304 if ( count( $bidtable ) + count( $unidtable ) == 0 ) {
1305 return $this->mRules;
1306 } else {
1307 // display current variant in bidirectional array
1308 $disp = $this->getTextInBidtable( $variant );
1309 // or display current variant in fallbacks
1310 if ( !$disp ) {
1311 $disp = $this->getTextInBidtable(
1312 $this->mConverter->getVariantFallbacks( $variant ) );
1313 }
1314 // or display current variant in unidirectional array
1315 if ( !$disp && array_key_exists( $variant, $unidtable ) ) {
1316 $disp = array_values( $unidtable[$variant] );
1317 $disp = $disp[0];
1318 }
1319 // or display frist text under disable manual convert
1320 if ( !$disp
1321 && $this->mConverter->mManualLevel[$variant] == 'disable' ) {
1322 if ( count( $bidtable ) > 0 ) {
1323 $disp = array_values( $bidtable );
1324 $disp = $disp[0];
1325 } else {
1326 $disp = array_values( $unidtable );
1327 $disp = array_values( $disp[0] );
1328 $disp = $disp[0];
1329 }
1330 }
1331 return $disp;
1332 }
1333 }
1334
1335 /**
1336 * Generate conversion table for all text.
1337 * @private
1338 */
1339 function generateConvTable() {
1340 // Special case optimisation
1341 if ( !$this->mBidtable && !$this->mUnidtable ) {
1342 $this->mConvTable = array();
1343 return;
1344 }
1345
1346 $bidtable = $this->mBidtable;
1347 $unidtable = $this->mUnidtable;
1348 $manLevel = $this->mConverter->mManualLevel;
1349
1350 $vmarked = array();
1351 foreach ( $this->mConverter->mVariants as $v ) {
1352 /* for bidirectional array
1353 fill in the missing variants, if any,
1354 with fallbacks */
1355 if ( !isset( $bidtable[$v] ) ) {
1356 $variantFallbacks =
1357 $this->mConverter->getVariantFallbacks( $v );
1358 $vf = $this->getTextInBidtable( $variantFallbacks );
1359 if ( $vf ) {
1360 $bidtable[$v] = $vf;
1361 }
1362 }
1363
1364 if ( isset( $bidtable[$v] ) ) {
1365 foreach ( $vmarked as $vo ) {
1366 // use syntax: -{A|zh:WordZh;zh-tw:WordTw}-
1367 // or -{H|zh:WordZh;zh-tw:WordTw}-
1368 // or -{-|zh:WordZh;zh-tw:WordTw}-
1369 // to introduce a custom mapping between
1370 // words WordZh and WordTw in the whole text
1371 if ( $manLevel[$v] == 'bidirectional' ) {
1372 $this->mConvTable[$v][$bidtable[$vo]] = $bidtable[$v];
1373 }
1374 if ( $manLevel[$vo] == 'bidirectional' ) {
1375 $this->mConvTable[$vo][$bidtable[$v]] = $bidtable[$vo];
1376 }
1377 }
1378 $vmarked[] = $v;
1379 }
1380 /* for unidirectional array fill to convert tables */
1381 if ( ( $manLevel[$v] == 'bidirectional' || $manLevel[$v] == 'unidirectional' )
1382 && isset( $unidtable[$v] ) )
1383 {
1384 if ( isset( $this->mConvTable[$v] ) ) {
1385 $this->mConvTable[$v] = array_merge( $this->mConvTable[$v], $unidtable[$v] );
1386 } else {
1387 $this->mConvTable[$v] = $unidtable[$v];
1388 }
1389 }
1390 }
1391 }
1392
1393 /**
1394 * Parse rules and flags.
1395 * @param $variant String: variant language code
1396 */
1397 public function parse( $variant = null ) {
1398 if ( !$variant ) {
1399 $variant = $this->mConverter->getPreferredVariant();
1400 }
1401
1402 $this->parseFlags();
1403 $flags = $this->mFlags;
1404
1405 // convert to specified variant
1406 // syntax: -{zh-hans;zh-hant[;...]|<text to convert>}-
1407 if ( $this->mVariantFlags ) {
1408 // check if current variant in flags
1409 if ( isset( $this->mVariantFlags[$variant] ) ) {
1410 // then convert <text to convert> to current language
1411 $this->mRules = $this->mConverter->autoConvert( $this->mRules,
1412 $variant );
1413 } else { // if current variant no in flags,
1414 // then we check its fallback variants.
1415 $variantFallbacks =
1416 $this->mConverter->getVariantFallbacks( $variant );
1417 if( is_array( $variantFallbacks ) ) {
1418 foreach ( $variantFallbacks as $variantFallback ) {
1419 // if current variant's fallback exist in flags
1420 if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
1421 // then convert <text to convert> to fallback language
1422 $this->mRules =
1423 $this->mConverter->autoConvert( $this->mRules,
1424 $variantFallback );
1425 break;
1426 }
1427 }
1428 }
1429 }
1430 $this->mFlags = $flags = array( 'R' => true );
1431 }
1432
1433 if ( !isset( $flags['R'] ) && !isset( $flags['N'] ) ) {
1434 // decode => HTML entities modified by Sanitizer::removeHTMLtags
1435 $this->mRules = str_replace( '=&gt;', '=>', $this->mRules );
1436 $this->parseRules();
1437 }
1438 $rules = $this->mRules;
1439
1440 if ( !$this->mBidtable && !$this->mUnidtable ) {
1441 if ( isset( $flags['+'] ) || isset( $flags['-'] ) ) {
1442 // fill all variants if text in -{A/H/-|text} without rules
1443 foreach ( $this->mConverter->mVariants as $v ) {
1444 $this->mBidtable[$v] = $rules;
1445 }
1446 } elseif ( !isset( $flags['N'] ) && !isset( $flags['T'] ) ) {
1447 $this->mFlags = $flags = array( 'R' => true );
1448 }
1449 }
1450
1451 $this->mRuleDisplay = false;
1452 foreach ( $flags as $flag => $unused ) {
1453 switch ( $flag ) {
1454 case 'R':
1455 // if we don't do content convert, still strip the -{}- tags
1456 $this->mRuleDisplay = $rules;
1457 break;
1458 case 'N':
1459 // process N flag: output current variant name
1460 $ruleVar = trim( $rules );
1461 if ( isset( $this->mConverter->mVariantNames[$ruleVar] ) ) {
1462 $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar];
1463 } else {
1464 $this->mRuleDisplay = '';
1465 }
1466 break;
1467 case 'D':
1468 // process D flag: output rules description
1469 $this->mRuleDisplay = $this->getRulesDesc();
1470 break;
1471 case 'H':
1472 // process H,- flag or T only: output nothing
1473 $this->mRuleDisplay = '';
1474 break;
1475 case '-':
1476 $this->mRulesAction = 'remove';
1477 $this->mRuleDisplay = '';
1478 break;
1479 case '+':
1480 $this->mRulesAction = 'add';
1481 $this->mRuleDisplay = '';
1482 break;
1483 case 'S':
1484 $this->mRuleDisplay = $this->getRuleConvertedStr( $variant );
1485 break;
1486 case 'T':
1487 $this->mRuleTitle = $this->getRuleConvertedStr( $variant );
1488 $this->mRuleDisplay = '';
1489 break;
1490 default:
1491 // ignore unknown flags (but see error case below)
1492 }
1493 }
1494 if ( $this->mRuleDisplay === false ) {
1495 $this->mRuleDisplay = '<span class="error">'
1496 . wfMessage( 'converter-manual-rule-error' )->inContentLanguage()->escaped()
1497 . '</span>';
1498 }
1499
1500 $this->generateConvTable();
1501 }
1502
1503 /**
1504 * @todo FIXME: code this function :)
1505 */
1506 public function hasRules() {
1507 // TODO:
1508 }
1509
1510 /**
1511 * Get display text on markup -{...}-
1512 * @return string
1513 */
1514 public function getDisplay() {
1515 return $this->mRuleDisplay;
1516 }
1517
1518 /**
1519 * Get converted title.
1520 * @return string
1521 */
1522 public function getTitle() {
1523 return $this->mRuleTitle;
1524 }
1525
1526 /**
1527 * Return how deal with conversion rules.
1528 * @return string
1529 */
1530 public function getRulesAction() {
1531 return $this->mRulesAction;
1532 }
1533
1534 /**
1535 * Get conversion table. (bidirectional and unidirectional
1536 * conversion table)
1537 * @return array
1538 */
1539 public function getConvTable() {
1540 return $this->mConvTable;
1541 }
1542
1543 /**
1544 * Get conversion rules string.
1545 * @return string
1546 */
1547 public function getRules() {
1548 return $this->mRules;
1549 }
1550
1551 /**
1552 * Get conversion flags.
1553 * @return array
1554 */
1555 public function getFlags() {
1556 return $this->mFlags;
1557 }
1558 }