var $mLangObj;
var $mMarkup;
var $mFlags;
+ var $mUcfirst = false;
/**
* Constructor
*
* @param array $flags array defining the custom strings that maps to the flags
* @access public
*/
- function LanguageConverter($langobj, $maincode,
- $variants=array(),
- $variantfallbacks=array(),
+ function LanguageConverter($langobj, $maincode,
+ $variants=array(),
+ $variantfallbacks=array(),
$markup=array(),
$flags = array()) {
global $wgDBname;
$this->mLangObj = $langobj;
$this->mMainLanguageCode = $maincode;
- $this->mVariants = $variants;
+ $this->mVariants = $variants;
$this->mVariantFallbacks = $variantfallbacks;
$this->mCacheKey = $wgDBname . ":conversiontables";
$m = array('begin'=>'-{', 'flagsep'=>'|', 'codesep'=>':',
/**
* @access public
*/
- function getVariants() {
+ function getVariants() {
return $this->mVariants;
}
* will define zh-cn and zh-tw, but less so for zh-sg or zh-hk.
* when zh-sg is preferred but not defined, we will pick zh-cn
* in this case. right now this is only used by zh.
- *
+ *
* @param string $v the language code of the variant
* @return string the code of the fallback language or false if there is no fallback
* @access private
return $this->mVariantFallbacks[$v];
}
-
- /**
+
+ /**
* get preferred language variants.
* @return string the preferred language code
* @access public
*/
function getPreferredVariant() {
global $wgUser, $wgRequest;
-
+
if($this->mPreferredVariant)
return $this->mPreferredVariant;
return $req;
}
- // get language variant preference from logged in users
+ // get language variant preference from logged in users
if(is_object($wgUser) && $wgUser->isLoggedIn() ) {
$this->mPreferredVariant = $wgUser->getOption('variant');
+ return $this->mPreferredVariant;
}
# FIXME rewrite code for parsing http header. The current code
# is written specific for detecting zh- variants
if( !$this->mPreferredVariant ) {
- // see if some zh- variant is set in the http header,
- $this->mPreferredVariant=$this->mMainLanguageCode;
+ // see if some supported language variant is set in the
+ // http header, but we don't set the mPreferredVariant
+ // variable in case this is called before the user's
+ // preference is loaded
+ $pv=$this->mMainLanguageCode;
if(array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
$header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
$zh = strstr($header, 'zh-');
if($zh) {
- $this->mPreferredVariant = substr($zh,0,5);
+ $pv = substr($zh,0,5);
}
}
+ return $pv;
}
-
- return $this->mPreferredVariant;
}
/**
if(!$this->mTablesLoaded)
$this->loadTables();
- if(!$toVariant)
+ if(!$toVariant)
$toVariant = $this->getPreferredVariant();
if(!in_array($toVariant, $this->mVariants))
return $text;
-
- $reg = '/<[^>]+>|&[a-z#][a-z0-9]+;|'.UNIQ_PREFIX.'-[a-zA-Z0-9]+/';
+ /* we convert everything except:
+ 1. html markups (anything between < and >)
+ 2. html entities
+ 3. place holders created by the parser
+ */
+ global $wgParser;
+ if (isset($wgParser))
+ $marker = '|' . $wgParser->UniqPrefix() . '[\-a-zA-Z0-9]+';
+ else
+ $marker = "";
+ $reg = '/<[^>]+>|&[a-z#][a-z0-9]+;' . $marker . '/';
$matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE);
$ret .= substr($text, $mstart, $m[1]-$mstart);
$ret .= strtr($m[0], $this->mTables[$toVariant]);
$mstart = $m[1] + strlen($m[0]);
- }
+ }
wfProfileOut( $fname );
return $ret;
}
-
+
/**
* convert text to all supported variants
*
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 )
+ {
+ return $text;
+ }
+
+ if($wgDisableLangConversion)
+ return $text;
+
+ $text = $this->convert( $text );
+ $parser->mOutput->setTitleText( $this->mTitleDisplay );
+ return $text;
+ }
+
/**
* convert text to different variants of a language. the automatic
- * conversion is done in autoConvert(). here we parse the text
- * marked with -{}-, which specifies special conversions of the
+ * conversion is done in autoConvert(). here we parse the text
+ * marked with -{}-, which specifies special conversions of the
* text that can not be accomplished in autoConvert()
*
* syntax of the markup:
* -{text}- in which case no conversion should take place for text
*
* @param string $text text to be converted
- * @param bool $isTitle whether this conversion is for the article title
+ * @param bool $isTitle whether this conversion is for the article title
* @return string converted text
* @access public
*/
function convert( $text , $isTitle=false) {
- global $wgDisableLangConversion;
- global $wgTitle;
-
- /* don't do anything if this is the conversion table */
- if($wgTitle->getNamespace() == NS_MEDIAWIKI &&
- strpos($wgTitle->getText(), "Conversiontable")!==false)
- return $text;
-
- if($wgDisableLangConversion)
- return $text;
-
$mw =& MagicWord::get( MAG_NOTITLECONVERT );
if( $mw->matchAndRemove( $text ) )
$this->mDoTitleConvert = false;
$tfirst = array_shift($tarray);
$text = $this->autoConvert($tfirst);
foreach($tarray as $txt) {
- $marked = explode($this->mMarkup['end'], $txt);
+ $marked = explode($this->mMarkup['end'], $txt, 2);
$flags = array();
$tt = explode($this->mMarkup['flagsep'], $marked[0], 2);
/* modify the conversion table for this session*/
/* fill in the missing variants, if any,
- with fallbacks */
+ with fallbacks */
foreach($this->mVariants as $v) {
if(!array_key_exists($v, $carray)) {
$vf = $this->getVariantFallback($v);
if(array_key_exists(1, $marked))
$text .= $this->autoConvert($marked[1]);
}
-
+
return $text;
}
/**
* if a language supports multiple variants, it is
* possible that non-existing link in one variant
- * actually exists in another variant. this function
+ * actually exists in another variant. this function
* tries to find it. See e.g. LanguageZh.php
*
* @param string $link the name of the link
static $cache=array();
global $wgDisableLangConversion;
$pref = $this->getPreferredVariant();
- if( $count > 50 )
+ $ns=0;
+ if(is_object($nt))
+ $ns = $nt->getNamespace();
+ if( $count > 50 && $ns != NS_CATEGORY )
return;
$count++;
$variants = $this->autoConvertToAllVariants($link);
$varnt = Title::newFromText( $v );
if( $varnt && $varnt->getArticleID() > 0 ) {
$nt = $varnt;
- if( !$wgDisableLangConversion && $pref != 'zh' )
+ if( !$wgDisableLangConversion )
$link = $v;
break;
}
/**
* returns language specific hash options
- *
+ *
* @access public
*/
function getExtraHashOptions() {
break;
sleep(1);
}
- return $success;
+ return $success;
}
/**
/**
* Load default conversion tables
* This method must be implemented in derived class
- *
+ *
* @access private
*/
function loadDefaultTables() {
$name = get_class($this);
- die("Must implement loadDefaultTables() method in class $name");
+ wfDie("Must implement loadDefaultTables() method in class $name");
}
/**
if( !empty( $this->mTables ) ) //all done
return;
}
- // not in cache, or we need a fresh reload.
+ // not in cache, or we need a fresh reload.
// we will first load the default tables
// then update them using things in MediaWiki:Zhconversiontable/*
global $wgMessageCache;
}
$this->postLoadTables();
-
+
if($this->lockCache()) {
$wgMemc->set($this->mCacheKey, $this->mTables, 43200);
$this->unlockCache();
}
}
-
+
/**
* Hook for post processig after conversion tables are loaded
*
*/
function postLoadTables() {}
- /* deprecated? */
- function updateTablexxxx($code, $table) {
- global $wgMemc;
- if(!$this->mTablesLoaded)
- $this->loadTables();
-
- $this->mTables[$code] = array_merge($this->mTables[$code], $table);
- if($this->lockCache()) {
- $wgMemc->delete($this->mCacheKey);
- $wgMemc->set($this->mCacheKey, $this->mTables, 43200);
- $this->unlockCache();
- }
- }
-
/**
* Reload the conversion tables
- *
+ *
* @access private
*/
function reloadTables() {
/**
- * parse the conversion table stored in the cache
+ * parse the conversion table stored in the cache
*
* the tables should be in blocks of the following form:
* word => word ;
* ...
* }-
- *
+ *
* to make the tables more manageable, subpages are allowed
* and will be parsed recursively if $recursive=true
*
if(array_key_exists($key, $parsed))
return array();
-
+
$txt = $wgMessageCache->get( $key, true, true, true );
$b = explode('/', trim($b[0]), 3);
if(count($b)==3)
$sublink = $b[2];
- else
+ else
$sublink = '';
if($b[0] == $linkhead && $b[1] == $code) {
// parse the mappings in this page
$blocks = explode($this->mMarkup['begin'], $txt);
array_shift($blocks);
- $ret = array();
+ $ret = array();
foreach($blocks as $block) {
$mappings = explode($this->mMarkup['end'], $block, 2);
$stripped = str_replace(array("'", '"', '*','#'), '', $mappings[0]);
$ret = array_merge($ret, $s);
}
}
+
+ if ($this->mUcfirst) {
+ foreach ($ret as $k => $v) {
+ $ret[LanguageUtf8::ucfirst($k)] = LanguageUtf8::ucfirst($v);
+ }
+ }
return $ret;
}
/**
* Enclose a string with the "no conversion" tag. This is used by
* various functions in the Parser
- *
+ *
* @param string $text text to be tagged for no conversion
* @return string the tagged text
*/
}
/**
- * hook to refresh the cache of conversion tables when
+ * convert the sorting key for category links. this should make different
+ * keys that are variants of each other map to the same key
+ */
+ function convertCategoryKey( $key ) {
+ return $key;
+ }
+ /**
+ * hook to refresh the cache of conversion tables when
* MediaWiki:conversiontable* is updated
* @access private
*/
function OnArticleSaveComplete($article, $user, $text, $summary, $isminor, $iswatch, $section) {
$titleobj = $article->getTitle();
- if($titleobj->getNamespace() == NS_MEDIAWIKI) {
+ if($titleobj->getNamespace() == NS_MEDIAWIKI) {
/*
global $wgContLang; // should be an LanguageZh.
- if(get_class($wgContLang) != 'languagezh')
+ if(get_class($wgContLang) != 'languagezh')
return true;
*/
$title = $titleobj->getDBkey();