LanguageConverter fix of empty and numeric strings

Bug: T51072
Bug: T48634
Bug: T53551
Change-Id: I2c88f1cf7c0014bebf5c798916b660b334a0b78b
This commit is contained in:
Liangent 2014-05-06 16:16:12 +00:00
parent cf08f986af
commit ca38682dda
4 changed files with 75 additions and 20 deletions

View file

@ -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;
}

View file

@ -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 );

View file

@ -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;
}
}

View file

@ -18293,6 +18293,61 @@ Raw: -{R|zh:China;zh-tw:Taiwan}-
</p>
!! 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
<p>0
</p>
!! 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
<p>D12345EE12345
</p>
!! 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
<p>foobar
</p>
!! 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
<p>foobar
</p>
!! 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
<p>foobar
</p>
!! end
!! test
Nested using of manual convert syntax
!! options