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 );
156 $vv = $this->mConverter->validateVariant( $v );
159 if (
count( $u ) == 1 && $to !==
'' && $vv ) {
160 $bidtable[$vv] = $to;
161 } elseif (
count( $u ) == 2 ) {
162 $from = trim( $u[0] );
164 $vv = $this->mConverter->validateVariant( $v );
166 if ( array_key_exists( $vv, $unidtable )
167 && !is_array( $unidtable[$vv] )
170 $unidtable[$vv] = [ $from => $to ];
171 } elseif ( $from !==
'' && $vv ) {
172 $unidtable[$vv][$from] = $to;
176 if ( !isset( $this->mConverter->mVariantNames[$vv] ) ) {
182 $this->mBidtable = $bidtable;
183 $this->mUnidtable = $unidtable;
192 $codesep = $this->mConverter->mDescCodeSep;
193 $varsep = $this->mConverter->mDescVarSep;
195 foreach ( $this->mBidtable
as $k => $v ) {
196 $text .= $this->mConverter->mVariantNames[$k] .
"$codesep$v$varsep";
198 foreach ( $this->mUnidtable
as $k => $a ) {
199 foreach ( $a
as $from => $to ) {
200 $text .= $from .
'⇒' . $this->mConverter->mVariantNames[$k] .
201 "$codesep$to$varsep";
219 if (
count( $bidtable ) +
count( $unidtable ) == 0 ) {
225 if ( $disp ===
false ) {
227 $this->mConverter->getVariantFallbacks( $variant ) );
230 if ( $disp ===
false && array_key_exists( $variant, $unidtable ) ) {
231 $disp = array_values( $unidtable[$variant] )[0];
234 if ( $disp ===
false && $this->mConverter->mManualLevel[$variant] ==
'disable' ) {
235 if (
count( $bidtable ) > 0 ) {
236 $disp = array_values( $bidtable )[0];
238 $disp = array_values( array_values( $unidtable )[0] )[0];
256 if ( $variant === $this->mConverter->mMainLanguageCode ) {
265 if ( array_key_exists( $variant, $this->mUnidtable ) ) {
266 $disp = array_values( $this->mUnidtable[$variant] )[0];
281 if ( !$this->mBidtable && !$this->mUnidtable ) {
282 $this->mConvTable = [];
288 $manLevel = $this->mConverter->mManualLevel;
291 foreach ( $this->mConverter->mVariants
as $v ) {
295 if ( !isset( $bidtable[$v] ) ) {
297 $this->mConverter->getVariantFallbacks( $v );
304 if ( isset( $bidtable[$v] ) ) {
305 foreach ( $vmarked
as $vo ) {
311 if ( $manLevel[$v] ==
'bidirectional' ) {
312 $this->mConvTable[$v][$bidtable[$vo]] = $bidtable[$v];
314 if ( $manLevel[$vo] ==
'bidirectional' ) {
315 $this->mConvTable[$vo][$bidtable[$v]] = $bidtable[$vo];
321 if ( ( $manLevel[$v] ==
'bidirectional' || $manLevel[$v] ==
'unidirectional' )
322 && isset( $unidtable[$v] )
324 if ( isset( $this->mConvTable[$v] ) ) {
325 $this->mConvTable[$v] = $unidtable[$v] + $this->mConvTable[$v];
327 $this->mConvTable[$v] = $unidtable[$v];
337 public function parse( $variant =
null ) {
339 $variant = $this->mConverter->getPreferredVariant();
347 if ( $this->mVariantFlags ) {
349 if ( isset( $this->mVariantFlags[$variant] ) ) {
351 $this->mRules = $this->mConverter->autoConvert( $this->mRules,
357 $this->mConverter->getVariantFallbacks( $variant );
358 if ( is_array( $variantFallbacks ) ) {
359 foreach ( $variantFallbacks
as $variantFallback ) {
361 if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
364 $this->mConverter->autoConvert( $this->mRules,
371 $this->mFlags = $flags = [
'R' =>
true ];
374 if ( !isset( $flags[
'R'] ) && !isset( $flags[
'N'] ) ) {
376 $this->mRules = str_replace(
'=>',
'=>', $this->mRules );
381 if ( !$this->mBidtable && !$this->mUnidtable ) {
382 if ( isset( $flags[
'+'] ) || isset( $flags[
'-'] ) ) {
384 if ( $rules !==
'' ) {
385 foreach ( $this->mConverter->mVariants
as $v ) {
386 $this->mBidtable[$v] = $rules;
389 } elseif ( !isset( $flags[
'N'] ) && !isset( $flags[
'T'] ) ) {
390 $this->mFlags = $flags = [
'R' =>
true ];
394 $this->mRuleDisplay =
false;
395 foreach ( $flags
as $flag => $unused ) {
399 $this->mRuleDisplay = $rules;
403 $ruleVar = trim( $rules );
404 $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar] ??
'';
412 $this->mRuleDisplay =
'';
415 $this->mRulesAction =
'remove';
416 $this->mRuleDisplay =
'';
419 $this->mRulesAction =
'add';
420 $this->mRuleDisplay =
'';
427 $this->mRuleDisplay =
'';
433 if ( $this->mRuleDisplay ===
false ) {
434 $this->mRuleDisplay =
'<span class="error">'
435 .
wfMessage(
'converter-manual-rule-error' )->inContentLanguage()->escaped()
447 return $this->mRules !==
'';