fix for Bug #93 “tilde signatures inside nowiki tags sometimes get expanded (<include...
[lhc/web/wiklou.git] / includes / parser / Preprocessor_Hash.php
index ec600c0..7f175e5 100644 (file)
@@ -5,7 +5,7 @@
  * @file
  * @ingroup Parser
  */
+
 /**
  * Differences from DOM schema:
  *   * attribute nodes are children
  * @ingroup Parser
  */
 class Preprocessor_Hash implements Preprocessor {
+       /**
+        * @var Parser
+        */
        var $parser;
-       
+
        const CACHE_VERSION = 1;
 
        function __construct( $parser ) {
                $this->parser = $parser;
        }
 
+       /**
+        * @return PPFrame_Hash
+        */
        function newFrame() {
                return new PPFrame_Hash( $this );
        }
 
+       /**
+        * @param $args
+        * @return PPCustomFrame_Hash
+        */
        function newCustomFrame( $args ) {
                return new PPCustomFrame_Hash( $this, $args );
        }
@@ -80,12 +90,11 @@ class Preprocessor_Hash implements Preprocessor {
         */
        function preprocessToObj( $text, $flags = 0 ) {
                wfProfileIn( __METHOD__ );
-       
-       
+
                // Check cache.
                global $wgMemc, $wgPreprocessorCacheThreshold;
-               
-               $cacheable = strlen( $text ) > $wgPreprocessorCacheThreshold;
+
+               $cacheable = $wgPreprocessorCacheThreshold !== false && strlen( $text ) > $wgPreprocessorCacheThreshold;
                if ( $cacheable ) {
                        wfProfileIn( __METHOD__.'-cacheable' );
 
@@ -135,6 +144,9 @@ 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' );
@@ -272,7 +284,7 @@ class Preprocessor_Hash implements Preprocessor {
                                                // Search backwards for leading whitespace
                                                $wsStart = $i ? ( $i - strspn( $revText, ' ', strlen( $text ) - $i ) ) : 0;
                                                // Search forwards for trailing whitespace
-                                               // $wsEnd will be the position of the last space
+                                               // $wsEnd will be the position of the last space (or the '>' if there's none)
                                                $wsEnd = $endPos + 2 + strspn( $text, ' ', $endPos + 3 );
                                                // Eat the line if possible
                                                // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
@@ -302,13 +314,11 @@ class Preprocessor_Hash implements Preprocessor {
 
                                                if ( $stack->top ) {
                                                        $part = $stack->top->getCurrentPart();
-                                                       if ( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) {
-                                                               // Comments abutting, no change in visual end
-                                                               $part->commentEnd = $wsEnd;
-                                                       } else {
+                                                       if ( ! (isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 )) {
                                                                $part->visualEnd = $wsStart;
-                                                               $part->commentEnd = $endPos;
                                                        }
+                                                       // Else comments abutting, no change in visual end
+                                                       $part->commentEnd = $endPos;
                                                }
                                                $i = $endPos + 1;
                                                $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
@@ -348,8 +358,8 @@ class Preprocessor_Hash implements Preprocessor {
                                } else {
                                        $attrEnd = $tagEndPos;
                                        // Find closing tag
-                                       if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i", 
-                                                       $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) ) 
+                                       if ( 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] );
@@ -491,7 +501,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,7 +522,6 @@ class Preprocessor_Hash implements Preprocessor {
 
                                # check for maximum matching characters (if there are 5 closing
                                # characters, we will probably need only 3 - depending on the rules)
-                               $matchingCount = 0;
                                $rule = $rules[$piece->open];
                                if ( $count > $rule['max'] ) {
                                        # The specified maximum exists in the callback array, unless the caller
@@ -559,7 +568,7 @@ class Preprocessor_Hash implements Preprocessor {
                                        $titleNode->lastChild = $titleAccum->lastNode;
                                        $element->addChild( $titleNode );
                                        $argIndex = 1;
-                                       foreach ( $parts as $partIndex => $part ) {
+                                       foreach ( $parts as $part ) {
                                                if ( isset( $part->eqpos ) ) {
                                                        // Find equals
                                                        $lastNode = false;
@@ -677,7 +686,7 @@ class Preprocessor_Hash implements Preprocessor {
                $rootNode = new PPNode_Hash_Tree( 'root' );
                $rootNode->firstChild = $stack->rootAccum->firstNode;
                $rootNode->lastChild = $stack->rootAccum->lastNode;
-               
+
                // Cache
                if ($cacheable) {
                        $cacheValue = sprintf( "%08d", self::CACHE_VERSION ) . serialize( $rootNode );
@@ -686,7 +695,7 @@ class Preprocessor_Hash implements Preprocessor {
                        wfProfileOut( __METHOD__.'-cacheable' );
                        wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
                }
-               
+
                wfProfileOut( __METHOD__ );
                return $rootNode;
        }
@@ -819,7 +828,21 @@ class PPDAccum_Hash {
  * @ingroup Parser
  */
 class PPFrame_Hash implements PPFrame {
-       var $preprocessor, $parser, $title;
+
+       /**
+        * @var Parser
+        */
+       var $parser;
+
+       /**
+        * @var Preprocessor
+        */
+       var $preprocessor;
+
+       /**
+        * @var Title
+        */
+       var $title;
        var $titleCache;
 
        /**
@@ -887,8 +910,7 @@ class PPFrame_Hash implements PPFrame {
                        return $root;
                }
 
-               if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() )
-               {
+               if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
                        return '<span class="error">Node-count limit exceeded</span>';
                }
                if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
@@ -1017,7 +1039,7 @@ class PPFrame_Hash implements PPFrame {
                                                $serial = count( $this->parser->mHeadings ) - 1;
                                                $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser::MARKER_SUFFIX;
                                                $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
-                                               $this->parser->mStripState->general->setPair( $marker, '' );
+                                               $this->parser->mStripState->addGeneral( $marker, '' );
                                                $out .= $s;
                                        } else {
                                                # Expand in virtual stack
@@ -1196,6 +1218,8 @@ class PPFrame_Hash implements PPFrame {
 
        /**
         * Returns true if the infinite loop check is OK, false if a loop is detected
+        *
+        * @param $title Title
         */
        function loopCheck( $title ) {
                return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
@@ -1217,6 +1241,13 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
        var $numberedArgs, $namedArgs, $parent;
        var $numberedExpansionCache, $namedExpansionCache;
 
+       /**
+        * @param $preprocessor
+        * @param $parent
+        * @param $numberedArgs array
+        * @param $namedArgs array
+        * @param $title Title
+        */
        function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
                parent::__construct( $preprocessor );
 
@@ -1253,11 +1284,16 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
        }
        /**
         * Returns true if there are no arguments in this frame
+        *
+        * @return bool
         */
        function isEmpty() {
                return !count( $this->numberedArgs ) && !count( $this->namedArgs );
        }
 
+       /**
+        * @return array
+        */
        function getArguments() {
                $arguments = array();
                foreach ( array_merge(
@@ -1267,7 +1303,10 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
                }
                return $arguments;
        }
-       
+
+       /**
+        * @return array
+        */
        function getNumberedArguments() {
                $arguments = array();
                foreach ( array_keys($this->numberedArgs) as $key ) {
@@ -1275,7 +1314,10 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
                }
                return $arguments;
        }
-       
+
+       /**
+        * @return array
+        */
        function getNamedArguments() {
                $arguments = array();
                foreach ( array_keys($this->namedArgs) as $key ) {