From ca38682dda7348bde8f4ad0ef64c782c7c928427 Mon Sep 17 00:00:00 2001 From: Liangent Date: Tue, 6 May 2014 16:16:12 +0000 Subject: [PATCH] LanguageConverter fix of empty and numeric strings Bug: T51072 Bug: T48634 Bug: T53551 Change-Id: I2c88f1cf7c0014bebf5c798916b660b334a0b78b --- includes/libs/ReplacementArray.php | 4 +-- languages/ConverterRule.php | 26 ++++++++------ languages/LanguageConverter.php | 10 ++---- tests/parser/parserTests.txt | 55 ++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 20 deletions(-) diff --git a/includes/libs/ReplacementArray.php b/includes/libs/ReplacementArray.php index 7fdb309383..185914ceb0 100644 --- a/includes/libs/ReplacementArray.php +++ b/includes/libs/ReplacementArray.php @@ -76,7 +76,7 @@ class ReplacementArray { * @param array $data */ public function mergeArray( $data ) { - $this->data = array_merge( $this->data, $data ); + $this->data = $data + $this->data; $this->fss = false; } @@ -84,7 +84,7 @@ class ReplacementArray { * @param ReplacementArray $other */ public function merge( ReplacementArray $other ) { - $this->data = array_merge( $this->data, $other->data ); + $this->data = $other->data + $this->data; $this->fss = false; } diff --git a/languages/ConverterRule.php b/languages/ConverterRule.php index e6625c1a2a..e3a4f77ec4 100644 --- a/languages/ConverterRule.php +++ b/languages/ConverterRule.php @@ -155,18 +155,20 @@ class ConverterRule { $to = trim( $v[1] ); $v = trim( $v[0] ); $u = explode( '=>', $v, 2 ); - // if $to is empty, strtr() could return a wrong result - if ( count( $u ) == 1 && $to && in_array( $v, $variants ) ) { + // if $to is empty (which is also used as $from in bidtable), + // strtr() could return a wrong result. + if ( count( $u ) == 1 && $to !== '' && in_array( $v, $variants ) ) { $bidtable[$v] = $to; } elseif ( count( $u ) == 2 ) { $from = trim( $u[0] ); $v = trim( $u[1] ); + // if $from is empty, strtr() could return a wrong result. if ( array_key_exists( $v, $unidtable ) && !is_array( $unidtable[$v] ) - && $to + && $from !== '' && in_array( $v, $variants ) ) { $unidtable[$v] = array( $from => $to ); - } elseif ( $to && in_array( $v, $variants ) ) { + } elseif ( $from !== '' && in_array( $v, $variants ) ) { $unidtable[$v][$from] = $to; } } @@ -220,17 +222,17 @@ class ConverterRule { // display current variant in bidirectional array $disp = $this->getTextInBidtable( $variant ); // or display current variant in fallbacks - if ( !$disp ) { + if ( $disp === false ) { $disp = $this->getTextInBidtable( $this->mConverter->getVariantFallbacks( $variant ) ); } // or display current variant in unidirectional array - if ( !$disp && array_key_exists( $variant, $unidtable ) ) { + if ( $disp === false && array_key_exists( $variant, $unidtable ) ) { $disp = array_values( $unidtable[$variant] ); $disp = $disp[0]; } // or display frist text under disable manual convert - if ( !$disp && $this->mConverter->mManualLevel[$variant] == 'disable' ) { + if ( $disp === false && $this->mConverter->mManualLevel[$variant] == 'disable' ) { if ( count( $bidtable ) > 0 ) { $disp = array_values( $bidtable ); $disp = $disp[0]; @@ -325,7 +327,7 @@ class ConverterRule { && isset( $unidtable[$v] ) ) { if ( isset( $this->mConvTable[$v] ) ) { - $this->mConvTable[$v] = array_merge( $this->mConvTable[$v], $unidtable[$v] ); + $this->mConvTable[$v] = $unidtable[$v] + $this->mConvTable[$v]; } else { $this->mConvTable[$v] = $unidtable[$v]; } @@ -383,9 +385,11 @@ class ConverterRule { if ( !$this->mBidtable && !$this->mUnidtable ) { if ( isset( $flags['+'] ) || isset( $flags['-'] ) ) { - // fill all variants if text in -{A/H/-|text} without rules - foreach ( $this->mConverter->mVariants as $v ) { - $this->mBidtable[$v] = $rules; + // fill all variants if text in -{A/H/-|text}- is non-empty but without rules + if ( $rules !== '' ) { + foreach ( $this->mConverter->mVariants as $v ) { + $this->mBidtable[$v] = $rules; + } } } elseif ( !isset( $flags['N'] ) && !isset( $flags['T'] ) ) { $this->mFlags = $flags = array( 'R' => true ); diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php index a6b687cfae..787930907e 100644 --- a/languages/LanguageConverter.php +++ b/languages/LanguageConverter.php @@ -502,13 +502,9 @@ class LanguageConverter { } if ( $action == 'add' ) { + // More efficient than array_merge(), about 2.5 times. foreach ( $pair as $from => $to ) { - // to ensure that $from and $to not be left blank - // so $this->translate() could always return a string - if ( $from || $to ) { - // more efficient than array_merge(), about 2.5 times. - $this->mTables[$variant]->setPair( $from, $to ); - } + $this->mTables[$variant]->setPair( $from, $to ); } } elseif ( $action == 'remove' ) { $this->mTables[$variant]->removeArray( $pair ); @@ -996,7 +992,7 @@ class LanguageConverter { if ( $recursive ) { foreach ( $sublinks as $link ) { $s = $this->parseCachedTable( $code, $link, $recursive ); - $ret = array_merge( $ret, $s ); + $ret = $s + $ret; } } diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index e96535292b..18d9aa85e0 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -18293,6 +18293,61 @@ Raw: -{R|zh:China;zh-tw:Taiwan}-

!! end +!! test +Strings evaluating false shouldn't be ignored by Language converter (T51072) +!! options +language=zh variant=zh-cn +!! input +-{zh-cn:0;zh-sg:1;zh-tw:2;zh-hk:3}- +!! result +

0 +

+!! end + +!! test +Conversion rules from [numeric-only string] to [something else] (T48634) +!! options +language=zh variant=zh-cn +!! input +-{H|0=>zh-cn:B}--{H|0=>zh-cn:C;0=>zh-cn:D}--{H|0=>zh-hans:A}-012345-{A|zh-tw:0;zh-cn:E;}-012345 +!! result +

D12345EE12345 +

+!! end + +!! test +Bidirectional converter rule entries with an empty value should be ignored (T53551) +!! options +language=zh variant=zh-cn +!! input +-{H|zh-cn:foo;zh-tw:;}-foobar +!! result +

foobar +

+!! end + +!! test +Unidirectional converter rule entries with an empty "from" string should be ignored (T53551) +!! options +language=zh variant=zh-cn +!! input +-{H|=>zh-cn:foo;}-foobar +!! result +

foobar +

+!! end + +!! test +Empty converter rule entries shouldn't be inserted into the conversion table (T53551) +!! options +language=zh variant=zh-cn +!! input +-{H|}-foobar +!! result +

foobar +

+!! end + !! test Nested using of manual convert syntax !! options -- 2.20.1