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