languageconverter.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 661 行 · 第 1/2 页

PHP
661
字号
						}					}				}			}			else {				$text .= $marked[0];			}			if(array_key_exists(1, $marked))				$text .= $this->autoConvert($marked[1]);		}		return $text;	}	/**	 * parse the manually marked conversion rule	 * @param string $rule the text of the rule	 * @return array of the translation in each variant	 * @private	 */	function parseManualRule($rules, $flags=array()) {		$choice = explode($this->mMarkup['varsep'], $rules);		$carray = array();		if(sizeof($choice) == 1) {			/* a single choice */			foreach($this->mVariants as $v)				$carray[$v] = $choice[0];		}		else {			foreach($choice as $c) {				$v = explode($this->mMarkup['codesep'], $c);				if(sizeof($v) != 2) // syntax error, skip					continue;				$carray[trim($v[0])] = trim($v[1]);			}		}		return $carray;	}	/**	 * if a language supports multiple variants, it is	 * possible that non-existing link in one variant	 * actually exists in another variant. this function	 * 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	 * @return null the input parameters may be modified upon return     * @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;				if( !$wgDisableLangConversion )					$link = $v;				break;			}		}	}    /**     * returns language specific hash options     *     * @access public     */	function getExtraHashOptions() {		$variant = $this->getPreferredVariant();		return '!' . $variant ;	}    /**     * get title text as defined in the body of the article text     *     * @access public     */	function getParsedTitle() {		return $this->mTitleDisplay;	}	/**     * a write lock to the cache     *     * @private     */	function lockCache() {		global $wgMemc;		$success = false;		for($i=0; $i<30; $i++) {			if($success = $wgMemc->add($this->mCacheKey . "lock", 1, 10))				break;			sleep(1);		}		return $success;	}	/**     * unlock cache     *     * @private     */	function unlockCache() {		global $wgMemc;		$wgMemc->delete($this->mCacheKey . "lock");	}	/**     * Load default conversion tables     * This method must be implemented in derived class     *     * @private     */	function loadDefaultTables() {		$name = get_class($this);		wfDie("Must implement loadDefaultTables() method in class $name");	}	/**     * load conversion tables either from the cache or the disk     * @private     */	function loadTables($fromcache=true) {		global $wgMemc;		if( $this->mTablesLoaded )			return;		$this->mTablesLoaded = true;		if($fromcache) {			$this->mTables = $wgMemc->get( $this->mCacheKey );			if( !empty( $this->mTables ) ) //all done				return;		}		// 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->loadDefaultTables();		foreach($this->mVariants as $var) {			$cached = $this->parseCachedTable($var);			$this->mTables[$var] = array_merge($this->mTables[$var], $cached);		}		$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() {}    /**     * Reload the conversion tables     *     * @private     */	function reloadTables() {		if($this->mTables)			unset($this->mTables);		$this->mTablesLoaded = false;		$this->loadTables(false);	}	/**     * 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     *     * @private	 */	function parseCachedTable($code, $subpage='', $recursive=true) {		global $wgMessageCache;		static $parsed = array();		if(!is_object($wgMessageCache))			return array();		$key = 'Conversiontable/'.$code;		if($subpage)			$key .= '/' . $subpage;		if(array_key_exists($key, $parsed))			return array();		$txt = $wgMessageCache->get( $key, true, true, true );		// get all subpage links of the form		// [[MediaWiki:conversiontable/zh-xx/...|...]]		$linkhead = $this->mLangObj->getNsText(NS_MEDIAWIKI) . ':Conversiontable';		$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];			else				$sublink = '';			if($b[0] == $linkhead && $b[1] == $code) {				$sublinks[] = $sublink;			}		}		// parse the mappings in this page		$blocks = explode($this->mMarkup['begin'], $txt);		array_shift($blocks);		$ret = array();		foreach($blocks as $block) {			$mappings = explode($this->mMarkup['end'], $block, 2);			$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);			}		}		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	*/	function markNoConversion($text) {		# don't mark if already marked		if(strpos($text, $this->mMarkup['begin']) || 		   strpos($text, $this->mMarkup['end']))			return $text;		$ret = $this->mMarkup['begin'] . $text . $this->mMarkup['end'];		return $ret;	}	/**	 * 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     * @private	*/	function OnArticleSaveComplete($article, $user, $text, $summary, $isminor, $iswatch, $section) {		$titleobj = $article->getTitle();		if($titleobj->getNamespace() == NS_MEDIAWIKI) {            /*			global $wgContLang; // should be an LanguageZh.			if(get_class($wgContLang) != 'languagezh')				return true;            */			$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;	}}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?