Two new hooks to EditPage.php, EditPage::AfterEdit:Form and EditPage:BeforeDisplaying...
[lhc/web/wiklou.git] / includes / AjaxResponse.php
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
6
7 if( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
9 }
10
11 /**
12 * @todo document
13 * @ingroup Ajax
14 */
15 class AjaxResponse {
16
17 /** Number of seconds to get the response cached by a proxy */
18 private $mCacheDuration;
19
20 /** HTTP header Content-Type */
21 private $mContentType;
22
23 /** @todo document */
24 private $mDisabled;
25
26 /** Date for the HTTP header Last-modified */
27 private $mLastModified;
28
29 /** HTTP response code */
30 private $mResponseCode;
31
32 /** HTTP Vary header */
33 private $mVary;
34
35 /** Content of our HTTP response */
36 private $mText;
37
38 function __construct( $text = NULL ) {
39 $this->mCacheDuration = NULL;
40 $this->mVary = NULL;
41
42 $this->mDisabled = false;
43 $this->mText = '';
44 $this->mResponseCode = '200 OK';
45 $this->mLastModified = false;
46 $this->mContentType= 'text/html; charset=utf-8';
47
48 if ( $text ) {
49 $this->addText( $text );
50 }
51 }
52
53 function setCacheDuration( $duration ) {
54 $this->mCacheDuration = $duration;
55 }
56
57 function setVary( $vary ) {
58 $this->mVary = $vary;
59 }
60
61 function setResponseCode( $code ) {
62 $this->mResponseCode = $code;
63 }
64
65 function setContentType( $type ) {
66 $this->mContentType = $type;
67 }
68
69 function disable() {
70 $this->mDisabled = true;
71 }
72
73 /** Add content to the response */
74 function addText( $text ) {
75 if ( ! $this->mDisabled && $text ) {
76 $this->mText .= $text;
77 }
78 }
79
80 /** Output text */
81 function printText() {
82 if ( ! $this->mDisabled ) {
83 print $this->mText;
84 }
85 }
86
87 /** Construct the header and output it */
88 function sendHeaders() {
89 global $wgUseSquid, $wgUseESI;
90
91 if ( $this->mResponseCode ) {
92 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
93 header( "Status: " . $this->mResponseCode, true, (int)$n );
94 }
95
96 header ("Content-Type: " . $this->mContentType );
97
98 if ( $this->mLastModified ) {
99 header ("Last-Modified: " . $this->mLastModified );
100 }
101 else {
102 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
103 }
104
105 if ( $this->mCacheDuration ) {
106
107 # If squid caches are configured, tell them to cache the response,
108 # and tell the client to always check with the squid. Otherwise,
109 # tell the client to use a cached copy, without a way to purge it.
110
111 if( $wgUseSquid ) {
112
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-max-age={$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 if( !$wgCachePages ) {
158 wfDebug( "$fname: CACHE DISABLED\n", false );
159 return;
160 }
161 if( $wgUser->getOption( 'nocache' ) ) {
162 wfDebug( "$fname: USER DISABLED CACHE\n", false );
163 return;
164 }
165
166 $timestamp = wfTimestamp( TS_MW, $timestamp );
167 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
168
169 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
170 # IE sends sizes after the date like this:
171 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
172 # this breaks strtotime().
173 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
174 $modsinceTime = strtotime( $modsince );
175 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
176 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
177 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
178 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
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 function loadFromMemcached( $mckey, $touched ) {
197 global $wgMemc;
198 if ( !$touched ) return false;
199
200 $mcvalue = $wgMemc->get( $mckey );
201 if ( $mcvalue ) {
202 # Check to see if the value has been invalidated
203 if ( $touched <= $mcvalue['timestamp'] ) {
204 wfDebug( "Got $mckey from cache\n" );
205 $this->mText = $mcvalue['value'];
206 return true;
207 } else {
208 wfDebug( "$mckey has expired\n" );
209 }
210 }
211
212 return false;
213 }
214
215 function storeInMemcached( $mckey, $expiry = 86400 ) {
216 global $wgMemc;
217
218 $wgMemc->set( $mckey,
219 array(
220 'timestamp' => wfTimestampNow(),
221 'value' => $this->mText
222 ), $expiry
223 );
224
225 return true;
226 }
227 }