* API: Watchlist feed allows 'hours' parameter of how many hours to go back
[mediawiki.git] / includes / AjaxResponse.php
blobe910a523fae3dcaec8b14475e728a89d3b25d21c
1 <?php
2 if( !defined( 'MEDIAWIKI' ) ) {
3 die( 1 );
6 /**
7 * @todo document
8 * @addtogroup Ajax
9 */
10 class AjaxResponse {
12 /** Number of seconds to get the response cached by a proxy */
13 private $mCacheDuration;
15 /** HTTP header Content-Type */
16 private $mContentType;
18 /** @todo document */
19 private $mDisabled;
21 /** Date for the HTTP header Last-modified */
22 private $mLastModified;
24 /** HTTP response code */
25 private $mResponseCode;
27 /** HTTP Vary header */
28 private $mVary;
30 /** Content of our HTTP response */
31 private $mText;
33 function __construct( $text = NULL ) {
34 $this->mCacheDuration = NULL;
35 $this->mVary = NULL;
37 $this->mDisabled = false;
38 $this->mText = '';
39 $this->mResponseCode = '200 OK';
40 $this->mLastModified = false;
41 $this->mContentType= 'text/html; charset=utf-8';
43 if ( $text ) {
44 $this->addText( $text );
48 function setCacheDuration( $duration ) {
49 $this->mCacheDuration = $duration;
52 function setVary( $vary ) {
53 $this->mVary = $vary;
56 function setResponseCode( $code ) {
57 $this->mResponseCode = $code;
60 function setContentType( $type ) {
61 $this->mContentType = $type;
64 function disable() {
65 $this->mDisabled = true;
68 /** Add content to the response */
69 function addText( $text ) {
70 if ( ! $this->mDisabled && $text ) {
71 $this->mText .= $text;
75 /** Output text */
76 function printText() {
77 if ( ! $this->mDisabled ) {
78 print $this->mText;
82 /** Construct the header and output it */
83 function sendHeaders() {
84 global $wgUseSquid, $wgUseESI;
86 if ( $this->mResponseCode ) {
87 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
88 header( "Status: " . $this->mResponseCode, true, (int)$n );
91 header ("Content-Type: " . $this->mContentType );
93 if ( $this->mLastModified ) {
94 header ("Last-Modified: " . $this->mLastModified );
96 else {
97 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
100 if ( $this->mCacheDuration ) {
102 # If squid caches are configured, tell them to cache the response,
103 # and tell the client to always check with the squid. Otherwise,
104 # tell the client to use a cached copy, without a way to purge it.
106 if( $wgUseSquid ) {
108 # Expect explicite purge of the proxy cache, but require end user agents
109 # to revalidate against the proxy on each visit.
110 # Surrogate-Control controls our Squid, Cache-Control downstream caches
112 if ( $wgUseESI ) {
113 header( 'Surrogate-Control: max-age='.$this->mCacheDuration.', content="ESI/1.0"');
114 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
115 } else {
116 header( 'Cache-Control: s-maxage='.$this->mCacheDuration.', must-revalidate, max-age=0' );
119 } else {
121 # Let the client do the caching. Cache is not purged.
122 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT");
123 header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
126 } else {
127 # always expired, always modified
128 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
129 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
130 header ("Pragma: no-cache"); // HTTP/1.0
133 if ( $this->mVary ) {
134 header ( "Vary: " . $this->mVary );
139 * checkLastModified tells the client to use the client-cached response if
140 * possible. If sucessful, the AjaxResponse is disabled so that
141 * any future call to AjaxResponse::printText() have no effect. The method
142 * returns true iff the response code was set to 304 Not Modified.
144 function checkLastModified ( $timestamp ) {
145 global $wgCachePages, $wgCacheEpoch, $wgUser;
146 $fname = 'AjaxResponse::checkLastModified';
148 if ( !$timestamp || $timestamp == '19700101000000' ) {
149 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
150 return;
152 if( !$wgCachePages ) {
153 wfDebug( "$fname: CACHE DISABLED\n", false );
154 return;
156 if( $wgUser->getOption( 'nocache' ) ) {
157 wfDebug( "$fname: USER DISABLED CACHE\n", false );
158 return;
161 $timestamp = wfTimestamp( TS_MW, $timestamp );
162 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
164 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
165 # IE sends sizes after the date like this:
166 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
167 # this breaks strtotime().
168 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
169 $modsinceTime = strtotime( $modsince );
170 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
171 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
172 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
173 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
174 $this->setResponseCode( "304 Not Modified" );
175 $this->disable();
176 $this->mLastModified = $lastmod;
178 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
180 return true;
181 } else {
182 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
183 $this->mLastModified = $lastmod;
185 } else {
186 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
187 $this->mLastModified = $lastmod;
191 function loadFromMemcached( $mckey, $touched ) {
192 global $wgMemc;
193 if ( !$touched ) return false;
195 $mcvalue = $wgMemc->get( $mckey );
196 if ( $mcvalue ) {
197 # Check to see if the value has been invalidated
198 if ( $touched <= $mcvalue['timestamp'] ) {
199 wfDebug( "Got $mckey from cache\n" );
200 $this->mText = $mcvalue['value'];
201 return true;
202 } else {
203 wfDebug( "$mckey has expired\n" );
207 return false;
210 function storeInMemcached( $mckey, $expiry = 86400 ) {
211 global $wgMemc;
213 $wgMemc->set( $mckey,
214 array(
215 'timestamp' => wfTimestampNow(),
216 'value' => $this->mText
217 ), $expiry
220 return true;