Merge "Use interwiki cache directly to resolve transwiki import sources"
[lhc/web/wiklou.git] / includes / parser / Preprocessor_Hash.php
index 9429e44..4f12414 100644 (file)
@@ -26,9 +26,9 @@
  *   * attribute nodes are children
  *   * "<h>" nodes that aren't at the top are replaced with <possible-h>
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
-class Preprocessor_Hash implements Preprocessor {
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
+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;
@@ -88,6 +88,7 @@ class Preprocessor_Hash implements Preprocessor {
                return $node;
        }
 
+
        /**
         * Preprocess some wikitext and return the document tree.
         * This is the ghost of Parser::replace_variables().
@@ -112,45 +113,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();
@@ -200,7 +167,7 @@ class Preprocessor_Hash implements Preprocessor {
                $fakeLineStart = true;
 
                while ( true ) {
-                       //$this->memCheck();
+                       // $this->memCheck();
 
                        if ( $findOnlyinclude ) {
                                // Ignore all input up to the next <onlyinclude>
@@ -267,9 +234,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
@@ -572,7 +539,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 +596,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 +650,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 +697,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;
        }
@@ -749,8 +706,8 @@ class Preprocessor_Hash implements Preprocessor {
 /**
  * Stack class to help Preprocessor::preprocessToObj()
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPDStack_Hash extends PPDStack {
        // @codingStandardsIgnoreEnd
 
@@ -763,8 +720,8 @@ class PPDStack_Hash extends PPDStack {
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPDStackElement_Hash extends PPDStackElement {
        // @codingStandardsIgnoreENd
 
@@ -804,8 +761,8 @@ class PPDStackElement_Hash extends PPDStackElement {
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPDPart_Hash extends PPDPart {
        // @codingStandardsIgnoreEnd
 
@@ -820,8 +777,8 @@ class PPDPart_Hash extends PPDPart {
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPDAccum_Hash {
        // @codingStandardsIgnoreEnd
 
@@ -891,8 +848,8 @@ class PPDAccum_Hash {
 /**
  * An expansion frame, used as a context to expand the result of preprocessToObj()
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPFrame_Hash implements PPFrame {
        // @codingStandardsIgnoreEnd
 
@@ -1154,7 +1111,7 @@ class PPFrame_Hash implements PPFrame {
                                        ) {
                                                $out .= $contextNode->firstChild->value;
                                        } else {
-                                               //$out .= '';
+                                               // $out .= '';
                                        }
                                } elseif ( $contextNode->name == 'ext' ) {
                                        # Extension tag
@@ -1477,8 +1434,8 @@ class PPFrame_Hash implements PPFrame {
 /**
  * Expansion frame with template arguments
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPTemplateFrame_Hash extends PPFrame_Hash {
        // @codingStandardsIgnoreEnd
 
@@ -1660,8 +1617,8 @@ class PPTemplateFrame_Hash extends PPFrame_Hash {
 /**
  * Expansion frame with custom arguments
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPCustomFrame_Hash extends PPFrame_Hash {
        // @codingStandardsIgnoreEnd
 
@@ -1713,8 +1670,8 @@ class PPCustomFrame_Hash extends PPFrame_Hash {
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPNode_Hash_Tree implements PPNode {
        // @codingStandardsIgnoreEnd
 
@@ -1938,8 +1895,8 @@ class PPNode_Hash_Tree implements PPNode {
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPNode_Hash_Text implements PPNode {
        // @codingStandardsIgnoreEnd
 
@@ -1999,8 +1956,8 @@ class PPNode_Hash_Text implements PPNode {
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPNode_Hash_Array implements PPNode {
        // @codingStandardsIgnoreEnd
 
@@ -2057,8 +2014,8 @@ class PPNode_Hash_Array implements PPNode {
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPNode_Hash_Attr implements PPNode {
        // @codingStandardsIgnoreEnd