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