6 * @author Zhengzhu Feng <zhengzhu@gmail.com>
7 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
8 * @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>
11 class LanguageConverter
{
12 var $mPreferredVariant='';
13 var $mMainLanguageCode;
14 var $mVariants, $mVariantFallbacks, $mVariantNames;
15 var $mTablesLoaded = false;
17 var $mTitleDisplay='';
18 var $mDoTitleConvert=true, $mDoContentConvert=true;
19 var $mManualLevel; // 'bidirectional' 'unidirectional' 'disable' for each variants
20 var $mManualCodeError='<span style="color: red;">code error!</span>';
21 var $mTitleFromFlag = false;
26 var $mUcfirst = false;
28 const CACHE_VERSION_KEY
= 'VERSION 6';
33 * @param string $maincode the main language code of this language
34 * @param array $variants the supported variants of this language
35 * @param array $variantfallback the fallback language of each variant
36 * @param array $markup array defining the markup used for manual conversion
37 * @param array $flags array defining the custom strings that maps to the flags
40 function __construct($langobj, $maincode,
42 $variantfallbacks=array(),
45 $manualLevel = array() ) {
46 $this->mLangObj
= $langobj;
47 $this->mMainLanguageCode
= $maincode;
48 $this->mVariants
= $variants;
49 $this->mVariantFallbacks
= $variantfallbacks;
50 global $wgLanguageNames;
51 $this->mVariantNames
= $wgLanguageNames;
52 $this->mCacheKey
= wfMemcKey( 'conversiontables', $maincode );
56 'unidsep'=>'=>', //for unidirectional conversion
61 $this->mMarkup
= array_merge($m, $markup);
63 // 'S' show converted text
64 // '+' add rules for alltext
65 // 'E' the gave flags is error
66 // these flags above are reserved for program
67 'A'=>'A', // add rule for convert code (all text convert)
68 'T'=>'T', // title convert
69 'R'=>'R', // raw content
70 'D'=>'D', // convert description (subclass implement)
71 '-'=>'-', // remove convert (not implement)
72 'H'=>'H', // add rule for convert code (but no display in placed code )
73 'N'=>'N' // current variant name
75 $this->mFlags
= array_merge($f, $flags);
76 foreach( $this->mVariants
as $v)
77 $this->mManualLevel
[$v]=array_key_exists($v,$manualLevel)
85 function getVariants() {
86 return $this->mVariants
;
90 * in case some variant is not defined in the markup, we need
91 * to have some fallback. for example, in zh, normally people
92 * will define zh-hans and zh-hant, but less so for zh-sg or zh-hk.
93 * when zh-sg is preferred but not defined, we will pick zh-hans
94 * in this case. right now this is only used by zh.
96 * @param string $v the language code of the variant
97 * @return string array the code of the fallback language or false if there is no fallback
100 function getVariantFallbacks($v) {
101 if( isset( $this->mVariantFallbacks
[$v] ) ) {
102 return $this->mVariantFallbacks
[$v];
104 return $this->mMainLanguageCode
;
108 * check if variants array in convert array
110 * @param string $variant Variant language code
111 * @param array $carray convert array
112 * @param string $text Text to convert
113 * @return string Translated text
116 function getTextInCArray($variants,$carray){
117 if(is_string($variants)){ $variants=array($variants); }
118 if(!is_array($variants)) return false;
119 foreach ($variants as $variant){
120 if(array_key_exists($variant, $carray)){
121 return $carray[$variant];
128 * get preferred language variants.
129 * @param boolean $fromUser Get it from $wgUser's preferences
130 * @return string the preferred language code
133 function getPreferredVariant( $fromUser = true ) {
134 global $wgUser, $wgRequest, $wgVariantArticlePath, $wgDefaultLanguageVariant;
136 if($this->mPreferredVariant
)
137 return $this->mPreferredVariant
;
139 // see if the preference is set in the request
140 $req = $wgRequest->getText( 'variant' );
141 if( in_array( $req, $this->mVariants
) ) {
142 $this->mPreferredVariant
= $req;
146 // check the syntax /code/ArticleTitle
147 if($wgVariantArticlePath!=false && isset($_SERVER['SCRIPT_NAME'])){
148 // Note: SCRIPT_NAME probably won't hold the correct value if PHP is run as CGI
149 // (it will hold path to php.cgi binary), and might not exist on some very old PHP installations
150 $scriptBase = basename( $_SERVER['SCRIPT_NAME'] );
151 if(in_array($scriptBase,$this->mVariants
)){
152 $this->mPreferredVariant
= $scriptBase;
153 return $this->mPreferredVariant
;
157 // get language variant preference from logged in users
158 // Don't call this on stub objects because that causes infinite
159 // recursion during initialisation
160 if( $fromUser && $wgUser->isLoggedIn() ) {
161 $this->mPreferredVariant
= $wgUser->getOption('variant');
162 return $this->mPreferredVariant
;
165 // see if default variant is globaly set
166 if($wgDefaultLanguageVariant != false && in_array( $wgDefaultLanguageVariant, $this->mVariants
)){
167 $this->mPreferredVariant
= $wgDefaultLanguageVariant;
168 return $this->mPreferredVariant
;
171 # FIXME rewrite code for parsing http header. The current code
172 # is written specific for detecting zh- variants
173 if( !$this->mPreferredVariant
) {
174 // see if some supported language variant is set in the
175 // http header, but we don't set the mPreferredVariant
176 // variable in case this is called before the user's
177 // preference is loaded
178 $pv=$this->mMainLanguageCode
;
179 if(array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
180 $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
181 $zh = strstr($header, $pv.'-');
183 $pv = substr($zh,0,5);
186 // don't try to return bad variant
187 if(in_array( $pv, $this->mVariants
))
191 return $this->mMainLanguageCode
;
196 * dictionary-based conversion
198 * @param string $text the text to be converted
199 * @param string $toVariant the target language code
200 * @return string the converted text
203 function autoConvert($text, $toVariant=false) {
204 $fname="LanguageConverter::autoConvert";
206 wfProfileIn( $fname );
208 if(!$this->mTablesLoaded
)
212 $toVariant = $this->getPreferredVariant();
213 if(!in_array($toVariant, $this->mVariants
))
216 /* we convert everything except:
217 1. html markups (anything between < and >)
219 3. place holders created by the parser
222 if (isset($wgParser) && $wgParser->UniqPrefix()!=''){
223 $marker = '|' . $wgParser->UniqPrefix() . '[\-a-zA-Z0-9]+';
227 // this one is needed when the text is inside an html markup
228 $htmlfix = '|<[^>]+$|^[^<>]*>';
230 // disable convert to variants between <code></code> tags
231 $codefix = '<code>.+?<\/code>|';
232 // disable convertsion of <script type="text/javascript"> ... </script>
233 $scriptfix = '<script.*?>.*?<\/script>|';
234 // disable conversion of <pre xxxx> ... </pre>
235 $prefix = '<pre.*?>.*?<\/pre>|';
237 $reg = '/'.$codefix . $scriptfix . $prefix . '<[^>]+>|&[a-zA-Z#][a-z0-9]+;' . $marker . $htmlfix . '/s';
239 $matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE
);
241 $m = array_shift($matches);
243 $ret = $this->translate($m[0], $toVariant);
244 $mstart = $m[1]+
strlen($m[0]);
245 foreach($matches as $m) {
246 $ret .= substr($text, $mstart, $m[1]-$mstart);
247 $ret .= $this->translate($m[0], $toVariant);
248 $mstart = $m[1] +
strlen($m[0]);
250 wfProfileOut( $fname );
255 * Translate a string to a variant
256 * Doesn't process markup or do any of that other stuff, for that use convert()
258 * @param string $text Text to convert
259 * @param string $variant Variant language code
260 * @return string Translated text
263 function translate( $text, $variant ) {
264 wfProfileIn( __METHOD__
);
265 if( !$this->mTablesLoaded
)
267 $text = $this->mTables
[$variant]->replace( $text );
268 wfProfileOut( __METHOD__
);
273 * convert text to all supported variants
275 * @param string $text the text to be converted
276 * @return array of string
279 function autoConvertToAllVariants($text) {
280 $fname="LanguageConverter::autoConvertToAllVariants";
281 wfProfileIn( $fname );
282 if( !$this->mTablesLoaded
)
286 foreach($this->mVariants
as $variant) {
287 $ret[$variant] = $this->translate($text, $variant);
290 wfProfileOut( $fname );
295 * convert link text to all supported variants
297 * @param string $text the text to be converted
298 * @return array of string
301 function convertLinkToAllVariants($text) {
302 if( !$this->mTablesLoaded
)
306 $tarray = explode($this->mMarkup
['begin'], $text);
307 $tfirst = array_shift($tarray);
309 foreach($this->mVariants
as $variant)
310 $ret[$variant] = $this->translate($tfirst,$variant);
312 foreach($tarray as $txt) {
313 $marked = explode($this->mMarkup
['end'], $txt, 2);
315 foreach($this->mVariants
as $variant){
316 $ret[$variant] .= $this->mMarkup
['begin'].$marked[0].$this->mMarkup
['end'];
317 if(array_key_exists(1, $marked))
318 $ret[$variant] .= $this->translate($marked[1],$variant);
328 * Convert text using a parser object for context
331 function parserConvert( $text, &$parser ) {
332 global $wgDisableLangConversion;
333 /* don't do anything if this is the conversion table */
334 if ( $parser->getTitle()->getNamespace() == NS_MEDIAWIKI
&&
335 strpos($parser->mTitle
->getText(), "Conversiontable") !== false )
340 if($wgDisableLangConversion)
343 $text = $this->convert( $text );
344 $parser->mOutput
->setTitleText( $this->mTitleDisplay
);
349 * Parse flags with syntax -{FLAG| ... }-
352 function parseFlags($marked){
356 if(strlen($marked) < 2 )
357 return array($marked,array('R'));
359 $tt = explode($this->mMarkup
['flagsep'], $marked, 2);
361 if(count($tt) == 2) {
362 $f = explode($this->mMarkup
['varsep'], $tt[0]);
365 if(array_key_exists($ff, $this->mFlags
) &&
366 !in_array($this->mFlags
[$ff], $flags))
367 $flags[] = $this->mFlags
[$ff];
374 if( !in_array('R',$flags) ){
375 //FIXME: may cause trouble here...
376 //strip since it interferes with the parsing, plus,
377 //all spaces should be stripped in this tag anyway.
378 $rules = str_replace(' ', '', $rules);
379 $rules = str_replace('=>','=>',$rules);
383 if( in_array('R',$flags) ){
384 $flags = array('R');// remove other flags
385 } elseif ( in_array('N',$flags) ){
386 $flags = array('N');// remove other flags
387 } elseif ( in_array('-',$flags) ){
388 $flags = array('-');// remove other flags
389 } elseif (count($flags)==1 && $flags[0]=='T'){
391 } elseif ( in_array('H',$flags) ){
392 // replace A flag, and remove other flags except T
393 $temp=array('+','H');
394 if(in_array('T',$flags)) $temp[] = 'T';
395 if(in_array('D',$flags)) $temp[] = 'D';
398 if ( in_array('A',$flags)) {
402 if ( in_array('D',$flags) )
403 $flags=array_diff($flags,array('S'));
405 if ( count($flags)==0 )
407 return array($rules,$flags);
413 function getRulesDesc($bidtable,$unidtable){
415 foreach($bidtable as $k => $v)
416 $text .= $this->mVariantNames
[$k].':'.$v.';';
417 foreach($unidtable as $k => $a)
418 foreach($a as $from=>$to)
419 $text.=$from.'⇒'.$this->mVariantNames
[$k].':'.$to.';';
424 * parse the manually marked conversion rule
425 * @param string $rule the text of the rule
426 * @return array of the translation in each variant
429 function getConvTableFromRules($rules,$flags=array()) {
431 $unidtable = array();
432 $choice = explode($this->mMarkup
['varsep'], $rules );
433 foreach($choice as $c) {
434 $v = explode($this->mMarkup
['codesep'], $c);
436 continue;// syntax error, skip
439 $u = explode($this->mMarkup
['unidsep'], $v);
442 } else if(count($u) == 2){
443 $from=trim($u[0]);$v=trim($u[1]);
444 if( array_key_exists($v,$unidtable) && !is_array($unidtable[$v]) )
445 $unidtable[$v]=array($from=>$to);
447 $unidtable[$v][$from]=$to;
449 // syntax error, pass
451 return array($bidtable,$unidtable);
455 * get display text on markup -{...}-
456 * @param string $rules the original code
457 * @param array $flags FLAGs
458 * @param array $bidtable bidirectional convert table
459 * @param string $unidtable unidirectional convert table
460 * @param string $variant the current variant
461 * @param bool $$doConvert if do convert
464 function getRulesDisplay($rules,$flags,
465 $bidtable,$unidtable,
466 $variant=false,$doConvert=true){
467 if(!$variant) $variant = $this->getPreferredVariant();
468 $is_mc_disable = $this->mManualLevel
[$variant]=='disable';
470 if( in_array('R',$flags) ) {
471 // if we don't do content convert, still strip the -{}- tags
473 } elseif ( in_array('N',$flags) ){
474 // proces N flag: output current variant name
475 $disp = $this->mVariantNames
[trim($rules)];
476 } elseif ( in_array('D',$flags) ){
477 // proces D flag: output rules description
478 $disp = $this->getRulesDesc($bidtable,$unidtable);
479 } elseif ( in_array('H',$flags) ||
in_array('-',$flags) ) {
480 // proces H,- flag or T only: output nothing
482 } elseif ( in_array('S',$flags) ){
483 if( count($bidtable) +
count($unidtable) == 0 ){
485 } elseif ($doConvert){// the text converted
486 // display current variant in bidirectional array
487 $disp = $this->getTextInCArray($variant,$bidtable);
488 // or display current variant in fallbacks
490 $disp = $this->getTextInCArray($this->getVariantFallbacks($variant),$bidtable);
491 // or display current variant in unidirectional array
492 if(!$disp && array_key_exists($variant,$unidtable)){
493 $disp = array_values($unidtable[$variant]);
496 // or display frist text under disable manual convert
497 if(!$disp && $is_mc_disable) {
498 if(count($bidtable)>0){
499 $disp = array_values($bidtable);
502 $disp = array_values($unidtable);
503 $disp = array_values($disp[0]);
507 } else {// no convert
510 } elseif ( in_array('T',$flags) ) {
511 // proces T flag : output nothing
515 $disp= $this->mManualCodeError
;
523 function applyManualFlag($rules,$flags,$bidtable,$unidtable,$variant=false){
524 if(!$variant) $variant = $this->getPreferredVariant();
526 $is_title_flag = in_array('T', $flags);
527 // use syntax -{T|zh:TitleZh;zh-tw:TitleTw}- for custom conversion in title
529 $this->mTitleFromFlag
= true;
530 $this->mTitleDisplay
= $this->getRulesDisplay($rules,array('S'),
531 $bidtable,$unidtable,
533 $this->mDoTitleConvert
);
536 if($this->mManualLevel
[$variant]=='disable') return;
538 $is_remove_flag = !$is_title_flag && in_array('-', $flags);
539 $is_add_flag = !$is_remove_flag && in_array('+', $flags);
540 $is_bidMC = $this->mManualLevel
[$variant]=='bidirectional';
541 $is_unidMC = $this->mManualLevel
[$variant]=='unidirectional';
544 foreach($this->mVariants
as $v) {
545 /* for bidirectional array
546 fill in the missing variants, if any,
548 if($is_bidMC && !array_key_exists($v, $bidtable)) {
549 $vf = $this->getTextInCArray($this->getVariantFallbacks($v),$bidtable);
550 if($vf) $bidtable[$v] = $vf;
552 if($is_bidMC && array_key_exists($v,$bidtable)){
553 foreach($vmarked as $vo){
555 // -{A|zh:WordZh;zh-tw:WordTw}- or -{+|zh:WordZh;zh-tw:WordTw}-
556 // to introduce a custom mapping between
557 // words WordZh and WordTw in the whole text
559 $this->mTables
[$v]->setPair($bidtable[$vo], $bidtable[$v]);
560 $this->mTables
[$vo]->setPair($bidtable[$v], $bidtable[$vo]);
562 // use syntax -{-|zh:WordZh;zh-tw:WordTw}- to remove a conversion
563 // words WordZh and WordTw in the whole text
565 $this->mTables
[$v]->removePair($bidtable[$vo]);
566 $this->mTables
[$vo]->removePair($bidtable[$v]);
571 /*for unidirectional array
572 fill to convert tables */
573 if($is_unidMC && array_key_exists($v,$unidtable)){
574 if($is_add_flag)$this->mTables
[$v]->mergeArray($unidtable[$v]);
575 if($is_remove_flag)$this->mTables
[$v]->removeArray($unidtable[$v]);
581 * Parse rules and flags
584 function parseRules($rules,$flags,$variant=false){
585 if(!$variant) $variant = $this->getPreferredVariant();
587 list($bidtable,$unidtable) = $this->getConvTableFromRules($rules, $flags);
588 if(count($bidtable)==0 && count($unidtable)==0
589 && !in_array('N',$flags) && !in_array('T',$flags) )
591 $disp = $this->getRulesDisplay($rules,$flags,
592 $bidtable,$unidtable,
594 $this->mDoContentConvert
);
595 $this->applyManualFlag($rules,$flags,$bidtable,$unidtable);
604 function convertTitle($text){
605 // check for __NOTC__ tag
606 if( !$this->mDoTitleConvert
) {
607 $this->mTitleDisplay
= $text;
611 // use the title from the T flag if any
612 if($this->mTitleFromFlag
){
613 $this->mTitleFromFlag
= false;
614 return $this->mTitleDisplay
;
618 $isredir = $wgRequest->getText( 'redirect', 'yes' );
619 $action = $wgRequest->getText( 'action' );
620 if ( $isredir == 'no' ||
$action == 'edit' ) {
623 $this->mTitleDisplay
= $this->convert($text);
624 return $this->mTitleDisplay
;
629 * convert text to different variants of a language. the automatic
630 * conversion is done in autoConvert(). here we parse the text
631 * marked with -{}-, which specifies special conversions of the
632 * text that can not be accomplished in autoConvert()
634 * syntax of the markup:
635 * -{code1:text1;code2:text2;...}- or
636 * -{flags|code1:text1;code2:text2;...}- or
637 * -{text}- in which case no conversion should take place for text
639 * @param string $text text to be converted
640 * @param bool $isTitle whether this conversion is for the article title
641 * @return string converted text
644 function convert( $text , $isTitle=false) {
646 $mw =& MagicWord
::get( 'notitleconvert' );
647 if( $mw->matchAndRemove( $text ) )
648 $this->mDoTitleConvert
= false;
649 $mw =& MagicWord
::get( 'nocontentconvert' );
650 if( $mw->matchAndRemove( $text ) ) {
651 $this->mDoContentConvert
= false;
654 // no conversion if redirecting
655 $mw =& MagicWord
::get( 'redirect' );
656 if( $mw->matchStart( $text ))
659 // for title convertion
660 if ($isTitle) return $this->convertTitle($text);
662 $plang = $this->getPreferredVariant();
664 $tarray = explode($this->mMarkup
['begin'], $text);
665 $tfirst = array_shift($tarray);
666 if($this->mDoContentConvert
)
667 $text = $this->autoConvert($tfirst,$plang);
670 foreach($tarray as $txt) {
671 $marked = explode($this->mMarkup
['end'], $txt, 2);
673 // strip the flags from syntax like -{T| ... }-
674 list($rules,$flags) = $this->parseFlags($marked[0]);
676 $text .= $this->parseRules($rules,$flags,$plang);
678 if(array_key_exists(1, $marked)){
679 if( $this->mDoContentConvert
)
680 $text .= $this->autoConvert($marked[1],$plang);
690 * if a language supports multiple variants, it is
691 * possible that non-existing link in one variant
692 * actually exists in another variant. this function
693 * tries to find it. See e.g. LanguageZh.php
695 * @param string $link the name of the link
696 * @param mixed $nt the title object of the link
697 * @return null the input parameters may be modified upon return
700 function findVariantLink( &$link, &$nt ) {
701 global $wgDisableLangConversion;
702 $linkBatch = new LinkBatch();
707 $ns = $nt->getNamespace();
709 $variants = $this->autoConvertToAllVariants($link);
710 if($variants == false) //give up
715 foreach( $variants as $v ) {
717 $varnt = Title
::newFromText( $v, $ns );
718 if(!is_null($varnt)){
719 $linkBatch->addObj($varnt);
725 // fetch all variants in single query
726 $linkBatch->execute();
728 foreach( $titles as $varnt ) {
729 if( $varnt->getArticleID() > 0 ) {
731 if( !$wgDisableLangConversion )
739 * returns language specific hash options
743 function getExtraHashOptions() {
744 $variant = $this->getPreferredVariant();
745 return '!' . $variant ;
749 * get title text as defined in the body of the article text
753 function getParsedTitle() {
754 return $this->mTitleDisplay
;
758 * a write lock to the cache
762 function lockCache() {
765 for($i=0; $i<30; $i++
) {
766 if($success = $wgMemc->add($this->mCacheKey
. "lock", 1, 10))
778 function unlockCache() {
780 $wgMemc->delete($this->mCacheKey
. "lock");
785 * Load default conversion tables
786 * This method must be implemented in derived class
790 function loadDefaultTables() {
791 $name = get_class($this);
792 wfDie("Must implement loadDefaultTables() method in class $name");
796 * load conversion tables either from the cache or the disk
799 function loadTables($fromcache=true) {
801 if( $this->mTablesLoaded
)
803 wfProfileIn( __METHOD__
);
804 $this->mTablesLoaded
= true;
805 $this->mTables
= false;
807 wfProfileIn( __METHOD__
.'-cache' );
808 $this->mTables
= $wgMemc->get( $this->mCacheKey
);
809 wfProfileOut( __METHOD__
.'-cache' );
811 if ( !$this->mTables ||
!isset( $this->mTables
[self
::CACHE_VERSION_KEY
] ) ) {
812 wfProfileIn( __METHOD__
.'-recache' );
813 // not in cache, or we need a fresh reload.
814 // we will first load the default tables
815 // then update them using things in MediaWiki:Zhconversiontable/*
816 $this->loadDefaultTables();
817 foreach($this->mVariants
as $var) {
818 $cached = $this->parseCachedTable($var);
819 $this->mTables
[$var]->mergeArray($cached);
822 $this->postLoadTables();
823 $this->mTables
[self
::CACHE_VERSION_KEY
] = true;
825 if($this->lockCache()) {
826 $wgMemc->set($this->mCacheKey
, $this->mTables
, 43200);
827 $this->unlockCache();
829 wfProfileOut( __METHOD__
.'-recache' );
831 wfProfileOut( __METHOD__
);
835 * Hook for post processig after conversion tables are loaded
838 function postLoadTables() {}
841 * Reload the conversion tables
845 function reloadTables() {
847 unset($this->mTables
);
848 $this->mTablesLoaded
= false;
849 $this->loadTables(false);
854 * parse the conversion table stored in the cache
856 * the tables should be in blocks of the following form:
863 * to make the tables more manageable, subpages are allowed
864 * and will be parsed recursively if $recursive=true
867 function parseCachedTable($code, $subpage='', $recursive=true) {
868 global $wgMessageCache;
869 static $parsed = array();
871 if(!is_object($wgMessageCache))
874 $key = 'Conversiontable/'.$code;
876 $key .= '/' . $subpage;
878 if(array_key_exists($key, $parsed))
881 if ( strpos( $code, '/' ) === false ) {
882 $txt = $wgMessageCache->get( 'Conversiontable', true, $code );
884 $title = Title
::makeTitleSafe( NS_MEDIAWIKI
, "Conversiontable/$code" );
885 if ( $title && $title->exists() ) {
886 $article = new Article( $title );
887 $txt = $article->getContents();
893 // get all subpage links of the form
894 // [[MediaWiki:conversiontable/zh-xx/...|...]]
895 $linkhead = $this->mLangObj
->getNsText(NS_MEDIAWIKI
) . ':Conversiontable';
896 $subs = explode('[[', $txt);
898 foreach( $subs as $sub ) {
899 $link = explode(']]', $sub, 2);
900 if(count($link) != 2)
902 $b = explode('|', $link[0]);
903 $b = explode('/', trim($b[0]), 3);
909 if($b[0] == $linkhead && $b[1] == $code) {
910 $sublinks[] = $sublink;
915 // parse the mappings in this page
916 $blocks = explode($this->mMarkup
['begin'], $txt);
917 array_shift($blocks);
919 foreach($blocks as $block) {
920 $mappings = explode($this->mMarkup
['end'], $block, 2);
921 $stripped = str_replace(array("'", '"', '*','#'), '', $mappings[0]);
922 $table = explode( ';', $stripped );
923 foreach( $table as $t ) {
924 $m = explode( '=>', $t );
925 if( count( $m ) != 2)
927 // trim any trailling comments starting with '//'
928 $tt = explode('//', $m[1], 2);
929 $ret[trim($m[0])] = trim($tt[0]);
932 $parsed[$key] = true;
935 // recursively parse the subpages
937 foreach($sublinks as $link) {
938 $s = $this->parseCachedTable($code, $link, $recursive);
939 $ret = array_merge($ret, $s);
943 if ($this->mUcfirst
) {
944 foreach ($ret as $k => $v) {
945 $ret[Language
::ucfirst($k)] = Language
::ucfirst($v);
952 * Enclose a string with the "no conversion" tag. This is used by
953 * various functions in the Parser
955 * @param string $text text to be tagged for no conversion
956 * @return string the tagged text
959 function markNoConversion($text, $noParse=false) {
960 # don't mark if already marked
961 if(strpos($text, $this->mMarkup
['begin']) ||
962 strpos($text, $this->mMarkup
['end']))
965 $ret = $this->mMarkup
['begin'] .'R|'. $text . $this->mMarkup
['end'];
970 * convert the sorting key for category links. this should make different
971 * keys that are variants of each other map to the same key
973 function convertCategoryKey( $key ) {
977 * hook to refresh the cache of conversion tables when
978 * MediaWiki:conversiontable* is updated
981 function OnArticleSaveComplete($article, $user, $text, $summary, $isminor, $iswatch, $section, $flags, $revision) {
982 $titleobj = $article->getTitle();
983 if($titleobj->getNamespace() == NS_MEDIAWIKI
) {
984 $title = $titleobj->getDBkey();
985 $t = explode('/', $title, 3);
987 if( $c > 1 && $t[0] == 'Conversiontable' ) {
988 if(in_array($t[1], $this->mVariants
)) {
989 $this->reloadTables();
997 * Armour rendered math against conversion
998 * Wrap math into rawoutput -{R| math }- syntax
1001 function armourMath($text){
1002 $ret = $this->mMarkup
['begin'] . 'R|' . $text . $this->mMarkup
['end'];