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