3 * Response handler for Ajax requests.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Handle responses for Ajax requests (send headers, print
26 * content, that sort of thing)
32 * Number of seconds to get the response cached by a proxy
33 * @var int $mCacheDuration
35 private $mCacheDuration;
38 * HTTP header Content-Type
39 * @var string $mContentType
41 private $mContentType;
44 * Disables output. Can be set by calling $AjaxResponse->disable()
45 * @var bool $mDisabled
50 * Date for the HTTP header Last-modified
51 * @var string|bool $mLastModified
53 private $mLastModified;
57 * @var string $mResponseCode
59 private $mResponseCode;
68 * Content of our HTTP response
74 * @param string|null $text
76 function __construct( $text = null ) {
77 $this->mCacheDuration
= null;
80 $this->mDisabled
= false;
82 $this->mResponseCode
= '200 OK';
83 $this->mLastModified
= false;
84 $this->mContentType
= 'application/x-wiki';
87 $this->addText( $text );
92 * Set the number of seconds to get the response cached by a proxy
93 * @param int $duration
95 function setCacheDuration( $duration ) {
96 $this->mCacheDuration
= $duration;
100 * Set the HTTP Vary header
101 * @param string $vary
103 function setVary( $vary ) {
104 $this->mVary
= $vary;
108 * Set the HTTP response code
109 * @param string $code
111 function setResponseCode( $code ) {
112 $this->mResponseCode
= $code;
116 * Set the HTTP header Content-Type
117 * @param string $type
119 function setContentType( $type ) {
120 $this->mContentType
= $type;
127 $this->mDisabled
= true;
131 * Add content to the response
132 * @param string $text
134 function addText( $text ) {
135 if ( ! $this->mDisabled
&& $text ) {
136 $this->mText
.= $text;
143 function printText() {
144 if ( ! $this->mDisabled
) {
150 * Construct the header and output it
152 function sendHeaders() {
153 global $wgUseSquid, $wgUseESI;
155 if ( $this->mResponseCode
) {
156 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
157 header( "Status: " . $this->mResponseCode
, true, (int)$n );
160 header ( "Content-Type: " . $this->mContentType
);
162 if ( $this->mLastModified
) {
163 header ( "Last-Modified: " . $this->mLastModified
);
165 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
168 if ( $this->mCacheDuration
) {
169 # If squid caches are configured, tell them to cache the response,
170 # and tell the client to always check with the squid. Otherwise,
171 # tell the client to use a cached copy, without a way to purge it.
174 # Expect explicit purge of the proxy cache, but require end user agents
175 # to revalidate against the proxy on each visit.
176 # Surrogate-Control controls our Squid, Cache-Control downstream caches
179 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration
. ', content="ESI/1.0"' );
180 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
182 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration
. ', must-revalidate, max-age=0' );
186 # Let the client do the caching. Cache is not purged.
187 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT" );
188 header ( "Cache-Control: s-maxage={$this->mCacheDuration}," .
189 "public,max-age={$this->mCacheDuration}" );
193 # always expired, always modified
194 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
195 header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
196 header ( "Pragma: no-cache" ); // HTTP/1.0
199 if ( $this->mVary
) {
200 header ( "Vary: " . $this->mVary
);
205 * checkLastModified tells the client to use the client-cached response if
206 * possible. If successful, the AjaxResponse is disabled so that
207 * any future call to AjaxResponse::printText() have no effect.
209 * @param string $timestamp
210 * @return bool Returns true if the response code was set to 304 Not Modified.
212 function checkLastModified( $timestamp ) {
213 global $wgCachePages, $wgCacheEpoch, $wgUser;
214 $fname = 'AjaxResponse::checkLastModified';
216 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
217 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n", 'log' );
221 if ( !$wgCachePages ) {
222 wfDebug( "$fname: CACHE DISABLED\n", 'log' );
226 $timestamp = wfTimestamp( TS_MW
, $timestamp );
227 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
229 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
230 # IE sends sizes after the date like this:
231 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
232 # this breaks strtotime().
233 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
234 $modsinceTime = strtotime( $modsince );
235 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
236 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", 'log' );
237 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", 'log' );
239 if ( ( $ismodsince >= $timestamp )
240 && $wgUser->validateCache( $ismodsince ) &&
241 $ismodsince >= $wgCacheEpoch
243 ini_set( 'zlib.output_compression', 0 );
244 $this->setResponseCode( "304 Not Modified" );
246 $this->mLastModified
= $lastmod;
248 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
249 "page: $timestamp ; site $wgCacheEpoch\n", 'log' );
253 wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
254 "page: $timestamp ; site $wgCacheEpoch\n", 'log' );
255 $this->mLastModified
= $lastmod;
258 wfDebug( "$fname: client did not send If-Modified-Since header\n", 'log' );
259 $this->mLastModified
= $lastmod;
265 * @param string $mckey
266 * @param int $touched
269 function loadFromMemcached( $mckey, $touched ) {
276 $mcvalue = $wgMemc->get( $mckey );
278 # Check to see if the value has been invalidated
279 if ( $touched <= $mcvalue['timestamp'] ) {
280 wfDebug( "Got $mckey from cache\n" );
281 $this->mText
= $mcvalue['value'];
285 wfDebug( "$mckey has expired\n" );
293 * @param string $mckey
297 function storeInMemcached( $mckey, $expiry = 86400 ) {
300 $wgMemc->set( $mckey,
302 'timestamp' => wfTimestampNow(),
303 'value' => $this->mText