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