wiki.techinc.nl/includes/parser/ParserCache.php

126 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/**
* @ingroup Cache Parser
* @todo document
*/
class ParserCache {
/**
* Get an instance of this object
*/
public static function singleton() {
static $instance;
if ( !isset( $instance ) ) {
global $parserMemc;
$instance = new ParserCache( $parserMemc );
}
return $instance;
}
2006-01-07 13:31:29 +00:00
/**
* Setup a cache pathway with a given back-end storage mechanism.
* May be a memcached client or a BagOStuff derivative.
*
2010-05-15 20:16:26 +00:00
* @param $memCached Object
*/
function __construct( $memCached ) {
$this->mMemc = $memCached;
}
function getKey( $article, $popts ) {
global $wgRequest;
if( $popts instanceof User ) // It used to be getKey( &$article, &$user )
$popts = ParserOptions::newFromUser( $popts );
$user = $popts->mUser;
$printable = ( $popts->getIsPrintable() ) ? '!printable=1' : '';
$hash = $user->getPageRenderingHash();
Revert r38196, r38204 -- "(bugs 6089, 13079) Show edit section links for transcluded template if, and only if the user can edit it, made Title::getUserPermissionsErrorsInternal() public so that it can be used in Parser and it can pass the User object from ParserOptions. " & co Cause regression in 19 parser test cases, looks like messing up the tooltips for section edit links. 19 previously failing test(s) now PASSING! :) * Bug 6563: Edit link generation for section shown by <includeonly> [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Bug 6563: Edit link generation for section suppressed by <includeonly> [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Basic section headings [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Section headings with TOC [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Handling of sections up to level 6 and beyond [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * TOC regression (bug 9764) [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * TOC with wgMaxTocLevel=3 (bug 6204) [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Resolving duplicate section names [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Resolving duplicate section names with differing case (bug 10721) [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Template with sections, __NOTOC__ [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Link inside a section heading [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * TOC regression (bug 12077) [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Fuzz testing: Parser14 [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Fuzz testing: Parser14-table [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Inclusion of !userCanEdit() content [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Out-of-order TOC heading levels [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * -{}- tags within headlines (within html for parserConvert()) [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * Morwen/13: Unclosed link followed by heading [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)] * HHP2.2: Heuristics for headings in preprocessor parenthetical structures [Fixed between 29-Jul-2008 22:42:06, 1.14alpha (r38207) and 29-Jul-2008 23:54:51, 1.14alpha (r38207)]
2008-07-29 23:56:30 +00:00
if( !$article->mTitle->quickUserCan( 'edit' ) ) {
// section edit links are suppressed even if the user has them on
$edit = '!edit=0';
} else {
$edit = '';
}
$pageid = $article->getID();
$renderkey = (int)($wgRequest->getVal('action') == 'render');
$key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}{$edit}{$printable}" );
2004-05-27 15:24:04 +00:00
return $key;
}
function getETag( $article, $popts ) {
return 'W/"' . $this->getKey($article, $popts) . "--" . $article->mTouched. '"';
}
function getDirty( $article, $popts ) {
$key = $this->getKey( $article, $popts );
2004-05-30 08:18:40 +00:00
wfDebug( "Trying parser cache $key\n" );
$value = $this->mMemc->get( $key );
return is_object( $value ) ? $value : false;
}
function get( $article, $popts ) {
global $wgCacheEpoch;
wfProfileIn( __METHOD__ );
$value = $this->getDirty( $article, $popts );
if ( !$value ) {
2004-10-23 10:22:38 +00:00
wfDebug( "Parser cache miss.\n" );
2005-06-19 03:05:51 +00:00
wfIncrStats( "pcache_miss_absent" );
wfProfileOut( __METHOD__ );
return false;
}
wfDebug( "Found.\n" );
# Invalid if article has changed since the cache was made
$canCache = $article->checkTouched();
$cacheTime = $value->getCacheTime();
$touched = $article->mTouched;
if ( !$canCache || $value->expired( $touched ) ) {
if ( !$canCache ) {
wfIncrStats( "pcache_miss_invalid" );
wfDebug( "Invalid cached redirect, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
} else {
wfIncrStats( "pcache_miss_expired" );
wfDebug( "Key expired, touched $touched, epoch $wgCacheEpoch, cached $cacheTime\n" );
}
2004-05-27 15:24:04 +00:00
$value = false;
} else {
if ( isset( $value->mTimestamp ) ) {
$article->mTimestamp = $value->mTimestamp;
}
wfIncrStats( "pcache_hit" );
}
wfProfileOut( __METHOD__ );
2004-05-27 15:24:04 +00:00
return $value;
}
function save( $parserOutput, $article, $popts ){
global $wgParserCacheExpireTime;
$key = $this->getKey( $article, $popts );
2007-04-21 21:35:21 +00:00
if( $parserOutput->getCacheTime() != -1 ) {
2007-04-21 21:35:21 +00:00
$now = wfTimestampNow();
$parserOutput->setCacheTime( $now );
2007-04-21 21:35:21 +00:00
// Save the timestamp so that we don't have to load the revision row on view
$parserOutput->mTimestamp = $article->getTimestamp();
2007-04-21 21:35:21 +00:00
$parserOutput->mText .= "\n<!-- Saved in parser cache with key $key and timestamp $now -->\n";
wfDebug( "Saved in parser cache with key $key and timestamp $now\n" );
2007-04-21 21:35:21 +00:00
if( $parserOutput->containsOldMagic() ){
$expire = 3600; # 1 hour
} else {
$expire = $wgParserCacheExpireTime;
}
$this->mMemc->set( $key, $parserOutput, $expire );
2007-04-21 21:35:21 +00:00
} else {
wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
}
}
2007-04-21 21:35:21 +00:00
}