]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/parser/Preprocessor_Hash.php
MediaWiki 1.15.0
[autoinstallsdev/mediawiki.git] / includes / parser / Preprocessor_Hash.php
index 620282914d6337cc72e36d4e487a8417d686d843..f46ee40ce92209eac980617fbf21321fbec8e2a7 100644 (file)
@@ -8,6 +8,8 @@
  */
 class Preprocessor_Hash implements Preprocessor {
        var $parser;
+       
+       const CACHE_VERSION = 1;
 
        function __construct( $parser ) {
                $this->parser = $parser;
@@ -45,6 +47,31 @@ class Preprocessor_Hash implements Preprocessor {
         */
        function preprocessToObj( $text, $flags = 0 ) {
                wfProfileIn( __METHOD__ );
+       
+       
+               // Check cache.
+               global $wgMemc, $wgPreprocessorCacheThreshold;
+               
+               $cacheable = strlen( $text ) > $wgPreprocessorCacheThreshold;
+               if ( $cacheable ) {
+                       wfProfileIn( __METHOD__.'-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)" );
+                                       wfProfileOut( __METHOD__.'-cacheable' );
+                                       wfProfileOut( __METHOD__ );
+                                       return $hash;
+                               }
+                       }
+                       wfProfileIn( __METHOD__.'-cache-miss' );
+               }
 
                $rules = array(
                        '{' => array(
@@ -288,7 +315,9 @@ class Preprocessor_Hash implements Preprocessor {
                                } else {
                                        $attrEnd = $tagEndPos;
                                        // Find closing tag
-                                       if ( preg_match( "/<\/$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] );
                                                $close = $matches[0][0];
@@ -615,6 +644,16 @@ 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 );;
+                       $wgMemc->set( $cacheKey, $cacheValue, 86400 );
+                       wfProfileOut( __METHOD__.'-cache-miss' );
+                       wfProfileOut( __METHOD__.'-cacheable' );
+                       wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
+               }
+               
                wfProfileOut( __METHOD__ );
                return $rootNode;
        }