7 if ( !defined( 'MEDIAWIKI' ) ) {
12 * Handle responses for Ajax requests (send headers, print
13 * content, that sort of thing)
18 /** Number of seconds to get the response cached by a proxy */
19 private $mCacheDuration;
21 /** HTTP header Content-Type */
22 private $mContentType;
24 /** Disables output. Can be set by calling $AjaxResponse->disable() */
27 /** Date for the HTTP header Last-modified */
28 private $mLastModified;
30 /** HTTP response code */
31 private $mResponseCode;
33 /** HTTP Vary header */
36 /** Content of our HTTP response */
39 function __construct( $text = null ) {
40 $this->mCacheDuration
= null;
43 $this->mDisabled
= false;
45 $this->mResponseCode
= '200 OK';
46 $this->mLastModified
= false;
47 $this->mContentType
= 'application/x-wiki';
50 $this->addText( $text );
54 function setCacheDuration( $duration ) {
55 $this->mCacheDuration
= $duration;
58 function setVary( $vary ) {
62 function setResponseCode( $code ) {
63 $this->mResponseCode
= $code;
66 function setContentType( $type ) {
67 $this->mContentType
= $type;
71 $this->mDisabled
= true;
74 /** Add content to the response */
75 function addText( $text ) {
76 if ( ! $this->mDisabled
&& $text ) {
77 $this->mText
.= $text;
82 function printText() {
83 if ( ! $this->mDisabled
) {
88 /** Construct the header and output it */
89 function sendHeaders() {
90 global $wgUseSquid, $wgUseESI;
92 if ( $this->mResponseCode
) {
93 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
94 header( "Status: " . $this->mResponseCode
, true, (int)$n );
97 header ( "Content-Type: " . $this->mContentType
);
99 if ( $this->mLastModified
) {
100 header ( "Last-Modified: " . $this->mLastModified
);
102 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
105 if ( $this->mCacheDuration
) {
106 # If squid caches are configured, tell them to cache the response,
107 # and tell the client to always check with the squid. Otherwise,
108 # tell the client to use a cached copy, without a way to purge it.
111 # Expect explicite purge of the proxy cache, but require end user agents
112 # to revalidate against the proxy on each visit.
113 # Surrogate-Control controls our Squid, Cache-Control downstream caches
116 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration
. ', content="ESI/1.0"' );
117 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
119 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration
. ', must-revalidate, max-age=0' );
124 # Let the client do the caching. Cache is not purged.
125 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT" );
126 header ( "Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" );
130 # always expired, always modified
131 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
132 header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
133 header ( "Pragma: no-cache" ); // HTTP/1.0
136 if ( $this->mVary
) {
137 header ( "Vary: " . $this->mVary
);
142 * checkLastModified tells the client to use the client-cached response if
143 * possible. If sucessful, the AjaxResponse is disabled so that
144 * any future call to AjaxResponse::printText() have no effect. The method
145 * returns true iff the response code was set to 304 Not Modified.
147 function checkLastModified ( $timestamp ) {
148 global $wgCachePages, $wgCacheEpoch, $wgUser;
149 $fname = 'AjaxResponse::checkLastModified';
151 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
152 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
156 if ( !$wgCachePages ) {
157 wfDebug( "$fname: CACHE DISABLED\n", false );
161 if ( $wgUser->getOption( 'nocache' ) ) {
162 wfDebug( "$fname: USER DISABLED CACHE\n", false );
166 $timestamp = wfTimestamp( TS_MW
, $timestamp );
167 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->mTouched
, $wgCacheEpoch ) );
169 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
170 # IE sends sizes after the date like this:
171 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
172 # this breaks strtotime().
173 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
174 $modsinceTime = strtotime( $modsince );
175 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
176 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
177 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
179 if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
180 ini_set( 'zlib.output_compression', 0 );
181 $this->setResponseCode( "304 Not Modified" );
183 $this->mLastModified
= $lastmod;
185 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
189 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
190 $this->mLastModified
= $lastmod;
193 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
194 $this->mLastModified
= $lastmod;
198 function loadFromMemcached( $mckey, $touched ) {
205 $mcvalue = $wgMemc->get( $mckey );
207 # Check to see if the value has been invalidated
208 if ( $touched <= $mcvalue['timestamp'] ) {
209 wfDebug( "Got $mckey from cache\n" );
210 $this->mText
= $mcvalue['value'];
214 wfDebug( "$mckey has expired\n" );
221 function storeInMemcached( $mckey, $expiry = 86400 ) {
224 $wgMemc->set( $mckey,
226 'timestamp' => wfTimestampNow(),
227 'value' => $this->mText