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