1. Add a new feature to LanguageConverter, for supporting nested using of manual...
authorPhilip Tzou <philip@users.mediawiki.org>
Tue, 12 Jan 2010 20:54:26 +0000 (20:54 +0000)
committerPhilip Tzou <philip@users.mediawiki.org>
Tue, 12 Jan 2010 20:54:26 +0000 (20:54 +0000)
2. Fixed a little bug find in getURLVariant().

RELEASE-NOTES
languages/LanguageConverter.php
maintenance/parserTests.txt

index 91befc1..77a45e1 100644 (file)
@@ -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 ===
 
index 8666495..8d253e1 100644 (file)
@@ -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;
        }
 
index 6e2ca10..b8f44d4 100644 (file)
@@ -6961,6 +6961,17 @@ Raw: -{R|zh:China;zh-tw:Taiwan}-
 </p>
 !! 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
+<p>Nested: Hello Hong Kong!
+</p>
+!! end
+
 !! test
 Do not convert roman numbers to language variants
 !! options