2 if( !defined( 'MEDIAWIKI' ) ) {
20 function __construct( $text = NULL ) {
21 $this->mCacheDuration
= NULL;
24 $this->mDisabled
= false;
26 $this->mResponseCode
= '200 OK';
27 $this->mLastModified
= false;
28 $this->mContentType
= 'text/html; charset=utf-8';
31 $this->addText( $text );
35 function setCacheDuration( $duration ) {
36 $this->mCacheDuration
= $duration;
39 function setVary( $vary ) {
43 function setResponseCode( $code ) {
44 $this->mResponseCode
= $code;
47 function setContentType( $type ) {
48 $this->mContentType
= $type;
52 $this->mDisabled
= true;
55 function addText( $text ) {
56 if ( ! $this->mDisabled
&& $text ) {
57 $this->mText
.= $text;
61 function printText() {
62 if ( ! $this->mDisabled
) {
67 function sendHeaders() {
68 global $wgUseSquid, $wgUseESI;
70 if ( $this->mResponseCode
) {
71 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
72 header( "Status: " . $this->mResponseCode
, true, (int)$n );
75 header ("Content-Type: " . $this->mContentType
);
77 if ( $this->mLastModified
) {
78 header ("Last-Modified: " . $this->mLastModified
);
81 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
84 if ( $this->mCacheDuration
) {
86 # If squid caches are configured, tell them to cache the response,
87 # and tell the client to always check with the squid. Otherwise,
88 # tell the client to use a cached copy, without a way to purge it.
92 # Expect explicite purge of the proxy cache, but require end user agents
93 # to revalidate against the proxy on each visit.
94 # Surrogate-Control controls our Squid, Cache-Control downstream caches
97 header( 'Surrogate-Control: max-age='.$this->mCacheDuration
.', content="ESI/1.0"');
98 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
100 header( 'Cache-Control: s-maxage='.$this->mCacheDuration
.', must-revalidate, max-age=0' );
105 # Let the client do the caching. Cache is not purged.
106 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT");
107 header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
111 # always expired, always modified
112 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
113 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
114 header ("Pragma: no-cache"); // HTTP/1.0
117 if ( $this->mVary
) {
118 header ( "Vary: " . $this->mVary
);
123 * checkLastModified tells the client to use the client-cached response if
124 * possible. If sucessful, the AjaxResponse is disabled so that
125 * any future call to AjaxResponse::printText() have no effect. The method
126 * returns true iff the response code was set to 304 Not Modified.
128 function checkLastModified ( $timestamp ) {
129 global $wgCachePages, $wgCacheEpoch, $wgUser;
130 $fname = 'AjaxResponse::checkLastModified';
132 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
133 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
136 if( !$wgCachePages ) {
137 wfDebug( "$fname: CACHE DISABLED\n", false );
140 if( $wgUser->getOption( 'nocache' ) ) {
141 wfDebug( "$fname: USER DISABLED CACHE\n", false );
145 $timestamp = wfTimestamp( TS_MW
, $timestamp );
146 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->mTouched
, $wgCacheEpoch ) );
148 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
149 # IE sends sizes after the date like this:
150 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
151 # this breaks strtotime().
152 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
153 $modsinceTime = strtotime( $modsince );
154 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
155 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
156 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
157 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
158 $this->setResponseCode( "304 Not Modified" );
160 $this->mLastModified
= $lastmod;
162 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
166 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
167 $this->mLastModified
= $lastmod;
170 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
171 $this->mLastModified
= $lastmod;
175 function loadFromMemcached( $mckey, $touched ) {
177 if ( !$touched ) return false;
179 $mcvalue = $wgMemc->get( $mckey );
181 # Check to see if the value has been invalidated
182 if ( $touched <= $mcvalue['timestamp'] ) {
183 wfDebug( "Got $mckey from cache\n" );
184 $this->mText
= $mcvalue['value'];
187 wfDebug( "$mckey has expired\n" );
194 function storeInMemcached( $mckey, $expiry = 86400 ) {
197 $wgMemc->set( $mckey,
199 'timestamp' => wfTimestampNow(),
200 'value' => $this->mText