46 $this->mConverter = $converter;
56 $variants = (
array)$variants;
60 foreach ( $variants
as $variant ) {
61 if ( isset( $this->mBidtable[$variant] ) ) {
62 return $this->mBidtable[$variant];
77 $sepPos = strpos( $text,
'|' );
78 if ( $sepPos !==
false ) {
79 $validFlags = $this->mConverter->mFlags;
81 foreach ( $f
as $ff ) {
83 if ( isset( $validFlags[$ff] ) ) {
84 $flags[$validFlags[$ff]] =
true;
87 $text = strval( substr( $text, $sepPos + 1 ) );
92 } elseif ( isset( $flags[
'R'] ) ) {
93 $flags = [
'R' =>
true ];
94 } elseif ( isset( $flags[
'N'] ) ) {
95 $flags = [
'N' =>
true ];
96 } elseif ( isset( $flags[
'-'] ) ) {
97 $flags = [
'-' =>
true ];
98 } elseif (
count( $flags ) == 1 && isset( $flags[
'T'] ) ) {
100 } elseif ( isset( $flags[
'H'] ) ) {
102 $temp = [
'+' =>
true,
'H' =>
true ];
103 if ( isset( $flags[
'T'] ) ) {
106 if ( isset( $flags[
'D'] ) ) {
111 if ( isset( $flags[
'A'] ) ) {
115 if ( isset( $flags[
'D'] ) ) {
116 unset( $flags[
'S'] );
120 $variantFlags = array_intersect( array_keys( $flags ), $this->mConverter->mVariants );
121 if ( $variantFlags ) {
122 $variantFlags = array_flip( $variantFlags );
126 $this->mVariantFlags = $variantFlags;
127 $this->mRules = $text;
128 $this->mFlags = $flags;
139 $variants = $this->mConverter->mVariants;
140 $varsep_pattern = $this->mConverter->getVarSeparatorPattern();
143 $rules = preg_replace(
'/(&[#a-zA-Z0-9]+);/',
"$1\x01", $rules );
144 $choice = preg_split( $varsep_pattern, $rules );
145 $choice = str_replace(
"\x01",
';', $choice );
147 foreach ( $choice
as $c ) {
148 $v = explode(
':', $c, 2 );
149 if (
count( $v ) != 2 ) {
155 $u = explode(
'=>', $v, 2 );
158 if (
count( $u ) == 1 && $to !==
'' && in_array( $v, $variants ) ) {
160 } elseif (
count( $u ) == 2 ) {
161 $from = trim( $u[0] );
164 if ( array_key_exists( $v, $unidtable )
165 && !is_array( $unidtable[$v] )
167 && in_array( $v, $variants ) ) {
168 $unidtable[$v] = [ $from => $to ];
169 } elseif ( $from !==
'' && in_array( $v, $variants ) ) {
170 $unidtable[$v][$from] = $to;
174 if ( !isset( $this->mConverter->mVariantNames[$v] ) ) {
180 $this->mBidtable = $bidtable;
181 $this->mUnidtable = $unidtable;
190 $codesep = $this->mConverter->mDescCodeSep;
191 $varsep = $this->mConverter->mDescVarSep;
193 foreach ( $this->mBidtable
as $k => $v ) {
194 $text .= $this->mConverter->mVariantNames[$k] .
"$codesep$v$varsep";
196 foreach ( $this->mUnidtable
as $k => $a ) {
197 foreach ( $a
as $from => $to ) {
198 $text .= $from .
'⇒' . $this->mConverter->mVariantNames[$k] .
199 "$codesep$to$varsep";
217 if (
count( $bidtable ) +
count( $unidtable ) == 0 ) {
223 if ( $disp ===
false ) {
225 $this->mConverter->getVariantFallbacks( $variant ) );
228 if ( $disp ===
false && array_key_exists( $variant, $unidtable ) ) {
229 $disp = array_values( $unidtable[$variant] )[0];
232 if ( $disp ===
false && $this->mConverter->mManualLevel[$variant] ==
'disable' ) {
233 if (
count( $bidtable ) > 0 ) {
234 $disp = array_values( $bidtable )[0];
236 $disp = array_values( array_values( $unidtable )[0] )[0];
254 if ( $variant === $this->mConverter->mMainLanguageCode ) {
263 if ( array_key_exists( $variant, $this->mUnidtable ) ) {
264 $disp = array_values( $this->mUnidtable[$variant] )[0];
279 if ( !$this->mBidtable && !$this->mUnidtable ) {
280 $this->mConvTable = [];
286 $manLevel = $this->mConverter->mManualLevel;
289 foreach ( $this->mConverter->mVariants
as $v ) {
293 if ( !isset( $bidtable[$v] ) ) {
295 $this->mConverter->getVariantFallbacks( $v );
302 if ( isset( $bidtable[$v] ) ) {
303 foreach ( $vmarked
as $vo ) {
309 if ( $manLevel[$v] ==
'bidirectional' ) {
310 $this->mConvTable[$v][$bidtable[$vo]] = $bidtable[$v];
312 if ( $manLevel[$vo] ==
'bidirectional' ) {
313 $this->mConvTable[$vo][$bidtable[$v]] = $bidtable[$vo];
319 if ( ( $manLevel[$v] ==
'bidirectional' || $manLevel[$v] ==
'unidirectional' )
320 && isset( $unidtable[$v] )
322 if ( isset( $this->mConvTable[$v] ) ) {
323 $this->mConvTable[$v] = $unidtable[$v] + $this->mConvTable[$v];
325 $this->mConvTable[$v] = $unidtable[$v];
335 public function parse( $variant =
null ) {
337 $variant = $this->mConverter->getPreferredVariant();
345 if ( $this->mVariantFlags ) {
347 if ( isset( $this->mVariantFlags[$variant] ) ) {
349 $this->mRules = $this->mConverter->autoConvert( $this->mRules,
355 $this->mConverter->getVariantFallbacks( $variant );
356 if ( is_array( $variantFallbacks ) ) {
357 foreach ( $variantFallbacks
as $variantFallback ) {
359 if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
362 $this->mConverter->autoConvert( $this->mRules,
369 $this->mFlags = $flags = [
'R' =>
true ];
372 if ( !isset( $flags[
'R'] ) && !isset( $flags[
'N'] ) ) {
374 $this->mRules = str_replace(
'=>',
'=>', $this->mRules );
379 if ( !$this->mBidtable && !$this->mUnidtable ) {
380 if ( isset( $flags[
'+'] ) || isset( $flags[
'-'] ) ) {
382 if ( $rules !==
'' ) {
383 foreach ( $this->mConverter->mVariants
as $v ) {
384 $this->mBidtable[$v] = $rules;
387 } elseif ( !isset( $flags[
'N'] ) && !isset( $flags[
'T'] ) ) {
388 $this->mFlags = $flags = [
'R' =>
true ];
392 $this->mRuleDisplay =
false;
393 foreach ( $flags
as $flag => $unused ) {
397 $this->mRuleDisplay = $rules;
401 $ruleVar = trim( $rules );
402 if ( isset( $this->mConverter->mVariantNames[$ruleVar] ) ) {
403 $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar];
405 $this->mRuleDisplay =
'';
414 $this->mRuleDisplay =
'';
417 $this->mRulesAction =
'remove';
418 $this->mRuleDisplay =
'';
421 $this->mRulesAction =
'add';
422 $this->mRuleDisplay =
'';
429 $this->mRuleDisplay =
'';
435 if ( $this->mRuleDisplay ===
false ) {
436 $this->mRuleDisplay =
'<span class="error">'
437 .
wfMessage(
'converter-manual-rule-error' )->inContentLanguage()->escaped()
449 return $this->mRules !==
'';