* Two new functions, wfUsePHP() and wfUseMW() which work like "use VERSION" in
[lhc/web/wiklou.git] / includes / ParserCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 *
10 * @package MediaWiki
11 */
12 class ParserCache {
13 /**
14 * Setup a cache pathway with a given back-end storage mechanism.
15 * May be a memcached client or a BagOStuff derivative.
16 *
17 * @param object $memCached
18 */
19 function ParserCache( &$memCached ) {
20 $this->mMemc =& $memCached;
21 }
22
23 function getKey( &$article, &$user ) {
24 global $wgDBname, $action;
25 $hash = $user->getPageRenderingHash();
26 if( !$article->mTitle->userCanEdit() ) {
27 // section edit links are suppressed even if the user has them on
28 $edit = '!edit=0';
29 } else {
30 $edit = '';
31 }
32 $pageid = intval( $article->getID() );
33 $renderkey = (int)($action == 'render');
34 $key = "$wgDBname:pcache:idhash:$pageid-$renderkey!$hash$edit";
35 return $key;
36 }
37
38 function getETag( &$article, &$user ) {
39 return 'W/"' . $this->getKey($article, $user) . "--" . $article->mTouched. '"';
40 }
41
42 function get( &$article, &$user ) {
43 global $wgCacheEpoch;
44 $fname = 'ParserCache::get';
45 wfProfileIn( $fname );
46
47 $hash = $user->getPageRenderingHash();
48 $pageid = intval( $article->getID() );
49 $key = $this->getKey( $article, $user );
50
51 wfDebug( "Trying parser cache $key\n" );
52 $value = $this->mMemc->get( $key );
53 if ( is_object( $value ) ) {
54 wfDebug( "Found.\n" );
55 # Delete if article has changed since the cache was made
56 $canCache = $article->checkTouched();
57 $cacheTime = $value->getCacheTime();
58 $touched = $article->mTouched;
59 if ( !$canCache || $value->expired( $touched ) ) {
60 if ( !$canCache ) {
61 wfIncrStats( "pcache_miss_invalid" );
62 wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
63 } else {
64 wfIncrStats( "pcache_miss_expired" );
65 wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
66 }
67 $this->mMemc->delete( $key );
68 $value = false;
69
70 } else {
71 wfIncrStats( "pcache_hit" );
72 }
73 } else {
74 wfDebug( "Parser cache miss.\n" );
75 wfIncrStats( "pcache_miss_absent" );
76 $value = false;
77 }
78
79 wfProfileOut( $fname );
80 return $value;
81 }
82
83 function save( $parserOutput, &$article, &$user ){
84 $key = $this->getKey( $article, $user );
85 $now = wfTimestampNow();
86 $parserOutput->setCacheTime( $now );
87 $parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
88 wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
89
90 if( $parserOutput->containsOldMagic() ){
91 $expire = 3600; # 1 hour
92 } else {
93 $expire = 86400; # 1 day
94 }
95 $this->mMemc->set( $key, $parserOutput, $expire );
96 }
97 }
98
99
100 ?>