Merge "API: Allow anonymous CORS from anywhere, when specifically requested"
[lhc/web/wiklou.git] / includes / parser / Preprocessor_Hash.php
index 24baae4..0e11967 100644 (file)
@@ -120,6 +120,7 @@ class Preprocessor_Hash extends Preprocessor {
                $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
 
                $xmlishElements = $this->parser->getStripList();
+               $xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ];
                $enableOnlyinclude = false;
                if ( $forInclusion ) {
                        $ignoredTags = [ 'includeonly', '/includeonly' ];
@@ -160,6 +161,8 @@ class Preprocessor_Hash extends Preprocessor {
                $inHeading = false;
                // True if there are no more greater-than (>) signs right of $i
                $noMoreGT = false;
+               // Map of tag name => true if there are no more closing tags of given type right of $i
+               $noMoreClosingTag = [];
                // True to ignore all input up to the next <onlyinclude>
                $findOnlyinclude = $enableOnlyinclude;
                // Do a line-start run without outputting an LF character
@@ -380,17 +383,29 @@ class Preprocessor_Hash extends Preprocessor {
                                } else {
                                        $attrEnd = $tagEndPos;
                                        // Find closing tag
-                                       if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
+                                       if (
+                                               !isset( $noMoreClosingTag[$name] ) &&
+                                               preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
                                                        $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
                                        ) {
                                                $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
                                                $i = $matches[0][1] + strlen( $matches[0][0] );
                                                $close = $matches[0][0];
                                        } else {
-                                               // No end tag -- let it run out to the end of the text.
-                                               $inner = substr( $text, $tagEndPos + 1 );
-                                               $i = $lengthText;
-                                               $close = null;
+                                               // No end tag
+                                               if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
+                                                       // Let it run out to the end of the text.
+                                                       $inner = substr( $text, $tagEndPos + 1 );
+                                                       $i = $lengthText;
+                                                       $close = null;
+                                               } else {
+                                                       // Don't match the tag, treat opening tag as literal and resume parsing.
+                                                       $i = $tagEndPos + 1;
+                                                       $accum->addLiteral( substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
+                                                       // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
+                                                       $noMoreClosingTag[$name] = true;
+                                                       continue;
+                                               }
                                        }
                                }
                                // <includeonly> and <noinclude> just become <ignore> tags
@@ -1355,8 +1370,8 @@ class PPFrame_Hash implements PPFrame {
        }
 
        /**
-        * @param string $name
-        * @return bool
+        * @param int|string $name
+        * @return bool Always false in this implementation.
         */
        public function getArgument( $name ) {
                return false;
@@ -1549,7 +1564,7 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
 
        /**
         * @param int $index
-        * @return array|bool
+        * @return string|bool
         */
        public function getNumberedArgument( $index ) {
                if ( !isset( $this->numberedArgs[$index] ) ) {
@@ -1567,7 +1582,7 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
 
        /**
         * @param string $name
-        * @return bool
+        * @return string|bool
         */
        public function getNamedArgument( $name ) {
                if ( !isset( $this->namedArgs[$name] ) ) {
@@ -1582,8 +1597,8 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
        }
 
        /**
-        * @param string $name
-        * @return array|bool
+        * @param int|string $name
+        * @return string|bool
         */
        public function getArgument( $name ) {
                $text = $this->getNumberedArgument( $name );
@@ -1652,8 +1667,8 @@ class PPCustomFrame_Hash extends PPFrame_Hash {
        }
 
        /**
-        * @param int $index
-        * @return bool
+        * @param int|string $index
+        * @return string|bool
         */
        public function getArgument( $index ) {
                if ( !isset( $this->args[$index] ) ) {