2005-04-15 14:12:39 +00:00
|
|
|
<?php
|
2008-06-26 03:00:34 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
/**
|
2008-07-09 08:16:39 +00:00
|
|
|
* Contains the LanguageConverter class and ConverterRule class
|
|
|
|
|
* @ingroup Language
|
|
|
|
|
*
|
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2008-07-09 08:16:39 +00:00
|
|
|
/**
|
|
|
|
|
* base class for language convert
|
|
|
|
|
* @ingroup Language
|
|
|
|
|
*
|
|
|
|
|
* @author Zhengzhu Feng <zhengzhu@gmail.com>
|
2009-01-17 05:01:43 +00:00
|
|
|
* @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>, PhiLiP <philip.npc@gmail.com>
|
2008-07-09 08:16:39 +00:00
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
class LanguageConverter {
|
|
|
|
|
var $mPreferredVariant='';
|
|
|
|
|
var $mMainLanguageCode;
|
2009-01-29 08:59:53 +00:00
|
|
|
var $mVariants, $mVariantFallbacks, $mVariantNames;
|
2005-04-15 14:12:39 +00:00
|
|
|
var $mTablesLoaded = false;
|
|
|
|
|
var $mTables;
|
2009-02-06 13:21:11 +00:00
|
|
|
var $mNamespaceTables;
|
2005-04-15 14:12:39 +00:00
|
|
|
var $mTitleDisplay='';
|
|
|
|
|
var $mDoTitleConvert=true, $mDoContentConvert=true;
|
2008-06-26 03:00:34 +00:00
|
|
|
var $mManualLevel; // 'bidirectional' 'unidirectional' 'disable' for each variants
|
2007-01-01 17:20:19 +00:00
|
|
|
var $mTitleFromFlag = false;
|
2005-04-15 14:12:39 +00:00
|
|
|
var $mCacheKey;
|
|
|
|
|
var $mLangObj;
|
|
|
|
|
var $mMarkup;
|
2005-04-20 02:33:04 +00:00
|
|
|
var $mFlags;
|
2008-07-09 08:16:39 +00:00
|
|
|
var $mDescCodeSep = ':',$mDescVarSep = ';';
|
2005-05-19 17:34:09 +00:00
|
|
|
var $mUcfirst = false;
|
2007-12-03 04:54:28 +00:00
|
|
|
|
2008-06-26 03:00:34 +00:00
|
|
|
const CACHE_VERSION_KEY = 'VERSION 6';
|
2007-12-03 04:54:28 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* Constructor
|
2005-04-15 14:12:39 +00:00
|
|
|
*
|
2008-06-26 03:00:34 +00:00
|
|
|
* @param string $maincode the main language code of this language
|
|
|
|
|
* @param array $variants the supported variants of this language
|
|
|
|
|
* @param array $variantfallback the fallback language of each variant
|
|
|
|
|
* @param array $markup array defining the markup used for manual conversion
|
2005-04-20 02:33:04 +00:00
|
|
|
* @param array $flags array defining the custom strings that maps to the flags
|
2008-07-10 13:56:23 +00:00
|
|
|
* @param array $manualLevel limit for supported variants
|
2008-07-09 08:16:39 +00:00
|
|
|
* @public
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2009-09-17 19:51:33 +00:00
|
|
|
function __construct( $langobj, $maincode,
|
2005-08-15 19:27:58 +00:00
|
|
|
$variants=array(),
|
|
|
|
|
$variantfallbacks=array(),
|
2005-04-20 02:33:04 +00:00
|
|
|
$markup=array(),
|
2008-06-26 03:00:34 +00:00
|
|
|
$flags = array(),
|
|
|
|
|
$manualLevel = array() ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$this->mLangObj = $langobj;
|
|
|
|
|
$this->mMainLanguageCode = $maincode;
|
2009-09-17 19:51:33 +00:00
|
|
|
|
|
|
|
|
global $wgDisabledVariants;
|
|
|
|
|
$this->mVariants = array();
|
|
|
|
|
foreach( $variants as $variant ) {
|
|
|
|
|
if( !in_array( $variant, $wgDisabledVariants ) )
|
|
|
|
|
$this->mVariants[] = $variant;
|
|
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
$this->mVariantFallbacks = $variantfallbacks;
|
2008-06-26 03:00:34 +00:00
|
|
|
global $wgLanguageNames;
|
2009-01-29 08:59:53 +00:00
|
|
|
$this->mVariantNames = $wgLanguageNames;
|
2006-12-27 01:49:09 +00:00
|
|
|
$this->mCacheKey = wfMemcKey( 'conversiontables', $maincode );
|
2008-06-26 03:00:34 +00:00
|
|
|
$m = array(
|
|
|
|
|
'begin'=>'-{',
|
|
|
|
|
'flagsep'=>'|',
|
|
|
|
|
'unidsep'=>'=>', //for unidirectional conversion
|
|
|
|
|
'codesep'=>':',
|
|
|
|
|
'varsep'=>';',
|
|
|
|
|
'end'=>'}-'
|
|
|
|
|
);
|
2005-04-20 02:33:04 +00:00
|
|
|
$this->mMarkup = array_merge($m, $markup);
|
2008-06-26 03:00:34 +00:00
|
|
|
$f = array(
|
|
|
|
|
// 'S' show converted text
|
|
|
|
|
// '+' add rules for alltext
|
|
|
|
|
// 'E' the gave flags is error
|
|
|
|
|
// these flags above are reserved for program
|
|
|
|
|
'A'=>'A', // add rule for convert code (all text convert)
|
|
|
|
|
'T'=>'T', // title convert
|
2009-03-04 06:59:23 +00:00
|
|
|
'R'=>'R', // raw content
|
2008-06-26 03:00:34 +00:00
|
|
|
'D'=>'D', // convert description (subclass implement)
|
|
|
|
|
'-'=>'-', // remove convert (not implement)
|
|
|
|
|
'H'=>'H', // add rule for convert code (but no display in placed code )
|
|
|
|
|
'N'=>'N' // current variant name
|
|
|
|
|
);
|
2005-04-20 02:33:04 +00:00
|
|
|
$this->mFlags = array_merge($f, $flags);
|
2009-01-17 05:01:43 +00:00
|
|
|
foreach( $this->mVariants as $v) {
|
2008-06-26 03:00:34 +00:00
|
|
|
$this->mManualLevel[$v]=array_key_exists($v,$manualLevel)
|
|
|
|
|
?$manualLevel[$v]
|
|
|
|
|
:'bidirectional';
|
2009-02-06 13:21:11 +00:00
|
|
|
$this->mNamespaceTables[$v] = array();
|
2009-03-04 06:56:37 +00:00
|
|
|
$this->mFlags[$v] = $v;
|
2009-01-17 05:01:43 +00:00
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-07-08 02:34:11 +00:00
|
|
|
* @public
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2005-08-15 19:27:58 +00:00
|
|
|
function getVariants() {
|
2005-04-15 14:12:39 +00:00
|
|
|
return $this->mVariants;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* in case some variant is not defined in the markup, we need
|
|
|
|
|
* to have some fallback. for example, in zh, normally people
|
2008-06-26 03:00:34 +00:00
|
|
|
* will define zh-hans and zh-hant, but less so for zh-sg or zh-hk.
|
|
|
|
|
* when zh-sg is preferred but not defined, we will pick zh-hans
|
2005-04-15 14:12:39 +00:00
|
|
|
* in this case. right now this is only used by zh.
|
2005-08-15 19:27:58 +00:00
|
|
|
*
|
2005-04-15 14:12:39 +00:00
|
|
|
* @param string $v the language code of the variant
|
2008-06-26 03:00:34 +00:00
|
|
|
* @return string array the code of the fallback language or false if there is no fallback
|
2008-07-09 08:16:39 +00:00
|
|
|
* @public
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
|
|
|
|
function getVariantFallbacks($v) {
|
|
|
|
|
if( isset( $this->mVariantFallbacks[$v] ) ) {
|
|
|
|
|
return $this->mVariantFallbacks[$v];
|
|
|
|
|
}
|
|
|
|
|
return $this->mMainLanguageCode;
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-29 08:59:53 +00:00
|
|
|
/**
|
|
|
|
|
* get preferred language variants.
|
|
|
|
|
* @param boolean $fromUser Get it from $wgUser's preferences
|
|
|
|
|
* @return string the preferred language code
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
2006-07-26 07:15:39 +00:00
|
|
|
function getPreferredVariant( $fromUser = true ) {
|
2009-01-29 08:59:53 +00:00
|
|
|
global $wgUser, $wgRequest, $wgVariantArticlePath, $wgDefaultLanguageVariant;
|
|
|
|
|
|
|
|
|
|
if($this->mPreferredVariant)
|
|
|
|
|
return $this->mPreferredVariant;
|
|
|
|
|
|
|
|
|
|
// figure out user lang without constructing wgLang to avoid infinite recursion
|
|
|
|
|
if( $fromUser )
|
|
|
|
|
$defaultUserLang = $wgUser->getOption( 'language' );
|
|
|
|
|
else
|
|
|
|
|
$defaultUserLang = $this->mMainLanguageCode;
|
|
|
|
|
$userLang = $wgRequest->getVal( 'uselang', $defaultUserLang );
|
|
|
|
|
// see if interface language is same as content, if not, prevent conversion
|
|
|
|
|
if( ! in_array( $userLang, $this->mVariants ) ){
|
|
|
|
|
$this->mPreferredVariant = $this->mMainLanguageCode; // no conversion
|
|
|
|
|
return $this->mPreferredVariant;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// see if the preference is set in the request
|
|
|
|
|
$req = $wgRequest->getText( 'variant' );
|
|
|
|
|
if( in_array( $req, $this->mVariants ) ) {
|
|
|
|
|
$this->mPreferredVariant = $req;
|
|
|
|
|
return $req;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check the syntax /code/ArticleTitle
|
|
|
|
|
if($wgVariantArticlePath!=false && isset($_SERVER['SCRIPT_NAME'])){
|
|
|
|
|
// Note: SCRIPT_NAME probably won't hold the correct value if PHP is run as CGI
|
|
|
|
|
// (it will hold path to php.cgi binary), and might not exist on some very old PHP installations
|
|
|
|
|
$scriptBase = basename( $_SERVER['SCRIPT_NAME'] );
|
|
|
|
|
if(in_array($scriptBase,$this->mVariants)){
|
|
|
|
|
$this->mPreferredVariant = $scriptBase;
|
|
|
|
|
return $this->mPreferredVariant;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get language variant preference from logged in users
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// see if default variant is globaly set
|
|
|
|
|
if($wgDefaultLanguageVariant != false && in_array( $wgDefaultLanguageVariant, $this->mVariants )){
|
|
|
|
|
$this->mPreferredVariant = $wgDefaultLanguageVariant;
|
|
|
|
|
return $this->mPreferredVariant;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( !$this->mPreferredVariant ) {
|
|
|
|
|
// 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
|
2009-04-03 17:35:53 +00:00
|
|
|
if( array_key_exists( 'HTTP_ACCEPT_LANGUAGE', $_SERVER ) ) {
|
2009-05-30 05:07:46 +00:00
|
|
|
$acceptLanguage = strtolower( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
|
2009-05-22 17:40:00 +00:00
|
|
|
|
|
|
|
|
// explode by comma
|
2009-05-23 15:27:52 +00:00
|
|
|
$result = explode(',', $acceptLanguage);
|
|
|
|
|
|
2009-08-21 16:00:01 +00:00
|
|
|
$languages = array();
|
2009-05-23 15:27:52 +00:00
|
|
|
|
|
|
|
|
foreach( $result as $elem ) {
|
|
|
|
|
// if $elem likes 'zh-cn;q=0.9'
|
|
|
|
|
if(($posi = strpos( $elem, ';' )) !== false ) {
|
|
|
|
|
// get the real language code likes 'zh-cn'
|
|
|
|
|
$languages[] = substr( $elem, 0, $posi );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$languages[] = $elem;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-05-22 17:40:00 +00:00
|
|
|
|
2009-08-21 16:00:01 +00:00
|
|
|
$fallback_languages = array();
|
2009-04-03 17:35:53 +00:00
|
|
|
foreach( $languages as $language ) {
|
2009-05-22 17:40:00 +00:00
|
|
|
// strip whitespace
|
|
|
|
|
$language = trim( $language );
|
2009-04-03 17:35:53 +00:00
|
|
|
if( in_array( $language, $this->mVariants ) ) {
|
2009-08-21 16:00:01 +00:00
|
|
|
return $language;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// To see if there are fallbacks of current language.
|
|
|
|
|
// We record these fallback variants, and process
|
|
|
|
|
// them later.
|
|
|
|
|
$fallbacks = $this->getVariantFallbacks( $language );
|
|
|
|
|
if( is_string( $fallbacks ) )
|
|
|
|
|
$fallback_languages[] = $fallbacks;
|
|
|
|
|
elseif( is_array( $fallbacks ) )
|
|
|
|
|
$fallback_languages = array_merge( $fallback_languages, $fallbacks );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// process fallback languages now
|
|
|
|
|
$fallback_languages = array_unique( $fallback_languages );
|
|
|
|
|
foreach( $fallback_languages as $language ) {
|
|
|
|
|
if( in_array( $language, $this->mVariants ) ) {
|
|
|
|
|
return $language;
|
2009-04-03 17:35:53 +00:00
|
|
|
}
|
2009-01-29 08:59:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-20 11:45:49 +00:00
|
|
|
return $this->mMainLanguageCode;
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2008-06-26 03:00:34 +00:00
|
|
|
|
2009-01-28 07:02:42 +00:00
|
|
|
/**
|
|
|
|
|
* caption convert, base on preg_replace_callback
|
|
|
|
|
*
|
|
|
|
|
* to convert text in "title" or "alt", like '<img alt="text" ... '
|
|
|
|
|
* or '<span title="text" ... '
|
|
|
|
|
*
|
|
|
|
|
* @return string like ' alt="yyyy"' or ' title="yyyy"'
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function captionConvert( $matches ) {
|
|
|
|
|
$toVariant = $this->getPreferredVariant();
|
|
|
|
|
$title = $matches[1];
|
|
|
|
|
$text = $matches[2];
|
2009-01-29 10:09:19 +00:00
|
|
|
// we convert captions except URL
|
2009-01-28 07:02:42 +00:00
|
|
|
if( !strpos( $text, '://' ) )
|
|
|
|
|
$text = $this->translate($text, $toVariant);
|
|
|
|
|
return " $title=\"$text\"";
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* dictionary-based conversion
|
|
|
|
|
*
|
|
|
|
|
* @param string $text the text to be converted
|
|
|
|
|
* @param string $toVariant the target language code
|
|
|
|
|
* @return string the converted text
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
function autoConvert($text, $toVariant=false) {
|
|
|
|
|
$fname="LanguageConverter::autoConvert";
|
2005-04-20 02:33:04 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
|
|
|
|
if(!$this->mTablesLoaded)
|
|
|
|
|
$this->loadTables();
|
|
|
|
|
|
2005-08-15 19:27:58 +00:00
|
|
|
if(!$toVariant)
|
2005-04-15 14:12:39 +00:00
|
|
|
$toVariant = $this->getPreferredVariant();
|
|
|
|
|
if(!in_array($toVariant, $this->mVariants))
|
|
|
|
|
return $text;
|
|
|
|
|
|
2006-01-24 18:36:45 +00:00
|
|
|
/* we convert everything except:
|
|
|
|
|
1. html markups (anything between < and >)
|
|
|
|
|
2. html entities
|
|
|
|
|
3. place holders created by the parser
|
|
|
|
|
*/
|
|
|
|
|
global $wgParser;
|
2008-07-04 15:01:41 +00:00
|
|
|
if (isset($wgParser) && $wgParser->UniqPrefix()!=''){
|
2006-01-24 18:36:45 +00:00
|
|
|
$marker = '|' . $wgParser->UniqPrefix() . '[\-a-zA-Z0-9]+';
|
2008-07-04 15:01:41 +00:00
|
|
|
} else
|
2006-01-24 18:36:45 +00:00
|
|
|
$marker = "";
|
2006-06-16 22:30:39 +00:00
|
|
|
|
|
|
|
|
// this one is needed when the text is inside an html markup
|
2006-09-20 10:22:12 +00:00
|
|
|
$htmlfix = '|<[^>]+$|^[^<>]*>';
|
2006-07-19 19:17:36 +00:00
|
|
|
|
2006-09-20 10:22:12 +00:00
|
|
|
// disable convert to variants between <code></code> tags
|
|
|
|
|
$codefix = '<code>.+?<\/code>|';
|
2006-12-26 12:19:45 +00:00
|
|
|
// disable convertsion of <script type="text/javascript"> ... </script>
|
|
|
|
|
$scriptfix = '<script.*?>.*?<\/script>|';
|
2008-07-04 15:01:41 +00:00
|
|
|
// disable conversion of <pre xxxx> ... </pre>
|
|
|
|
|
$prefix = '<pre.*?>.*?<\/pre>|';
|
2006-09-20 10:22:12 +00:00
|
|
|
|
2008-07-04 15:01:41 +00:00
|
|
|
$reg = '/'.$codefix . $scriptfix . $prefix . '<[^>]+>|&[a-zA-Z#][a-z0-9]+;' . $marker . $htmlfix . '/s';
|
2008-06-26 03:00:34 +00:00
|
|
|
|
2005-04-20 02:33:04 +00:00
|
|
|
$matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE);
|
|
|
|
|
|
2006-09-15 21:02:35 +00:00
|
|
|
$m = array_shift($matches);
|
2006-09-20 10:22:12 +00:00
|
|
|
|
2006-07-26 14:13:07 +00:00
|
|
|
$ret = $this->translate($m[0], $toVariant);
|
2005-04-20 02:33:04 +00:00
|
|
|
$mstart = $m[1]+strlen($m[0]);
|
2009-01-28 07:02:42 +00:00
|
|
|
|
|
|
|
|
// enable convertsion of '<img alt="xxxx" ... ' or '<span title="xxxx" ... '
|
|
|
|
|
$captionpattern = '/\s(title|alt)\s*=\s*"([\s\S]*?)"/';
|
2005-04-20 02:33:04 +00:00
|
|
|
foreach($matches as $m) {
|
2009-01-28 07:02:42 +00:00
|
|
|
$mark = substr($text, $mstart, $m[1]-$mstart);
|
|
|
|
|
$mark = preg_replace_callback($captionpattern, array(&$this, 'captionConvert'), $mark);
|
|
|
|
|
$ret .= $mark;
|
2006-07-26 14:13:07 +00:00
|
|
|
$ret .= $this->translate($m[0], $toVariant);
|
2005-04-20 02:33:04 +00:00
|
|
|
$mstart = $m[1] + strlen($m[0]);
|
2005-08-15 19:27:58 +00:00
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
2005-08-15 19:27:58 +00:00
|
|
|
|
2006-07-26 14:13:07 +00:00
|
|
|
/**
|
|
|
|
|
* 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
|
2008-07-04 15:01:41 +00:00
|
|
|
* @private
|
2006-07-26 14:13:07 +00:00
|
|
|
*/
|
|
|
|
|
function translate( $text, $variant ) {
|
2006-11-21 09:53:45 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2006-07-26 14:13:07 +00:00
|
|
|
if( !$this->mTablesLoaded )
|
|
|
|
|
$this->loadTables();
|
2006-11-21 09:53:45 +00:00
|
|
|
$text = $this->mTables[$variant]->replace( $text );
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $text;
|
2006-07-26 14:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* convert text to all supported variants
|
|
|
|
|
*
|
|
|
|
|
* @param string $text the text to be converted
|
|
|
|
|
* @return array of string
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
2006-07-19 20:13:39 +00:00
|
|
|
function autoConvertToAllVariants($text) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$fname="LanguageConverter::autoConvertToAllVariants";
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
if( !$this->mTablesLoaded )
|
|
|
|
|
$this->loadTables();
|
|
|
|
|
|
|
|
|
|
$ret = array();
|
|
|
|
|
foreach($this->mVariants as $variant) {
|
2006-07-26 14:13:07 +00:00
|
|
|
$ret[$variant] = $this->translate($text, $variant);
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2006-09-20 10:22:12 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-20 10:22:12 +00:00
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* convert link text to all supported variants
|
|
|
|
|
*
|
|
|
|
|
* @param string $text the text to be converted
|
|
|
|
|
* @return array of string
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
2006-09-20 10:22:12 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2009-01-17 05:01:43 +00:00
|
|
|
|
2008-07-09 08:16:39 +00:00
|
|
|
/**
|
2009-01-17 05:01:43 +00:00
|
|
|
* prepare manual conversion table
|
2008-07-09 08:16:39 +00:00
|
|
|
* @private
|
|
|
|
|
*/
|
2009-07-10 11:45:08 +00:00
|
|
|
function applyManualConv( $convRule ){
|
2008-07-09 08:16:39 +00:00
|
|
|
// use syntax -{T|zh:TitleZh;zh-tw:TitleTw}- for custom conversion in title
|
|
|
|
|
$title = $convRule->getTitle();
|
2009-02-04 19:01:14 +00:00
|
|
|
if( $title ){
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mTitleFromFlag = true;
|
|
|
|
|
$this->mTitleDisplay = $title;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-10 13:56:23 +00:00
|
|
|
//apply manual conversion table to global table
|
2008-07-09 08:16:39 +00:00
|
|
|
$convTable = $convRule->getConvTable();
|
2008-07-10 13:56:23 +00:00
|
|
|
$action = $convRule->getRulesAction();
|
2009-07-10 11:45:08 +00:00
|
|
|
foreach( $convTable as $variant => $pair ) {
|
|
|
|
|
if( !in_array( $variant, $this->mVariants ) )continue;
|
2009-01-17 05:01:43 +00:00
|
|
|
if( $action=="add" ) {
|
2009-07-10 11:45:08 +00:00
|
|
|
foreach( $pair as $from => $to ) {
|
2009-01-29 10:09:19 +00:00
|
|
|
// to ensure that $from and $to not be left blank
|
|
|
|
|
// so $this->translate() could always return a string
|
2009-02-04 19:01:14 +00:00
|
|
|
if ( $from || $to )
|
2009-01-29 10:09:19 +00:00
|
|
|
// more efficient than array_merge(), about 2.5 times.
|
2009-07-10 11:45:08 +00:00
|
|
|
$this->mTables[$variant]->setPair( $from, $to );
|
2009-01-29 10:09:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-02-04 19:01:14 +00:00
|
|
|
elseif ( $action == "remove" ) {
|
2009-07-10 11:45:08 +00:00
|
|
|
$this->mTables[$variant]->removeArray( $pair );
|
2009-01-17 05:01:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-01 01:57:53 +00:00
|
|
|
/**
|
|
|
|
|
* Convert text using a parser object for context
|
2008-07-04 15:01:41 +00:00
|
|
|
* @public
|
2006-03-01 01:57:53 +00:00
|
|
|
*/
|
|
|
|
|
function parserConvert( $text, &$parser ) {
|
2009-06-04 09:48:11 +00:00
|
|
|
global $wgDisableLangConversion;
|
2006-03-01 01:57:53 +00:00
|
|
|
/* don't do anything if this is the conversion table */
|
2007-01-17 22:57:06 +00:00
|
|
|
if ( $parser->getTitle()->getNamespace() == NS_MEDIAWIKI &&
|
2009-06-03 14:51:08 +00:00
|
|
|
strpos($parser->mTitle->getText(), "Conversiontable") !== false )
|
2006-03-01 01:57:53 +00:00
|
|
|
{
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-04 09:48:11 +00:00
|
|
|
if ( $wgDisableLangConversion )
|
2006-03-01 01:57:53 +00:00
|
|
|
return $text;
|
|
|
|
|
|
|
|
|
|
$text = $this->convert( $text );
|
2009-02-04 19:01:14 +00:00
|
|
|
|
|
|
|
|
if ( $this->mTitleFromFlag )
|
|
|
|
|
$parser->mOutput->setTitleText( $this->mTitleDisplay );
|
2006-03-01 01:57:53 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
2009-02-06 13:21:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* convert namespace
|
|
|
|
|
* @param string $title the title included namespace
|
|
|
|
|
* @return array of string
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function convertNamespace( $title, $variant ) {
|
|
|
|
|
$splittitle = explode( ':', $title );
|
|
|
|
|
if (count($splittitle) < 2)
|
|
|
|
|
return $title;
|
|
|
|
|
if ( isset( $this->mNamespaceTables[$variant][$splittitle[0]] ) )
|
|
|
|
|
$splittitle[0] = $this->mNamespaceTables[$variant][$splittitle[0]];
|
|
|
|
|
$ret = implode(':', $splittitle );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
2006-03-01 01:57:53 +00:00
|
|
|
|
2008-07-04 15:01:41 +00:00
|
|
|
/**
|
2009-02-06 13:21:11 +00:00
|
|
|
* convert title
|
2008-07-04 15:01:41 +00:00
|
|
|
* @private
|
|
|
|
|
*/
|
2009-02-06 13:21:11 +00:00
|
|
|
function convertTitle( $text, $variant ){
|
2008-08-07 14:26:34 +00:00
|
|
|
global $wgDisableTitleConversion, $wgUser;
|
|
|
|
|
|
|
|
|
|
// check for global param and __NOTC__ tag
|
|
|
|
|
if( $wgDisableTitleConversion || !$this->mDoTitleConvert || $wgUser->getOption('noconvertlink') == 1 ) {
|
2008-06-26 03:00:34 +00:00
|
|
|
$this->mTitleDisplay = $text;
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use the title from the T flag if any
|
2009-02-04 19:01:14 +00:00
|
|
|
if( $this->mTitleFromFlag ){
|
2008-06-26 03:00:34 +00:00
|
|
|
$this->mTitleFromFlag = false;
|
|
|
|
|
return $this->mTitleDisplay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global $wgRequest;
|
|
|
|
|
$isredir = $wgRequest->getText( 'redirect', 'yes' );
|
|
|
|
|
$action = $wgRequest->getText( 'action' );
|
2008-08-07 14:26:34 +00:00
|
|
|
$linkconvert = $wgRequest->getText( 'linkconvert', 'yes' );
|
2008-08-16 10:09:18 +00:00
|
|
|
if ( $isredir == 'no' || $action == 'edit' || $action == 'submit' || $linkconvert == 'no' ) {
|
2008-06-26 03:00:34 +00:00
|
|
|
return $text;
|
|
|
|
|
} else {
|
2009-02-06 13:21:11 +00:00
|
|
|
$text = $this->convertNamespace( $text, $variant );
|
|
|
|
|
$this->mTitleDisplay = $this->convert( $text );
|
2008-06-26 03:00:34 +00:00
|
|
|
return $this->mTitleDisplay;
|
|
|
|
|
}
|
2007-01-01 17:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
/**
|
|
|
|
|
* convert text to different variants of a language. the automatic
|
2005-08-15 19:27:58 +00:00
|
|
|
* conversion is done in autoConvert(). here we parse the text
|
|
|
|
|
* marked with -{}-, which specifies special conversions of the
|
2005-04-15 14:12:39 +00:00
|
|
|
* text that can not be accomplished in autoConvert()
|
|
|
|
|
*
|
|
|
|
|
* syntax of the markup:
|
|
|
|
|
* -{code1:text1;code2:text2;...}- or
|
2008-06-26 03:00:34 +00:00
|
|
|
* -{flags|code1:text1;code2:text2;...}- or
|
2005-04-15 14:12:39 +00:00
|
|
|
* -{text}- in which case no conversion should take place for text
|
2008-06-26 03:00:34 +00:00
|
|
|
*
|
|
|
|
|
* @param string $text text to be converted
|
|
|
|
|
* @param bool $isTitle whether this conversion is for the article title
|
|
|
|
|
* @return string converted text
|
2008-07-08 02:34:11 +00:00
|
|
|
* @public
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2009-05-26 07:46:29 +00:00
|
|
|
function convert( $text, $isTitle = false ) {
|
2008-06-26 03:00:34 +00:00
|
|
|
|
|
|
|
|
$mw =& MagicWord::get( 'notitleconvert' );
|
2005-04-15 14:12:39 +00:00
|
|
|
if( $mw->matchAndRemove( $text ) )
|
|
|
|
|
$this->mDoTitleConvert = false;
|
2008-06-26 03:00:34 +00:00
|
|
|
$mw =& MagicWord::get( 'nocontentconvert' );
|
2005-04-15 14:12:39 +00:00
|
|
|
if( $mw->matchAndRemove( $text ) ) {
|
|
|
|
|
$this->mDoContentConvert = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no conversion if redirecting
|
2008-06-26 03:00:34 +00:00
|
|
|
$mw =& MagicWord::get( 'redirect' );
|
2009-02-04 19:01:14 +00:00
|
|
|
if( $mw->matchStart( $text ) )
|
2005-04-15 14:12:39 +00:00
|
|
|
return $text;
|
|
|
|
|
|
2009-05-26 07:46:29 +00:00
|
|
|
$plang = $this->getPreferredVariant();
|
2009-02-06 13:21:11 +00:00
|
|
|
|
2008-06-26 03:00:34 +00:00
|
|
|
// for title convertion
|
2009-02-06 13:21:11 +00:00
|
|
|
if ( $isTitle ) return $this->convertTitle( $text, $plang );
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2009-02-04 19:01:14 +00:00
|
|
|
$tarray = StringUtils::explode( $this->mMarkup['end'], $text );
|
2008-07-09 08:16:39 +00:00
|
|
|
$text = '';
|
2009-01-17 05:01:43 +00:00
|
|
|
|
2009-02-04 19:01:14 +00:00
|
|
|
foreach ( $tarray as $txt ) {
|
2009-07-10 11:45:08 +00:00
|
|
|
|
2009-02-04 19:01:14 +00:00
|
|
|
$marked = explode( $this->mMarkup['begin'], $txt, 2 );
|
2009-07-10 11:45:08 +00:00
|
|
|
|
|
|
|
|
if( $this->mDoContentConvert )
|
|
|
|
|
// Bug 19620: should convert a string immediately after a new rule added.
|
|
|
|
|
$text .= $this->autoConvert( $marked[0], $plang );
|
2009-09-17 04:16:17 +00:00
|
|
|
else
|
|
|
|
|
$text .= $marked[0];
|
2009-07-10 11:45:08 +00:00
|
|
|
|
2009-02-04 19:01:14 +00:00
|
|
|
if ( array_key_exists( 1, $marked ) ) {
|
2009-01-14 19:14:20 +00:00
|
|
|
$crule = new ConverterRule($marked[1], $this);
|
2009-02-04 19:01:14 +00:00
|
|
|
$crule->parse( $plang );
|
2009-07-10 11:45:08 +00:00
|
|
|
$text .= $crule->getDisplay();
|
|
|
|
|
$this->applyManualConv( $crule );
|
2009-01-14 19:14:20 +00:00
|
|
|
}
|
2009-01-17 05:01:43 +00:00
|
|
|
else
|
2009-07-10 11:45:08 +00:00
|
|
|
$text .= $this->mMarkup['end'];
|
|
|
|
|
|
2008-08-26 14:37:15 +00:00
|
|
|
}
|
2009-07-10 11:45:08 +00:00
|
|
|
|
2009-01-17 05:01:43 +00:00
|
|
|
// Remove the last delimiter (wasn't real)
|
|
|
|
|
$text = substr( $text, 0, -strlen( $this->mMarkup['end'] ) );
|
2005-04-15 14:12:39 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* if a language supports multiple variants, it is
|
|
|
|
|
* possible that non-existing link in one variant
|
2005-08-15 19:27:58 +00:00
|
|
|
* actually exists in another variant. this function
|
2005-04-15 14:12:39 +00:00
|
|
|
* tries to find it. See e.g. LanguageZh.php
|
|
|
|
|
*
|
|
|
|
|
* @param string $link the name of the link
|
|
|
|
|
* @param mixed $nt the title object of the link
|
2009-02-02 07:54:43 +00:00
|
|
|
* @param boolean $ignoreOtherCond: to disable other conditions when
|
|
|
|
|
* we need to transclude a template or update a category's link
|
2005-04-15 14:12:39 +00:00
|
|
|
* @return null the input parameters may be modified upon return
|
2008-07-08 02:34:11 +00:00
|
|
|
* @public
|
2005-04-15 14:12:39 +00:00
|
|
|
*/
|
2009-02-02 07:54:43 +00:00
|
|
|
function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) {
|
2009-08-10 08:01:08 +00:00
|
|
|
# If the article has already existed, there is no need to
|
|
|
|
|
# check it again, otherwise it may cause a fault.
|
2009-09-16 23:59:25 +00:00
|
|
|
if ( is_object( $nt ) && $nt->exists() )
|
2009-08-10 08:01:08 +00:00
|
|
|
return;
|
|
|
|
|
|
2009-06-04 09:48:11 +00:00
|
|
|
global $wgDisableLangConversion, $wgDisableTitleConversion, $wgRequest, $wgUser;
|
2008-08-07 14:26:34 +00:00
|
|
|
$isredir = $wgRequest->getText( 'redirect', 'yes' );
|
|
|
|
|
$action = $wgRequest->getText( 'action' );
|
|
|
|
|
$linkconvert = $wgRequest->getText( 'linkconvert', 'yes' );
|
2009-06-04 09:48:11 +00:00
|
|
|
$disableLinkConversion = $wgDisableLangConversion || $wgDisableTitleConversion;
|
2006-12-11 23:33:27 +00:00
|
|
|
$linkBatch = new LinkBatch();
|
|
|
|
|
|
|
|
|
|
$ns=NS_MAIN;
|
|
|
|
|
|
2009-02-02 07:54:43 +00:00
|
|
|
if ( $disableLinkConversion || ( !$ignoreOtherCond && ( $isredir == 'no' || $action == 'edit'
|
|
|
|
|
|| $action == 'submit' || $linkconvert == 'no' || $wgUser->getOption('noconvertlink') == 1 ) ) )
|
|
|
|
|
return;
|
2008-08-07 14:26:34 +00:00
|
|
|
|
2009-08-10 08:01:08 +00:00
|
|
|
if ( is_object( $nt ) )
|
2005-04-28 03:49:23 +00:00
|
|
|
$ns = $nt->getNamespace();
|
2006-09-20 10:22:12 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
$variants = $this->autoConvertToAllVariants($link);
|
|
|
|
|
if($variants == false) //give up
|
|
|
|
|
return;
|
2006-12-11 23:33:27 +00:00
|
|
|
|
|
|
|
|
$titles = array();
|
|
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
foreach( $variants as $v ) {
|
2006-12-11 23:33:27 +00:00
|
|
|
if($v != $link){
|
|
|
|
|
$varnt = Title::newFromText( $v, $ns );
|
|
|
|
|
if(!is_null($varnt)){
|
|
|
|
|
$linkBatch->addObj($varnt);
|
|
|
|
|
$titles[]=$varnt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fetch all variants in single query
|
|
|
|
|
$linkBatch->execute();
|
|
|
|
|
|
|
|
|
|
foreach( $titles as $varnt ) {
|
|
|
|
|
if( $varnt->getArticleID() > 0 ) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$nt = $varnt;
|
2009-02-02 06:53:49 +00:00
|
|
|
$link = $varnt->getText();
|
2005-04-15 14:12:39 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* returns language specific hash options
|
|
|
|
|
*
|
2008-07-08 02:34:11 +00:00
|
|
|
* @public
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
function getExtraHashOptions() {
|
|
|
|
|
$variant = $this->getPreferredVariant();
|
|
|
|
|
return '!' . $variant ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* get title text as defined in the body of the article text
|
|
|
|
|
*
|
2008-07-08 02:34:11 +00:00
|
|
|
* @public
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
function getParsedTitle() {
|
|
|
|
|
return $this->mTitleDisplay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* a write lock to the cache
|
|
|
|
|
*
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
function lockCache() {
|
|
|
|
|
global $wgMemc;
|
|
|
|
|
$success = false;
|
|
|
|
|
for($i=0; $i<30; $i++) {
|
|
|
|
|
if($success = $wgMemc->add($this->mCacheKey . "lock", 1, 10))
|
|
|
|
|
break;
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
2005-08-15 19:27:58 +00:00
|
|
|
return $success;
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* unlock cache
|
|
|
|
|
*
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
function unlockCache() {
|
|
|
|
|
global $wgMemc;
|
|
|
|
|
$wgMemc->delete($this->mCacheKey . "lock");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* Load default conversion tables
|
|
|
|
|
* This method must be implemented in derived class
|
|
|
|
|
*
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
function loadDefaultTables() {
|
|
|
|
|
$name = get_class($this);
|
2006-01-14 02:49:43 +00:00
|
|
|
wfDie("Must implement loadDefaultTables() method in class $name");
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* load conversion tables either from the cache or the disk
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2009-06-03 14:51:08 +00:00
|
|
|
function loadTables($fromcache=true) {
|
2005-04-15 14:12:39 +00:00
|
|
|
global $wgMemc;
|
|
|
|
|
if( $this->mTablesLoaded )
|
|
|
|
|
return;
|
2006-10-16 03:16:12 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2005-04-15 14:12:39 +00:00
|
|
|
$this->mTablesLoaded = true;
|
2006-10-16 02:36:28 +00:00
|
|
|
$this->mTables = false;
|
2005-04-15 14:12:39 +00:00
|
|
|
if($fromcache) {
|
2006-10-16 03:16:12 +00:00
|
|
|
wfProfileIn( __METHOD__.'-cache' );
|
2005-04-15 14:12:39 +00:00
|
|
|
$this->mTables = $wgMemc->get( $this->mCacheKey );
|
2006-10-16 03:16:12 +00:00
|
|
|
wfProfileOut( __METHOD__.'-cache' );
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2007-12-03 04:54:28 +00:00
|
|
|
if ( !$this->mTables || !isset( $this->mTables[self::CACHE_VERSION_KEY] ) ) {
|
2006-11-21 09:53:45 +00:00
|
|
|
wfProfileIn( __METHOD__.'-recache' );
|
2006-10-16 02:36:28 +00:00
|
|
|
// not in cache, or we need a fresh reload.
|
|
|
|
|
// we will first load the default tables
|
|
|
|
|
// then update them using things in MediaWiki:Zhconversiontable/*
|
|
|
|
|
$this->loadDefaultTables();
|
2009-06-03 14:51:08 +00:00
|
|
|
foreach($this->mVariants as $var) {
|
|
|
|
|
$cached = $this->parseCachedTable($var);
|
|
|
|
|
$this->mTables[$var]->mergeArray($cached);
|
2006-10-16 02:36:28 +00:00
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
|
2006-10-16 02:36:28 +00:00
|
|
|
$this->postLoadTables();
|
2007-12-03 04:54:28 +00:00
|
|
|
$this->mTables[self::CACHE_VERSION_KEY] = true;
|
2005-08-15 19:27:58 +00:00
|
|
|
|
2006-10-16 02:36:28 +00:00
|
|
|
if($this->lockCache()) {
|
|
|
|
|
$wgMemc->set($this->mCacheKey, $this->mTables, 43200);
|
|
|
|
|
$this->unlockCache();
|
|
|
|
|
}
|
2006-10-16 03:16:12 +00:00
|
|
|
wfProfileOut( __METHOD__.'-recache' );
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2006-10-16 03:16:12 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-07-26 14:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* Hook for post processig after conversion tables are loaded
|
|
|
|
|
*
|
|
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
function postLoadTables() {}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* Reload the conversion tables
|
|
|
|
|
*
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2005-04-15 14:12:39 +00:00
|
|
|
function reloadTables() {
|
|
|
|
|
if($this->mTables)
|
|
|
|
|
unset($this->mTables);
|
|
|
|
|
$this->mTablesLoaded = false;
|
|
|
|
|
$this->loadTables(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* parse the conversion table stored in the cache
|
|
|
|
|
*
|
|
|
|
|
* the tables should be in blocks of the following form:
|
|
|
|
|
* -{
|
|
|
|
|
* word => word ;
|
|
|
|
|
* word => word ;
|
|
|
|
|
* ...
|
|
|
|
|
* }-
|
|
|
|
|
*
|
|
|
|
|
* to make the tables more manageable, subpages are allowed
|
|
|
|
|
* and will be parsed recursively if $recursive=true
|
|
|
|
|
*
|
2005-04-15 14:12:39 +00:00
|
|
|
*/
|
2009-06-03 14:51:08 +00:00
|
|
|
function parseCachedTable($code, $subpage='', $recursive=true) {
|
2005-04-15 14:12:39 +00:00
|
|
|
global $wgMessageCache;
|
|
|
|
|
static $parsed = array();
|
|
|
|
|
|
|
|
|
|
if(!is_object($wgMessageCache))
|
|
|
|
|
return array();
|
|
|
|
|
|
2009-06-03 14:51:08 +00:00
|
|
|
$key = 'Conversiontable/'.$code;
|
2005-04-15 14:12:39 +00:00
|
|
|
if($subpage)
|
|
|
|
|
$key .= '/' . $subpage;
|
|
|
|
|
|
|
|
|
|
if(array_key_exists($key, $parsed))
|
|
|
|
|
return array();
|
2009-06-03 14:51:08 +00:00
|
|
|
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( strpos( $code, '/' ) === false ) {
|
2009-06-03 14:51:08 +00:00
|
|
|
$txt = $wgMessageCache->get( 'Conversiontable', true, $code );
|
2008-07-05 15:39:10 +00:00
|
|
|
} else {
|
2009-06-03 14:51:08 +00:00
|
|
|
$title = Title::makeTitleSafe( NS_MEDIAWIKI, "Conversiontable/$code" );
|
2008-07-05 15:39:10 +00:00
|
|
|
if ( $title && $title->exists() ) {
|
|
|
|
|
$article = new Article( $title );
|
|
|
|
|
$txt = $article->getContents();
|
|
|
|
|
} else {
|
|
|
|
|
$txt = '';
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-03 14:51:08 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
// get all subpage links of the form
|
|
|
|
|
// [[MediaWiki:conversiontable/zh-xx/...|...]]
|
2009-06-03 14:51:08 +00:00
|
|
|
$linkhead = $this->mLangObj->getNsText(NS_MEDIAWIKI) . ':Conversiontable';
|
2005-04-15 14:12:39 +00:00
|
|
|
$subs = explode('[[', $txt);
|
|
|
|
|
$sublinks = array();
|
|
|
|
|
foreach( $subs as $sub ) {
|
|
|
|
|
$link = explode(']]', $sub, 2);
|
|
|
|
|
if(count($link) != 2)
|
|
|
|
|
continue;
|
|
|
|
|
$b = explode('|', $link[0]);
|
|
|
|
|
$b = explode('/', trim($b[0]), 3);
|
|
|
|
|
if(count($b)==3)
|
|
|
|
|
$sublink = $b[2];
|
2005-08-15 19:27:58 +00:00
|
|
|
else
|
2005-04-15 14:12:39 +00:00
|
|
|
$sublink = '';
|
|
|
|
|
|
|
|
|
|
if($b[0] == $linkhead && $b[1] == $code) {
|
|
|
|
|
$sublinks[] = $sublink;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-03 14:51:08 +00:00
|
|
|
|
2005-04-15 14:12:39 +00:00
|
|
|
// parse the mappings in this page
|
2005-04-20 02:33:04 +00:00
|
|
|
$blocks = explode($this->mMarkup['begin'], $txt);
|
2005-04-15 14:12:39 +00:00
|
|
|
array_shift($blocks);
|
2005-08-15 19:27:58 +00:00
|
|
|
$ret = array();
|
2005-04-15 14:12:39 +00:00
|
|
|
foreach($blocks as $block) {
|
2005-04-20 02:33:04 +00:00
|
|
|
$mappings = explode($this->mMarkup['end'], $block, 2);
|
2005-04-15 14:12:39 +00:00
|
|
|
$stripped = str_replace(array("'", '"', '*','#'), '', $mappings[0]);
|
|
|
|
|
$table = explode( ';', $stripped );
|
|
|
|
|
foreach( $table as $t ) {
|
|
|
|
|
$m = explode( '=>', $t );
|
|
|
|
|
if( count( $m ) != 2)
|
|
|
|
|
continue;
|
|
|
|
|
// trim any trailling comments starting with '//'
|
|
|
|
|
$tt = explode('//', $m[1], 2);
|
|
|
|
|
$ret[trim($m[0])] = trim($tt[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$parsed[$key] = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// recursively parse the subpages
|
|
|
|
|
if($recursive) {
|
|
|
|
|
foreach($sublinks as $link) {
|
|
|
|
|
$s = $this->parseCachedTable($code, $link, $recursive);
|
|
|
|
|
$ret = array_merge($ret, $s);
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-15 19:27:58 +00:00
|
|
|
|
2005-05-19 17:34:09 +00:00
|
|
|
if ($this->mUcfirst) {
|
|
|
|
|
foreach ($ret as $k => $v) {
|
2006-08-06 14:23:53 +00:00
|
|
|
$ret[Language::ucfirst($k)] = Language::ucfirst($v);
|
2005-05-19 17:34:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enclose a string with the "no conversion" tag. This is used by
|
|
|
|
|
* various functions in the Parser
|
2005-08-15 19:27:58 +00:00
|
|
|
*
|
2005-04-15 14:12:39 +00:00
|
|
|
* @param string $text text to be tagged for no conversion
|
|
|
|
|
* @return string the tagged text
|
2008-07-04 15:01:41 +00:00
|
|
|
* @public
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2006-09-20 10:22:12 +00:00
|
|
|
function markNoConversion($text, $noParse=false) {
|
2005-04-20 02:33:04 +00:00
|
|
|
# don't mark if already marked
|
|
|
|
|
if(strpos($text, $this->mMarkup['begin']) ||
|
|
|
|
|
strpos($text, $this->mMarkup['end']))
|
|
|
|
|
return $text;
|
|
|
|
|
|
2008-06-26 03:00:34 +00:00
|
|
|
$ret = $this->mMarkup['begin'] .'R|'. $text . $this->mMarkup['end'];
|
2005-04-15 14:40:11 +00:00
|
|
|
return $ret;
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2005-08-15 19:27:58 +00:00
|
|
|
* convert the sorting key for category links. this should make different
|
2005-04-28 03:49:23 +00:00
|
|
|
* keys that are variants of each other map to the same key
|
2008-06-26 03:00:34 +00:00
|
|
|
*/
|
2005-04-28 03:49:23 +00:00
|
|
|
function convertCategoryKey( $key ) {
|
|
|
|
|
return $key;
|
|
|
|
|
}
|
|
|
|
|
/**
|
2008-06-26 03:00:34 +00:00
|
|
|
* hook to refresh the cache of conversion tables when
|
|
|
|
|
* MediaWiki:conversiontable* is updated
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2007-07-22 23:16:48 +00:00
|
|
|
function OnArticleSaveComplete($article, $user, $text, $summary, $isminor, $iswatch, $section, $flags, $revision) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$titleobj = $article->getTitle();
|
2005-08-15 19:27:58 +00:00
|
|
|
if($titleobj->getNamespace() == NS_MEDIAWIKI) {
|
2005-04-15 14:12:39 +00:00
|
|
|
$title = $titleobj->getDBkey();
|
|
|
|
|
$t = explode('/', $title, 3);
|
|
|
|
|
$c = count($t);
|
|
|
|
|
if( $c > 1 && $t[0] == 'Conversiontable' ) {
|
|
|
|
|
if(in_array($t[1], $this->mVariants)) {
|
|
|
|
|
$this->reloadTables();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2006-09-20 10:22:12 +00:00
|
|
|
|
2006-12-11 23:33:27 +00:00
|
|
|
/**
|
|
|
|
|
* Armour rendered math against conversion
|
2007-01-03 14:37:15 +00:00
|
|
|
* Wrap math into rawoutput -{R| math }- syntax
|
2008-07-04 15:01:41 +00:00
|
|
|
* @public
|
2006-12-11 23:33:27 +00:00
|
|
|
*/
|
|
|
|
|
function armourMath($text){
|
2009-04-03 17:35:53 +00:00
|
|
|
// we need to convert '-{' and '}-' to '-{' and '}-'
|
|
|
|
|
// to avoid a unwanted '}-' appeared after the math-image.
|
|
|
|
|
$text = strtr( $text, array('-{' => '-{', '}-' => '}-') );
|
2007-01-03 14:37:15 +00:00
|
|
|
$ret = $this->mMarkup['begin'] . 'R|' . $text . $this->mMarkup['end'];
|
|
|
|
|
return $ret;
|
2006-09-20 10:22:12 +00:00
|
|
|
}
|
2005-04-15 14:12:39 +00:00
|
|
|
}
|
2008-07-06 17:23:00 +00:00
|
|
|
|
|
|
|
|
/**
|
2008-07-08 02:34:11 +00:00
|
|
|
* parser for rules of language conversion , parse rules in -{ }- tag
|
2008-07-06 17:23:00 +00:00
|
|
|
* @ingroup Language
|
2009-03-04 06:56:37 +00:00
|
|
|
* @author fdcn <fdcn64@gmail.com>, PhiLiP <philip.npc@gmail.com>
|
2008-07-06 17:23:00 +00:00
|
|
|
*/
|
|
|
|
|
class ConverterRule {
|
|
|
|
|
var $mText; // original text in -{text}-
|
2008-07-09 08:16:39 +00:00
|
|
|
var $mConverter; // LanguageConverter object
|
|
|
|
|
var $mManualCodeError='<strong class="error">code error!</strong>';
|
|
|
|
|
var $mRuleDisplay = '',$mRuleTitle=false;
|
|
|
|
|
var $mRules = '';// string : the text of the rules
|
|
|
|
|
var $mRulesAction = 'none';
|
|
|
|
|
var $mFlags = array();
|
|
|
|
|
var $mConvTable = array();
|
|
|
|
|
var $mBidtable = array();// array of the translation in each variant
|
|
|
|
|
var $mUnidtable = array();// array of the translation in each variant
|
2008-07-06 17:23:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*
|
|
|
|
|
* @param string $text the text between -{ and }-
|
2008-07-09 08:16:39 +00:00
|
|
|
* @param object $converter a LanguageConverter object
|
2008-07-06 17:23:00 +00:00
|
|
|
* @access public
|
|
|
|
|
*/
|
2008-07-09 08:16:39 +00:00
|
|
|
function __construct($text,$converter){
|
2008-07-06 17:23:00 +00:00
|
|
|
$this->mText = $text;
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mConverter=$converter;
|
|
|
|
|
foreach($converter->mVariants as $v){
|
|
|
|
|
$this->mConvTable[$v]=array();
|
|
|
|
|
}
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* check if variants array in convert array
|
|
|
|
|
*
|
|
|
|
|
* @param string $variant Variant language code
|
|
|
|
|
* @return string Translated text
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
function getTextInBidtable($variants){
|
|
|
|
|
if(is_string($variants)){ $variants=array($variants); }
|
|
|
|
|
if(!is_array($variants)) return false;
|
|
|
|
|
foreach ($variants as $variant){
|
2008-07-09 08:16:39 +00:00
|
|
|
if(array_key_exists($variant, $this->mBidtable)){
|
|
|
|
|
return $this->mBidtable[$variant];
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-07-09 08:16:39 +00:00
|
|
|
|
2008-07-06 17:23:00 +00:00
|
|
|
/**
|
|
|
|
|
* Parse flags with syntax -{FLAG| ... }-
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function parseFlags(){
|
|
|
|
|
$text = $this->mText;
|
2008-07-07 01:12:15 +00:00
|
|
|
if(strlen($text) < 2 ) {
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mFlags = array( 'R' );
|
|
|
|
|
$this->mRules = $text;
|
2008-07-07 01:12:15 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2008-07-06 17:23:00 +00:00
|
|
|
|
2008-07-08 02:34:11 +00:00
|
|
|
$flags = array();
|
2008-07-09 08:16:39 +00:00
|
|
|
$markup = $this->mConverter->mMarkup;
|
|
|
|
|
$validFlags = $this->mConverter->mFlags;
|
2009-03-04 06:56:37 +00:00
|
|
|
$variants = $this->mConverter->mVariants;
|
2008-07-06 17:23:00 +00:00
|
|
|
|
2008-07-09 08:16:39 +00:00
|
|
|
$tt = explode($markup['flagsep'], $text, 2);
|
2008-07-06 17:23:00 +00:00
|
|
|
if(count($tt) == 2) {
|
2008-07-09 08:16:39 +00:00
|
|
|
$f = explode($markup['varsep'], $tt[0]);
|
2008-07-06 17:23:00 +00:00
|
|
|
foreach($f as $ff) {
|
|
|
|
|
$ff = trim($ff);
|
2008-07-09 08:16:39 +00:00
|
|
|
if(array_key_exists($ff, $validFlags) &&
|
|
|
|
|
!in_array($validFlags[$ff], $flags))
|
|
|
|
|
$flags[] = $validFlags[$ff];
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
$rules = $tt[1];
|
|
|
|
|
} else {
|
|
|
|
|
$rules = $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//check flags
|
|
|
|
|
if( in_array('R',$flags) ){
|
|
|
|
|
$flags = array('R');// remove other flags
|
|
|
|
|
} elseif ( in_array('N',$flags) ){
|
|
|
|
|
$flags = array('N');// remove other flags
|
|
|
|
|
} elseif ( in_array('-',$flags) ){
|
|
|
|
|
$flags = array('-');// remove other flags
|
|
|
|
|
} elseif (count($flags)==1 && $flags[0]=='T'){
|
|
|
|
|
$flags[]='H';
|
|
|
|
|
} elseif ( in_array('H',$flags) ){
|
|
|
|
|
// replace A flag, and remove other flags except T
|
|
|
|
|
$temp=array('+','H');
|
|
|
|
|
if(in_array('T',$flags)) $temp[] = 'T';
|
|
|
|
|
if(in_array('D',$flags)) $temp[] = 'D';
|
|
|
|
|
$flags = $temp;
|
|
|
|
|
} else {
|
2009-03-04 06:56:37 +00:00
|
|
|
if ( in_array('A',$flags) ) {
|
2008-07-06 17:23:00 +00:00
|
|
|
$flags[]='+';
|
|
|
|
|
$flags[]='S';
|
|
|
|
|
}
|
|
|
|
|
if ( in_array('D',$flags) )
|
|
|
|
|
$flags=array_diff($flags,array('S'));
|
2009-03-04 06:56:37 +00:00
|
|
|
$flags_temp = array();
|
|
|
|
|
foreach ($variants as $variant) {
|
2009-03-05 02:23:24 +00:00
|
|
|
// try to find flags like "zh-hans", "zh-hant"
|
|
|
|
|
// allow syntaxes like "-{zh-hans;zh-hant|XXXX}-"
|
2009-03-04 06:56:37 +00:00
|
|
|
if ( in_array($variant, $flags) )
|
|
|
|
|
$flags_temp[] = $variant;
|
|
|
|
|
}
|
2009-03-25 17:30:59 +00:00
|
|
|
if ( count($flags_temp) !== 0 )
|
2009-03-04 06:56:37 +00:00
|
|
|
$flags = $flags_temp;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
2009-03-04 06:56:37 +00:00
|
|
|
if ( count($flags) == 0 )
|
2008-07-06 17:23:00 +00:00
|
|
|
$flags = array('S');
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mRules=$rules;
|
|
|
|
|
$this->mFlags=$flags;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate conversion table
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2008-07-09 08:16:39 +00:00
|
|
|
function parseRules() {
|
|
|
|
|
$rules = $this->mRules;
|
|
|
|
|
$flags = $this->mFlags;
|
2008-07-06 17:23:00 +00:00
|
|
|
$bidtable = array();
|
|
|
|
|
$unidtable = array();
|
2008-07-09 08:16:39 +00:00
|
|
|
$markup = $this->mConverter->mMarkup;
|
2009-03-04 11:53:45 +00:00
|
|
|
$variants = $this->mConverter->mVariants;
|
|
|
|
|
|
2009-03-05 02:23:24 +00:00
|
|
|
// varsep_pattern for preg_split:
|
|
|
|
|
// text should be splited by ";" only if a valid variant
|
|
|
|
|
// name exist after the markup, for example:
|
|
|
|
|
// -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:<span style="font-size:120%;">yyy</span>;}-
|
|
|
|
|
// we should split it as:
|
|
|
|
|
// array(
|
|
|
|
|
// [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>'
|
|
|
|
|
// [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>'
|
|
|
|
|
// [2] => ''
|
|
|
|
|
// )
|
2009-03-04 11:53:45 +00:00
|
|
|
$varsep_pattern = '/' . $markup['varsep'] . '\s*' . '(?=';
|
2009-03-05 02:44:58 +00:00
|
|
|
foreach( $variants as $variant ) {
|
|
|
|
|
$varsep_pattern .= $variant . '\s*' . $markup['codesep'] . '|'; // zh-hans:xxx;zh-hant:yyy
|
|
|
|
|
$varsep_pattern .= '[^;]*?' . $markup['unidsep'] . '\s*' . $variant
|
|
|
|
|
. '\s*' . $markup['codesep'] . '|'; // xxx=>zh-hans:yyy; xxx=>zh-hant:zzz
|
|
|
|
|
}
|
2009-03-04 11:53:45 +00:00
|
|
|
$varsep_pattern .= '\s*$)/';
|
2008-07-09 08:16:39 +00:00
|
|
|
|
2009-03-04 11:53:45 +00:00
|
|
|
$choice = preg_split($varsep_pattern, $rules);
|
2009-03-05 02:44:58 +00:00
|
|
|
|
2009-03-04 11:53:45 +00:00
|
|
|
foreach( $choice as $c ) {
|
2009-03-04 06:56:37 +00:00
|
|
|
$v = explode($markup['codesep'], $c, 2);
|
|
|
|
|
if( count($v) != 2 )
|
2008-07-06 17:23:00 +00:00
|
|
|
continue;// syntax error, skip
|
2009-03-04 06:56:37 +00:00
|
|
|
$to = trim($v[1]);
|
|
|
|
|
$v = trim($v[0]);
|
|
|
|
|
$u = explode($markup['unidsep'], $v);
|
2009-03-05 02:23:24 +00:00
|
|
|
// if $to is empty, strtr() could return a wrong result
|
2009-03-04 11:53:45 +00:00
|
|
|
if( count($u) == 1 && $to && in_array( $v, $variants ) ) {
|
2008-07-06 17:23:00 +00:00
|
|
|
$bidtable[$v] = $to;
|
|
|
|
|
} else if(count($u) == 2){
|
2009-03-04 06:56:37 +00:00
|
|
|
$from = trim($u[0]);
|
|
|
|
|
$v = trim($u[1]);
|
2009-03-04 11:53:45 +00:00
|
|
|
if( array_key_exists( $v, $unidtable ) && !is_array( $unidtable[$v] )
|
|
|
|
|
&& $to && in_array( $v, $variants ) )
|
2009-03-04 06:56:37 +00:00
|
|
|
$unidtable[$v] = array( $from=>$to );
|
2009-03-04 11:53:45 +00:00
|
|
|
elseif ( $to && in_array( $v, $variants ) )
|
2009-03-04 06:56:37 +00:00
|
|
|
$unidtable[$v][$from] = $to;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
// syntax error, pass
|
2009-03-04 06:56:37 +00:00
|
|
|
if ( !array_key_exists( $v, $this->mConverter->mVariantNames ) ){
|
2008-07-09 08:16:39 +00:00
|
|
|
$bidtable = array();
|
|
|
|
|
$unidtable = array();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mBidtable = $bidtable;
|
|
|
|
|
$this->mUnidtable = $unidtable;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function getRulesDesc(){
|
2008-07-09 08:16:39 +00:00
|
|
|
$codesep = $this->mConverter->mDescCodeSep;
|
|
|
|
|
$varsep = $this->mConverter->mDescVarSep;
|
2008-07-06 17:23:00 +00:00
|
|
|
$text='';
|
2008-07-09 08:16:39 +00:00
|
|
|
foreach($this->mBidtable as $k => $v)
|
2009-01-29 08:59:53 +00:00
|
|
|
$text .= $this->mConverter->mVariantNames[$k]."$codesep$v$varsep";
|
2008-07-09 08:16:39 +00:00
|
|
|
foreach($this->mUnidtable as $k => $a)
|
2008-07-06 17:23:00 +00:00
|
|
|
foreach($a as $from=>$to)
|
2009-01-29 08:59:53 +00:00
|
|
|
$text.=$from.'⇒'.$this->mConverter->mVariantNames[$k]."$codesep$to$varsep";
|
2008-07-06 17:23:00 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse rules conversion
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2008-07-09 08:16:39 +00:00
|
|
|
function getRuleConvertedStr($variant,$doConvert){
|
|
|
|
|
$bidtable = $this->mBidtable;
|
|
|
|
|
$unidtable = $this->mUnidtable;
|
2008-07-06 17:23:00 +00:00
|
|
|
|
|
|
|
|
if( count($bidtable) + count($unidtable) == 0 ){
|
2008-07-09 08:16:39 +00:00
|
|
|
return $this->mRules;
|
2008-07-06 17:23:00 +00:00
|
|
|
} elseif ($doConvert){// the text converted
|
|
|
|
|
// display current variant in bidirectional array
|
|
|
|
|
$disp = $this->getTextInBidtable($variant);
|
|
|
|
|
// or display current variant in fallbacks
|
|
|
|
|
if(!$disp)
|
2008-07-09 08:16:39 +00:00
|
|
|
$disp = $this->getTextInBidtable(
|
|
|
|
|
$this->mConverter->getVariantFallbacks($variant));
|
2008-07-06 17:23:00 +00:00
|
|
|
// or display current variant in unidirectional array
|
|
|
|
|
if(!$disp && array_key_exists($variant,$unidtable)){
|
|
|
|
|
$disp = array_values($unidtable[$variant]);
|
|
|
|
|
$disp = $disp[0];
|
|
|
|
|
}
|
|
|
|
|
// or display frist text under disable manual convert
|
2008-07-09 08:16:39 +00:00
|
|
|
if(!$disp && $this->mConverter->mManualLevel[$variant]=='disable') {
|
2008-07-06 17:23:00 +00:00
|
|
|
if(count($bidtable)>0){
|
|
|
|
|
$disp = array_values($bidtable);
|
|
|
|
|
$disp = $disp[0];
|
|
|
|
|
} else {
|
|
|
|
|
$disp = array_values($unidtable);
|
|
|
|
|
$disp = array_values($disp[0]);
|
|
|
|
|
$disp = $disp[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $disp;
|
|
|
|
|
} else {// no convert
|
2008-07-09 08:16:39 +00:00
|
|
|
return $this->mRules;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate conversion table for all text
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function generateConvTable(){
|
|
|
|
|
$flags = $this->mFlags;
|
|
|
|
|
$bidtable = $this->mBidtable;
|
|
|
|
|
$unidtable = $this->mUnidtable;
|
|
|
|
|
$manLevel = $this->mConverter->mManualLevel;
|
|
|
|
|
|
|
|
|
|
$vmarked=array();
|
|
|
|
|
foreach($this->mConverter->mVariants as $v) {
|
|
|
|
|
/* for bidirectional array
|
|
|
|
|
fill in the missing variants, if any,
|
|
|
|
|
with fallbacks */
|
|
|
|
|
if(!array_key_exists($v, $bidtable)) {
|
|
|
|
|
$variantFallbacks = $this->mConverter->getVariantFallbacks($v);
|
|
|
|
|
$vf = $this->getTextInBidtable($variantFallbacks);
|
|
|
|
|
if($vf) $bidtable[$v] = $vf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(array_key_exists($v,$bidtable)){
|
|
|
|
|
foreach($vmarked as $vo){
|
|
|
|
|
// use syntax: -{A|zh:WordZh;zh-tw:WordTw}-
|
|
|
|
|
// or -{H|zh:WordZh;zh-tw:WordTw}- or -{-|zh:WordZh;zh-tw:WordTw}-
|
|
|
|
|
// to introduce a custom mapping between
|
|
|
|
|
// words WordZh and WordTw in the whole text
|
|
|
|
|
if($manLevel[$v]=='bidirectional'){
|
|
|
|
|
$this->mConvTable[$v][$bidtable[$vo]]=$bidtable[$v];
|
|
|
|
|
}
|
|
|
|
|
if($manLevel[$vo]=='bidirectional'){
|
|
|
|
|
$this->mConvTable[$vo][$bidtable[$v]]=$bidtable[$vo];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$vmarked[]=$v;
|
|
|
|
|
}
|
|
|
|
|
/*for unidirectional array
|
|
|
|
|
fill to convert tables */
|
|
|
|
|
$allow_unid = $manLevel[$v]=='bidirectional'
|
|
|
|
|
|| $manLevel[$v]=='unidirectional';
|
|
|
|
|
if($allow_unid && array_key_exists($v,$unidtable)){
|
|
|
|
|
$ct=$this->mConvTable[$v];
|
|
|
|
|
$this->mConvTable[$v] = array_merge($ct,$unidtable[$v]);
|
|
|
|
|
}
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-07-08 02:34:11 +00:00
|
|
|
|
2008-07-06 17:23:00 +00:00
|
|
|
/**
|
|
|
|
|
* Parse rules and flags
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
2008-07-09 08:16:39 +00:00
|
|
|
function parse($variant){
|
2009-03-04 06:56:37 +00:00
|
|
|
if(!$variant)
|
|
|
|
|
$variant = $this->mConverter->getPreferredVariant();
|
2008-07-06 17:23:00 +00:00
|
|
|
|
2009-03-04 06:56:37 +00:00
|
|
|
$variants = $this->mConverter->mVariants;
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->parseFlags();
|
2008-07-10 13:56:23 +00:00
|
|
|
$flags = $this->mFlags;
|
2008-07-09 08:16:39 +00:00
|
|
|
|
2009-03-05 02:23:24 +00:00
|
|
|
// convert to specified variant
|
|
|
|
|
// syntax: -{zh-hans;zh-hant[;...]|<text to convert>}-
|
2009-03-04 06:56:37 +00:00
|
|
|
if( count( array_diff( $flags, $variants ) ) == 0 and count( $flags ) != 0 ) {
|
2009-03-05 02:23:24 +00:00
|
|
|
if ( in_array( $variant, $flags ) ) // check if current variant in flags
|
|
|
|
|
// then convert <text to convert> to current language
|
2009-03-04 06:56:37 +00:00
|
|
|
$this->mRules = $this->mConverter->autoConvert( $this->mRules, $variant );
|
2009-03-05 02:23:24 +00:00
|
|
|
else { // if current variant no in flags,
|
|
|
|
|
// then we check its fallback variants.
|
2009-03-04 06:56:37 +00:00
|
|
|
$variantFallbacks = $this->mConverter->getVariantFallbacks($variant);
|
|
|
|
|
foreach ( $variantFallbacks as $variantFallback ) {
|
2009-03-05 02:23:24 +00:00
|
|
|
// if current variant's fallback exist in flags
|
2009-03-04 06:56:37 +00:00
|
|
|
if ( in_array( $variantFallback, $flags ) ) {
|
2009-03-05 02:23:24 +00:00
|
|
|
// then convert <text to convert> to fallback language
|
2009-03-04 06:56:37 +00:00
|
|
|
$this->mRules = $this->mConverter->autoConvert( $this->mRules, $variantFallback );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->mFlags = $flags = array('R');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( !in_array( 'R', $flags ) || !in_array( 'N', $flags ) ) {
|
2008-07-10 13:56:23 +00:00
|
|
|
// decode => HTML entities modified by Sanitizer::removeHTMLtags
|
|
|
|
|
$this->mRules = str_replace('=>','=>',$this->mRules);
|
|
|
|
|
|
|
|
|
|
$this->parseRules();
|
|
|
|
|
}
|
2008-07-09 08:16:39 +00:00
|
|
|
$rules = $this->mRules;
|
|
|
|
|
|
2009-03-04 06:56:37 +00:00
|
|
|
if( count( $this->mBidtable ) == 0 && count( $this->mUnidtable ) == 0 ){
|
2008-07-09 08:16:39 +00:00
|
|
|
if(in_array('+',$flags) || in_array('-',$flags))
|
|
|
|
|
// fill all variants if text in -{A/H/-|text} without rules
|
|
|
|
|
foreach($this->mConverter->mVariants as $v)
|
|
|
|
|
$this->mBidtable[$v] = $rules;
|
|
|
|
|
elseif (!in_array('N',$flags) && !in_array('T',$flags) )
|
|
|
|
|
$this->mFlags = $flags = array('R');
|
2008-07-07 01:12:15 +00:00
|
|
|
}
|
2008-07-06 17:23:00 +00:00
|
|
|
|
|
|
|
|
if( in_array('R',$flags) ) {
|
|
|
|
|
// if we don't do content convert, still strip the -{}- tags
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mRuleDisplay = $rules;
|
2008-07-06 17:23:00 +00:00
|
|
|
} elseif ( in_array('N',$flags) ){
|
|
|
|
|
// proces N flag: output current variant name
|
2009-01-29 08:59:53 +00:00
|
|
|
$this->mRuleDisplay = $this->mConverter->mVariantNames[trim($rules)];
|
2008-07-06 17:23:00 +00:00
|
|
|
} elseif ( in_array('D',$flags) ){
|
|
|
|
|
// proces D flag: output rules description
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mRuleDisplay = $this->getRulesDesc();
|
2008-07-06 17:23:00 +00:00
|
|
|
} elseif ( in_array('H',$flags) || in_array('-',$flags) ) {
|
|
|
|
|
// proces H,- flag or T only: output nothing
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mRuleDisplay = '';
|
2008-07-06 17:23:00 +00:00
|
|
|
} elseif ( in_array('S',$flags) ){
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mRuleDisplay = $this->getRuleConvertedStr($variant,
|
|
|
|
|
$this->mConverter->mDoContentConvert);
|
2008-07-06 17:23:00 +00:00
|
|
|
} else {
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mRuleDisplay= $this->mManualCodeError;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
2008-07-10 13:56:23 +00:00
|
|
|
// proces T flag
|
2008-07-06 17:23:00 +00:00
|
|
|
if ( in_array('T',$flags) ) {
|
2008-07-09 08:16:39 +00:00
|
|
|
$this->mRuleTitle = $this->getRuleConvertedStr($variant,
|
|
|
|
|
$this->mConverter->mDoTitleConvert);
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
2008-07-09 08:16:39 +00:00
|
|
|
|
|
|
|
|
if (in_array('-', $flags))
|
|
|
|
|
$this->mRulesAction='remove';
|
|
|
|
|
if (in_array('+', $flags))
|
|
|
|
|
$this->mRulesAction='add';
|
|
|
|
|
|
|
|
|
|
$this->generateConvTable();
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-09 08:16:39 +00:00
|
|
|
/**
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
function hasRules(){
|
|
|
|
|
// TODO:
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-06 17:23:00 +00:00
|
|
|
/**
|
|
|
|
|
* get display text on markup -{...}-
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
2008-07-08 02:34:11 +00:00
|
|
|
function getDisplay(){
|
2008-07-09 08:16:39 +00:00
|
|
|
return $this->mRuleDisplay;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* get converted title
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
2008-07-08 02:34:11 +00:00
|
|
|
function getTitle(){
|
2008-07-09 08:16:39 +00:00
|
|
|
return $this->mRuleTitle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* return how deal with conversion rules
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
function getRulesAction(){
|
|
|
|
|
return $this->mRulesAction;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get conversion table ( bidirectional and unidirectional conversion table )
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
function getConvTable(){
|
2008-07-09 08:16:39 +00:00
|
|
|
return $this->mConvTable;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get conversion rules string
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
function getRules(){
|
2008-07-09 08:16:39 +00:00
|
|
|
return $this->mRules;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get conversion flags
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
function getFlags(){
|
2008-07-09 08:16:39 +00:00
|
|
|
return $this->mFlags;
|
2008-07-06 17:23:00 +00:00
|
|
|
}
|
|
|
|
|
}
|