Merge "$wgUseSquid unused since parent commit (fa1a8d5)"
[lhc/web/wiklou.git] / includes / parser / Preprocessor_DOM.php
index bb60a34..f991df2 100644 (file)
@@ -2,6 +2,21 @@
 /**
  * Preprocessor using PHP's dom extension
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Parser
  */
@@ -41,7 +56,7 @@ class Preprocessor_DOM implements Preprocessor {
        }
 
        /**
-        * @param $args
+        * @param $args array
         * @return PPCustomFrame_DOM
         */
        function newCustomFrame( $args ) {
@@ -81,7 +96,7 @@ class Preprocessor_DOM implements Preprocessor {
         */
        function memCheck() {
                if ( $this->memoryLimit === false ) {
-                       return;
+                       return true;
                }
                $usage = memory_get_usage();
                if ( $usage > $this->memoryLimit * 0.9 ) {
@@ -155,7 +170,8 @@ class Preprocessor_DOM implements Preprocessor {
                if ( !$result ) {
                        // Try running the XML through UtfNormal to get rid of invalid characters
                        $xml = UtfNormal::cleanUp( $xml );
-                       $result = $dom->loadXML( $xml );
+                       // 1 << 19 == XML_PARSE_HUGE, needed so newer versions of libxml2 don't barf when the XML is >256 levels deep
+                       $result = $dom->loadXML( $xml, 1 << 19 );
                        if ( !$result ) {
                                throw new MWException( __METHOD__.' generated invalid XML' );
                        }
@@ -205,9 +221,6 @@ class Preprocessor_DOM implements Preprocessor {
                        if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) {
                                $enableOnlyinclude = true;
                        }
-               } else if ( isset( $this->parser->ot ) && $this->parser->ot['wiki'] ) {
-                       $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude', 'includeonly', '/includeonly' );
-                       $ignoredElements = array();
                } else {
                        $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
                        $ignoredElements = array( 'includeonly' );
@@ -481,7 +494,7 @@ class Preprocessor_DOM implements Preprocessor {
                        } elseif ( $found == 'line-end' ) {
                                $piece = $stack->top;
                                // A heading must be open, otherwise \n wouldn't have been in the search list
-                               assert( $piece->open == "\n" );
+                               assert( '$piece->open == "\n"' );
                                $part = $piece->getCurrentPart();
                                // Search back through the input to see if it has a proper close
                                // Do this using the reversed string since the other solutions (end anchor, etc.) are inefficient
@@ -545,7 +558,7 @@ class Preprocessor_DOM implements Preprocessor {
                                                'open' => $curChar,
                                                'close' => $rule['end'],
                                                'count' => $count,
-                                               'lineStart' => ($i == 0 || $text[$i-1] == "\n"),
+                                               'lineStart' => ($i > 0 && $text[$i-1] == "\n"),
                                        );
 
                                        $stack->push( $piece );
@@ -913,7 +926,7 @@ class PPFrame_DOM implements PPFrame {
         *
         * @return PPTemplateFrame_DOM
         */
-       function newChild( $args = false, $title = false ) {
+       function newChild( $args = false, $title = false, $indexOffset = 0 ) {
                $namedArgs = array();
                $numberedArgs = array();
                if ( $title === false ) {
@@ -925,6 +938,9 @@ class PPFrame_DOM implements PPFrame {
                                $args = $args->node;
                        }
                        foreach ( $args as $arg ) {
+                               if ( $arg instanceof PPNode ) {
+                                       $arg = $arg->node;
+                               }
                                if ( !$xpath ) {
                                        $xpath = new DOMXPath( $arg->ownerDocument );
                                }
@@ -934,6 +950,7 @@ class PPFrame_DOM implements PPFrame {
                                if ( $nameNodes->item( 0 )->hasAttributes() ) {
                                        // Numbered parameter
                                        $index = $nameNodes->item( 0 )->attributes->getNamedItem( 'index' )->textContent;
+                                       $index = $index - $indexOffset;
                                        $numberedArgs[$index] = $value->item( 0 );
                                        unset( $namedArgs[$index] );
                                } else {
@@ -959,16 +976,26 @@ class PPFrame_DOM implements PPFrame {
                        return $root;
                }
 
-               if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() )
-               {
+               if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
+                       $this->parser->limitationWarn( 'node-count-exceeded',
+                               $this->parser->mPPNodeCount,
+                               $this->parser->mOptions->getMaxPPNodeCount()
+                       );
                        return '<span class="error">Node-count limit exceeded</span>';
                }
 
                if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
+                       $this->parser->limitationWarn( 'expansion-depth-exceeded',
+                               $expansionDepth,
+                               $this->parser->mOptions->getMaxPPExpandDepth()
+                       );
                        return '<span class="error">Expansion depth limit exceeded</span>';
                }
                wfProfileIn( __METHOD__ );
                ++$expansionDepth;
+               if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
+                       $this->parser->mHighestExpansionDepth = $expansionDepth;
+               }
 
                if ( $root instanceof PPNode_DOM ) {
                        $root = $root->node;
@@ -1253,6 +1280,7 @@ class PPFrame_DOM implements PPFrame {
 
        /**
         * Virtual implode with brackets
+        * @return array
         */
        function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
                $args = array_slice( func_get_args(), 3 );
@@ -1342,6 +1370,15 @@ class PPFrame_DOM implements PPFrame {
        function isTemplate() {
                return false;
        }
+
+       /**
+        * Get a title of frame
+        *
+        * @return Title
+        */
+       function getTitle() {
+               return $this->title;
+       }
 }
 
 /**
@@ -1516,6 +1553,10 @@ class PPCustomFrame_DOM extends PPFrame_DOM {
                }
                return $this->args[$index];
        }
+
+       function getArguments() {
+               return $this->args;
+       }
 }
 
 /**
@@ -1668,6 +1709,7 @@ class PPNode_DOM implements PPNode {
 
        /**
         * Split a <h> node
+        * @return array
         */
        function splitHeading() {
                if ( $this->getName() !== 'h' ) {