Merge "Add a 'revdelete-selected-file' message on Special:RevisionDelete"
[lhc/web/wiklou.git] / includes / AjaxResponse.php
1 <?php
2 /**
3 * Response handler for Ajax requests.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Ajax
22 */
23
24 /**
25 * Handle responses for Ajax requests (send headers, print
26 * content, that sort of thing)
27 *
28 * @ingroup Ajax
29 */
30 class AjaxResponse {
31 /**
32 * Number of seconds to get the response cached by a proxy
33 * @var int $mCacheDuration
34 */
35 private $mCacheDuration;
36
37 /**
38 * HTTP header Content-Type
39 * @var string $mContentType
40 */
41 private $mContentType;
42
43 /**
44 * Disables output. Can be set by calling $AjaxResponse->disable()
45 * @var bool $mDisabled
46 */
47 private $mDisabled;
48
49 /**
50 * Date for the HTTP header Last-modified
51 * @var string|false $mLastModified
52 */
53 private $mLastModified;
54
55 /**
56 * HTTP response code
57 * @var string $mResponseCode
58 */
59 private $mResponseCode;
60
61 /**
62 * HTTP Vary header
63 * @var string $mVary
64 */
65 private $mVary;
66
67 /**
68 * Content of our HTTP response
69 * @var string $mText
70 */
71 private $mText;
72
73 /**
74 * @param $text string|null
75 */
76 function __construct( $text = null ) {
77 $this->mCacheDuration = null;
78 $this->mVary = null;
79
80 $this->mDisabled = false;
81 $this->mText = '';
82 $this->mResponseCode = '200 OK';
83 $this->mLastModified = false;
84 $this->mContentType = 'application/x-wiki';
85
86 if ( $text ) {
87 $this->addText( $text );
88 }
89 }
90
91 /**
92 * Set the number of seconds to get the response cached by a proxy
93 * @param $duration int
94 */
95 function setCacheDuration( $duration ) {
96 $this->mCacheDuration = $duration;
97 }
98
99 /**
100 * Set the HTTP Vary header
101 * @param $vary string
102 */
103 function setVary( $vary ) {
104 $this->mVary = $vary;
105 }
106
107 /**
108 * Set the HTTP response code
109 * @param $code string
110 */
111 function setResponseCode( $code ) {
112 $this->mResponseCode = $code;
113 }
114
115 /**
116 * Set the HTTP header Content-Type
117 * @param $type string
118 */
119 function setContentType( $type ) {
120 $this->mContentType = $type;
121 }
122
123 /**
124 * Disable output.
125 */
126 function disable() {
127 $this->mDisabled = true;
128 }
129
130 /**
131 * Add content to the response
132 * @param $text string
133 */
134 function addText( $text ) {
135 if ( ! $this->mDisabled && $text ) {
136 $this->mText .= $text;
137 }
138 }
139
140 /**
141 * Output text
142 */
143 function printText() {
144 if ( ! $this->mDisabled ) {
145 print $this->mText;
146 }
147 }
148
149 /**
150 * Construct the header and output it
151 */
152 function sendHeaders() {
153 global $wgUseSquid, $wgUseESI;
154
155 if ( $this->mResponseCode ) {
156 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
157 header( "Status: " . $this->mResponseCode, true, (int)$n );
158 }
159
160 header ( "Content-Type: " . $this->mContentType );
161
162 if ( $this->mLastModified ) {
163 header ( "Last-Modified: " . $this->mLastModified );
164 } else {
165 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
166 }
167
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.
172
173 if ( $wgUseSquid ) {
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
177
178 if ( $wgUseESI ) {
179 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' );
180 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
181 } else {
182 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
183 }
184
185 } else {
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}" );
190 }
191
192 } else {
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
197 }
198
199 if ( $this->mVary ) {
200 header ( "Vary: " . $this->mVary );
201 }
202 }
203
204 /**
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.
208 *
209 * @param $timestamp string
210 * @return bool Returns true if the response code was set to 304 Not Modified.
211 */
212 function checkLastModified( $timestamp ) {
213 global $wgCachePages, $wgCacheEpoch, $wgUser;
214 $fname = 'AjaxResponse::checkLastModified';
215
216 if ( !$timestamp || $timestamp == '19700101000000' ) {
217 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n", 'log' );
218 return false;
219 }
220
221 if ( !$wgCachePages ) {
222 wfDebug( "$fname: CACHE DISABLED\n", 'log' );
223 return false;
224 }
225
226 $timestamp = wfTimestamp( TS_MW, $timestamp );
227 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
228
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' );
238
239 if ( ( $ismodsince >= $timestamp )
240 && $wgUser->validateCache( $ismodsince ) &&
241 $ismodsince >= $wgCacheEpoch
242 ) {
243 ini_set( 'zlib.output_compression', 0 );
244 $this->setResponseCode( "304 Not Modified" );
245 $this->disable();
246 $this->mLastModified = $lastmod;
247
248 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
249 "page: $timestamp ; site $wgCacheEpoch\n", 'log' );
250
251 return true;
252 } else {
253 wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
254 "page: $timestamp ; site $wgCacheEpoch\n", 'log' );
255 $this->mLastModified = $lastmod;
256 }
257 } else {
258 wfDebug( "$fname: client did not send If-Modified-Since header\n", 'log' );
259 $this->mLastModified = $lastmod;
260 }
261 return false;
262 }
263
264 /**
265 * @param $mckey string
266 * @param $touched int
267 * @return bool
268 */
269 function loadFromMemcached( $mckey, $touched ) {
270 global $wgMemc;
271
272 if ( !$touched ) {
273 return false;
274 }
275
276 $mcvalue = $wgMemc->get( $mckey );
277 if ( $mcvalue ) {
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'];
282
283 return true;
284 } else {
285 wfDebug( "$mckey has expired\n" );
286 }
287 }
288
289 return false;
290 }
291
292 /**
293 * @param $mckey string
294 * @param $expiry int
295 * @return bool
296 */
297 function storeInMemcached( $mckey, $expiry = 86400 ) {
298 global $wgMemc;
299
300 $wgMemc->set( $mckey,
301 array(
302 'timestamp' => wfTimestampNow(),
303 'value' => $this->mText
304 ), $expiry
305 );
306
307 return true;
308 }
309 }