Non-word characters don't terminate tag names.
[mediawiki.git] / includes / AjaxResponse.php
blobd5536529e9846b9a308aabd7dc9a93ed44cd51a0
1 <?php
2 /**
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
20 * @file
21 * @ingroup Ajax
24 /**
25 * Handle responses for Ajax requests (send headers, print
26 * content, that sort of thing)
28 * @ingroup Ajax
30 class AjaxResponse {
32 /**
33 * Number of seconds to get the response cached by a proxy
34 * @var int $mCacheDuration
36 private $mCacheDuration;
38 /**
39 * HTTP header Content-Type
40 * @var string $mContentType
42 private $mContentType;
44 /**
45 * Disables output. Can be set by calling $AjaxResponse->disable()
46 * @var bool $mDisabled
48 private $mDisabled;
50 /**
51 * Date for the HTTP header Last-modified
52 * @var string|false $mLastModified
54 private $mLastModified;
56 /**
57 * HTTP response code
58 * @var string $mResponseCode
60 private $mResponseCode;
62 /**
63 * HTTP Vary header
64 * @var string $mVary
66 private $mVary;
68 /**
69 * Content of our HTTP response
70 * @var string $mText
72 private $mText;
74 /**
75 * @param $text string|null
77 function __construct( $text = null ) {
78 $this->mCacheDuration = null;
79 $this->mVary = null;
81 $this->mDisabled = false;
82 $this->mText = '';
83 $this->mResponseCode = '200 OK';
84 $this->mLastModified = false;
85 $this->mContentType = 'application/x-wiki';
87 if ( $text ) {
88 $this->addText( $text );
92 /**
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;
125 * Disable output.
127 function disable() {
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;
142 * Output text
144 function printText() {
145 if ( ! $this->mDisabled ) {
146 print $this->mText;
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 );
165 } else {
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.
174 if ( $wgUseSquid ) {
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
179 if ( $wgUseESI ) {
180 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' );
181 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
182 } else {
183 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
186 } else {
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}" );
193 } else {
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" );
219 return false;
222 if ( !$wgCachePages ) {
223 wfDebug( "$fname: CACHE DISABLED\n", false );
224 return false;
227 if ( $wgUser->getOption( 'nocache' ) ) {
228 wfDebug( "$fname: USER DISABLED CACHE\n", false );
229 return 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" );
248 $this->disable();
249 $this->mLastModified = $lastmod;
251 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
253 return true;
254 } else {
255 wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
256 $this->mLastModified = $lastmod;
258 } else {
259 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
260 $this->mLastModified = $lastmod;
262 return false;
266 * @param $mckey string
267 * @param $touched int
268 * @return bool
270 function loadFromMemcached( $mckey, $touched ) {
271 global $wgMemc;
273 if ( !$touched ) {
274 return false;
277 $mcvalue = $wgMemc->get( $mckey );
278 if ( $mcvalue ) {
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'];
284 return true;
285 } else {
286 wfDebug( "$mckey has expired\n" );
290 return false;
294 * @param $mckey string
295 * @param $expiry int
296 * @return bool
298 function storeInMemcached( $mckey, $expiry = 86400 ) {
299 global $wgMemc;
301 $wgMemc->set( $mckey,
302 array(
303 'timestamp' => wfTimestampNow(),
304 'value' => $this->mText
305 ), $expiry
308 return true;