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