User:AlefZet/crh/LanguageCrh.php

From Wikimedia Incubator
Jump to navigation Jump to search

/*

*/
<?php
/** Crimea Tatar (Qırımtatarca)
  * converter routines
  *
  * @package MediaWiki
  * @subpackage Language
  */

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

class CrhConverter extends LanguageConverter {

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

		$this->mTables = array(
			'crh-latn' => new ReplacementArray( $crh2Cyrl ),
			'crh-cyrl' => new ReplacementArray( $crh2Latn ),
			'crh'      => new ReplacementArray()
		);
	}

	function regsConverter( $text, $toVariant ) {
		require( 'LanguageCrh.inc' );
		if ($toVariant == 'crh'){
			# keep as placeholder please
			return $text;
		}
		if ($toVariant == 'crh-latn'){
			# process regexes for Cyrillic -> Latin
			/*$pattern = $this->patCrh_cyrl;
			$replacement = $this->repCrh_latn;
			$text = preg_replace($pattern, $replacement, $text);*/
			return $text;
		}
		if ($toVariant == 'crh-cyrl'){
			# process regexes for Latin -> Cyrillic
			$pattern = $patCrh_latn;
			$replacement = $repCrh_cyrl;
			$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 LanguageCrh extends LanguageCrh_latn {

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

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

		$this->mConverter = new CrhConverter( $this, 'crh', $variants, $variantfallbacks );
		$wgHooks['ArticleSaveComplete'][] = $this->mConverter;
	}

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

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

}

?>
/*

*/