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