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)
33 * Number of seconds to get the response cached by a proxy
34 * @var int $mCacheDuration
36 private $mCacheDuration;
39 * HTTP header Content-Type
40 * @var string $mContentType
42 private $mContentType;
45 * Disables output. Can be set by calling $AjaxResponse->disable()
46 * @var bool $mDisabled
51 * Date for the HTTP header Last-modified
52 * @var string|false $mLastModified
54 private $mLastModified;
58 * @var string $mResponseCode
60 private $mResponseCode;
69 * Content of our HTTP response
75 * @param $text string|null
77 function __construct( $text = null ) {
78 $this->mCacheDuration
= null;
81 $this->mDisabled
= false;
83 $this->mResponseCode
= '200 OK';
84 $this->mLastModified
= false;
85 $this->mContentType
= 'application/x-wiki';
88 $this->addText( $text );
93 * Set the number of seconds to get the response cached by a proxy
94 * @param $duration int
96 function setCacheDuration( $duration ) {
97 $this->mCacheDuration
= $duration;
101 * Set the HTTP Vary header
102 * @param $vary string
104 function setVary( $vary ) {
105 $this->mVary
= $vary;
109 * Set the HTTP response code
110 * @param $code string
112 function setResponseCode( $code ) {
113 $this->mResponseCode
= $code;
117 * Set the HTTP header Content-Type
118 * @param $type string
120 function setContentType( $type ) {
121 $this->mContentType
= $type;
128 $this->mDisabled
= true;
132 * Add content to the response
133 * @param $text string
135 function addText( $text ) {
136 if ( ! $this->mDisabled
&& $text ) {
137 $this->mText
.= $text;
144 function printText() {
145 if ( ! $this->mDisabled
) {
151 * Construct the header and output it
153 function sendHeaders() {
154 global $wgUseSquid, $wgUseESI;
156 if ( $this->mResponseCode
) {
157 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
158 header( "Status: " . $this->mResponseCode
, true, (int)$n );
161 header ( "Content-Type: " . $this->mContentType
);
163 if ( $this->mLastModified
) {
164 header ( "Last-Modified: " . $this->mLastModified
);
166 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
169 if ( $this->mCacheDuration
) {
170 # If squid caches are configured, tell them to cache the response,
171 # and tell the client to always check with the squid. Otherwise,
172 # tell the client to use a cached copy, without a way to purge it.
175 # Expect explicit purge of the proxy cache, but require end user agents
176 # to revalidate against the proxy on each visit.
177 # Surrogate-Control controls our Squid, Cache-Control downstream caches
180 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration
. ', content="ESI/1.0"' );
181 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
183 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration
. ', must-revalidate, max-age=0' );
188 # Let the client do the caching. Cache is not purged.
189 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT" );
190 header ( "Cache-Control: s-maxage={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" );
194 # always expired, always modified
195 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
196 header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
197 header ( "Pragma: no-cache" ); // HTTP/1.0
200 if ( $this->mVary
) {
201 header ( "Vary: " . $this->mVary
);
206 * checkLastModified tells the client to use the client-cached response if
207 * possible. If successful, the AjaxResponse is disabled so that
208 * any future call to AjaxResponse::printText() have no effect.
210 * @param $timestamp string
211 * @return bool Returns true if the response code was set to 304 Not Modified.
213 function checkLastModified( $timestamp ) {
214 global $wgCachePages, $wgCacheEpoch, $wgUser;
215 $fname = 'AjaxResponse::checkLastModified';
217 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
218 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
222 if ( !$wgCachePages ) {
223 wfDebug( "$fname: CACHE DISABLED\n", false );
227 if ( $wgUser->getOption( 'nocache' ) ) {
228 wfDebug( "$fname: USER DISABLED CACHE\n", false );
232 $timestamp = wfTimestamp( TS_MW
, $timestamp );
233 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
235 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
236 # IE sends sizes after the date like this:
237 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
238 # this breaks strtotime().
239 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
240 $modsinceTime = strtotime( $modsince );
241 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
242 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
243 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
245 if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
246 ini_set( 'zlib.output_compression', 0 );
247 $this->setResponseCode( "304 Not Modified" );
249 $this->mLastModified
= $lastmod;
251 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
255 wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
256 $this->mLastModified
= $lastmod;
259 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
260 $this->mLastModified
= $lastmod;
266 * @param $mckey string
267 * @param $touched int
270 function loadFromMemcached( $mckey, $touched ) {
277 $mcvalue = $wgMemc->get( $mckey );
279 # Check to see if the value has been invalidated
280 if ( $touched <= $mcvalue['timestamp'] ) {
281 wfDebug( "Got $mckey from cache\n" );
282 $this->mText
= $mcvalue['value'];
286 wfDebug( "$mckey has expired\n" );
294 * @param $mckey string
298 function storeInMemcached( $mckey, $expiry = 86400 ) {
301 $wgMemc->set( $mckey,
303 'timestamp' => wfTimestampNow(),
304 'value' => $this->mText