+
+/**
+ * parser for rules of language conversion , parse rules in -{ }- tag
+ * @ingroup Language
+ * @author fdcn <fdcn64@gmail.com>
+ */
+class ConverterRule {
+ var $mText; // original text in -{text}-
+ 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
+
+ /**
+ * Constructor
+ *
+ * @param string $text the text between -{ and }-
+ * @param object $converter a LanguageConverter object
+ * @access public
+ */
+ function __construct($text,$converter){
+ $this->mText = $text;
+ $this->mConverter=$converter;
+ foreach($converter->mVariants as $v){
+ $this->mConvTable[$v]=array();
+ }
+ }
+
+ /**
+ * 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){
+ if(array_key_exists($variant, $this->mBidtable)){
+ return $this->mBidtable[$variant];
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Parse flags with syntax -{FLAG| ... }-
+ * @private
+ */
+ function parseFlags(){
+ $text = $this->mText;
+ if(strlen($text) < 2 ) {
+ $this->mFlags = array( 'R' );
+ $this->mRules = $text;
+ return;
+ }
+
+ $flags = array();
+ $markup = $this->mConverter->mMarkup;
+ $validFlags = $this->mConverter->mFlags;
+
+ $tt = explode($markup['flagsep'], $text, 2);
+ if(count($tt) == 2) {
+ $f = explode($markup['varsep'], $tt[0]);
+ foreach($f as $ff) {
+ $ff = trim($ff);
+ if(array_key_exists($ff, $validFlags) &&
+ !in_array($validFlags[$ff], $flags))
+ $flags[] = $validFlags[$ff];
+ }
+ $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 {
+ if ( in_array('A',$flags)) {
+ $flags[]='+';
+ $flags[]='S';
+ }
+ if ( in_array('D',$flags) )
+ $flags=array_diff($flags,array('S'));
+ }
+ if ( count($flags)==0 )
+ $flags = array('S');
+ $this->mRules=$rules;
+ $this->mFlags=$flags;
+ }
+
+ /**
+ * generate conversion table
+ * @private
+ */
+ function parseRules() {
+ $rules = $this->mRules;
+ $flags = $this->mFlags;
+ $bidtable = array();
+ $unidtable = array();
+ $markup = $this->mConverter->mMarkup;
+
+ $choice = explode($markup['varsep'], $rules );
+ foreach($choice as $c) {
+ $v = explode($markup['codesep'], $c);
+ if(count($v) != 2)
+ continue;// syntax error, skip
+ $to=trim($v[1]);
+ $v=trim($v[0]);
+ $u = explode($markup['unidsep'], $v);
+ if(count($u) == 1) {
+ $bidtable[$v] = $to;
+ } else if(count($u) == 2){
+ $from=trim($u[0]);$v=trim($u[1]);
+ if( array_key_exists($v,$unidtable) && !is_array($unidtable[$v]) )
+ $unidtable[$v]=array($from=>$to);
+ else
+ $unidtable[$v][$from]=$to;
+ }
+ // syntax error, pass
+ if (!array_key_exists($v,$this->mConverter->mVariantNames)){
+ $bidtable = array();
+ $unidtable = array();
+ break;
+ }
+ }
+ $this->mBidtable = $bidtable;
+ $this->mUnidtable = $unidtable;
+ }
+
+ /**
+ * @private
+ */
+ function getRulesDesc(){
+ $codesep = $this->mConverter->mDescCodeSep;
+ $varsep = $this->mConverter->mDescVarSep;
+ $text='';
+ foreach($this->mBidtable as $k => $v)
+ $text .= $this->mConverter->mVariantNames[$k]."$codesep$v$varsep";
+ foreach($this->mUnidtable as $k => $a)
+ foreach($a as $from=>$to)
+ $text.=$from.'⇒'.$this->mConverter->mVariantNames[$k]."$codesep$to$varsep";
+ return $text;
+ }
+
+ /**
+ * Parse rules conversion
+ * @private
+ */
+ function getRuleConvertedStr($variant,$doConvert){
+ $bidtable = $this->mBidtable;
+ $unidtable = $this->mUnidtable;
+
+ if( count($bidtable) + count($unidtable) == 0 ){
+ return $this->mRules;
+ } elseif ($doConvert){// the text converted
+ // display current variant in bidirectional array
+ $disp = $this->getTextInBidtable($variant);
+ // or display current variant in fallbacks
+ if(!$disp)
+ $disp = $this->getTextInBidtable(
+ $this->mConverter->getVariantFallbacks($variant));
+ // 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
+ if(!$disp && $this->mConverter->mManualLevel[$variant]=='disable') {
+ 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
+ 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]);
+ }
+ }
+ }
+
+ /**
+ * Parse rules and flags
+ * @public
+ */
+ function parse($variant){
+ if(!$variant) $variant = $this->mConverter->getPreferredVariant();
+
+ $this->parseFlags();
+ $flags = $this->mFlags;
+
+ if( !in_array('R',$flags) || !in_array('N',$flags) ){
+ //FIXME: may cause trouble here...
+ //strip since it interferes with the parsing, plus,
+ //all spaces should be stripped in this tag anyway.
+ $this->mRules = str_replace(' ', '', $this->mRules);
+ // decode => HTML entities modified by Sanitizer::removeHTMLtags
+ $this->mRules = str_replace('=>','=>',$this->mRules);
+
+ $this->parseRules();
+ }
+ $rules = $this->mRules;
+
+ if(count($this->mBidtable)==0 && count($this->mUnidtable)==0){
+ 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');
+ }
+
+ if( in_array('R',$flags) ) {
+ // if we don't do content convert, still strip the -{}- tags
+ $this->mRuleDisplay = $rules;
+ } elseif ( in_array('N',$flags) ){
+ // proces N flag: output current variant name
+ $this->mRuleDisplay = $this->mConverter->mVariantNames[trim($rules)];
+ } elseif ( in_array('D',$flags) ){
+ // proces D flag: output rules description
+ $this->mRuleDisplay = $this->getRulesDesc();
+ } elseif ( in_array('H',$flags) || in_array('-',$flags) ) {
+ // proces H,- flag or T only: output nothing
+ $this->mRuleDisplay = '';
+ } elseif ( in_array('S',$flags) ){
+ $this->mRuleDisplay = $this->getRuleConvertedStr($variant,
+ $this->mConverter->mDoContentConvert);
+ } else {
+ $this->mRuleDisplay= $this->mManualCodeError;
+ }
+ // proces T flag
+ if ( in_array('T',$flags) ) {
+ $this->mRuleTitle = $this->getRuleConvertedStr($variant,
+ $this->mConverter->mDoTitleConvert);
+ }
+
+ if (in_array('-', $flags))
+ $this->mRulesAction='remove';
+ if (in_array('+', $flags))
+ $this->mRulesAction='add';
+
+ $this->generateConvTable();
+ }
+
+ /**
+ * @public
+ */
+ function hasRules(){
+ // TODO:
+ }
+
+ /**
+ * get display text on markup -{...}-
+ * @public
+ */
+ function getDisplay(){
+ return $this->mRuleDisplay;
+ }
+ /**
+ * get converted title
+ * @public
+ */
+ function getTitle(){
+ return $this->mRuleTitle;
+ }
+
+ /**
+ * return how deal with conversion rules
+ * @public
+ */
+ function getRulesAction(){
+ return $this->mRulesAction;
+ }
+
+ /**
+ * get conversion table ( bidirectional and unidirectional conversion table )
+ * @public
+ */
+ function getConvTable(){
+ return $this->mConvTable;
+ }
+
+ /**
+ * get conversion rules string
+ * @public
+ */
+ function getRules(){
+ return $this->mRules;
+ }
+
+ /**
+ * get conversion flags
+ * @public
+ */
+ function getFlags(){
+ return $this->mFlags;
+ }
+}