Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups().
[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 ) ) {
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 return $this->recursiveConvertTopLevel( $text, $variant );
601 }
602
603 /**
604 * Recursively convert text on the outside. Allow to use nested
605 * markups to custom rules.
606 *
607 * @param $text String: text to be converted
608 * @param $variant String: the target variant code
609 * @param $depth Integer: depth of recursion
610 * @return String: converted text
611 */
612 protected function recursiveConvertTopLevel( $text, $variant, $depth = 0 ) {
613 $startPos = 0;
614 $out = '';
615 $length = strlen( $text );
616 $shouldConvert = !$this->guessVariant( $text, $variant );
617
618 while ( $startPos < $length ) {
619 $pos = strpos( $text, '-{', $startPos );
620
621 if ( $pos === false ) {
622 // No more markup, append final segment
623 $fragment = substr( $text, $startPos );
624 $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment;
625 return $out;
626 }
627
628 // Markup found
629 // Append initial segment
630 $fragment = substr( $text, $startPos, $pos - $startPos );
631 $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment;
632
633 // Advance position
634 $startPos = $pos;
635
636 // Do recursive conversion
637 $out .= $this->recursiveConvertRule( $text, $variant, $startPos, $depth + 1 );
638 }
639
640 return $out;
641 }
642
643 /**
644 * Recursively convert text on the inside.
645 *
646 * @param $text String: text to be converted
647 * @param $variant String: the target variant code
648 * @param $startPos int
649 * @param $depth Integer: depth of recursion
650 *
651 * @throws MWException
652 * @return String: converted text
653 */
654 protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) {
655 // Quick sanity check (no function calls)
656 if ( $text[$startPos] !== '-' || $text[$startPos + 1] !== '{' ) {
657 throw new MWException( __METHOD__ . ': invalid input string' );
658 }
659
660 $startPos += 2;
661 $inner = '';
662 $warningDone = false;
663 $length = strlen( $text );
664
665 while ( $startPos < $length ) {
666 $m = false;
667 preg_match( '/-\{|\}-/', $text, $m, PREG_OFFSET_CAPTURE, $startPos );
668 if ( !$m ) {
669 // Unclosed rule
670 break;
671 }
672
673 $token = $m[0][0];
674 $pos = $m[0][1];
675
676 // Markup found
677 // Append initial segment
678 $inner .= substr( $text, $startPos, $pos - $startPos );
679
680 // Advance position
681 $startPos = $pos;
682
683 switch ( $token ) {
684 case '-{':
685 // Check max depth
686 if ( $depth >= $this->mMaxDepth ) {
687 $inner .= '-{';
688 if ( !$warningDone ) {
689 $inner .= '<span class="error">' .
690 wfMsgForContent( 'language-converter-depth-warning',
691 $this->mMaxDepth ) .
692 '</span>';
693 $warningDone = true;
694 }
695 $startPos += 2;
696 continue;
697 }
698 // Recursively parse another rule
699 $inner .= $this->recursiveConvertRule( $text, $variant, $startPos, $depth + 1 );
700 break;
701 case '}-':
702 // Apply the rule
703 $startPos += 2;
704 $rule = new ConverterRule( $inner, $this );
705 $rule->parse( $variant );
706 $this->applyManualConv( $rule );
707 return $rule->getDisplay();
708 default:
709 throw new MWException( __METHOD__ . ': invalid regex match' );
710 }
711 }
712
713 // Unclosed rule
714 if ( $startPos < $length ) {
715 $inner .= substr( $text, $startPos );
716 }
717 $startPos = $length;
718 return '-{' . $this->autoConvert( $inner, $variant );
719 }
720
721 /**
722 * If a language supports multiple variants, it is possible that
723 * non-existing link in one variant actually exists in another variant.
724 * This function tries to find it. See e.g. LanguageZh.php
725 *
726 * @param $link String: the name of the link
727 * @param $nt Mixed: the title object of the link
728 * @param $ignoreOtherCond Boolean: to disable other conditions when
729 * we need to transclude a template or update a category's link
730 * @return Null, the input parameters may be modified upon return
731 */
732 public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
733 # If the article has already existed, there is no need to
734 # check it again, otherwise it may cause a fault.
735 if ( is_object( $nt ) && $nt->exists() ) {
736 return;
737 }
738
739 global $wgDisableLangConversion, $wgDisableTitleConversion, $wgRequest,
740 $wgUser;
741 $isredir = $wgRequest->getText( 'redirect', 'yes' );
742 $action = $wgRequest->getText( 'action' );
743 $linkconvert = $wgRequest->getText( 'linkconvert', 'yes' );
744 $disableLinkConversion = $wgDisableLangConversion
745 || $wgDisableTitleConversion;
746 $linkBatch = new LinkBatch();
747
748 $ns = NS_MAIN;
749
750 if ( $disableLinkConversion ||
751 ( !$ignoreOtherCond &&
752 ( $isredir == 'no'
753 || $action == 'edit'
754 || $action == 'submit'
755 || $linkconvert == 'no'
756 || $wgUser->getOption( 'noconvertlink' ) == 1 ) ) ) {
757 return;
758 }
759
760 if ( is_object( $nt ) ) {
761 $ns = $nt->getNamespace();
762 }
763
764 $variants = $this->autoConvertToAllVariants( $link );
765 if ( !$variants ) { // give up
766 return;
767 }
768
769 $titles = array();
770
771 foreach ( $variants as $v ) {
772 if ( $v != $link ) {
773 $varnt = Title::newFromText( $v, $ns );
774 if ( !is_null( $varnt ) ) {
775 $linkBatch->addObj( $varnt );
776 $titles[] = $varnt;
777 }
778 }
779 }
780
781 // fetch all variants in single query
782 $linkBatch->execute();
783
784 foreach ( $titles as $varnt ) {
785 if ( $varnt->getArticleID() > 0 ) {
786 $nt = $varnt;
787 $link = $varnt->getText();
788 break;
789 }
790 }
791 }
792
793 /**
794 * Returns language specific hash options.
795 *
796 * @return string
797 */
798 public function getExtraHashOptions() {
799 $variant = $this->getPreferredVariant();
800 return '!' . $variant;
801 }
802
803 /**
804 * Guess if a text is written in a variant. This should be implemented in subclasses.
805 *
806 * @param string $text the text to be checked
807 * @param string $variant language code of the variant to be checked for
808 * @return bool true if $text appears to be written in $variant, false if not
809 *
810 * @author Nikola Smolenski <smolensk@eunet.rs>
811 * @since 1.19
812 */
813 public function guessVariant($text, $variant) {
814 return false;
815 }
816
817 /**
818 * Load default conversion tables.
819 * This method must be implemented in derived class.
820 *
821 * @private
822 * @throws MWException
823 */
824 function loadDefaultTables() {
825 $name = get_class( $this );
826 throw new MWException( "Must implement loadDefaultTables() method in class $name" );
827 }
828
829 /**
830 * Load conversion tables either from the cache or the disk.
831 * @private
832 * @param $fromCache Boolean: load from memcached? Defaults to true.
833 */
834 function loadTables( $fromCache = true ) {
835 global $wgLangConvMemc;
836
837 if ( $this->mTablesLoaded ) {
838 return;
839 }
840
841 wfProfileIn( __METHOD__ );
842 $this->mTablesLoaded = true;
843 $this->mTables = false;
844 if ( $fromCache ) {
845 wfProfileIn( __METHOD__ . '-cache' );
846 $this->mTables = $wgLangConvMemc->get( $this->mCacheKey );
847 wfProfileOut( __METHOD__ . '-cache' );
848 }
849 if ( !$this->mTables
850 || !array_key_exists( self::CACHE_VERSION_KEY, $this->mTables ) ) {
851 wfProfileIn( __METHOD__ . '-recache' );
852 // not in cache, or we need a fresh reload.
853 // We will first load the default tables
854 // then update them using things in MediaWiki:Conversiontable/*
855 $this->loadDefaultTables();
856 foreach ( $this->mVariants as $var ) {
857 $cached = $this->parseCachedTable( $var );
858 $this->mTables[$var]->mergeArray( $cached );
859 }
860
861 $this->postLoadTables();
862 $this->mTables[self::CACHE_VERSION_KEY] = true;
863
864 $wgLangConvMemc->set( $this->mCacheKey, $this->mTables, 43200 );
865 wfProfileOut( __METHOD__ . '-recache' );
866 }
867 wfProfileOut( __METHOD__ );
868 }
869
870 /**
871 * Hook for post processing after conversion tables are loaded.
872 */
873 function postLoadTables() { }
874
875 /**
876 * Reload the conversion tables.
877 *
878 * @private
879 */
880 function reloadTables() {
881 if ( $this->mTables ) {
882 unset( $this->mTables );
883 }
884 $this->mTablesLoaded = false;
885 $this->loadTables( false );
886 }
887
888 /**
889 * Parse the conversion table stored in the cache.
890 *
891 * The tables should be in blocks of the following form:
892 * -{
893 * word => word ;
894 * word => word ;
895 * ...
896 * }-
897 *
898 * To make the tables more manageable, subpages are allowed
899 * and will be parsed recursively if $recursive == true.
900 *
901 * @param $code String: language code
902 * @param $subpage String: subpage name
903 * @param $recursive Boolean: parse subpages recursively? Defaults to true.
904 *
905 * @return array
906 */
907 function parseCachedTable( $code, $subpage = '', $recursive = true ) {
908 static $parsed = array();
909
910 $key = 'Conversiontable/' . $code;
911 if ( $subpage ) {
912 $key .= '/' . $subpage;
913 }
914 if ( array_key_exists( $key, $parsed ) ) {
915 return array();
916 }
917
918 $parsed[$key] = true;
919
920 if ( $subpage === '' ) {
921 $txt = MessageCache::singleton()->get( 'conversiontable', true, $code );
922 } else {
923 $txt = false;
924 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
925 if ( $title && $title->exists() ) {
926 $revision = Revision::newFromTitle( $title );
927 if ( $revision ) {
928 $txt = $revision->getRawText();
929 }
930 }
931 }
932
933 # Nothing to parse if there's no text
934 if ( $txt === false || $txt === null || $txt === '' ) {
935 return array();
936 }
937
938 // get all subpage links of the form
939 // [[MediaWiki:Conversiontable/zh-xx/...|...]]
940 $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
941 ':Conversiontable';
942 $subs = StringUtils::explode( '[[', $txt );
943 $sublinks = array();
944 foreach ( $subs as $sub ) {
945 $link = explode( ']]', $sub, 2 );
946 if ( count( $link ) != 2 ) {
947 continue;
948 }
949 $b = explode( '|', $link[0], 2 );
950 $b = explode( '/', trim( $b[0] ), 3 );
951 if ( count( $b ) == 3 ) {
952 $sublink = $b[2];
953 } else {
954 $sublink = '';
955 }
956
957 if ( $b[0] == $linkhead && $b[1] == $code ) {
958 $sublinks[] = $sublink;
959 }
960 }
961
962 // parse the mappings in this page
963 $blocks = StringUtils::explode( '-{', $txt );
964 $ret = array();
965 $first = true;
966 foreach ( $blocks as $block ) {
967 if ( $first ) {
968 // Skip the part before the first -{
969 $first = false;
970 continue;
971 }
972 $mappings = explode( '}-', $block, 2 );
973 $stripped = str_replace( array( "'", '"', '*', '#' ), '',
974 $mappings[0] );
975 $table = StringUtils::explode( ';', $stripped );
976 foreach ( $table as $t ) {
977 $m = explode( '=>', $t, 3 );
978 if ( count( $m ) != 2 ) {
979 continue;
980 }
981 // trim any trailling comments starting with '//'
982 $tt = explode( '//', $m[1], 2 );
983 $ret[trim( $m[0] )] = trim( $tt[0] );
984 }
985 }
986
987 // recursively parse the subpages
988 if ( $recursive ) {
989 foreach ( $sublinks as $link ) {
990 $s = $this->parseCachedTable( $code, $link, $recursive );
991 $ret = array_merge( $ret, $s );
992 }
993 }
994
995 if ( $this->mUcfirst ) {
996 foreach ( $ret as $k => $v ) {
997 $ret[$this->mLangObj->ucfirst( $k )] = $this->mLangObj->ucfirst( $v );
998 }
999 }
1000 return $ret;
1001 }
1002
1003 /**
1004 * Enclose a string with the "no conversion" tag. This is used by
1005 * various functions in the Parser.
1006 *
1007 * @param $text String: text to be tagged for no conversion
1008 * @param $noParse Boolean: unused
1009 * @return String: the tagged text
1010 */
1011 public function markNoConversion( $text, $noParse = false ) {
1012 # don't mark if already marked
1013 if ( strpos( $text, '-{' ) || strpos( $text, '}-' ) ) {
1014 return $text;
1015 }
1016
1017 $ret = "-{R|$text}-";
1018 return $ret;
1019 }
1020
1021 /**
1022 * Convert the sorting key for category links. This should make different
1023 * keys that are variants of each other map to the same key.
1024 *
1025 * @param $key string
1026 *
1027 * @return string
1028 */
1029 function convertCategoryKey( $key ) {
1030 return $key;
1031 }
1032
1033 /**
1034 * Hook to refresh the cache of conversion tables when
1035 * MediaWiki:Conversiontable* is updated.
1036 * @private
1037 *
1038 * @param $article Article object
1039 * @param $user Object: User object for the current user
1040 * @param $text String: article text (?)
1041 * @param $summary String: edit summary of the edit
1042 * @param $isMinor Boolean: was the edit marked as minor?
1043 * @param $isWatch Boolean: did the user watch this page or not?
1044 * @param $section
1045 * @param $flags int Bitfield
1046 * @param $revision Object: new Revision object or null
1047 * @return Boolean: true
1048 */
1049 function OnArticleSaveComplete( $article, $user, $text, $summary, $isMinor,
1050 $isWatch, $section, $flags, $revision ) {
1051 $titleobj = $article->getTitle();
1052 if ( $titleobj->getNamespace() == NS_MEDIAWIKI ) {
1053 $title = $titleobj->getDBkey();
1054 $t = explode( '/', $title, 3 );
1055 $c = count( $t );
1056 if ( $c > 1 && $t[0] == 'Conversiontable' ) {
1057 if ( $this->validateVariant( $t[1] ) ) {
1058 $this->reloadTables();
1059 }
1060 }
1061 }
1062 return true;
1063 }
1064
1065 /**
1066 * Armour rendered math against conversion.
1067 * Escape special chars in parsed math text. (in most cases are img elements)
1068 *
1069 * @param $text String: text to armour against conversion
1070 * @return String: armoured text where { and } have been converted to
1071 * &#123; and &#125;
1072 */
1073 public function armourMath( $text ) {
1074 // convert '-{' and '}-' to '-&#123;' and '&#125;-' to prevent
1075 // any unwanted markup appearing in the math image tag.
1076 $text = strtr( $text, array( '-{' => '-&#123;', '}-' => '&#125;-' ) );
1077 return $text;
1078 }
1079
1080 /**
1081 * Get the cached separator pattern for ConverterRule::parseRules()
1082 */
1083 function getVarSeparatorPattern() {
1084 if ( is_null( $this->mVarSeparatorPattern ) ) {
1085 // varsep_pattern for preg_split:
1086 // text should be splited by ";" only if a valid variant
1087 // name exist after the markup, for example:
1088 // -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:\
1089 // <span style="font-size:120%;">yyy</span>;}-
1090 // we should split it as:
1091 // array(
1092 // [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>'
1093 // [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>'
1094 // [2] => ''
1095 // )
1096 $pat = '/;\s*(?=';
1097 foreach ( $this->mVariants as $variant ) {
1098 // zh-hans:xxx;zh-hant:yyy
1099 $pat .= $variant . '\s*:|';
1100 // xxx=>zh-hans:yyy; xxx=>zh-hant:zzz
1101 $pat .= '[^;]*?=>\s*' . $variant . '\s*:|';
1102 }
1103 $pat .= '\s*$)/';
1104 $this->mVarSeparatorPattern = $pat;
1105 }
1106 return $this->mVarSeparatorPattern;
1107 }
1108 }
1109
1110 /**
1111 * Parser for rules of language conversion , parse rules in -{ }- tag.
1112 * @ingroup Language
1113 * @author fdcn <fdcn64@gmail.com>, PhiLiP <philip.npc@gmail.com>
1114 */
1115 class ConverterRule {
1116 var $mText; // original text in -{text}-
1117 var $mConverter; // LanguageConverter object
1118 var $mManualCodeError = '<strong class="error">code error!</strong>';
1119 var $mRuleDisplay = '';
1120 var $mRuleTitle = false;
1121 var $mRules = '';// string : the text of the rules
1122 var $mRulesAction = 'none';
1123 var $mFlags = array();
1124 var $mVariantFlags = array();
1125 var $mConvTable = array();
1126 var $mBidtable = array();// array of the translation in each variant
1127 var $mUnidtable = array();// array of the translation in each variant
1128
1129 /**
1130 * Constructor
1131 *
1132 * @param $text String: the text between -{ and }-
1133 * @param $converter LanguageConverter object
1134 */
1135 public function __construct( $text, $converter ) {
1136 $this->mText = $text;
1137 $this->mConverter = $converter;
1138 }
1139
1140 /**
1141 * Check if variants array in convert array.
1142 *
1143 * @param $variants Array or string: variant language code
1144 * @return String: translated text
1145 */
1146 public function getTextInBidtable( $variants ) {
1147 $variants = (array)$variants;
1148 if ( !$variants ) {
1149 return false;
1150 }
1151 foreach ( $variants as $variant ) {
1152 if ( isset( $this->mBidtable[$variant] ) ) {
1153 return $this->mBidtable[$variant];
1154 }
1155 }
1156 return false;
1157 }
1158
1159 /**
1160 * Parse flags with syntax -{FLAG| ... }-
1161 * @private
1162 */
1163 function parseFlags() {
1164 $text = $this->mText;
1165 $flags = array();
1166 $variantFlags = array();
1167
1168 $sepPos = strpos( $text, '|' );
1169 if ( $sepPos !== false ) {
1170 $validFlags = $this->mConverter->mFlags;
1171 $f = StringUtils::explode( ';', substr( $text, 0, $sepPos ) );
1172 foreach ( $f as $ff ) {
1173 $ff = trim( $ff );
1174 if ( isset( $validFlags[$ff] ) ) {
1175 $flags[$validFlags[$ff]] = true;
1176 }
1177 }
1178 $text = strval( substr( $text, $sepPos + 1 ) );
1179 }
1180
1181 if ( !$flags ) {
1182 $flags['S'] = true;
1183 } elseif ( isset( $flags['R'] ) ) {
1184 $flags = array( 'R' => true );// remove other flags
1185 } elseif ( isset( $flags['N'] ) ) {
1186 $flags = array( 'N' => true );// remove other flags
1187 } elseif ( isset( $flags['-'] ) ) {
1188 $flags = array( '-' => true );// remove other flags
1189 } elseif ( count( $flags ) == 1 && isset( $flags['T'] ) ) {
1190 $flags['H'] = true;
1191 } elseif ( isset( $flags['H'] ) ) {
1192 // replace A flag, and remove other flags except T
1193 $temp = array( '+' => true, 'H' => true );
1194 if ( isset( $flags['T'] ) ) {
1195 $temp['T'] = true;
1196 }
1197 if ( isset( $flags['D'] ) ) {
1198 $temp['D'] = true;
1199 }
1200 $flags = $temp;
1201 } else {
1202 if ( isset( $flags['A'] ) ) {
1203 $flags['+'] = true;
1204 $flags['S'] = true;
1205 }
1206 if ( isset( $flags['D'] ) ) {
1207 unset( $flags['S'] );
1208 }
1209 // try to find flags like "zh-hans", "zh-hant"
1210 // allow syntaxes like "-{zh-hans;zh-hant|XXXX}-"
1211 $variantFlags = array_intersect( array_keys( $flags ), $this->mConverter->mVariants );
1212 if ( $variantFlags ) {
1213 $variantFlags = array_flip( $variantFlags );
1214 $flags = array();
1215 }
1216 }
1217 $this->mVariantFlags = $variantFlags;
1218 $this->mRules = $text;
1219 $this->mFlags = $flags;
1220 }
1221
1222 /**
1223 * Generate conversion table.
1224 * @private
1225 */
1226 function parseRules() {
1227 $rules = $this->mRules;
1228 $bidtable = array();
1229 $unidtable = array();
1230 $variants = $this->mConverter->mVariants;
1231 $varsep_pattern = $this->mConverter->getVarSeparatorPattern();
1232
1233 $choice = preg_split( $varsep_pattern, $rules );
1234
1235 foreach ( $choice as $c ) {
1236 $v = explode( ':', $c, 2 );
1237 if ( count( $v ) != 2 ) {
1238 // syntax error, skip
1239 continue;
1240 }
1241 $to = trim( $v[1] );
1242 $v = trim( $v[0] );
1243 $u = explode( '=>', $v, 2 );
1244 // if $to is empty, strtr() could return a wrong result
1245 if ( count( $u ) == 1 && $to && in_array( $v, $variants ) ) {
1246 $bidtable[$v] = $to;
1247 } elseif ( count( $u ) == 2 ) {
1248 $from = trim( $u[0] );
1249 $v = trim( $u[1] );
1250 if ( array_key_exists( $v, $unidtable )
1251 && !is_array( $unidtable[$v] )
1252 && $to
1253 && in_array( $v, $variants ) ) {
1254 $unidtable[$v] = array( $from => $to );
1255 } elseif ( $to && in_array( $v, $variants ) ) {
1256 $unidtable[$v][$from] = $to;
1257 }
1258 }
1259 // syntax error, pass
1260 if ( !isset( $this->mConverter->mVariantNames[$v] ) ) {
1261 $bidtable = array();
1262 $unidtable = array();
1263 break;
1264 }
1265 }
1266 $this->mBidtable = $bidtable;
1267 $this->mUnidtable = $unidtable;
1268 }
1269
1270 /**
1271 * @private
1272 *
1273 * @return string
1274 */
1275 function getRulesDesc() {
1276 $codesep = $this->mConverter->mDescCodeSep;
1277 $varsep = $this->mConverter->mDescVarSep;
1278 $text = '';
1279 foreach ( $this->mBidtable as $k => $v ) {
1280 $text .= $this->mConverter->mVariantNames[$k] . "$codesep$v$varsep";
1281 }
1282 foreach ( $this->mUnidtable as $k => $a ) {
1283 foreach ( $a as $from => $to ) {
1284 $text .= $from . '⇒' . $this->mConverter->mVariantNames[$k] .
1285 "$codesep$to$varsep";
1286 }
1287 }
1288 return $text;
1289 }
1290
1291 /**
1292 * Parse rules conversion.
1293 * @private
1294 *
1295 * @param $variant
1296 *
1297 * @return string
1298 */
1299 function getRuleConvertedStr( $variant ) {
1300 $bidtable = $this->mBidtable;
1301 $unidtable = $this->mUnidtable;
1302
1303 if ( count( $bidtable ) + count( $unidtable ) == 0 ) {
1304 return $this->mRules;
1305 } else {
1306 // display current variant in bidirectional array
1307 $disp = $this->getTextInBidtable( $variant );
1308 // or display current variant in fallbacks
1309 if ( !$disp ) {
1310 $disp = $this->getTextInBidtable(
1311 $this->mConverter->getVariantFallbacks( $variant ) );
1312 }
1313 // or display current variant in unidirectional array
1314 if ( !$disp && array_key_exists( $variant, $unidtable ) ) {
1315 $disp = array_values( $unidtable[$variant] );
1316 $disp = $disp[0];
1317 }
1318 // or display frist text under disable manual convert
1319 if ( !$disp
1320 && $this->mConverter->mManualLevel[$variant] == 'disable' ) {
1321 if ( count( $bidtable ) > 0 ) {
1322 $disp = array_values( $bidtable );
1323 $disp = $disp[0];
1324 } else {
1325 $disp = array_values( $unidtable );
1326 $disp = array_values( $disp[0] );
1327 $disp = $disp[0];
1328 }
1329 }
1330 return $disp;
1331 }
1332 }
1333
1334 /**
1335 * Generate conversion table for all text.
1336 * @private
1337 */
1338 function generateConvTable() {
1339 // Special case optimisation
1340 if ( !$this->mBidtable && !$this->mUnidtable ) {
1341 $this->mConvTable = array();
1342 return;
1343 }
1344
1345 $bidtable = $this->mBidtable;
1346 $unidtable = $this->mUnidtable;
1347 $manLevel = $this->mConverter->mManualLevel;
1348
1349 $vmarked = array();
1350 foreach ( $this->mConverter->mVariants as $v ) {
1351 /* for bidirectional array
1352 fill in the missing variants, if any,
1353 with fallbacks */
1354 if ( !isset( $bidtable[$v] ) ) {
1355 $variantFallbacks =
1356 $this->mConverter->getVariantFallbacks( $v );
1357 $vf = $this->getTextInBidtable( $variantFallbacks );
1358 if ( $vf ) {
1359 $bidtable[$v] = $vf;
1360 }
1361 }
1362
1363 if ( isset( $bidtable[$v] ) ) {
1364 foreach ( $vmarked as $vo ) {
1365 // use syntax: -{A|zh:WordZh;zh-tw:WordTw}-
1366 // or -{H|zh:WordZh;zh-tw:WordTw}-
1367 // or -{-|zh:WordZh;zh-tw:WordTw}-
1368 // to introduce a custom mapping between
1369 // words WordZh and WordTw in the whole text
1370 if ( $manLevel[$v] == 'bidirectional' ) {
1371 $this->mConvTable[$v][$bidtable[$vo]] = $bidtable[$v];
1372 }
1373 if ( $manLevel[$vo] == 'bidirectional' ) {
1374 $this->mConvTable[$vo][$bidtable[$v]] = $bidtable[$vo];
1375 }
1376 }
1377 $vmarked[] = $v;
1378 }
1379 /* for unidirectional array fill to convert tables */
1380 if ( ( $manLevel[$v] == 'bidirectional' || $manLevel[$v] == 'unidirectional' )
1381 && isset( $unidtable[$v] ) )
1382 {
1383 if ( isset( $this->mConvTable[$v] ) ) {
1384 $this->mConvTable[$v] = array_merge( $this->mConvTable[$v], $unidtable[$v] );
1385 } else {
1386 $this->mConvTable[$v] = $unidtable[$v];
1387 }
1388 }
1389 }
1390 }
1391
1392 /**
1393 * Parse rules and flags.
1394 * @param $variant String: variant language code
1395 */
1396 public function parse( $variant = null ) {
1397 if ( !$variant ) {
1398 $variant = $this->mConverter->getPreferredVariant();
1399 }
1400
1401 $this->parseFlags();
1402 $flags = $this->mFlags;
1403
1404 // convert to specified variant
1405 // syntax: -{zh-hans;zh-hant[;...]|<text to convert>}-
1406 if ( $this->mVariantFlags ) {
1407 // check if current variant in flags
1408 if ( isset( $this->mVariantFlags[$variant] ) ) {
1409 // then convert <text to convert> to current language
1410 $this->mRules = $this->mConverter->autoConvert( $this->mRules,
1411 $variant );
1412 } else { // if current variant no in flags,
1413 // then we check its fallback variants.
1414 $variantFallbacks =
1415 $this->mConverter->getVariantFallbacks( $variant );
1416 if( is_array( $variantFallbacks ) ) {
1417 foreach ( $variantFallbacks as $variantFallback ) {
1418 // if current variant's fallback exist in flags
1419 if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
1420 // then convert <text to convert> to fallback language
1421 $this->mRules =
1422 $this->mConverter->autoConvert( $this->mRules,
1423 $variantFallback );
1424 break;
1425 }
1426 }
1427 }
1428 }
1429 $this->mFlags = $flags = array( 'R' => true );
1430 }
1431
1432 if ( !isset( $flags['R'] ) && !isset( $flags['N'] ) ) {
1433 // decode => HTML entities modified by Sanitizer::removeHTMLtags
1434 $this->mRules = str_replace( '=&gt;', '=>', $this->mRules );
1435 $this->parseRules();
1436 }
1437 $rules = $this->mRules;
1438
1439 if ( !$this->mBidtable && !$this->mUnidtable ) {
1440 if ( isset( $flags['+'] ) || isset( $flags['-'] ) ) {
1441 // fill all variants if text in -{A/H/-|text} without rules
1442 foreach ( $this->mConverter->mVariants as $v ) {
1443 $this->mBidtable[$v] = $rules;
1444 }
1445 } elseif ( !isset( $flags['N'] ) && !isset( $flags['T'] ) ) {
1446 $this->mFlags = $flags = array( 'R' => true );
1447 }
1448 }
1449
1450 $this->mRuleDisplay = false;
1451 foreach ( $flags as $flag => $unused ) {
1452 switch ( $flag ) {
1453 case 'R':
1454 // if we don't do content convert, still strip the -{}- tags
1455 $this->mRuleDisplay = $rules;
1456 break;
1457 case 'N':
1458 // process N flag: output current variant name
1459 $ruleVar = trim( $rules );
1460 if ( isset( $this->mConverter->mVariantNames[$ruleVar] ) ) {
1461 $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar];
1462 } else {
1463 $this->mRuleDisplay = '';
1464 }
1465 break;
1466 case 'D':
1467 // process D flag: output rules description
1468 $this->mRuleDisplay = $this->getRulesDesc();
1469 break;
1470 case 'H':
1471 // process H,- flag or T only: output nothing
1472 $this->mRuleDisplay = '';
1473 break;
1474 case '-':
1475 $this->mRulesAction = 'remove';
1476 $this->mRuleDisplay = '';
1477 break;
1478 case '+':
1479 $this->mRulesAction = 'add';
1480 $this->mRuleDisplay = '';
1481 break;
1482 case 'S':
1483 $this->mRuleDisplay = $this->getRuleConvertedStr( $variant );
1484 break;
1485 case 'T':
1486 $this->mRuleTitle = $this->getRuleConvertedStr( $variant );
1487 $this->mRuleDisplay = '';
1488 break;
1489 default:
1490 // ignore unknown flags (but see error case below)
1491 }
1492 }
1493 if ( $this->mRuleDisplay === false ) {
1494 $this->mRuleDisplay = $this->mManualCodeError;
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 }