Added configuration global $wgDisableQueryPageUpdate to disable certain pages from...
[mediawiki.git] / includes / ParserCache.php
blob37a42b7f5cbf672ef61aa513094ff6bd23f8327d
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage Cache
6 */
8 /**
10 * @package MediaWiki
12 class ParserCache {
13 /**
14 * Get an instance of this object
16 public static function &singleton() {
17 static $instance;
18 if ( !isset( $instance ) ) {
19 global $parserMemc;
20 $instance = new ParserCache( $parserMemc );
22 return $instance;
25 /**
26 * Setup a cache pathway with a given back-end storage mechanism.
27 * May be a memcached client or a BagOStuff derivative.
29 * @param object $memCached
31 function ParserCache( &$memCached ) {
32 $this->mMemc =& $memCached;
35 function getKey( &$article, &$user ) {
36 global $action;
37 $hash = $user->getPageRenderingHash();
38 if( !$article->mTitle->userCanEdit() ) {
39 // section edit links are suppressed even if the user has them on
40 $edit = '!edit=0';
41 } else {
42 $edit = '';
44 $pageid = intval( $article->getID() );
45 $renderkey = (int)($action == 'render');
46 $key = wfMemcKey( 'pcache', 'idhash', "$pageid-$renderkey!$hash$edit" );
47 return $key;
50 function getETag( &$article, &$user ) {
51 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
54 function get( &$article, &$user ) {
55 global $wgCacheEpoch;
56 $fname = 'ParserCache::get';
57 wfProfileIn( $fname );
59 $key = $this->getKey( $article, $user );
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" );
77 $this->mMemc->delete( $key );
78 $value = false;
79 } else {
80 if ( isset( $value->mTimestamp ) ) {
81 $article->mTimestamp = $value->mTimestamp;
83 wfIncrStats( "pcache_hit" );
85 } else {
86 wfDebug( "Parser cache miss.\n" );
87 wfIncrStats( "pcache_miss_absent" );
88 $value = false;
91 wfProfileOut( $fname );
92 return $value;
95 function save( $parserOutput, &$article, &$user ){
96 global $wgParserCacheExpireTime;
97 $key = $this->getKey( $article, $user );
99 if( $parserOutput->getCacheTime() != -1 ) {
101 $now = wfTimestampNow();
102 $parserOutput->setCacheTime( $now );
104 // Save the timestamp so that we don't have to load the revision row on view
105 $parserOutput->mTimestamp = $article->getTimestamp();
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" );
110 if( $parserOutput->containsOldMagic() ){
111 $expire = 3600; # 1 hour
112 } else {
113 $expire = $wgParserCacheExpireTime;
115 $this->mMemc->set( $key, $parserOutput, $expire );
117 } else {
118 wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );