(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / AjaxResponse.php
blob26b6f443981e86f57de444bbce1326feffde7c7d
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
7 if( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
11 /**
12 * Handle responses for Ajax requests (send headers, print
13 * content, that sort of thing)
15 * @ingroup Ajax
17 class AjaxResponse {
19 /** Number of seconds to get the response cached by a proxy */
20 private $mCacheDuration;
22 /** HTTP header Content-Type */
23 private $mContentType;
25 /** Disables output. Can be set by calling $AjaxResponse->disable() */
26 private $mDisabled;
28 /** Date for the HTTP header Last-modified */
29 private $mLastModified;
31 /** HTTP response code */
32 private $mResponseCode;
34 /** HTTP Vary header */
35 private $mVary;
37 /** Content of our HTTP response */
38 private $mText;
40 function __construct( $text = NULL ) {
41 $this->mCacheDuration = NULL;
42 $this->mVary = NULL;
44 $this->mDisabled = false;
45 $this->mText = '';
46 $this->mResponseCode = '200 OK';
47 $this->mLastModified = false;
48 $this->mContentType= 'application/x-wiki';
50 if ( $text ) {
51 $this->addText( $text );
55 function setCacheDuration( $duration ) {
56 $this->mCacheDuration = $duration;
59 function setVary( $vary ) {
60 $this->mVary = $vary;
63 function setResponseCode( $code ) {
64 $this->mResponseCode = $code;
67 function setContentType( $type ) {
68 $this->mContentType = $type;
71 function disable() {
72 $this->mDisabled = true;
75 /** Add content to the response */
76 function addText( $text ) {
77 if ( ! $this->mDisabled && $text ) {
78 $this->mText .= $text;
82 /** Output text */
83 function printText() {
84 if ( ! $this->mDisabled ) {
85 print $this->mText;
89 /** Construct the header and output it */
90 function sendHeaders() {
91 global $wgUseSquid, $wgUseESI;
93 if ( $this->mResponseCode ) {
94 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
95 header( "Status: " . $this->mResponseCode, true, (int)$n );
98 header ("Content-Type: " . $this->mContentType );
100 if ( $this->mLastModified ) {
101 header ("Last-Modified: " . $this->mLastModified );
103 else {
104 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
107 if ( $this->mCacheDuration ) {
109 # If squid caches are configured, tell them to cache the response,
110 # and tell the client to always check with the squid. Otherwise,
111 # tell the client to use a cached copy, without a way to purge it.
113 if( $wgUseSquid ) {
115 # Expect explicite purge of the proxy cache, but require end user agents
116 # to revalidate against the proxy on each visit.
117 # Surrogate-Control controls our Squid, Cache-Control downstream caches
119 if ( $wgUseESI ) {
120 header( 'Surrogate-Control: max-age='.$this->mCacheDuration.', content="ESI/1.0"');
121 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
122 } else {
123 header( 'Cache-Control: s-maxage='.$this->mCacheDuration.', must-revalidate, max-age=0' );
126 } else {
128 # Let the client do the caching. Cache is not purged.
129 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT");
130 header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
133 } else {
134 # always expired, always modified
135 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
136 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
137 header ("Pragma: no-cache"); // HTTP/1.0
140 if ( $this->mVary ) {
141 header ( "Vary: " . $this->mVary );
146 * checkLastModified tells the client to use the client-cached response if
147 * possible. If sucessful, the AjaxResponse is disabled so that
148 * any future call to AjaxResponse::printText() have no effect. The method
149 * returns true iff the response code was set to 304 Not Modified.
151 function checkLastModified ( $timestamp ) {
152 global $wgCachePages, $wgCacheEpoch, $wgUser;
153 $fname = 'AjaxResponse::checkLastModified';
155 if ( !$timestamp || $timestamp == '19700101000000' ) {
156 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
157 return;
159 if( !$wgCachePages ) {
160 wfDebug( "$fname: CACHE DISABLED\n", false );
161 return;
163 if( $wgUser->getOption( 'nocache' ) ) {
164 wfDebug( "$fname: USER DISABLED CACHE\n", false );
165 return;
168 $timestamp = wfTimestamp( TS_MW, $timestamp );
169 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
171 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
172 # IE sends sizes after the date like this:
173 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
174 # this breaks strtotime().
175 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
176 $modsinceTime = strtotime( $modsince );
177 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
178 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
179 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
180 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
181 ini_set('zlib.output_compression', 0);
182 $this->setResponseCode( "304 Not Modified" );
183 $this->disable();
184 $this->mLastModified = $lastmod;
186 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
188 return true;
189 } else {
190 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
191 $this->mLastModified = $lastmod;
193 } else {
194 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
195 $this->mLastModified = $lastmod;
199 function loadFromMemcached( $mckey, $touched ) {
200 global $wgMemc;
201 if ( !$touched ) return false;
203 $mcvalue = $wgMemc->get( $mckey );
204 if ( $mcvalue ) {
205 # Check to see if the value has been invalidated
206 if ( $touched <= $mcvalue['timestamp'] ) {
207 wfDebug( "Got $mckey from cache\n" );
208 $this->mText = $mcvalue['value'];
209 return true;
210 } else {
211 wfDebug( "$mckey has expired\n" );
215 return false;
218 function storeInMemcached( $mckey, $expiry = 86400 ) {
219 global $wgMemc;
221 $wgMemc->set( $mckey,
222 array(
223 'timestamp' => wfTimestampNow(),
224 'value' => $this->mText
225 ), $expiry
228 return true;