Удзельнік:EugeneZelenko/Канвэртар/LanguageBe tarask.php

<?php
/** Belarusian language, classic spelling
  * (Беларуская, клясычны)
  *
  * @package MediaWiki
  * @subpackage Language
  */

require_once( dirname(__FILE__).'/../LanguageConverter.php' );
require_once( dirname(__FILE__).'/LanguageBe_tarask_cyrl.php' );

class BeTaraskConverter extends LanguageConverter {

	function loadDefaultTables() {
		require( "includes/BeTaraskConversion.php" );

		$this->mTables = array(
			'be-tarask-cyrl' => new ReplacementArray( $BeTarask2Cyrl ),
			'be-tarask-latn' => new ReplacementArray( $BeTarask2Latn ),
			'be-tarask'      => new ReplacementArray()
		);
	}

	function regsConverter( $text, $toVariant ) {
		require( 'LanguageBe_tarask-ToCyrl.inc' );
		require( 'LanguageBe_tarask-ToLatn.inc' );
		if ($toVariant == 'be-tarask'){
			# keep as placeholder please
			return $text;
		}
		if ($toVariant == 'be-tarask-cyrl'){
			# process regexes for Cyrillic -> Latin
			$pattern = $patBeTarask_latn;
			$replacement = $repBeTarask_cyrl;
			$text = preg_replace($pattern, $replacement, $text);
			return $text;
		}
		if ($toVariant == 'be-tarask-latn'){
			# process regexes for Latin -> Cyrillic
			$pattern = $patBeTarask_cyrl;
			$replacement = $repBeTarask_latn;
			$text = preg_replace($pattern, $replacement, $text);
			return $text;
		}
	}

	/*
	 * Override function from LanguageConvertor
	 * Additional checks:
	 *  - There should be no conversion for Talk pages
	 */
	function getPreferredVariant(){
		global $wgTitle;
		if( is_object( $wgTitle ) && $wgTitle->isTalkPage()) {
			return $this->mMainLanguageCode;
		}
		return parent::getPreferredVariant();
	}

	/*
	 * A function wrapper, if there is no selected variant,
	 * leave the link names as they were
	 */
	function findVariantLink( &$link, &$nt ) {
		$oldlink=$link;
		parent::findVariantLink($link,$nt);
		if($this->getPreferredVariant()==$this->mMainLanguageCode)
			$link=$oldlink;
	}

	/*
	 * We want our external link captions to be converted in variants,
	 * so we return the original text instead -{$text}-, except for URLs
	 */
	function markNoConversion($text, $noParse=false) {
		if($noParse || preg_match("/^https?:\/\/|ftp:\/\/|irc:\/\//",$text))
			return parent::markNoConversion($text);
		return $text;
	}

	/*
	 * An ugly function wrapper for parsing Image titles
	 * (to prevent image name conversion)
	 */
	function autoConvert($text, $toVariant=false) {
		global $wgTitle;
		if($wgTitle->getNameSpace()==NS_IMAGE){
			$imagename = $wgTitle->getNsText();
			if(preg_match("/^$imagename:/",$text)) return $text;
		}
		return parent::autoConvert($text,$toVariant);
	}

	/**
	 *  It translates text into variant, specials:
	 *	- ommiting roman numbers
	 */
	function translate($text, $toVariant){
		$breaks = '[^\w\x80-\xff]';

		// regexp for roman numbers
		$roman = 'M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})';

		$reg = '/^'.$roman.'$|^'.$roman.$breaks.'|'.$breaks.$roman.'$|'.$breaks.$roman.$breaks.'/';

		$matches = preg_split($reg, $text, -1, PREG_SPLIT_OFFSET_CAPTURE);

		$m = array_shift($matches);
		$ret = $this->mTables[$toVariant]->replace( $m[0] );
		$mstart = $m[1]+strlen($m[0]);
		foreach($matches as $m) {
			$ret .= substr($text, $mstart, $m[1]-$mstart);
			$ret .= parent::translate($m[0], $toVariant);
			$mstart = $m[1] + strlen($m[0]);
		}
		$text = $this->regsConverter( $ret, $toVariant );
		return $text;
	}

}

class LanguageBe_tarask extends LanguageBe_tarask_cyrl {

	function __construct() {
		global $wgHooks;
		parent::__construct();

		$variants = array( 'be-tarask', 'be-tarask-cyrl', 'be-tarask-latn' );
		$variantfallbacks = array(
			'be-tarask'	     => 'be-tarask-cyrl',
			'be-tarask-cyrl' => 'be-tarask-cyrl',
			'be-tarask-latn' => 'be-tarask-latn'
		);

		$this->mConverter = new BeTaraskConverter( $this, 'be-tarask', $variants, $variantfallbacks );
		$wgHooks['ArticleSaveComplete'][] = $this->mConverter;
	}

	function convertGrammar( $word, $case ) {
		$fname="LanguageBeTarask::convertGrammar";
		wfProfileIn( $fname );

		//always convert to -cyrl before convertGrammar
		$w1 = $word;
		$word = $this->mConverter->autoConvert( $word, 'be-tarask-cyrl' );
		$w2 = $word;
		$case = $this->mConverter->autoConvert( $case, 'be-tarask-cyrl' );
		$word = parent::convertGrammar( $word, $case );
		//restore encoding
		if( $w1 != $w2 ) {
			$word = $this->mConverter->translate( $word, 'be-tarask-latn' );
		}
		wfProfileOut( $fname );
		return $word;
	}

}

?>