Merge "Revert "Adding sanity check to Title::isRedirect().""
[lhc/web/wiklou.git] / includes / parser / Preprocessor_Hash.php
index 7f175e5..f455a1d 100644 (file)
@@ -2,6 +2,21 @@
 /**
  * Preprocessor using PHP arrays
  *
+ * 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
  */
@@ -32,13 +47,17 @@ class Preprocessor_Hash implements Preprocessor {
        }
 
        /**
-        * @param $args
+        * @param $args array
         * @return PPCustomFrame_Hash
         */
        function newCustomFrame( $args ) {
                return new PPCustomFrame_Hash( $this, $args );
        }
 
+       /**
+        * @param $values array
+        * @return PPNode_Hash_Array
+        */
        function newPartNodeArray( $values ) {
                $list = array();
 
@@ -86,7 +105,7 @@ class Preprocessor_Hash implements Preprocessor {
         * cache may be implemented at a later date which takes further advantage of these strict
         * dependency requirements.
         *
-        * @private
+        * @return PPNode_Hash_Tree
         */
        function preprocessToObj( $text, $flags = 0 ) {
                wfProfileIn( __METHOD__ );
@@ -144,9 +163,6 @@ class Preprocessor_Hash implements Preprocessor {
                        if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) {
                                $enableOnlyinclude = true;
                        }
-               } else if ( $this->parser->ot['wiki'] ) {
-                       $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude', 'includeonly', '/includeonly' );
-                       $ignoredElements = array();
                } else {
                        $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
                        $ignoredElements = array( 'includeonly' );
@@ -424,12 +440,10 @@ class Preprocessor_Hash implements Preprocessor {
                                        extract( $stack->getFlags() );
                                        $i += $count;
                                }
-                       }
-
-                       elseif ( $found == 'line-end' ) {
+                       } 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
@@ -488,9 +502,7 @@ class Preprocessor_Hash implements Preprocessor {
                                // another heading. Infinite loops are avoided because the next iteration MUST
                                // hit the heading open case above, which unconditionally increments the
                                // input pointer.
-                       }
-
-                       elseif ( $found == 'open' ) {
+                       } elseif ( $found == 'open' ) {
                                # count opening brace characters
                                $count = strspn( $text, $curChar, $i );
 
@@ -501,7 +513,7 @@ class Preprocessor_Hash 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 );
@@ -512,9 +524,7 @@ class Preprocessor_Hash implements Preprocessor {
                                        $accum->addLiteral( str_repeat( $curChar, $count ) );
                                }
                                $i += $count;
-                       }
-
-                       elseif ( $found == 'close' ) {
+                       } elseif ( $found == 'close' ) {
                                $piece = $stack->top;
                                # lets check if there are enough characters for closing brace
                                $maxCount = $piece->count;
@@ -654,16 +664,12 @@ class Preprocessor_Hash implements Preprocessor {
                                } else {
                                        $accum->addAccum( $element );
                                }
-                       }
-
-                       elseif ( $found == 'pipe' ) {
+                       } elseif ( $found == 'pipe' ) {
                                $findEquals = true; // shortcut for getFlags()
                                $stack->addPart();
                                $accum =& $stack->getAccum();
                                ++$i;
-                       }
-
-                       elseif ( $found == 'equals' ) {
+                       } elseif ( $found == 'equals' ) {
                                $findEquals = false; // shortcut for getFlags()
                                $accum->addNodeWithText( 'equals', '=' );
                                $stack->getCurrentPart()->eqpos = $accum->lastNode;
@@ -724,6 +730,8 @@ class PPDStackElement_Hash extends PPDStackElement {
 
        /**
         * Get the accumulator that would result if the close is not found.
+        *
+        * @return PPDAccum_Hash
         */
        function breakSyntax( $openingCount = false ) {
                if ( $this->open == "\n" ) {
@@ -874,8 +882,13 @@ class PPFrame_Hash implements PPFrame {
        /**
         * Create a new child frame
         * $args is optionally a multi-root PPNode or array containing the template arguments
+        *
+        * @param $args PPNode_Hash_Array|array
+        * @param $title Title|bool
+        *
+        * @return PPTemplateFrame_Hash
         */
-       function newChild( $args = false, $title = false ) {
+       function newChild( $args = false, $title = false, $indexOffset = 0 ) {
                $namedArgs = array();
                $numberedArgs = array();
                if ( $title === false ) {
@@ -891,8 +904,9 @@ class PPFrame_Hash implements PPFrame {
                                $bits = $arg->splitArg();
                                if ( $bits['index'] !== '' ) {
                                        // Numbered parameter
-                                       $numberedArgs[$bits['index']] = $bits['value'];
-                                       unset( $namedArgs[$bits['index']] );
+                                       $index = $bits['index'] - $indexOffset;
+                                       $numberedArgs[$index] = $bits['value'];
+                                       unset( $namedArgs[$index] );
                                } else {
                                        // Named parameter
                                        $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
@@ -904,6 +918,12 @@ class PPFrame_Hash implements PPFrame {
                return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
        }
 
+       /**
+        * @throws MWException
+        * @param $root
+        * @param $flags int
+        * @return string
+        */
        function expand( $root, $flags = 0 ) {
                static $expansionDepth = 0;
                if ( is_string( $root ) ) {
@@ -911,12 +931,23 @@ class PPFrame_Hash implements PPFrame {
                }
 
                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>';
                }
                ++$expansionDepth;
+               if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
+                       $this->parser->mHighestExpansionDepth = $expansionDepth;
+               }
 
                $outStack = array( '', '' );
                $iteratorStack = array( false, $root );
@@ -1073,6 +1104,11 @@ class PPFrame_Hash implements PPFrame {
                return $outStack[0];
        }
 
+       /**
+        * @param $sep
+        * @param $flags
+        * @return string
+        */
        function implodeWithFlags( $sep, $flags /*, ... */ ) {
                $args = array_slice( func_get_args(), 2 );
 
@@ -1100,6 +1136,7 @@ class PPFrame_Hash implements PPFrame {
        /**
         * Implode with no flags specified
         * This previously called implodeWithFlags but has now been inlined to reduce stack depth
+        * @return string
         */
        function implode( $sep /*, ... */ ) {
                $args = array_slice( func_get_args(), 1 );
@@ -1128,6 +1165,8 @@ class PPFrame_Hash implements PPFrame {
        /**
         * Makes an object that, when expand()ed, will be the same as one obtained
         * with implode()
+        *
+        * @return PPNode_Hash_Array
         */
        function virtualImplode( $sep /*, ... */ ) {
                $args = array_slice( func_get_args(), 1 );
@@ -1155,6 +1194,8 @@ class PPFrame_Hash implements PPFrame {
 
        /**
         * Virtual implode with brackets
+        *
+        * @return PPNode_Hash_Array
         */
        function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
                $args = array_slice( func_get_args(), 3 );
@@ -1185,6 +1226,10 @@ class PPFrame_Hash implements PPFrame {
                return 'frame{}';
        }
 
+       /**
+        * @param $level bool
+        * @return array|bool|String
+        */
        function getPDBK( $level = false ) {
                if ( $level === false ) {
                        return $this->title->getPrefixedDBkey();
@@ -1193,25 +1238,40 @@ class PPFrame_Hash implements PPFrame {
                }
        }
 
+       /**
+        * @return array
+        */
        function getArguments() {
                return array();
        }
 
+       /**
+        * @return array
+        */
        function getNumberedArguments() {
                return array();
        }
 
+       /**
+        * @return array
+        */
        function getNamedArguments() {
                return array();
        }
 
        /**
         * Returns true if there are no arguments in this frame
+        *
+        * @return bool
         */
        function isEmpty() {
                return true;
        }
 
+       /**
+        * @param $name
+        * @return bool
+        */
        function getArgument( $name ) {
                return false;
        }
@@ -1220,6 +1280,8 @@ class PPFrame_Hash implements PPFrame {
         * Returns true if the infinite loop check is OK, false if a loop is detected
         *
         * @param $title Title
+        *
+        * @return bool
         */
        function loopCheck( $title ) {
                return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
@@ -1227,10 +1289,21 @@ class PPFrame_Hash implements PPFrame {
 
        /**
         * Return true if the frame is a template frame
+        *
+        * @return bool
         */
        function isTemplate() {
                return false;
        }
+
+       /**
+        * Get a title of frame
+        *
+        * @return Title
+        */
+       function getTitle() {
+               return $this->title;
+       }
 }
 
 /**
@@ -1326,6 +1399,10 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
                return $arguments;
        }
 
+       /**
+        * @param $index
+        * @return array|bool
+        */
        function getNumberedArgument( $index ) {
                if ( !isset( $this->numberedArgs[$index] ) ) {
                        return false;
@@ -1337,6 +1414,10 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
                return $this->numberedExpansionCache[$index];
        }
 
+       /**
+        * @param $name
+        * @return bool
+        */
        function getNamedArgument( $name ) {
                if ( !isset( $this->namedArgs[$name] ) ) {
                        return false;
@@ -1349,6 +1430,10 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
                return $this->namedExpansionCache[$name];
        }
 
+       /**
+        * @param $name
+        * @return array|bool
+        */
        function getArgument( $name ) {
                $text = $this->getNumberedArgument( $name );
                if ( $text === false ) {
@@ -1359,6 +1444,8 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
 
        /**
         * Return true if the frame is a template frame
+        *
+        * @return bool
         */
        function isTemplate() {
                return true;
@@ -1393,16 +1480,27 @@ class PPCustomFrame_Hash extends PPFrame_Hash {
                return $s;
        }
 
+       /**
+        * @return bool
+        */
        function isEmpty() {
                return !count( $this->args );
        }
 
+       /**
+        * @param $index
+        * @return bool
+        */
        function getArgument( $index ) {
                if ( !isset( $this->args[$index] ) ) {
                        return false;
                }
                return $this->args[$index];
        }
+
+       function getArguments() {
+               return $this->args;
+       }
 }
 
 /**
@@ -1433,6 +1531,11 @@ class PPNode_Hash_Tree implements PPNode {
                }
        }
 
+       /**
+        * @param $name
+        * @param $text
+        * @return PPNode_Hash_Tree
+        */
        static function newWithText( $name, $text ) {
                $obj = new self( $name );
                $obj->addChild( new PPNode_Hash_Text( $text ) );
@@ -1448,6 +1551,9 @@ class PPNode_Hash_Tree implements PPNode {
                }
        }
 
+       /**
+        * @return PPNode_Hash_Array
+        */
        function getChildren() {
                $children = array();
                for ( $child = $this->firstChild; $child; $child = $child->nextSibling ) {
@@ -1474,9 +1580,24 @@ class PPNode_Hash_Tree implements PPNode {
                return $children;
        }
 
-       function getLength() { return false; }
-       function item( $i ) { return false; }
+       /**
+        * @return bool
+        */
+       function getLength() {
+               return false;
+       }
+
+       /**
+        * @param  $i
+        * @return bool
+        */
+       function item( $i ) {
+               return false;
+       }
 
+       /**
+        * @return string
+        */
        function getName() {
                return $this->name;
        }
@@ -1486,6 +1607,8 @@ class PPNode_Hash_Tree implements PPNode {
         *    name          PPNode name
         *    index         String index
         *    value         PPNode value
+        *
+        * @return array
         */
        function splitArg() {
                $bits = array();
@@ -1517,6 +1640,8 @@ class PPNode_Hash_Tree implements PPNode {
        /**
         * Split an <ext> node into an associative array containing name, attr, inner and close
         * All values in the resulting array are PPNodes. Inner and close are optional.
+        *
+        * @return array
         */
        function splitExt() {
                $bits = array();
@@ -1542,6 +1667,8 @@ class PPNode_Hash_Tree implements PPNode {
 
        /**
         * Split an <h> node
+        *
+        * @return array
         */
        function splitHeading() {
                if ( $this->name !== 'h' ) {
@@ -1566,6 +1693,8 @@ class PPNode_Hash_Tree implements PPNode {
 
        /**
         * Split a <template> or <tplarg> node
+        *
+        * @return array
         */
        function splitTemplate() {
                $parts = array();