]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/ParserCache.php
MediaWiki 1.11.0
[autoinstallsdev/mediawiki.git] / includes / ParserCache.php
1 <?php
2 /**
3  *
4  * @addtogroup Cache
5  * @todo document
6  */
7 class ParserCache {
8         /**
9          * Get an instance of this object
10          */
11         public static function &singleton() {
12                 static $instance;
13                 if ( !isset( $instance ) ) {
14                         global $parserMemc;
15                         $instance = new ParserCache( $parserMemc );
16                 }
17                 return $instance;
18         }
19
20         /**
21          * Setup a cache pathway with a given back-end storage mechanism.
22          * May be a memcached client or a BagOStuff derivative.
23          *
24          * @param object $memCached
25          */
26         function __construct( &$memCached ) {
27                 $this->mMemc =& $memCached;
28         }
29
30         function getKey( &$article, &$user ) {
31                 global $action;
32                 $hash = $user->getPageRenderingHash();
33                 if( !$article->mTitle->quickUserCan( 'edit' ) ) {
34                         // section edit links are suppressed even if the user has them on
35                         $edit = '!edit=0';
36                 } else {
37                         $edit = '';
38                 }
39                 $pageid = intval( $article->getID() );
40                 $renderkey = (int)($action == 'render');
41                 $key = wfMemcKey( 'pcache', 'idhash', "$pageid-$renderkey!$hash$edit" );
42                 return $key;
43         }
44
45         function getETag( &$article, &$user ) {
46                 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
47         }
48
49         function get( &$article, &$user ) {
50                 global $wgCacheEpoch;
51                 $fname = 'ParserCache::get';
52                 wfProfileIn( $fname );
53
54                 $key = $this->getKey( $article, $user );
55
56                 wfDebug( "Trying parser cache $key\n" );
57                 $value = $this->mMemc->get( $key );
58                 if ( is_object( $value ) ) {
59                         wfDebug( "Found.\n" );
60                         # Delete if article has changed since the cache was made
61                         $canCache = $article->checkTouched();
62                         $cacheTime = $value->getCacheTime();
63                         $touched = $article->mTouched;
64                         if ( !$canCache || $value->expired( $touched ) ) {
65                                 if ( !$canCache ) {
66                                         wfIncrStats( "pcache_miss_invalid" );
67                                         wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
68                                 } else {
69                                         wfIncrStats( "pcache_miss_expired" );
70                                         wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
71                                 }
72                                 $this->mMemc->delete( $key );
73                                 $value = false;
74                         } else {
75                                 if ( isset( $value->mTimestamp ) ) {
76                                         $article->mTimestamp = $value->mTimestamp;
77                                 }
78                                 wfIncrStats( "pcache_hit" );
79                         }
80                 } else {
81                         wfDebug( "Parser cache miss.\n" );
82                         wfIncrStats( "pcache_miss_absent" );
83                         $value = false;
84                 }
85
86                 wfProfileOut( $fname );
87                 return $value;
88         }
89
90         function save( $parserOutput, &$article, &$user ){
91                 global $wgParserCacheExpireTime;
92                 $key = $this->getKey( $article, $user );
93
94                 if( $parserOutput->getCacheTime() != -1 ) {
95
96                         $now = wfTimestampNow();
97                         $parserOutput->setCacheTime( $now );
98
99                         // Save the timestamp so that we don't have to load the revision row on view
100                         $parserOutput->mTimestamp = $article->getTimestamp();
101
102                         $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
103                         wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
104
105                         if( $parserOutput->containsOldMagic() ){
106                                 $expire = 3600; # 1 hour
107                         } else {
108                                 $expire = $wgParserCacheExpireTime;
109                         }
110                         $this->mMemc->set( $key, $parserOutput, $expire );
111
112                 } else {
113                         wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
114                 }
115         }
116
117 }
118
119