2 if( !defined( 'MEDIAWIKI' ) ) {
19 function __construct( $text = NULL ) {
20 $this->mCacheDuration
= NULL;
23 $this->mDisabled
= false;
25 $this->mResponseCode
= '200 OK';
26 $this->mLastModified
= false;
27 $this->mContentType
= 'text/html; charset=utf-8';
30 $this->addText( $text );
34 function setCacheDuration( $duration ) {
35 $this->mCacheDuration
= $duration;
38 function setVary( $vary ) {
42 function setResponseCode( $code ) {
43 $this->mResponseCode
= $code;
46 function setContentType( $type ) {
47 $this->mContentType
= $type;
51 $this->mDisabled
= true;
54 function addText( $text ) {
55 if ( ! $this->mDisabled
&& $text ) {
56 $this->mText
.= $text;
60 function printText() {
61 if ( ! $this->mDisabled
) {
66 function sendHeaders() {
67 global $wgUseSquid, $wgUseESI;
69 if ( $this->mResponseCode
) {
70 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
71 header( "Status: " . $this->mResponseCode
, true, (int)$n );
74 header ("Content-Type: " . $this->mContentType
);
76 if ( $this->mLastModified
) {
77 header ("Last-Modified: " . $this->mLastModified
);
80 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
83 if ( $this->mCacheDuration
) {
85 # If squid caches are configured, tell them to cache the response,
86 # and tell the client to always check with the squid. Otherwise,
87 # tell the client to use a cached copy, without a way to purge it.
91 # Expect explicite purge of the proxy cache, but require end user agents
92 # to revalidate against the proxy on each visit.
93 # Surrogate-Control controls our Squid, Cache-Control downstream caches
96 header( 'Surrogate-Control: max-age='.$this->mCacheDuration
.', content="ESI/1.0"');
97 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
99 header( 'Cache-Control: s-maxage='.$this->mCacheDuration
.', must-revalidate, max-age=0' );
104 # Let the client do the caching. Cache is not purged.
105 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT");
106 header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
110 # always expired, always modified
111 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
112 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
113 header ("Pragma: no-cache"); // HTTP/1.0
116 if ( $this->mVary
) {
117 header ( "Vary: " . $this->mVary
);
122 * checkLastModified tells the client to use the client-cached response if
123 * possible. If sucessful, the AjaxResponse is disabled so that
124 * any future call to AjaxResponse::printText() have no effect. The method
125 * returns true iff the response code was set to 304 Not Modified.
127 function checkLastModified ( $timestamp ) {
128 global $wgCachePages, $wgCacheEpoch, $wgUser;
129 $fname = 'AjaxResponse::checkLastModified';
131 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
132 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
135 if( !$wgCachePages ) {
136 wfDebug( "$fname: CACHE DISABLED\n", false );
139 if( $wgUser->getOption( 'nocache' ) ) {
140 wfDebug( "$fname: USER DISABLED CACHE\n", false );
144 $timestamp = wfTimestamp( TS_MW
, $timestamp );
145 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->mTouched
, $wgCacheEpoch ) );
147 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
148 # IE sends sizes after the date like this:
149 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
150 # this breaks strtotime().
151 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
152 $modsinceTime = strtotime( $modsince );
153 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
154 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
155 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
156 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
157 $this->setResponseCode( "304 Not Modified" );
159 $this->mLastModified
= $lastmod;
161 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
165 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
166 $this->mLastModified
= $lastmod;
169 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
170 $this->mLastModified
= $lastmod;
174 function loadFromMemcached( $mckey, $touched ) {
176 if ( !$touched ) return false;
178 $mcvalue = $wgMemc->get( $mckey );
180 # Check to see if the value has been invalidated
181 if ( $touched <= $mcvalue['timestamp'] ) {
182 wfDebug( "Got $mckey from cache\n" );
183 $this->mText
= $mcvalue['value'];
186 wfDebug( "$mckey has expired\n" );
193 function storeInMemcached( $mckey, $expiry = 86400 ) {
196 $wgMemc->set( $mckey,
198 'timestamp' => wfTimestampNow(),
199 'value' => $this->mText