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