2 if( !defined( 'MEDIAWIKI' ) ) {
12 /** Number of seconds to get the response cached by a proxy */
13 private $mCacheDuration;
15 /** HTTP header Content-Type */
16 private $mContentType;
21 /** Date for the HTTP header Last-modified */
22 private $mLastModified;
24 /** HTTP response code */
25 private $mResponseCode;
27 /** HTTP Vary header */
30 /** Content of our HTTP response */
33 function __construct( $text = NULL ) {
34 $this->mCacheDuration
= NULL;
37 $this->mDisabled
= false;
39 $this->mResponseCode
= '200 OK';
40 $this->mLastModified
= false;
41 $this->mContentType
= 'text/html; charset=utf-8';
44 $this->addText( $text );
48 function setCacheDuration( $duration ) {
49 $this->mCacheDuration
= $duration;
52 function setVary( $vary ) {
56 function setResponseCode( $code ) {
57 $this->mResponseCode
= $code;
60 function setContentType( $type ) {
61 $this->mContentType
= $type;
65 $this->mDisabled
= true;
68 /** Add content to the response */
69 function addText( $text ) {
70 if ( ! $this->mDisabled
&& $text ) {
71 $this->mText
.= $text;
76 function printText() {
77 if ( ! $this->mDisabled
) {
82 /** Construct the header and output it */
83 function sendHeaders() {
84 global $wgUseSquid, $wgUseESI;
86 if ( $this->mResponseCode
) {
87 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
88 header( "Status: " . $this->mResponseCode
, true, (int)$n );
91 header ("Content-Type: " . $this->mContentType
);
93 if ( $this->mLastModified
) {
94 header ("Last-Modified: " . $this->mLastModified
);
97 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
100 if ( $this->mCacheDuration
) {
102 # If squid caches are configured, tell them to cache the response,
103 # and tell the client to always check with the squid. Otherwise,
104 # tell the client to use a cached copy, without a way to purge it.
108 # Expect explicite purge of the proxy cache, but require end user agents
109 # to revalidate against the proxy on each visit.
110 # Surrogate-Control controls our Squid, Cache-Control downstream caches
113 header( 'Surrogate-Control: max-age='.$this->mCacheDuration
.', content="ESI/1.0"');
114 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
116 header( 'Cache-Control: s-maxage='.$this->mCacheDuration
.', must-revalidate, max-age=0' );
121 # Let the client do the caching. Cache is not purged.
122 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT");
123 header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
127 # always expired, always modified
128 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
129 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
130 header ("Pragma: no-cache"); // HTTP/1.0
133 if ( $this->mVary
) {
134 header ( "Vary: " . $this->mVary
);
139 * checkLastModified tells the client to use the client-cached response if
140 * possible. If sucessful, the AjaxResponse is disabled so that
141 * any future call to AjaxResponse::printText() have no effect. The method
142 * returns true iff the response code was set to 304 Not Modified.
144 function checkLastModified ( $timestamp ) {
145 global $wgCachePages, $wgCacheEpoch, $wgUser;
146 $fname = 'AjaxResponse::checkLastModified';
148 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
149 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
152 if( !$wgCachePages ) {
153 wfDebug( "$fname: CACHE DISABLED\n", false );
156 if( $wgUser->getOption( 'nocache' ) ) {
157 wfDebug( "$fname: USER DISABLED CACHE\n", false );
161 $timestamp = wfTimestamp( TS_MW
, $timestamp );
162 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->mTouched
, $wgCacheEpoch ) );
164 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
165 # IE sends sizes after the date like this:
166 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
167 # this breaks strtotime().
168 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
169 $modsinceTime = strtotime( $modsince );
170 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
171 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
172 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
173 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
174 $this->setResponseCode( "304 Not Modified" );
176 $this->mLastModified
= $lastmod;
178 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
182 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
183 $this->mLastModified
= $lastmod;
186 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
187 $this->mLastModified
= $lastmod;
191 function loadFromMemcached( $mckey, $touched ) {
193 if ( !$touched ) return false;
195 $mcvalue = $wgMemc->get( $mckey );
197 # Check to see if the value has been invalidated
198 if ( $touched <= $mcvalue['timestamp'] ) {
199 wfDebug( "Got $mckey from cache\n" );
200 $this->mText
= $mcvalue['value'];
203 wfDebug( "$mckey has expired\n" );
210 function storeInMemcached( $mckey, $expiry = 86400 ) {
213 $wgMemc->set( $mckey,
215 'timestamp' => wfTimestampNow(),
216 'value' => $this->mText