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