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