Merge "API: Add support for documenting dynamic parameters"
[lhc/web/wiklou.git] / includes / parser / Preprocessor_Hash.php
index 49fa8a1..28c49fd 100644 (file)
@@ -28,7 +28,7 @@
  * @ingroup Parser
  */
 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
-class Preprocessor_Hash implements Preprocessor {
+class Preprocessor_Hash extends Preprocessor {
        // @codingStandardsIgnoreEnd
 
        /**
@@ -36,7 +36,7 @@ class Preprocessor_Hash implements Preprocessor {
         */
        public $parser;
 
-       const CACHE_VERSION = 1;
+       const CACHE_PREFIX = 'preprocess-hash';
 
        public function __construct( $parser ) {
                $this->parser = $parser;
@@ -112,45 +112,11 @@ class Preprocessor_Hash implements Preprocessor {
         * @return PPNode_Hash_Tree
         */
        public function preprocessToObj( $text, $flags = 0 ) {
-               // Check cache.
-               global $wgMemc, $wgPreprocessorCacheThreshold;
-
-               $cacheable = $wgPreprocessorCacheThreshold !== false
-                       && strlen( $text ) > $wgPreprocessorCacheThreshold;
-
-               if ( $cacheable ) {
-                       $cacheKey = wfMemcKey( 'preprocess-hash', md5( $text ), $flags );
-                       $cacheValue = $wgMemc->get( $cacheKey );
-                       if ( $cacheValue ) {
-                               $version = substr( $cacheValue, 0, 8 );
-                               if ( intval( $version ) == self::CACHE_VERSION ) {
-                                       $hash = unserialize( substr( $cacheValue, 8 ) );
-                                       // From the cache
-                                       wfDebugLog( "Preprocessor",
-                                               "Loaded preprocessor hash from memcached (key $cacheKey)" );
-                                       return $hash;
-                               }
-                       }
+               $tree = $this->cacheGetTree( $text, $flags );
+               if ( $tree !== false ) {
+                       return unserialize( $tree );
                }
 
-               $rules = array(
-                       '{' => array(
-                               'end' => '}',
-                               'names' => array(
-                                       2 => 'template',
-                                       3 => 'tplarg',
-                               ),
-                               'min' => 2,
-                               'max' => 3,
-                       ),
-                       '[' => array(
-                               'end' => ']',
-                               'names' => array( 2 => null ),
-                               'min' => 2,
-                               'max' => 2,
-                       )
-               );
-
                $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
 
                $xmlishElements = $this->parser->getStripList();
@@ -194,6 +160,8 @@ class Preprocessor_Hash implements 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 = array();
                // True to ignore all input up to the next <onlyinclude>
                $findOnlyinclude = $enableOnlyinclude;
                // Do a line-start run without outputting an LF character
@@ -267,9 +235,9 @@ class Preprocessor_Hash implements Preprocessor {
                                                }
                                        } elseif ( $curChar == $currentClosing ) {
                                                $found = 'close';
-                                       } elseif ( isset( $rules[$curChar] ) ) {
+                                       } elseif ( isset( $this->rules[$curChar] ) ) {
                                                $found = 'open';
-                                               $rule = $rules[$curChar];
+                                               $rule = $this->rules[$curChar];
                                        } else {
                                                # Some versions of PHP have a strcspn which stops on null characters
                                                # Ignore and continue
@@ -414,17 +382,21 @@ class Preprocessor_Hash implements 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 -- 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
@@ -572,7 +544,7 @@ 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)
-                               $rule = $rules[$piece->open];
+                               $rule = $this->rules[$piece->open];
                                if ( $count > $rule['max'] ) {
                                        # The specified maximum exists in the callback array, unless the caller
                                        # has made an error
@@ -629,13 +601,11 @@ class Preprocessor_Hash implements Preprocessor {
                                                                $lastNode = $node;
                                                        }
                                                        if ( !$node ) {
-                                                               if ( $cacheable ) {
-                                                               }
+                                                               // if ( $cacheable ) { ... }
                                                                throw new MWException( __METHOD__ . ': eqpos not found' );
                                                        }
                                                        if ( $node->name !== 'equals' ) {
-                                                               if ( $cacheable ) {
-                                                               }
+                                                               // if ( $cacheable ) { ... }
                                                                throw new MWException( __METHOD__ . ': eqpos is not equals' );
                                                        }
                                                        $equalsNode = $node;
@@ -685,7 +655,7 @@ class Preprocessor_Hash implements Preprocessor {
                                        $piece->parts = array( new PPDPart_Hash );
                                        $piece->count -= $matchingCount;
                                        # do we still qualify for any callback with remaining count?
-                                       $min = $rules[$piece->open]['min'];
+                                       $min = $this->rules[$piece->open]['min'];
                                        if ( $piece->count >= $min ) {
                                                $stack->push( $piece );
                                                $accum =& $stack->getAccum();
@@ -732,15 +702,7 @@ class Preprocessor_Hash implements Preprocessor {
                $rootNode->lastChild = $stack->rootAccum->lastNode;
 
                // Cache
-               if ( $cacheable ) {
-                       $cacheValue = sprintf( "%08d", self::CACHE_VERSION ) . serialize( $rootNode );
-
-                       // T111289: Cache values should not exceed 1 Mb, but they do.
-                       if ( strlen( $cacheValue ) <= 1e6 ) {
-                               $wgMemc->set( $cacheKey, $cacheValue, 86400 );
-                               wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
-                       }
-               }
+               $this->cacheSetTree( $text, $flags, serialize( $rootNode ) );
 
                return $rootNode;
        }
@@ -1788,7 +1750,7 @@ class PPNode_Hash_Tree implements PPNode {
                                $children[] = $child;
                        }
                }
-               return $children;
+               return new PPNode_Hash_Array( $children );
        }
 
        /**