follow up r60763
authorMark A. Hershberger <mah@users.mediawiki.org>
Fri, 8 Jan 2010 08:22:19 +0000 (08:22 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Fri, 8 Jan 2010 08:22:19 +0000 (08:22 +0000)
Recover the -{T| }- rule.  Add the ability to test for it to the parserTests and add a test for it.  Add a couple of disabled tests that I think demonstrate bugs in the LanguageTranslator

includes/parser/Parser.php
languages/Language.php
languages/LanguageConverter.php
maintenance/parserTests.inc
maintenance/parserTests.txt

index dad1cca..f1ef4fb 100644 (file)
@@ -261,6 +261,12 @@ class Parser
                if ( !$t || $t instanceof FakeTitle ) {
                        $t = Title::newFromText( 'NO TITLE' );
                }
+               // If we still don't have a Title object, make sure we can
+               // convert whatever we've been passed to a string.
+               if ( !$t instanceOf Title && is_string( "$t" ) ) {
+                       $t = Title::newFromText( "$t" );
+               }
+
                if ( strval( $t->getFragment() ) !== '' ) {
                        # Strip the fragment to avoid various odd effects
                        $this->mTitle = clone $t;
@@ -329,14 +335,6 @@ class Parser
                # No more strip!
                wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
                $text = $this->internalParse( $text );
-               // internalParse took care of the notitleconvert bit, so title conversion is here.
-               if ( $this->mDoTitleConvert && !$this->mTitle->isConversionTable()) {
-                       $converted = $wgContLang->convert( $title );
-                       if ( !$converted instanceOf Title ) {
-                               $converted = Title::newFromText( $converted );
-                       }
-                       $this->setTitle( $converted );
-               }
 
                $text = $this->mStripState->unstripGeneral( $text );
 
@@ -355,13 +353,23 @@ class Parser
 
                $this->replaceLinkHolders( $text );
 
-               # the position of the convert() call should not be changed. it
-               # assumes that the links are all replaced and the only thing left
-               # is the <nowiki> mark.
+               // The position of the convert() call should not be changed. it
+               // assumes that the links are all replaced and the only thing left
+               // is the <nowiki> mark.
                if ( $this->mDoContentConvert && !$this->mTitle->isConversionTable()) {
                        $text = $wgContLang->convert( $text );
                }
 
+               // A title may have been set in a conversion rule.
+               // Note that if a user tries to set a title in a conversion
+               // rule but content conversion was not done, then the parser
+               // won't pick it up.  This is probably expected behavior.
+               if ( $wgContLang->getConvRuleTitle() ) {
+                       $this->setTitle( $wgContLang->getConvRuleTitle() );
+               } elseif ( $this->mDoTitleConvert && !$this->mTitle->isConversionTable() ) {
+                       $this->setTitle( $wgContLang->convert( $title ) );
+               }
+
                $text = $this->mStripState->unstripNoWiki( $text );
 
                wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
index 6aa9c9b..72b694f 100644 (file)
@@ -34,11 +34,12 @@ if( function_exists( 'mb_strtoupper' ) ) {
  */
 class FakeConverter {
        var $mLang;
+       var $mConvRuleTitle;
        function FakeConverter($langobj) {$this->mLang = $langobj;}
        function autoConvertToAllVariants($text) {return $text;}
        function convert($t, $i) {return $t;}
        function getVariants() { return array( $this->mLang->getCode() ); }
-       function getPreferredVariant() {return $this->mLang->getCode(); }
+       function getPreferredVariant() { return $this->mLang->getCode(); }
        function findVariantLink(&$l, &$n, $ignoreOtherCond = false) {}
        function getExtraHashOptions() {return '';}
        function getParsedTitle() {return '';}
@@ -2664,4 +2665,11 @@ class Language {
                $text = $this->getMessageFromDB( $msg );
                return str_replace( '$1', $this->formatNum( $size ), $text );
        }
+
+       /**
+        * Get the conversion rule title, if any.
+        */
+       function getConvRuleTitle() {
+               return $this->mConverter->mConvRuleTitle;
+       }
 }
index 7bd487d..113c470 100644 (file)
@@ -31,6 +31,7 @@ class LanguageConverter {
        var $mDescCodeSep = ':', $mDescVarSep = ';';
        var $mUcfirst = false;
        var $mHeaderVariant;
+       var $mConvRuleTitle = false;
 
        const CACHE_VERSION_KEY = 'VERSION 6';
 
@@ -459,8 +460,8 @@ class LanguageConverter {
         */
        function applyManualConv( $convRule ) {
                // use syntax -{T|zh:TitleZh;zh-tw:TitleTw}- for custom
-        // conversion in title
-               $title = $convRule->getTitle();
+               // conversion in title
+               $this->mConvRuleTitle = $convRule->getTitle();
 
                // apply manual conversion table to global table
                $convTable = $convRule->getConvTable();
@@ -544,7 +545,7 @@ class LanguageConverter {
         * @return string converted text
         * @public
         */
-       function convert( $text, $isTitle ) {
+       function convert( $text ) {
                global $wgDisableLangConversion;
                if ( $wgDisableLangConversion ) return $text;
 
@@ -558,10 +559,6 @@ class LanguageConverter {
 
                // Remove the last delimiter (wasn't real)
            $converted = substr( $converted, 0, - strlen( $this->mMarkup['end'] ) );
-               if ( $isTitle ) {
-                       error_log("title2: $converted\n");
-                       $this->mConvertedTitle = $converted;
-               }
                return $converted;
        }
 
@@ -1110,13 +1107,13 @@ class ConverterRule {
         * Parse rules conversion.
         * @private
         */
-       function getRuleConvertedStr( $variant, $doConvert ) {
+       function getRuleConvertedStr( $variant ) {
                $bidtable = $this->mBidtable;
                $unidtable = $this->mUnidtable;
 
                if ( count( $bidtable ) + count( $unidtable ) == 0 ) {
                        return $this->mRules;
-               } elseif ( $doConvert ) { // the text converted
+               } else {
                        // display current variant in bidirectional array
                        $disp = $this->getTextInBidtable( $variant );
                        // or display current variant in fallbacks
@@ -1142,8 +1139,6 @@ class ConverterRule {
                                }
                        }
                        return $disp;
-               } else { // no convert
-                       return $this->mRules;
                }
        }
 
@@ -1271,15 +1266,13 @@ class ConverterRule {
                        // proces H,- flag or T only: output nothing
                        $this->mRuleDisplay = '';
                } elseif ( in_array( 'S', $flags ) ) {
-                       // true hard-coded now since we shouldn't be called if we're not converting
-                       $this->mRuleDisplay = $this->getRuleConvertedStr( $variant, true );
+                       $this->mRuleDisplay = $this->getRuleConvertedStr( $variant );
                } else {
                        $this->mRuleDisplay = $this->mManualCodeError;
                }
                // process T flag
                if ( in_array( 'T', $flags ) ) {
-                       // true hard-coded now since we shouldn't be called if we're not converting
-                       $this->mRuleTitle = $this->getRuleConvertedStr( $variant, true );
+                       $this->mRuleTitle = $this->getRuleConvertedStr( $variant );
                }
 
                if ( in_array( '-', $flags ) ) {
index b59e3f5..bf4f00f 100644 (file)
@@ -493,6 +493,10 @@ class ParserTest {
                        $result = $this->tidy($result);
                }
 
+               if ( isset( $opts['showtitle'] ) ) {
+                       $out = $parser->mTitle . "\n$out";
+        }
+
                $this->teardownGlobals();
 
                if( $result === $out && ( $noxml === true || $this->wellFormed( $out ) ) ) {
index 68835ba..5dd0ef6 100644 (file)
@@ -6929,15 +6929,15 @@ Taiwan is Taiwan.
 !! test
 Adding explicit conversion rule for title (T flag)
 !! options
-language=zh variant=zh-tw
+language=zh variant=zh-tw showtitle
 !! input
 Should be stripped-{T|zh:China;zh-tw:Taiwan}-!
 !! result
+Taiwan
 <p>Should be stripped!
 </p>
 !! end
 
-
 !! test
 Raw output of variant escape tags (R flag)
 !! options
@@ -6949,7 +6949,6 @@ Raw: -{R|zh:China;zh-tw:Taiwan}-
 </p>
 !! end
 
-
 !! test
 Do not convert roman numbers to language variants
 !! options
@@ -7515,6 +7514,47 @@ title=[[Main Page]]
 <a href="/wiki/Main_Page#section" title="Main Page">#section</a>
 !! end
 
+!! test
+Adding explicit conversion rule for title (T flag)
+!! options
+language=zh variant=zh-tw showtitle **** disabled (for now) because I think it uncovers a bug and I want a reminder
+!! input
+Should be stripped-{T|zh:China;zh-tw:Taiwan}-!
+!! result
+Taiwan
+<p>Should be stripped!
+</p>
+!! end
+
+!! test
+Adding explicit conversion rule for title (T flag)
+!! options
+language=zh variant=zh showtitle **** disabled (for now) because I think it uncovers a bug and I want a reminder
+!! input
+Should be stripped-{T|zh:China;zh-tw:Taiwan}-!
+!! result
+China
+<p>Should be stripped!
+</p>
+!! end
+
+!! test
+Explicit session-wise language variant mapping (A flag and - flag)
+!! options
+language=zh variant=zh **** disabled (for now) because I think it uncovers a bug and I want a reminder
+!! input
+Taiwan is not China.
+But -{zh:China;zh-tw:Taiwan}- is China,
+(This-{-|zh:China;zh-tw:Taiwan}- should be stripped!)
+and -{China}- is China.
+!! result
+<p>Taiwan is not China.
+But Taiwan is Taiwan,
+(This should be stripped!)
+and China is China.
+</p>
+!! end
+
 TODO:
 more images
 more tables