3 * Response handler for Ajax requests
10 * Handle responses for Ajax requests (send headers, print
11 * content, that sort of thing)
16 /** Number of seconds to get the response cached by a proxy */
17 private $mCacheDuration;
19 /** HTTP header Content-Type */
20 private $mContentType;
22 /** Disables output. Can be set by calling $AjaxResponse->disable() */
25 /** Date for the HTTP header Last-modified */
26 private $mLastModified;
28 /** HTTP response code */
29 private $mResponseCode;
31 /** HTTP Vary header */
34 /** Content of our HTTP response */
38 * @param $text string|null
40 function __construct( $text = null ) {
41 $this->mCacheDuration
= null;
44 $this->mDisabled
= false;
46 $this->mResponseCode
= '200 OK';
47 $this->mLastModified
= false;
48 $this->mContentType
= 'application/x-wiki';
51 $this->addText( $text );
55 function setCacheDuration( $duration ) {
56 $this->mCacheDuration
= $duration;
59 function setVary( $vary ) {
63 function setResponseCode( $code ) {
64 $this->mResponseCode
= $code;
67 function setContentType( $type ) {
68 $this->mContentType
= $type;
72 $this->mDisabled
= true;
76 * Add content to the response
79 function addText( $text ) {
80 if ( ! $this->mDisabled
&& $text ) {
81 $this->mText
.= $text;
88 function printText() {
89 if ( ! $this->mDisabled
) {
95 * Construct the header and output it
97 function sendHeaders() {
98 global $wgUseSquid, $wgUseESI;
100 if ( $this->mResponseCode
) {
101 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
102 header( "Status: " . $this->mResponseCode
, true, (int)$n );
105 header ( "Content-Type: " . $this->mContentType
);
107 if ( $this->mLastModified
) {
108 header ( "Last-Modified: " . $this->mLastModified
);
110 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
113 if ( $this->mCacheDuration
) {
114 # If squid caches are configured, tell them to cache the response,
115 # and tell the client to always check with the squid. Otherwise,
116 # tell the client to use a cached copy, without a way to purge it.
119 # Expect explicite purge of the proxy cache, but require end user agents
120 # to revalidate against the proxy on each visit.
121 # Surrogate-Control controls our Squid, Cache-Control downstream caches
124 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration
. ', content="ESI/1.0"' );
125 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
127 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration
. ', must-revalidate, max-age=0' );
132 # Let the client do the caching. Cache is not purged.
133 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT" );
134 header ( "Cache-Control: s-maxage={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" );
138 # always expired, always modified
139 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
140 header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
141 header ( "Pragma: no-cache" ); // HTTP/1.0
144 if ( $this->mVary
) {
145 header ( "Vary: " . $this->mVary
);
150 * checkLastModified tells the client to use the client-cached response if
151 * possible. If sucessful, the AjaxResponse is disabled so that
152 * any future call to AjaxResponse::printText() have no effect.
154 * @param $timestamp string
155 * @return bool Returns true if the response code was set to 304 Not Modified.
157 function checkLastModified ( $timestamp ) {
158 global $wgCachePages, $wgCacheEpoch, $wgUser;
159 $fname = 'AjaxResponse::checkLastModified';
161 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
162 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
166 if ( !$wgCachePages ) {
167 wfDebug( "$fname: CACHE DISABLED\n", false );
171 if ( $wgUser->getOption( 'nocache' ) ) {
172 wfDebug( "$fname: USER DISABLED CACHE\n", false );
176 $timestamp = wfTimestamp( TS_MW
, $timestamp );
177 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
179 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
180 # IE sends sizes after the date like this:
181 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
182 # this breaks strtotime().
183 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
184 $modsinceTime = strtotime( $modsince );
185 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
186 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
187 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
189 if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
190 ini_set( 'zlib.output_compression', 0 );
191 $this->setResponseCode( "304 Not Modified" );
193 $this->mLastModified
= $lastmod;
195 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
199 wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
200 $this->mLastModified
= $lastmod;
203 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
204 $this->mLastModified
= $lastmod;
214 function loadFromMemcached( $mckey, $touched ) {
221 $mcvalue = $wgMemc->get( $mckey );
223 # Check to see if the value has been invalidated
224 if ( $touched <= $mcvalue['timestamp'] ) {
225 wfDebug( "Got $mckey from cache\n" );
226 $this->mText
= $mcvalue['value'];
230 wfDebug( "$mckey has expired\n" );
242 function storeInMemcached( $mckey, $expiry = 86400 ) {
245 $wgMemc->set( $mckey,
247 'timestamp' => wfTimestampNow(),
248 'value' => $this->mText