From: Philip Tzou
Date: Tue, 12 Jan 2010 20:54:26 +0000 (+0000)
Subject: 1. Add a new feature to LanguageConverter, for supporting nested using of manual...
X-Git-Tag: 1.31.0-rc.0~38302
X-Git-Url: http://git.cyclocoop.org/data/Fool?a=commitdiff_plain;h=28ca5b8bb69421088ae60d79893c2fb84d0e2613;p=lhc%2Fweb%2Fwiklou.git
1. Add a new feature to LanguageConverter, for supporting nested using of manual convert syntax like "-{-{}-}-".
2. Fixed a little bug find in getURLVariant().
---
diff --git a/RELEASE-NOTES b/RELEASE-NOTES
index 91befc1027..77a45e157b 100644
--- a/RELEASE-NOTES
+++ b/RELEASE-NOTES
@@ -295,6 +295,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
* (bug 22051) Returing false in SpecialContributionsBeforeMainOutput hook now
stops normal output
* Send new password e-mail in users preference language
+* LanguageConverter now support nested using of manual convert syntax like "-{-{}-}-"
=== Bug fixes in 1.16 ===
diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php
index 8666495bdd..8d253e1c5b 100644
--- a/languages/LanguageConverter.php
+++ b/languages/LanguageConverter.php
@@ -192,7 +192,7 @@ class LanguageConverter {
// see if the preference is set in the request
$ret = $wgRequest->getText( 'variant' );
- if ( $ret ) {
+ if ( !$ret ) {
$ret = $wgRequest->getVal( 'uselang' );
}
@@ -539,28 +539,69 @@ class LanguageConverter {
}
/**
- * Convert a text fragment.
+ * Convert a text array.
*
- * @param string $text text to be converted
+ * @param string $tarray text array to be converted
* @param string $plang preferred variant
* @return string converted text
* @private
*/
- function convertFragment( $text, $plang ) {
- $marked = explode( $this->mMarkup['begin'], $text, 2 );
+ function convertArray( $tarray, $plang ) {
+ $beginlen = strlen( $this->mMarkup['begin'] );
$converted = '';
+ $middle = '';
+
+ foreach ( $tarray as $text ) {
+ // for nested use
+ if( $middle ) {
+ $text = $middle . $text;
+ $middle = '';
+ }
- $converted .= $this->autoConvert( $marked[0], $plang );
+ // find first and last begin markup(s)
+ $firstbegin = strpos( $text, $this->mMarkup['begin'] );
+ $lastbegin = strrpos( $text, $this->mMarkup['begin'] );
+
+ // if $text contains no begin markup,
+ // append $text restore end markup to $converted
+ if( $firstbegin === false ) {
+ $converted .= $text;
+ $converted .= $this->mMarkup['end'];
+ continue;
+ }
- if ( array_key_exists( 1, $marked ) ) {
- $crule = new ConverterRule( $marked[1], $this );
+ // split $text into $left and $right,
+ // omit the begin markup in $right
+ $left = substr( $text, 0, $firstbegin );
+ $right = substr( $text, $lastbegin + $beginlen );
+
+ // always convert $left and append it to $converted
+ // for nested case, $left is blank but can also be converted
+ $converted .= $this->autoConvert( $left, $plang );
+
+ // parse and apply manual rule from $right
+ $crule = new ConverterRule( $right, $this );
$crule->parse( $plang );
- $converted .= $crule->getDisplay();
+ $right = $crule->getDisplay();
$this->applyManualConv( $crule );
- } else {
- $converted .= $this->mMarkup['end'];
- }
+ // if $text contains only one begin markup,
+ // append $left and $right to $converted.
+ //
+ // otherwise it's a nested use like "-{-{}-}-",
+ // this should be handled properly.
+ if( $firstbegin === $lastbegin ) {
+ $converted .= $right;
+ }
+ else {
+ // not omit the first begin markup
+ $middle = substr( $text, $firstbegin, $lastbegin - $firstbegin );
+ $middle .= $right;
+ //print $middle;
+ }
+ }
+ // Remove the last delimiter (wasn't real)
+ $converted = substr( $converted, 0, - strlen( $this->mMarkup['end'] ) );
return $converted;
}
@@ -586,14 +627,8 @@ class LanguageConverter {
$plang = $this->getPreferredVariant();
$tarray = StringUtils::explode( $this->mMarkup['end'], $text );
- $converted = '';
+ $converted = $this->convertArray( $tarray, $plang );
- foreach ( $tarray as $txt ) {
- $converted .= $this->convertFragment( $txt, $plang );
- }
-
- // Remove the last delimiter (wasn't real)
- $converted = substr( $converted, 0, - strlen( $this->mMarkup['end'] ) );
return $converted;
}
diff --git a/maintenance/parserTests.txt b/maintenance/parserTests.txt
index 6e2ca10d6b..b8f44d45a9 100644
--- a/maintenance/parserTests.txt
+++ b/maintenance/parserTests.txt
@@ -6961,6 +6961,17 @@ Raw: -{R|zh:China;zh-tw:Taiwan}-
!! end
+!! test
+Nested using of manual convert syntax
+!! options
+language=zh variant=zh-hk
+!! input
+Nested: -{zh-hans:Hi -{zh-cn:China;zh-sg:Singapore;}-;zh-hant:Hello -{zh-tw:Taiwan;zh-hk:H-{ong}- K-{}-ong;}-;}-!
+!! result
+Nested: Hello Hong Kong!
+
+!! end
+
!! test
Do not convert roman numbers to language variants
!! options