Merge "[FileBackend] Only show "copied file(s)" for files the script had to copy."
[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 /**
33 * Number of seconds to get the response cached by a proxy
34 * @var int $mCacheDuration
35 */
36 private $mCacheDuration;
37
38 /**
39 * HTTP header Content-Type
40 * @var string $mContentType
41 */
42 private $mContentType;
43
44 /**
45 * Disables output. Can be set by calling $AjaxResponse->disable()
46 * @var bool $mDisabled
47 */
48 private $mDisabled;
49
50 /**
51 * Date for the HTTP header Last-modified
52 * @var string|false $mLastModified
53 */
54 private $mLastModified;
55
56 /**
57 * HTTP response code
58 * @var string $mResponseCode
59 */
60 private $mResponseCode;
61
62 /**
63 * HTTP Vary header
64 * @var string $mVary
65 */
66 private $mVary;
67
68 /**
69 * Content of our HTTP response
70 * @var string $mText
71 */
72 private $mText;
73
74 /**
75 * @param $text string|null
76 */
77 function __construct( $text = null ) {
78 $this->mCacheDuration = null;
79 $this->mVary = null;
80
81 $this->mDisabled = false;
82 $this->mText = '';
83 $this->mResponseCode = '200 OK';
84 $this->mLastModified = false;
85 $this->mContentType = 'application/x-wiki';
86
87 if ( $text ) {
88 $this->addText( $text );
89 }
90 }
91
92 /**
93 * Set the number of seconds to get the response cached by a proxy
94 * @param $duration int
95 */
96 function setCacheDuration( $duration ) {
97 $this->mCacheDuration = $duration;
98 }
99
100 /**
101 * Set the HTTP Vary header
102 * @param $vary string
103 */
104 function setVary( $vary ) {
105 $this->mVary = $vary;
106 }
107
108 /**
109 * Set the HTTP response code
110 * @param $code string
111 */
112 function setResponseCode( $code ) {
113 $this->mResponseCode = $code;
114 }
115
116 /**
117 * Set the HTTP header Content-Type
118 * @param $type string
119 */
120 function setContentType( $type ) {
121 $this->mContentType = $type;
122 }
123
124 /**
125 * Disable output.
126 */
127 function disable() {
128 $this->mDisabled = true;
129 }
130
131 /**
132 * Add content to the response
133 * @param $text string
134 */
135 function addText( $text ) {
136 if ( ! $this->mDisabled && $text ) {
137 $this->mText .= $text;
138 }
139 }
140
141 /**
142 * Output text
143 */
144 function printText() {
145 if ( ! $this->mDisabled ) {
146 print $this->mText;
147 }
148 }
149
150 /**
151 * Construct the header and output it
152 */
153 function sendHeaders() {
154 global $wgUseSquid, $wgUseESI;
155
156 if ( $this->mResponseCode ) {
157 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
158 header( "Status: " . $this->mResponseCode, true, (int)$n );
159 }
160
161 header ( "Content-Type: " . $this->mContentType );
162
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" );
167 }
168
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.
173
174 if ( $wgUseSquid ) {
175 # Expect explicite 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
178
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' );
184 }
185
186 } else {
187
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}" );
191 }
192
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
198 }
199
200 if ( $this->mVary ) {
201 header ( "Vary: " . $this->mVary );
202 }
203 }
204
205 /**
206 * checkLastModified tells the client to use the client-cached response if
207 * possible. If sucessful, the AjaxResponse is disabled so that
208 * any future call to AjaxResponse::printText() have no effect.
209 *
210 * @param $timestamp string
211 * @return bool Returns true if the response code was set to 304 Not Modified.
212 */
213 function checkLastModified ( $timestamp ) {
214 global $wgCachePages, $wgCacheEpoch, $wgUser;
215 $fname = 'AjaxResponse::checkLastModified';
216
217 if ( !$timestamp || $timestamp == '19700101000000' ) {
218 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
219 return false;
220 }
221
222 if ( !$wgCachePages ) {
223 wfDebug( "$fname: CACHE DISABLED\n", false );
224 return false;
225 }
226
227 if ( $wgUser->getOption( 'nocache' ) ) {
228 wfDebug( "$fname: USER DISABLED CACHE\n", false );
229 return false;
230 }
231
232 $timestamp = wfTimestamp( TS_MW, $timestamp );
233 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
234
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 );
244
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;
250
251 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
252
253 return true;
254 } else {
255 wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
256 $this->mLastModified = $lastmod;
257 }
258 } else {
259 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
260 $this->mLastModified = $lastmod;
261 }
262 return false;
263 }
264
265 /**
266 * @param $mckey string
267 * @param $touched int
268 * @return bool
269 */
270 function loadFromMemcached( $mckey, $touched ) {
271 global $wgMemc;
272
273 if ( !$touched ) {
274 return false;
275 }
276
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'];
283
284 return true;
285 } else {
286 wfDebug( "$mckey has expired\n" );
287 }
288 }
289
290 return false;
291 }
292
293 /**
294 * @param $mckey string
295 * @param $expiry int
296 * @return bool
297 */
298 function storeInMemcached( $mckey, $expiry = 86400 ) {
299 global $wgMemc;
300
301 $wgMemc->set( $mckey,
302 array(
303 'timestamp' => wfTimestampNow(),
304 'value' => $this->mText
305 ), $expiry
306 );
307
308 return true;
309 }
310 }