Make RECOVER_ORIG preserve extension tags
[lhc/web/wiklou.git] / includes / parser / Parser.php
index d2a20df..66cfd55 100644 (file)
@@ -167,7 +167,6 @@ class Parser {
        var $mLinkID;
        var $mIncludeSizes, $mPPNodeCount, $mGeneratedPPNodeCount, $mHighestExpansionDepth;
        var $mDefaultSort;
-       var $mTplExpandCache; # empty-frame expansion cache
        var $mTplRedirCache, $mTplDomCache, $mHeadings, $mDoubleUnderscores;
        var $mExpensiveFunctionCount; # number of expensive parser function calls
        var $mShowToc, $mForceTocPosition;
@@ -321,7 +320,7 @@ class Parser {
                $this->mStripState = new StripState( $this->mUniqPrefix );
 
                # Clear these on every parse, bug 4549
-               $this->mTplExpandCache = $this->mTplRedirCache = $this->mTplDomCache = array();
+               $this->mTplRedirCache = $this->mTplDomCache = array();
 
                $this->mShowToc = true;
                $this->mForceTocPosition = false;
@@ -618,13 +617,15 @@ class Parser {
        /**
         * Expand templates and variables in the text, producing valid, static wikitext.
         * Also removes comments.
+        * Do not call this function recursively.
         * @param string $text
         * @param Title $title
         * @param ParserOptions $options
         * @param int|null $revid
+        * @param bool|PPFrame $frame
         * @return mixed|string
         */
-       function preprocess( $text, Title $title = null, ParserOptions $options, $revid = null ) {
+       function preprocess( $text, Title $title = null, ParserOptions $options, $revid = null, $frame = false ) {
                wfProfileIn( __METHOD__ );
                $magicScopeVariable = $this->lock();
                $this->startParse( $title, $options, self::OT_PREPROCESS, true );
@@ -633,7 +634,7 @@ class Parser {
                }
                wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$this->mStripState ) );
                wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
-               $text = $this->replaceVariables( $text );
+               $text = $this->replaceVariables( $text, $frame );
                $text = $this->mStripState->unstripBoth( $text );
                wfProfileOut( __METHOD__ );
                return $text;
@@ -2327,13 +2328,13 @@ class Parser {
                $result = $this->closeParagraph();
 
                if ( '*' === $char ) {
-                       $result .= "<ul>\n<li>";
+                       $result .= "<ul><li>";
                } elseif ( '#' === $char ) {
-                       $result .= "<ol>\n<li>";
+                       $result .= "<ol><li>";
                } elseif ( ':' === $char ) {
-                       $result .= "<dl>\n<dd>";
+                       $result .= "<dl><dd>";
                } elseif ( ';' === $char ) {
-                       $result .= "<dl>\n<dt>";
+                       $result .= "<dl><dt>";
                        $this->mDTopen = true;
                } else {
                        $result = '<!-- ERR 1 -->';
@@ -2377,20 +2378,20 @@ class Parser {
         */
        function closeList( $char ) {
                if ( '*' === $char ) {
-                       $text = "</li>\n</ul>";
+                       $text = "</li></ul>";
                } elseif ( '#' === $char ) {
-                       $text = "</li>\n</ol>";
+                       $text = "</li></ol>";
                } elseif ( ':' === $char ) {
                        if ( $this->mDTopen ) {
                                $this->mDTopen = false;
-                               $text = "</dt>\n</dl>";
+                               $text = "</dt></dl>";
                        } else {
-                               $text = "</dd>\n</dl>";
+                               $text = "</dd></dl>";
                        }
                } else {
                        return '<!-- ERR 3 -->';
                }
-               return $text . "\n";
+               return $text;
        }
        /**#@-*/
 
@@ -2488,6 +2489,9 @@ class Parser {
                                }
 
                                # Open prefixes where appropriate.
+                               if (  $lastPrefix && $prefixLength > $commonPrefixLength ) {
+                                       $output .= "\n";
+                               }
                                while ( $prefixLength > $commonPrefixLength ) {
                                        $char = substr( $prefix, $commonPrefixLength, 1 );
                                        $output .= $this->openList( $char );
@@ -2501,6 +2505,9 @@ class Parser {
                                        }
                                        ++$commonPrefixLength;
                                }
+                               if ( !$prefixLength && $lastPrefix ) {
+                                       $output .= "\n";
+                               }
                                $lastPrefix = $prefix2;
                        }
 
@@ -2582,12 +2589,18 @@ class Parser {
                                $this->mInPre = false;
                        }
                        if ( $paragraphStack === false ) {
-                               $output .= $t . "\n";
+                               $output .= $t;
+                               if ( $prefixLength === 0 ) {
+                                       $output .= "\n";
+                               }
                        }
                }
                while ( $prefixLength ) {
                        $output .= $this->closeList( $prefix2[$prefixLength - 1] );
                        --$prefixLength;
+                       if ( !$prefixLength ) {
+                               $output .= "\n";
+                       }
                }
                if ( $this->mLastSection != '' ) {
                        $output .= '</' . $this->mLastSection . '>';
@@ -3562,12 +3575,7 @@ class Parser {
                                $text = $newFrame->expand( $text, PPFrame::RECOVER_ORIG );
                        } elseif ( $titleText !== false && $newFrame->isEmpty() ) {
                                # Expansion is eligible for the empty-frame cache
-                               if ( isset( $this->mTplExpandCache[$titleText] ) ) {
-                                       $text = $this->mTplExpandCache[$titleText];
-                               } else {
-                                       $text = $newFrame->expand( $text );
-                                       $this->mTplExpandCache[$titleText] = $text;
-                               }
+                               $text = $newFrame->cachedExpand( $titleText, $text );
                        } else {
                                # Uncached expansion
                                $text = $newFrame->expand( $text );