Merge "maintenance: Document secondary purpose of --server"
[lhc/web/wiklou.git] / includes / parser / Parser.php
index f6526ac..b66031c 100644 (file)
@@ -358,7 +358,7 @@ class Parser {
                $this->mLangLinkLanguages = [];
                $this->currentRevisionCache = null;
 
-               $this->mStripState = new StripState;
+               $this->mStripState = new StripState( $this );
 
                # Clear these on every parse, T6549
                $this->mTplRedirCache = $this->mTplDomCache = [];
@@ -543,6 +543,11 @@ class Parser {
                $this->mOutput->setLimitReportData( 'limitreport-expensivefunctioncount',
                        [ $this->mExpensiveFunctionCount, $this->mOptions->getExpensiveParserFunctionLimit() ]
                );
+
+               foreach ( $this->mStripState->getLimitReport() as list( $key, $value ) ) {
+                       $this->mOutput->setLimitReportData( $key, $value );
+               }
+
                Hooks::run( 'ParserLimitReportPrepare', [ $this, $this->mOutput ] );
 
                $limitReport = "NewPP limit report\n";
@@ -1108,7 +1113,11 @@ class Parser {
                                        $line = "</{$last_tag}>{$line}";
                                }
                                array_pop( $tr_attributes );
-                               $outLine = $line . str_repeat( '</dd></dl>', $indent_level );
+                               if ( $indent_level > 0 ) {
+                                       $outLine = rtrim( $line ) . str_repeat( '</dd></dl>', $indent_level );
+                               } else {
+                                       $outLine = $line;
+                               }
                        } elseif ( $first_two === '|-' ) {
                                # Now we have a table row
                                $line = preg_replace( '#^\|-+#', '', $line );
@@ -1199,13 +1208,15 @@ class Parser {
                                        # be mistaken as delimiting cell parameters
                                        # Bug T153140: Neither should language converter markup.
                                        if ( preg_match( '/\[\[|-\{/', $cell_data[0] ) === 1 ) {
-                                               $cell = "{$previous}<{$last_tag}>{$cell}";
+                                               $cell = "{$previous}<{$last_tag}>" . trim( $cell );
                                        } elseif ( count( $cell_data ) == 1 ) {
-                                               $cell = "{$previous}<{$last_tag}>{$cell_data[0]}";
+                                               // Whitespace in cells is trimmed
+                                               $cell = "{$previous}<{$last_tag}>" . trim( $cell_data[0] );
                                        } else {
                                                $attributes = $this->mStripState->unstripBoth( $cell_data[0] );
                                                $attributes = Sanitizer::fixTagAttributes( $attributes, $last_tag );
-                                               $cell = "{$previous}<{$last_tag}{$attributes}>{$cell_data[1]}";
+                                               // Whitespace in cells is trimmed
+                                               $cell = "{$previous}<{$last_tag}{$attributes}>" . trim( $cell_data[1] );
                                        }
 
                                        $outLine .= $cell;
@@ -1460,7 +1471,7 @@ class Parser {
        /**
         * @throws MWException
         * @param array $m
-        * @return HTML|string
+        * @return string HTML
         */
        public function magicLinkCallback( $m ) {
                if ( isset( $m[1] ) && $m[1] !== '' ) {
@@ -1612,7 +1623,9 @@ class Parser {
        public function doHeadings( $text ) {
                for ( $i = 6; $i >= 1; --$i ) {
                        $h = str_repeat( '=', $i );
-                       $text = preg_replace( "/^$h(.+)$h\\s*$/m", "<h$i>\\1</h$i>", $text );
+                       // Trim non-newline whitespace from headings
+                       // Using \s* will break for: "==\n===\n" and parse as <h2>=</h2>
+                       $text = preg_replace( "/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m", "<h$i>\\1</h$i>", $text );
                }
                return $text;
        }
@@ -1863,8 +1876,8 @@ class Parser {
 
                        $dtrail = '';
 
-                       # Set linktype for CSS - if URL==text, link is essentially free
-                       $linktype = ( $text === $url ) ? 'free' : 'text';
+                       # Set linktype for CSS
+                       $linktype = 'text';
 
                        # No link text, e.g. [http://domain.tld/some.link]
                        if ( $text == '' ) {
@@ -4045,9 +4058,11 @@ class Parser {
 
                # Get all headlines for numbering them and adding funky stuff like [edit]
                # links - this is for later, but we need the number of headlines right now
+               # NOTE: white space in headings have been trimmed in doHeadings. They shouldn't
+               # be trimmed here since whitespace in HTML headings is significant.
                $matches = [];
                $numMatches = preg_match_all(
-                       '/<H(?P<level>[1-6])(?P<attrib>.*?>)\s*(?P<header>[\s\S]*?)\s*<\/H[1-6] *>/i',
+                       '/<H(?P<level>[1-6])(?P<attrib>.*?>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i',
                        $text,
                        $matches
                );
@@ -5993,11 +6008,13 @@ class Parser {
         * unserializeHalfParsedText(). The text can then be safely incorporated into
         * the return value of a parser hook.
         *
+        * @deprecated since 1.31
         * @param string $text
         *
         * @return array
         */
        public function serializeHalfParsedText( $text ) {
+               wfDeprecated( __METHOD__, '1.31' );
                $data = [
                        'text' => $text,
                        'version' => self::HALF_PARSED_VERSION,
@@ -6018,11 +6035,13 @@ class Parser {
         * If the $data array has been stored persistently, the caller should first
         * check whether it is still valid, by calling isValidHalfParsedText().
         *
+        * @deprecated since 1.31
         * @param array $data Serialized data
         * @throws MWException
         * @return string
         */
        public function unserializeHalfParsedText( $data ) {
+               wfDeprecated( __METHOD__, '1.31' );
                if ( !isset( $data['version'] ) || $data['version'] != self::HALF_PARSED_VERSION ) {
                        throw new MWException( __METHOD__ . ': invalid version' );
                }
@@ -6043,11 +6062,13 @@ class Parser {
         * serializeHalfParsedText(), is compatible with the current version of the
         * parser.
         *
+        * @deprecated since 1.31
         * @param array $data
         *
         * @return bool
         */
        public function isValidHalfParsedText( $data ) {
+               wfDeprecated( __METHOD__, '1.31' );
                return isset( $data['version'] ) && $data['version'] == self::HALF_PARSED_VERSION;
        }