48 $this->mConverter = $converter;
58 $variants = (
array)$variants;
62 foreach ( $variants
as $variant ) {
63 if ( isset( $this->mBidtable[$variant] ) ) {
64 return $this->mBidtable[$variant];
79 $sepPos = strpos( $text,
'|' );
80 if ( $sepPos !==
false ) {
81 $validFlags = $this->mConverter->mFlags;
83 foreach ( $f
as $ff ) {
85 if ( isset( $validFlags[$ff] ) ) {
86 $flags[$validFlags[$ff]] =
true;
89 $text = strval( substr( $text, $sepPos + 1 ) );
94 } elseif ( isset(
$flags[
'R'] ) ) {
96 } elseif ( isset(
$flags[
'N'] ) ) {
98 } elseif ( isset(
$flags[
'-'] ) ) {
102 } elseif ( isset(
$flags[
'H'] ) ) {
104 $temp = [
'+' =>
true,
'H' =>
true ];
105 if ( isset(
$flags[
'T'] ) ) {
108 if ( isset(
$flags[
'D'] ) ) {
113 if ( isset(
$flags[
'A'] ) ) {
117 if ( isset(
$flags[
'D'] ) ) {
122 $variantFlags = array_intersect( array_keys(
$flags ), $this->mConverter->mVariants );
123 if ( $variantFlags ) {
124 $variantFlags = array_flip( $variantFlags );
128 $this->mVariantFlags = $variantFlags;
129 $this->mRules = $text;
141 $variants = $this->mConverter->mVariants;
142 $varsep_pattern = $this->mConverter->getVarSeparatorPattern();
145 $rules = preg_replace(
'/(&[#a-zA-Z0-9]+);/',
"$1\x01", $rules );
146 $choice = preg_split( $varsep_pattern, $rules );
147 $choice = str_replace(
"\x01",
';', $choice );
149 foreach ( $choice
as $c ) {
150 $v = explode(
':', $c, 2 );
151 if (
count( $v ) != 2 ) {
157 $u = explode(
'=>', $v, 2 );
160 if (
count( $u ) == 1 && $to !==
'' && in_array( $v, $variants ) ) {
162 } elseif (
count( $u ) == 2 ) {
163 $from = trim( $u[0] );
166 if ( array_key_exists( $v, $unidtable )
167 && !is_array( $unidtable[$v] )
169 && in_array( $v, $variants ) ) {
170 $unidtable[$v] = [ $from => $to ];
171 } elseif ( $from !==
'' && in_array( $v, $variants ) ) {
172 $unidtable[$v][$from] = $to;
176 if ( !isset( $this->mConverter->mVariantNames[$v] ) ) {
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,
376 $this->mRules = str_replace(
'=>',
'=>', $this->mRules );
381 if ( !$this->mBidtable && !$this->mUnidtable ) {
384 if ( $rules !==
'' ) {
385 foreach ( $this->mConverter->mVariants
as $v ) {
386 $this->mBidtable[$v] = $rules;
389 } elseif ( !isset(
$flags[
'N'] ) && !isset(
$flags[
'T'] ) ) {
394 $this->mRuleDisplay =
false;
395 foreach (
$flags as $flag => $unused ) {
399 $this->mRuleDisplay = $rules;
403 $ruleVar = trim( $rules );
404 if ( isset( $this->mConverter->mVariantNames[$ruleVar] ) ) {
405 $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar];
407 $this->mRuleDisplay =
'';
416 $this->mRuleDisplay =
'';
419 $this->mRulesAction =
'remove';
420 $this->mRuleDisplay =
'';
423 $this->mRulesAction =
'add';
424 $this->mRuleDisplay =
'';
431 $this->mRuleDisplay =
'';
437 if ( $this->mRuleDisplay ===
false ) {
438 $this->mRuleDisplay =
'<span class="error">'
439 .
wfMessage(
'converter-manual-rule-error' )->inContentLanguage()->escaped()
451 return $this->mRules !==
'';