]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/parser/ParserCache.php
MediaWiki 1.15.3
[autoinstalls/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, $popts ) {
30                 global $wgRequest;
31
32                 if( $popts instanceof User )    // It used to be getKey( &$article, &$user )
33                         $popts = ParserOptions::newFromUser( $popts );
34
35                 $user = $popts->mUser;
36                 $printable = ( $popts->getIsPrintable() ) ? '!printable=1' : '';
37                 $hash = $user->getPageRenderingHash();
38                 if( !$article->mTitle->quickUserCan( 'edit' ) ) {
39                         // section edit links are suppressed even if the user has them on
40                         $edit = '!edit=0';
41                 } else {
42                         $edit = '';
43                 }
44                 $pageid = $article->getID();
45                 $renderkey = (int)($wgRequest->getVal('action') == 'render');
46                 $key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}{$edit}{$printable}" );
47                 return $key;
48         }
49
50         function getETag( &$article, $popts ) {
51                 return 'W/"' . $this->getKey($article, $popts) . "--" . $article->mTouched. '"';
52         }
53
54         function get( &$article, $popts ) {
55                 global $wgCacheEpoch;
56                 $fname = 'ParserCache::get';
57                 wfProfileIn( $fname );
58
59                 $key = $this->getKey( $article, $popts );
60
61                 wfDebug( "Trying parser cache $key\n" );
62                 $value = $this->mMemc->get( $key );
63                 if ( is_object( $value ) ) {
64                         wfDebug( "Found.\n" );
65                         # Delete if article has changed since the cache was made
66                         $canCache = $article->checkTouched();
67                         $cacheTime = $value->getCacheTime();
68                         $touched = $article->mTouched;
69                         if ( !$canCache || $value->expired( $touched ) ) {
70                                 if ( !$canCache ) {
71                                         wfIncrStats( "pcache_miss_invalid" );
72                                         wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
73                                 } else {
74                                         wfIncrStats( "pcache_miss_expired" );
75                                         wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
76                                 }
77                                 $this->mMemc->delete( $key );
78                                 $value = false;
79                         } else {
80                                 if ( isset( $value->mTimestamp ) ) {
81                                         $article->mTimestamp = $value->mTimestamp;
82                                 }
83                                 wfIncrStats( "pcache_hit" );
84                         }
85                 } else {
86                         wfDebug( "Parser cache miss.\n" );
87                         wfIncrStats( "pcache_miss_absent" );
88                         $value = false;
89                 }
90
91                 wfProfileOut( $fname );
92                 return $value;
93         }
94
95         function save( $parserOutput, &$article, $popts ){
96                 global $wgParserCacheExpireTime;
97                 $key = $this->getKey( $article, $popts );
98
99                 if( $parserOutput->getCacheTime() != -1 ) {
100
101                         $now = wfTimestampNow();
102                         $parserOutput->setCacheTime( $now );
103
104                         // Save the timestamp so that we don't have to load the revision row on view
105                         $parserOutput->mTimestamp = $article->getTimestamp();
106
107                         $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
108                         wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
109
110                         if( $parserOutput->containsOldMagic() ){
111                                 $expire = 3600; # 1 hour
112                         } else {
113                                 $expire = $wgParserCacheExpireTime;
114                         }
115                         $this->mMemc->set( $key, $parserOutput, $expire );
116
117                 } else {
118                         wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
119                 }
120         }
121
122 }