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