Refactored parser output handling slightly, and added a hook function, to allow exten...
[lhc/web/wiklou.git] / includes / ParserCache.php
index 70b9943..612790a 100644 (file)
 <?php
+/**
+ *
+ * @package MediaWiki
+ * @subpackage Cache
+ */
 
-class ParserCache
-{
-       function get( &$article, &$user ){
-               $hash = $user->getPageRenderingHash();
-               $pageid = intval( $id );
-               $res = wfQuery("SELECT pc_data FROM parsercache WHERE pc_pageid = {$pageid} ".
-                       " AND pc_prefhash = '{$hash}' AND pc_expire > NOW()", DB_WRITE);
-               $row = wfFetchObject ( $res );
-               if( $row ){
-                       $retVal = unserialize( gzuncompress($row->pc_data) );
-                       wfProfileOut( $fname );
-               } else {
-                       $retVal = false;
+/**
+ *
+ * @package MediaWiki
+ */
+class ParserCache {
+       /**
+        * Get an instance of this object
+        */
+       public static function &singleton() {
+               static $instance;
+               if ( !isset( $instance ) ) {
+                       global $parserMemc;
+                       $instance = new ParserCache( $parserMemc );
                }
-               return $retVal;
+               return $instance;
        }
 
-       function save( $parserOutput, &$article, &$user ){
+       /**
+        * Setup a cache pathway with a given back-end storage mechanism.
+        * May be a memcached client or a BagOStuff derivative.
+        *
+        * @param object $memCached
+        */
+       function ParserCache( &$memCached ) {
+               $this->mMemc =& $memCached;
+       }
+
+       function getKey( &$article, &$user ) {
+               global $wgDBname, $action;
                $hash = $user->getPageRenderingHash();
-               $pageid = intval( $article->getID() );
-               $title = wfStrencode( $article->mTitle->getPrefixedDBKey() );
-               $ser = addslashes( gzcompress( serialize( $parserOutput ) ) );
-               if( $parserOutput->containsOldMagic() ){
-                       $expire = "1 HOUR";
+               if( !$article->mTitle->userCanEdit() ) {
+                       // section edit links are suppressed even if the user has them on
+                       $edit = '!edit=0';
                } else {
-                       $expire = "7 DAY";
+                       $edit = '';
                }
-
-               wfQuery("REPLACE INTO parsercache (pc_prefhash,pc_pageid,pc_title,pc_data, pc_expire) ".
-                       "VALUES('{$hash}', {$pageid}, '{$title}', '{$ser}', ".
-                               "DATE_ADD(NOW(), INTERVAL {$expire}))", DB_WRITE);
-
-               if( rand() % 50 == 0 ){ // more efficient to just do it sometimes
-                       $this->purge();
-               }
-       }
-       
-       function purge(){
-               wfQuery("DELETE FROM parsercache WHERE pc_expire < NOW() LIMIT 250", DB_WRITE);
+               $pageid = intval( $article->getID() );
+               $renderkey = (int)($action == 'render');
+               $key = "$wgDBname:pcache:idhash:$pageid-$renderkey!$hash$edit";
+               return $key;
        }
 
-       function clearLinksTo( $pid ){
-               $pid = intval( $pid );
-               wfQuery("DELETE parsercache FROM parsercache,links ".
-                       "WHERE pc_pageid=links.l_from AND l_to={$pid}", DB_WRITE);
-               wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE);
+       function getETag( &$article, &$user ) {
+               return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
        }
 
-       # $title is a prefixed db title, for example like Title->getPrefixedDBkey() returns.
-       function clearBrokenLinksTo( $title ){
-               $title = wfStrencode( $title );
-               wfQuery("DELETE parsercache FROM parsercache,brokenlinks ".
-                       "WHERE pc_pageid=bl_from AND bl_to='{$title}'", DB_WRITE);
+       function get( &$article, &$user ) {
+               global $wgCacheEpoch;
+               $fname = 'ParserCache::get';
+               wfProfileIn( $fname );
+
+               $hash = $user->getPageRenderingHash();
+               $pageid = intval( $article->getID() );
+               $key = $this->getKey( $article, $user );
+
+               wfDebug( "Trying parser cache $key\n" );
+               $value = $this->mMemc->get( $key );
+               if ( is_object( $value ) ) {
+                       wfDebug( "Found.\n" );
+                       # Delete if article has changed since the cache was made
+                       $canCache = $article->checkTouched();
+                       $cacheTime = $value->getCacheTime();
+                       $touched = $article->mTouched;
+                       if ( !$canCache || $value->expired( $touched ) ) {
+                               if ( !$canCache ) {
+                                       wfIncrStats( "pcache_miss_invalid" );
+                                       wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
+                               } else {
+                                       wfIncrStats( "pcache_miss_expired" );
+                                       wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
+                               }
+                               $this->mMemc->delete( $key );
+                               $value = false;
+                       } else {
+                               if ( isset( $value->mTimestamp ) ) {
+                                       $article->mTimestamp = $value->mTimestamp;
+                               }
+                               wfIncrStats( "pcache_hit" );
+                       }
+               } else {
+                       wfDebug( "Parser cache miss.\n" );
+                       wfIncrStats( "pcache_miss_absent" );
+                       $value = false;
+               }
+
+               wfProfileOut( $fname );
+               return $value;
        }
 
-       # $pid is a page id
-       function clearPage( $pid, $namespace ){
-               $pid = intval( $pid );
-               if( $namespace == NS_MEDIAWIKI ){
-                       $this->clearLinksTo( $pid );
+       function save( $parserOutput, &$article, &$user ){
+               global $wgParserCacheExpireTime;
+               $key = $this->getKey( $article, $user );
+               
+               if( $parserOutput->getCacheTime() != -1 ) {
+               
+                       $now = wfTimestampNow();
+                       $parserOutput->setCacheTime( $now );
+       
+                       // Save the timestamp so that we don't have to load the revision row on view
+                       $parserOutput->mTimestamp = $article->getTimestamp();
+                       
+                       $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
+                       wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
+       
+                       if( $parserOutput->containsOldMagic() ){
+                               $expire = 3600; # 1 hour
+                       } else {
+                               $expire = $wgParserCacheExpireTime;
+                       }
+                       $this->mMemc->set( $key, $parserOutput, $expire );
+               
                } else {
-                       wfQuery("DELETE FROM parsercache WHERE pc_pageid='{$pid}'", DB_WRITE);
+                       wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
                }
+               
        }
+       
 }
 
-
 ?>