Удзельнік:Alexander Gouk/Канвертар/files/LanguageBe.php

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі
<?php
/**
  * @package MediaWiki
  * @subpackage Language
  */

// Language Conversion

require_once( dirname(__FILE__).'/../LanguageConverter.php' );
require_once( dirname(__FILE__).'/LanguageBe_of.php' );
require_once( dirname(__FILE__).'/LanguageBe_tr.php' );

class BeConverter extends LanguageConverter {

        var $mToAlternative = array(
                'іласоф'  => 'ілязоф',
                'Лондан' => 'Лёндан',
                'сць'  => 'сьць',
                'сля'  => 'сьля',
                'ненн'  => 'неньн',
        );

        var $mToOfficial = array(
                'ілязоф' => 'іласоф',
                'Лёндан' => 'Лондан',
                'сьць'  => 'сць',
                'сьля'   => 'сля',
                'еньн'  => 'енн',
                'аньн'  => 'анн',
                'менск' => 'мінск',
                'сьвя'  => 'свя',
                'Сьвя'  => 'Свя',
        );

	function loadDefaultTables() {
		$this->mTables = array(
			'be-of' => new ReplacementArray( $this->mToOfficial ),
			'be-tr' => new ReplacementArray( $this->mToAlternative),
			'be'    => new ReplacementArray( $this->mToOfficial ),
		);
	}

	/* rules should be defined as -{афіцыйная | тарашкевіца-} -or-
		-{code:text | code:text | ...}-
		update: delete all rule parsing because it's not used
		        currently, and just produces a couple of bugs
	*/
	function parseManualRule($rule, $flags=array()) {
		if(in_array('T',$flags)){
			return parent::parseManualRule($rule, $flags);
		}

		// otherwise ignore all formatting
		foreach($this->mVariants as $v) {
			$carray[$v] = $rule;
		}
		
		return $carray;
	}

	// Do not convert content on talk pages
	function parserConvert( $text, &$parser ){
		if(is_object($parser->mTitle) && $parser->mTitle->isTalkPage())
			$this->mDoContentConvert=false;
		else 
			$this->mDoContentConvert=true;

		return parent::parserConvert($text, $parser );
	}

	/*
	 * A function wrapper:
	 *   - if there is no selected variant, leave the link 
	 *     names as they were
	 *   - do not try to find variants for usernames
	 */
	function findVariantLink( &$link, &$nt ) {
		// check for user namespace
		if(is_object($nt)){
			$ns = $nt->getNamespace();
			if($ns==NS_USER || $ns==NS_USER_TALK)
				return;
		}

		$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(is_object($wgTitle) && $wgTitle->getNameSpace()==NS_IMAGE){ 
			$imagename = $wgTitle->getNsText();
			if(preg_match("/^$imagename:/",$text)) return $text;
		}
		return parent::autoConvert($text,$toVariant);
	} 


}

class LanguageBe extends LanguageBe_of {
	function __construct() {
		global $wgHooks;

		parent::__construct();

		// load Variants:
		$variants = array('be-of', 'be-tr');
		$variantfallbacks = array(
			'be'    => 'be-of',
			'be-of' => 'be',
			'be-tr' => 'be',
			); 

		$marker = array();//don't mess with these, leave them as they are
		$flags = array(
			'S' => 'S', 'пісьмо' => 'S', 'пісьмо' => 'S',
			'W' => 'W', 'в. мова'   => 'W', 'в. мова'   => 'W', 'в. мова' => 'W', 'в. мова' => 'W'
		);
		$this->mConverter = new BeConverter($this, 'be', $variants, $variantfallbacks, $marker, $flags);
		$wgHooks['ArticleSaveComplete'][] = $this->mConverter;
	}
}
?>