Merge "tests: Remove unused $wgMemc resets"
[lhc/web/wiklou.git] / includes / parser / Preprocessor_DOM.php
index 562062f..4ca3a87 100644 (file)
@@ -23,9 +23,9 @@
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
-class Preprocessor_DOM implements Preprocessor {
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
+class Preprocessor_DOM extends Preprocessor {
        // @codingStandardsIgnoreEnd
 
        /**
@@ -35,7 +35,7 @@ class Preprocessor_DOM implements Preprocessor {
 
        public $memoryLimit;
 
-       const CACHE_VERSION = 1;
+       const CACHE_PREFIX = 'preprocess-xml';
 
        public function __construct( $parser ) {
                $this->parser = $parser;
@@ -148,30 +148,11 @@ class Preprocessor_DOM implements Preprocessor {
         * @return PPNode_DOM
         */
        public function preprocessToObj( $text, $flags = 0 ) {
-               global $wgMemc, $wgPreprocessorCacheThreshold;
-
-               $xml = false;
-               $cacheable = ( $wgPreprocessorCacheThreshold !== false
-                       && strlen( $text ) > $wgPreprocessorCacheThreshold );
-               if ( $cacheable ) {
-                       $cacheKey = wfMemcKey( 'preprocess-xml', md5( $text ), $flags );
-                       $cacheValue = $wgMemc->get( $cacheKey );
-                       if ( $cacheValue ) {
-                               $version = substr( $cacheValue, 0, 8 );
-                               if ( intval( $version ) == self::CACHE_VERSION ) {
-                                       $xml = substr( $cacheValue, 8 );
-                                       // From the cache
-                                       wfDebugLog( "Preprocessor", "Loaded preprocessor XML from memcached (key $cacheKey)" );
-                               }
-                       }
-                       if ( $xml === false ) {
-                               $xml = $this->preprocessToXml( $text, $flags );
-                               $cacheValue = sprintf( "%08d", self::CACHE_VERSION ) . $xml;
-                               $wgMemc->set( $cacheKey, $cacheValue, 86400 );
-                               wfDebugLog( "Preprocessor", "Saved preprocessor XML to memcached (key $cacheKey)" );
-                       }
-               } else {
+
+               $xml = $this->cacheGetTree( $text, $flags );
+               if ( $xml === false ) {
                        $xml = $this->preprocessToXml( $text, $flags );
+                       $this->cacheSetTree( $text, $flags, $xml );
                }
 
                // Fail if the number of elements exceeds acceptable limits
@@ -179,8 +160,7 @@ class Preprocessor_DOM implements Preprocessor {
                $this->parser->mGeneratedPPNodeCount += substr_count( $xml, '<' );
                $max = $this->parser->mOptions->getMaxGeneratedPPNodeCount();
                if ( $this->parser->mGeneratedPPNodeCount > $max ) {
-                       if ( $cacheable ) {
-                       }
+                       // if ( $cacheable ) { ... }
                        throw new MWException( __METHOD__ . ': generated node count limit exceeded' );
                }
 
@@ -199,8 +179,7 @@ class Preprocessor_DOM implements Preprocessor {
                        $obj = new PPNode_DOM( $dom->documentElement );
                }
 
-               if ( $cacheable ) {
-               }
+               // if ( $cacheable ) { ... }
 
                if ( !$result ) {
                        throw new MWException( __METHOD__ . ' generated invalid XML' );
@@ -214,24 +193,6 @@ class Preprocessor_DOM implements Preprocessor {
         * @return string
         */
        public function preprocessToXml( $text, $flags = 0 ) {
-               $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();
@@ -349,9 +310,9 @@ class Preprocessor_DOM 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
@@ -648,7 +609,7 @@ class Preprocessor_DOM 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
@@ -717,7 +678,7 @@ class Preprocessor_DOM implements Preprocessor {
                                        $piece->parts = array( new PPDPart );
                                        $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();
@@ -848,12 +809,31 @@ class PPDStack {
  * @ingroup Parser
  */
 class PPDStackElement {
-       public $open,              // Opening character (\n for heading)
-               $close,             // Matching closing character
-               $count,             // Number of opening characters found (number of "=" for heading)
-               $parts,             // Array of PPDPart objects describing pipe-separated parts.
-               $lineStart;         // True if the open char appeared at the start of the input line.
-                                   // Not set for headings.
+       /**
+        * @var string Opening character (\n for heading)
+        */
+       public $open;
+
+       /**
+        * @var string Matching closing character
+        */
+       public $close;
+
+       /**
+        * @var int Number of opening characters found (number of "=" for heading)
+        */
+       public $count;
+
+       /**
+        * @var PPDPart[] Array of PPDPart objects describing pipe-separated parts.
+        */
+       public $parts;
+
+       /**
+        * @var bool True if the open char appeared at the start of the input line.
+        *  Not set for headings.
+        */
+       public $lineStart;
 
        public $partClass = 'PPDPart';
 
@@ -924,7 +904,10 @@ class PPDStackElement {
  * @ingroup Parser
  */
 class PPDPart {
-       public $out; // Output accumulator string
+       /**
+        * @var string Output accumulator string
+        */
+       public $out;
 
        // Optional member variables:
        //   eqpos        Position of equals sign in output accumulator
@@ -939,8 +922,8 @@ class PPDPart {
 /**
  * An expansion frame, used as a context to expand the result of preprocessToObj()
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPFrame_DOM implements PPFrame {
        // @codingStandardsIgnoreEnd
 
@@ -1553,8 +1536,8 @@ class PPFrame_DOM implements PPFrame {
 /**
  * Expansion frame with template arguments
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPTemplateFrame_DOM extends PPFrame_DOM {
        // @codingStandardsIgnoreEnd
 
@@ -1720,8 +1703,8 @@ class PPTemplateFrame_DOM extends PPFrame_DOM {
 /**
  * Expansion frame with custom arguments
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPCustomFrame_DOM extends PPFrame_DOM {
        // @codingStandardsIgnoreEnd
 
@@ -1769,8 +1752,8 @@ class PPCustomFrame_DOM extends PPFrame_DOM {
 
 /**
  * @ingroup Parser
- * @codingStandardsIgnoreStart
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPNode_DOM implements PPNode {
        // @codingStandardsIgnoreEnd