3 if( !defined( 'MEDIAWIKI' ) )
16 function AjaxResponse( $text = NULL ) {
17 $this->mCacheDuration
= NULL;
20 $this->mDisabled
= false;
22 $this->mResponseCode
= '200 OK';
23 $this->mLastModified
= false;
24 $this->mContentType
= 'text/html; charset=utf-8';
27 $this->addText( $text );
31 function setCacheDuration( $duration ) {
32 $this->mCacheDuration
= $duration;
35 function setVary( $vary ) {
39 function setResponseCode( $code ) {
40 $this->mResponseCode
= $code;
43 function setContentType( $type ) {
44 $this->mContentType
= $type;
48 $this->mDisabled
= true;
51 function addText( $text ) {
52 if ( ! $this->mDisabled
&& $text ) {
53 $this->mText
.= $text;
57 function printText() {
58 if ( ! $this->mDisabled
) {
63 function sendHeaders() {
64 global $wgUseSquid, $wgUseESI;
66 if ( $this->mResponseCode
) {
67 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
68 header( "Status: " . $this->mResponseCode
, true, (int)$n );
71 header ("Content-Type: " . $this->mContentType
);
73 if ( $this->mLastModified
) {
74 header ("Last-Modified: " . $this->mLastModified
);
77 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
80 if ( $this->mCacheDuration
) {
82 # If squid caches are configured, tell them to cache the response,
83 # and tell the client to always check with the squid. Otherwise,
84 # tell the client to use a cached copy, without a way to purge it.
88 # Expect explicite purge of the proxy cache, but require end user agents
89 # to revalidate against the proxy on each visit.
90 # Surrogate-Control controls our Squid, Cache-Control downstream caches
93 header( 'Surrogate-Control: max-age='.$this->mCacheDuration
.', content="ESI/1.0"');
94 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
96 header( 'Cache-Control: s-maxage='.$this->mCacheDuration
.', must-revalidate, max-age=0' );
101 # Let the client do the caching. Cache is not purged.
102 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT");
103 header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
107 # always expired, always modified
108 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
109 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
110 header ("Pragma: no-cache"); // HTTP/1.0
113 if ( $this->mVary
) {
114 header ( "Vary: " . $this->mVary
);
119 * checkLastModified tells the client to use the client-cached response if
120 * possible. If sucessful, the AjaxResponse is disabled so that
121 * any future call to AjaxResponse::printText() have no effect. The method
122 * returns true iff the response code was set to 304 Not Modified.
124 function checkLastModified ( $timestamp ) {
125 global $wgCachePages, $wgCacheEpoch, $wgUser;
126 $fname = 'AjaxResponse::checkLastModified';
128 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
129 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
132 if( !$wgCachePages ) {
133 wfDebug( "$fname: CACHE DISABLED\n", false );
136 if( $wgUser->getOption( 'nocache' ) ) {
137 wfDebug( "$fname: USER DISABLED CACHE\n", false );
141 $timestamp = wfTimestamp( TS_MW
, $timestamp );
142 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->mTouched
, $wgCacheEpoch ) );
144 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
145 # IE sends sizes after the date like this:
146 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
147 # this breaks strtotime().
148 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
149 $modsinceTime = strtotime( $modsince );
150 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
151 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
152 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
153 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
154 $this->setResponseCode( "304 Not Modified" );
156 $this->mLastModified
= $lastmod;
158 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
162 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
163 $this->mLastModified
= $lastmod;
166 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
167 $this->mLastModified
= $lastmod;
171 function loadFromMemcached( $mckey, $touched ) {
173 if ( !$touched ) return false;
175 $mcvalue = $wgMemc->get( $mckey );
177 # Check to see if the value has been invalidated
178 if ( $touched <= $mcvalue['timestamp'] ) {
179 wfDebug( "Got $mckey from cache\n" );
180 $this->mText
= $mcvalue['value'];
183 wfDebug( "$mckey has expired\n" );
190 function storeInMemcached( $mckey, $expiry = 86400 ) {
193 $wgMemc->set( $mckey,
195 'timestamp' => wfTimestampNow(),
196 'value' => $this->mText