diff options
Diffstat (limited to 'languages/LanguageConverter.php')
-rw-r--r-- | languages/LanguageConverter.php | 140 |
1 files changed, 110 insertions, 30 deletions
diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php index 9bb6715a..a949ad4e 100644 --- a/languages/LanguageConverter.php +++ b/languages/LanguageConverter.php @@ -12,7 +12,9 @@ class LanguageConverter { var $mMainLanguageCode; var $mVariants, $mVariantFallbacks; var $mTablesLoaded = false; + var $mUseFss = false; var $mTables; + var $mFssObjects; var $mTitleDisplay=''; var $mDoTitleConvert=true, $mDoContentConvert=true; var $mCacheKey; @@ -20,6 +22,7 @@ class LanguageConverter { var $mMarkup; var $mFlags; var $mUcfirst = false; + var $mNoTitleConvert = false; /** * Constructor * @@ -35,17 +38,20 @@ class LanguageConverter { $variantfallbacks=array(), $markup=array(), $flags = array()) { - global $wgDBname; + global $wgLegalTitleChars; $this->mLangObj = $langobj; $this->mMainLanguageCode = $maincode; $this->mVariants = $variants; $this->mVariantFallbacks = $variantfallbacks; - $this->mCacheKey = $wgDBname . ":conversiontables"; + $this->mCacheKey = wfMemcKey( 'conversiontables' ); $m = array('begin'=>'-{', 'flagsep'=>'|', 'codesep'=>':', 'varsep'=>';', 'end'=>'}-'); $this->mMarkup = array_merge($m, $markup); $f = array('A'=>'A', 'T'=>'T'); $this->mFlags = array_merge($f, $flags); + if ( function_exists( 'fss_prep_replace' ) ) { + $this->mUseFss = true; + } } /** @@ -72,11 +78,12 @@ class LanguageConverter { /** - * get preferred language variants. + * get preferred language variants. + * @param boolean $fromUser Get it from $wgUser's preferences * @return string the preferred language code * @access public */ - function getPreferredVariant() { + function getPreferredVariant( $fromUser = true ) { global $wgUser, $wgRequest; if($this->mPreferredVariant) @@ -90,7 +97,9 @@ class LanguageConverter { } // get language variant preference from logged in users - if(is_object($wgUser) && $wgUser->isLoggedIn() ) { + // Don't call this on stub objects because that causes infinite + // recursion during initialisation + if( $fromUser && $wgUser->isLoggedIn() ) { $this->mPreferredVariant = $wgUser->getOption('variant'); return $this->mPreferredVariant; } @@ -147,19 +156,22 @@ class LanguageConverter { $marker = ""; // this one is needed when the text is inside an html markup - $htmlfix = '|<[^>]+=\"[^(>=)]*$|^[^(<>=\")]*\"[^>]*>'; + $htmlfix = '|<[^>]+$|^[^<>]*>'; + + // disable convert to variants between <code></code> tags + $codefix = '<code>.+?<\/code>|'; - $reg = '/<[^>]+>|&[a-z#][a-z0-9]+;' . $marker . $htmlfix . '/'; + $reg = '/'.$codefix.'<[^>]+>|&[a-zA-Z#][a-z0-9]+;' . $marker . $htmlfix . '/s'; $matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE); - $m = array_shift($matches); - $ret = strtr($m[0], $this->mTables[$toVariant]); + + $ret = $this->translate($m[0], $toVariant); $mstart = $m[1]+strlen($m[0]); foreach($matches as $m) { $ret .= substr($text, $mstart, $m[1]-$mstart); - $ret .= strtr($m[0], $this->mTables[$toVariant]); + $ret .= $this->translate($m[0], $toVariant); $mstart = $m[1] + strlen($m[0]); } wfProfileOut( $fname ); @@ -167,11 +179,29 @@ class LanguageConverter { } /** + * Translate a string to a variant + * Doesn't process markup or do any of that other stuff, for that use convert() + * + * @param string $text Text to convert + * @param string $variant Variant language code + * @return string Translated text + */ + function translate( $text, $variant ) { + if( !$this->mTablesLoaded ) + $this->loadTables(); + if ( $this->mUseFss ) { + return fss_exec_replace( $this->mFssObjects[$variant], $text ); + } else { + return strtr( $text, $this->mTables[$variant] ); + } + } + + /** * convert text to all supported variants * * @param string $text the text to be converted * @return array of string - * @private + * @public */ function autoConvertToAllVariants($text) { $fname="LanguageConverter::autoConvertToAllVariants"; @@ -181,20 +211,54 @@ class LanguageConverter { $ret = array(); foreach($this->mVariants as $variant) { - $ret[$variant] = strtr($text, $this->mTables[$variant]); + $ret[$variant] = $this->translate($text, $variant); } + wfProfileOut( $fname ); return $ret; } /** + * convert link text to all supported variants + * + * @param string $text the text to be converted + * @return array of string + * @public + */ + function convertLinkToAllVariants($text) { + if( !$this->mTablesLoaded ) + $this->loadTables(); + + $ret = array(); + $tarray = explode($this->mMarkup['begin'], $text); + $tfirst = array_shift($tarray); + + foreach($this->mVariants as $variant) + $ret[$variant] = $this->translate($tfirst,$variant); + + foreach($tarray as $txt) { + $marked = explode($this->mMarkup['end'], $txt, 2); + + foreach($this->mVariants as $variant){ + $ret[$variant] .= $this->mMarkup['begin'].$marked[0].$this->mMarkup['end']; + if(array_key_exists(1, $marked)) + $ret[$variant] .= $this->translate($marked[1],$variant); + } + + } + + return $ret; + } + + + /** * Convert text using a parser object for context */ function parserConvert( $text, &$parser ) { global $wgDisableLangConversion; /* don't do anything if this is the conversion table */ if ( $parser->mTitle->getNamespace() == NS_MEDIAWIKI && - strpos($parser->mTitle->getText, "Conversiontable") !== false ) + strpos($parser->mTitle->getText(), "Conversiontable") !== false ) { return $text; } @@ -223,27 +287,30 @@ class LanguageConverter { * @access public */ function convert( $text , $isTitle=false) { - $mw =& MagicWord::get( MAG_NOTITLECONVERT ); + $mw =& MagicWord::get( 'notitleconvert' ); if( $mw->matchAndRemove( $text ) ) $this->mDoTitleConvert = false; - $mw =& MagicWord::get( MAG_NOCONTENTCONVERT ); + $mw =& MagicWord::get( 'nocontentconvert' ); if( $mw->matchAndRemove( $text ) ) { $this->mDoContentConvert = false; } // no conversion if redirecting - $mw =& MagicWord::get( MAG_REDIRECT ); + $mw =& MagicWord::get( 'redirect' ); if( $mw->matchStart( $text )) return $text; if( $isTitle ) { + if($this->mNoTitleConvert){ + $this->mTitleDisplay = $text; + return $text; + } + if( !$this->mDoTitleConvert ) { $this->mTitleDisplay = $text; return $text; } - if( !empty($this->mTitleDisplay)) - return $this->mTitleDisplay; global $wgRequest; $isredir = $wgRequest->getText( 'redirect', 'yes' ); @@ -252,7 +319,7 @@ class LanguageConverter { return $text; } else { - $this->mTitleDisplay = $this->autoConvert($text); + $this->mTitleDisplay = $this->convert($text); return $this->mTitleDisplay; } } @@ -289,7 +356,7 @@ class LanguageConverter { else $rules = $marked[0]; -#FIXME: may cause trouble here... + //FIXME: may cause trouble here... //strip since it interferes with the parsing, plus, //all spaces should be stripped in this tag anyway. $rules = str_replace(' ', '', $rules); @@ -331,6 +398,9 @@ class LanguageConverter { } } + if ( $this->mUseFss ) { + $this->generateFssObjects(); + } } } else { @@ -381,23 +451,16 @@ class LanguageConverter { * @access public */ function findVariantLink( &$link, &$nt ) { - static $count=0; //used to limit this operation - static $cache=array(); global $wgDisableLangConversion; $pref = $this->getPreferredVariant(); $ns=0; if(is_object($nt)) $ns = $nt->getNamespace(); - if( $count > 50 && $ns != NS_CATEGORY ) - return; - $count++; + $variants = $this->autoConvertToAllVariants($link); if($variants == false) //give up return; foreach( $variants as $v ) { - if(isset($cache[$v])) - continue; - $cache[$v] = 1; $varnt = Title::newFromText( $v, $ns ); if( $varnt && $varnt->getArticleID() > 0 ) { $nt = $varnt; @@ -495,6 +558,18 @@ class LanguageConverter { $wgMemc->set($this->mCacheKey, $this->mTables, 43200); $this->unlockCache(); } + if ( $this->mUseFss ) { + $this->generateFssObjects(); + } + } + + /** + * Generate FSS objects. The FSS extension must be available. + */ + function generateFssObjects() { + foreach ( $this->mTables as $variant => $table ) { + $this->mFssObjects[$variant] = fss_prep_replace( $table ); + } } /** @@ -601,7 +676,7 @@ class LanguageConverter { if ($this->mUcfirst) { foreach ($ret as $k => $v) { - $ret[LanguageUtf8::ucfirst($k)] = LanguageUtf8::ucfirst($v); + $ret[Language::ucfirst($k)] = Language::ucfirst($v); } } return $ret; @@ -614,7 +689,7 @@ class LanguageConverter { * @param string $text text to be tagged for no conversion * @return string the tagged text */ - function markNoConversion($text) { + function markNoConversion($text, $noParse=false) { # don't mark if already marked if(strpos($text, $this->mMarkup['begin']) || strpos($text, $this->mMarkup['end'])) @@ -655,6 +730,11 @@ class LanguageConverter { } return true; } + + function setNoTitleConvert(){ + $this->mNoTitleConvert = true; + } + } ?> |