62 $this->mConverter = $converter;
72 $variants = (array)$variants;
76 foreach ( $variants as $variant ) {
77 if ( isset( $this->mBidtable[$variant] ) ) {
78 return $this->mBidtable[$variant];
87 private function parseFlags() {
92 $sepPos = strpos( $text,
'|' );
93 if ( $sepPos !==
false ) {
94 $validFlags = $this->mConverter->getFlags();
96 foreach ( $f as $ff ) {
98 if ( isset( $validFlags[$ff] ) ) {
99 $flags[$validFlags[$ff]] =
true;
102 $text = strval( substr( $text, $sepPos + 1 ) );
107 } elseif ( isset( $flags[
'R'] ) ) {
109 $flags = [
'R' =>
true ];
110 } elseif ( isset( $flags[
'N'] ) ) {
112 $flags = [
'N' =>
true ];
113 } elseif ( isset( $flags[
'-'] ) ) {
115 $flags = [
'-' =>
true ];
116 } elseif ( count( $flags ) === 1 && isset( $flags[
'T'] ) ) {
118 } elseif ( isset( $flags[
'H'] ) ) {
120 $temp = [
'+' =>
true,
'H' =>
true ];
121 if ( isset( $flags[
'T'] ) ) {
124 if ( isset( $flags[
'D'] ) ) {
129 if ( isset( $flags[
'A'] ) ) {
133 if ( isset( $flags[
'D'] ) ) {
134 unset( $flags[
'S'] );
138 $variantFlags = array_intersect( array_keys( $flags ), $this->mConverter->getVariants() );
139 if ( $variantFlags ) {
140 $variantFlags = array_fill_keys( $variantFlags,
true );
144 $this->mVariantFlags = $variantFlags;
145 $this->mRules = $text;
146 $this->mFlags = $flags;
152 private function parseRules() {
156 $varsep_pattern = $this->mConverter->getVarSeparatorPattern();
159 $rules = preg_replace(
'/(&[#a-zA-Z0-9]+);/',
"$1\x01", $rules );
160 $choice = preg_split( $varsep_pattern, $rules );
161 $choice = str_replace(
"\x01",
';', $choice );
163 foreach ( $choice as $c ) {
164 $v = explode(
':', $c, 2 );
165 if ( count( $v ) !== 2 ) {
171 $u = explode(
'=>', $v, 2 );
172 $vv = $this->mConverter->validateVariant( $v );
175 if ( count( $u ) === 1 && $to !==
'' && $vv ) {
176 $bidtable[$vv] = $to;
177 } elseif ( count( $u ) === 2 ) {
178 $from = trim( $u[0] );
180 $vv = $this->mConverter->validateVariant( $v );
182 if ( array_key_exists( $vv, $unidtable )
183 && !is_array( $unidtable[$vv] )
186 $unidtable[$vv] = [ $from => $to ];
187 } elseif ( $from !==
'' && $vv ) {
188 $unidtable[$vv][$from] = $to;
192 if ( !isset( $this->mConverter->getVariantNames()[$vv] ) ) {
198 $this->mBidtable = $bidtable;
199 $this->mUnidtable = $unidtable;
205 private function getRulesDesc() {
206 $codesep = $this->mConverter->getDescCodeSeparator();
207 $varsep = $this->mConverter->getDescVarSeparator();
209 foreach ( $this->mBidtable as $k => $v ) {
210 $text .= $this->mConverter->getVariantNames()[$k] .
"$codesep$v$varsep";
212 foreach ( $this->mUnidtable as $k => $a ) {
213 foreach ( $a as $from => $to ) {
214 $text .= $from .
'⇒' . $this->mConverter->getVariantNames()[$k] .
215 "$codesep$to$varsep";
228 private function getRuleConvertedStr( $variant ) {
232 if ( count( $bidtable ) + count( $unidtable ) === 0 ) {
239 if ( $disp ===
false ) {
241 $this->mConverter->getVariantFallbacks( $variant ) );
244 if ( $disp ===
false && array_key_exists( $variant, $unidtable ) ) {
245 $disp = array_values( $unidtable[$variant] )[0];
248 if ( $disp ===
false && $this->mConverter->getManualLevel()[$variant] ===
'disable' ) {
249 if ( count( $bidtable ) > 0 ) {
250 $disp = array_values( $bidtable )[0];
252 $disp = array_values( array_values( $unidtable )[0] )[0];
269 private function getRuleConvertedTitle( $variant ) {
270 if ( $variant === $this->mConverter->getMainCode() ) {
279 if ( array_key_exists( $variant, $this->mUnidtable ) ) {
280 $disp = array_values( $this->mUnidtable[$variant] )[0];
286 return $this->getRuleConvertedStr( $variant );
292 private function generateConvTable() {
294 if ( !$this->mBidtable && !$this->mUnidtable ) {
295 $this->mConvTable = [];
301 $manLevel = $this->mConverter->getManualLevel();
304 foreach ( $this->mConverter->getVariants() as $v ) {
308 if ( !isset( $bidtable[$v] ) ) {
310 $this->mConverter->getVariantFallbacks( $v );
317 if ( isset( $bidtable[$v] ) ) {
318 foreach ( $vmarked as $vo ) {
324 if ( $manLevel[$v] ===
'bidirectional' ) {
325 $this->mConvTable[$v][$bidtable[$vo]] = $bidtable[$v];
327 if ( $manLevel[$vo] ===
'bidirectional' ) {
328 $this->mConvTable[$vo][$bidtable[$v]] = $bidtable[$vo];
334 if ( ( $manLevel[$v] ===
'bidirectional' || $manLevel[$v] ===
'unidirectional' )
335 && isset( $unidtable[$v] )
337 if ( isset( $this->mConvTable[$v] ) ) {
338 $this->mConvTable[$v] = $unidtable[$v] + $this->mConvTable[$v];
340 $this->mConvTable[$v] = $unidtable[$v];
350 public function parse( $variant =
null ) {
352 $variant = $this->mConverter->getPreferredVariant();
360 if ( $this->mVariantFlags ) {
362 if ( isset( $this->mVariantFlags[$variant] ) ) {
364 $this->mRules = $this->mConverter->autoConvert( $this->mRules,
370 $this->mConverter->getVariantFallbacks( $variant );
371 if ( is_array( $variantFallbacks ) ) {
372 foreach ( $variantFallbacks as $variantFallback ) {
374 if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
377 $this->mConverter->autoConvert( $this->mRules,
384 $this->mFlags = $flags = [
'R' =>
true ];
387 if ( !isset( $flags[
'R'] ) && !isset( $flags[
'N'] ) ) {
389 $this->mRules = str_replace(
'=>',
'=>', $this->mRules );
394 if ( !$this->mBidtable && !$this->mUnidtable ) {
395 if ( isset( $flags[
'+'] ) || isset( $flags[
'-'] ) ) {
397 if ( $rules !==
'' ) {
398 foreach ( $this->mConverter->getVariants() as $v ) {
399 $this->mBidtable[$v] = $rules;
402 } elseif ( !isset( $flags[
'N'] ) && !isset( $flags[
'T'] ) ) {
403 $this->mFlags = $flags = [
'R' =>
true ];
407 $this->mRuleDisplay =
false;
408 foreach ( $flags as $flag => $unused ) {
412 $this->mRuleDisplay = $rules;
416 $ruleVar = trim( $rules );
417 $this->mRuleDisplay = $this->mConverter->getVariantNames()[$ruleVar] ??
'';
421 $this->mRuleDisplay = $this->getRulesDesc();
425 $this->mRuleDisplay =
'';
428 $this->mRulesAction =
'remove';
429 $this->mRuleDisplay =
'';
432 $this->mRulesAction =
'add';
433 $this->mRuleDisplay =
'';
436 $this->mRuleDisplay = $this->getRuleConvertedStr( $variant );
439 $this->mRuleTitle = $this->getRuleConvertedTitle( $variant );
440 $this->mRuleDisplay =
'';
446 if ( $this->mRuleDisplay ===
false ) {
447 $this->mRuleDisplay =
'<span class="error">'
448 .
wfMessage(
'converter-manual-rule-error' )->inContentLanguage()->escaped()
452 $this->generateConvTable();
460 return $this->mRules !==
'';