Reduced some master queries by adding flags to Revision functions.
[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 /** Number of seconds to get the response cached by a proxy */
32 private $mCacheDuration;
33
34 /** HTTP header Content-Type */
35 private $mContentType;
36
37 /** Disables output. Can be set by calling $AjaxResponse->disable() */
38 private $mDisabled;
39
40 /** Date for the HTTP header Last-modified */
41 private $mLastModified;
42
43 /** HTTP response code */
44 private $mResponseCode;
45
46 /** HTTP Vary header */
47 private $mVary;
48
49 /** Content of our HTTP response */
50 private $mText;
51
52 /**
53 * @param $text string|null
54 */
55 function __construct( $text = null ) {
56 $this->mCacheDuration = null;
57 $this->mVary = null;
58
59 $this->mDisabled = false;
60 $this->mText = '';
61 $this->mResponseCode = '200 OK';
62 $this->mLastModified = false;
63 $this->mContentType = 'application/x-wiki';
64
65 if ( $text ) {
66 $this->addText( $text );
67 }
68 }
69
70 function setCacheDuration( $duration ) {
71 $this->mCacheDuration = $duration;
72 }
73
74 function setVary( $vary ) {
75 $this->mVary = $vary;
76 }
77
78 function setResponseCode( $code ) {
79 $this->mResponseCode = $code;
80 }
81
82 function setContentType( $type ) {
83 $this->mContentType = $type;
84 }
85
86 function disable() {
87 $this->mDisabled = true;
88 }
89
90 /**
91 * Add content to the response
92 * @param $text string
93 */
94 function addText( $text ) {
95 if ( ! $this->mDisabled && $text ) {
96 $this->mText .= $text;
97 }
98 }
99
100 /**
101 * Output text
102 */
103 function printText() {
104 if ( ! $this->mDisabled ) {
105 print $this->mText;
106 }
107 }
108
109 /**
110 * Construct the header and output it
111 */
112 function sendHeaders() {
113 global $wgUseSquid, $wgUseESI;
114
115 if ( $this->mResponseCode ) {
116 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
117 header( "Status: " . $this->mResponseCode, true, (int)$n );
118 }
119
120 header ( "Content-Type: " . $this->mContentType );
121
122 if ( $this->mLastModified ) {
123 header ( "Last-Modified: " . $this->mLastModified );
124 } else {
125 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
126 }
127
128 if ( $this->mCacheDuration ) {
129 # If squid caches are configured, tell them to cache the response,
130 # and tell the client to always check with the squid. Otherwise,
131 # tell the client to use a cached copy, without a way to purge it.
132
133 if ( $wgUseSquid ) {
134 # Expect explicite purge of the proxy cache, but require end user agents
135 # to revalidate against the proxy on each visit.
136 # Surrogate-Control controls our Squid, Cache-Control downstream caches
137
138 if ( $wgUseESI ) {
139 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' );
140 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
141 } else {
142 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
143 }
144
145 } else {
146
147 # Let the client do the caching. Cache is not purged.
148 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" );
149 header ( "Cache-Control: s-maxage={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" );
150 }
151
152 } else {
153 # always expired, always modified
154 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
155 header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
156 header ( "Pragma: no-cache" ); // HTTP/1.0
157 }
158
159 if ( $this->mVary ) {
160 header ( "Vary: " . $this->mVary );
161 }
162 }
163
164 /**
165 * checkLastModified tells the client to use the client-cached response if
166 * possible. If sucessful, the AjaxResponse is disabled so that
167 * any future call to AjaxResponse::printText() have no effect.
168 *
169 * @param $timestamp string
170 * @return bool Returns true if the response code was set to 304 Not Modified.
171 */
172 function checkLastModified ( $timestamp ) {
173 global $wgCachePages, $wgCacheEpoch, $wgUser;
174 $fname = 'AjaxResponse::checkLastModified';
175
176 if ( !$timestamp || $timestamp == '19700101000000' ) {
177 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
178 return false;
179 }
180
181 if ( !$wgCachePages ) {
182 wfDebug( "$fname: CACHE DISABLED\n", false );
183 return false;
184 }
185
186 if ( $wgUser->getOption( 'nocache' ) ) {
187 wfDebug( "$fname: USER DISABLED CACHE\n", false );
188 return false;
189 }
190
191 $timestamp = wfTimestamp( TS_MW, $timestamp );
192 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
193
194 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
195 # IE sends sizes after the date like this:
196 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
197 # this breaks strtotime().
198 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
199 $modsinceTime = strtotime( $modsince );
200 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
201 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
202 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
203
204 if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
205 ini_set( 'zlib.output_compression', 0 );
206 $this->setResponseCode( "304 Not Modified" );
207 $this->disable();
208 $this->mLastModified = $lastmod;
209
210 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
211
212 return true;
213 } else {
214 wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
215 $this->mLastModified = $lastmod;
216 }
217 } else {
218 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
219 $this->mLastModified = $lastmod;
220 }
221 return false;
222 }
223
224 /**
225 * @param $mckey
226 * @param $touched
227 * @return bool
228 */
229 function loadFromMemcached( $mckey, $touched ) {
230 global $wgMemc;
231
232 if ( !$touched ) {
233 return false;
234 }
235
236 $mcvalue = $wgMemc->get( $mckey );
237 if ( $mcvalue ) {
238 # Check to see if the value has been invalidated
239 if ( $touched <= $mcvalue['timestamp'] ) {
240 wfDebug( "Got $mckey from cache\n" );
241 $this->mText = $mcvalue['value'];
242
243 return true;
244 } else {
245 wfDebug( "$mckey has expired\n" );
246 }
247 }
248
249 return false;
250 }
251
252 /**
253 * @param $mckey
254 * @param $expiry int
255 * @return bool
256 */
257 function storeInMemcached( $mckey, $expiry = 86400 ) {
258 global $wgMemc;
259
260 $wgMemc->set( $mckey,
261 array(
262 'timestamp' => wfTimestampNow(),
263 'value' => $this->mText
264 ), $expiry
265 );
266
267 return true;
268 }
269 }