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();
95 $f = StringUtils::explode(
';', substr( $text, 0, $sepPos ) );
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];
270 private function getRuleConvertedTitle( $variant ) {
271 if ( $variant === $this->mConverter->getMainCode() ) {
280 if ( array_key_exists( $variant, $this->mUnidtable ) ) {
281 $disp = array_values( $this->mUnidtable[$variant] )[0];
287 return $this->getRuleConvertedStr( $variant );
293 private function generateConvTable() {
295 if ( !$this->mBidtable && !$this->mUnidtable ) {
296 $this->mConvTable = [];
302 $manLevel = $this->mConverter->getManualLevel();
305 foreach ( $this->mConverter->getVariants() as $v ) {
309 if ( !isset( $bidtable[$v] ) ) {
311 $this->mConverter->getVariantFallbacks( $v );
318 if ( isset( $bidtable[$v] ) ) {
319 foreach ( $vmarked as $vo ) {
325 if ( $manLevel[$v] ===
'bidirectional' ) {
326 $this->mConvTable[$v][$bidtable[$vo]] = $bidtable[$v];
328 if ( $manLevel[$vo] ===
'bidirectional' ) {
329 $this->mConvTable[$vo][$bidtable[$v]] = $bidtable[$vo];
335 if ( ( $manLevel[$v] ===
'bidirectional' || $manLevel[$v] ===
'unidirectional' )
336 && isset( $unidtable[$v] )
338 if ( isset( $this->mConvTable[$v] ) ) {
339 $this->mConvTable[$v] = $unidtable[$v] + $this->mConvTable[$v];
341 $this->mConvTable[$v] = $unidtable[$v];
351 public function parse( $variant =
null ) {
353 $variant = $this->mConverter->getPreferredVariant();
361 if ( $this->mVariantFlags ) {
363 if ( isset( $this->mVariantFlags[$variant] ) ) {
365 $this->mRules = $this->mConverter->autoConvert( $this->mRules,
371 $this->mConverter->getVariantFallbacks( $variant );
372 if ( is_array( $variantFallbacks ) ) {
373 foreach ( $variantFallbacks as $variantFallback ) {
375 if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
378 $this->mConverter->autoConvert( $this->mRules,
385 $this->mFlags = $flags = [
'R' =>
true ];
388 if ( !isset( $flags[
'R'] ) && !isset( $flags[
'N'] ) ) {
390 $this->mRules = str_replace(
'=>',
'=>', $this->mRules );
395 if ( !$this->mBidtable && !$this->mUnidtable ) {
396 if ( isset( $flags[
'+'] ) || isset( $flags[
'-'] ) ) {
398 if ( $rules !==
'' ) {
399 foreach ( $this->mConverter->getVariants() as $v ) {
400 $this->mBidtable[$v] = $rules;
403 } elseif ( !isset( $flags[
'N'] ) && !isset( $flags[
'T'] ) ) {
404 $this->mFlags = $flags = [
'R' =>
true ];
408 $this->mRuleDisplay =
false;
409 foreach ( $flags as $flag => $unused ) {
413 $this->mRuleDisplay = $rules;
417 $ruleVar = trim( $rules );
418 $this->mRuleDisplay = $this->mConverter->getVariantNames()[$ruleVar] ??
'';
422 $this->mRuleDisplay = $this->getRulesDesc();
426 $this->mRuleDisplay =
'';
429 $this->mRulesAction =
'remove';
430 $this->mRuleDisplay =
'';
433 $this->mRulesAction =
'add';
434 $this->mRuleDisplay =
'';
437 $this->mRuleDisplay = $this->getRuleConvertedStr( $variant );
440 $this->mRuleTitle = $this->getRuleConvertedTitle( $variant );
441 $this->mRuleDisplay =
'';
447 if ( $this->mRuleDisplay ===
false ) {
448 $this->mRuleDisplay =
'<span class="error">'
449 .
wfMessage(
'converter-manual-rule-error' )->inContentLanguage()->escaped()
453 $this->generateConvTable();
461 return $this->mRules !==
'';