Load all extension messages in the specified language code, not only messages defined...
[mediawiki.git] / includes / AjaxResponse.php
blobc79e928bbbf7bde7a1d51f9d3063527c94d023df
1 <?php
2 /**
3 * @file
4 * @ingroup Ajax
5 */
7 if( !defined( 'MEDIAWIKI' ) ) {
8 die( 1 );
11 /**
12 * @todo document
13 * @ingroup Ajax
15 class AjaxResponse {
17 /** Number of seconds to get the response cached by a proxy */
18 private $mCacheDuration;
20 /** HTTP header Content-Type */
21 private $mContentType;
23 /** @todo document */
24 private $mDisabled;
26 /** Date for the HTTP header Last-modified */
27 private $mLastModified;
29 /** HTTP response code */
30 private $mResponseCode;
32 /** HTTP Vary header */
33 private $mVary;
35 /** Content of our HTTP response */
36 private $mText;
38 function __construct( $text = NULL ) {
39 $this->mCacheDuration = NULL;
40 $this->mVary = NULL;
42 $this->mDisabled = false;
43 $this->mText = '';
44 $this->mResponseCode = '200 OK';
45 $this->mLastModified = false;
46 $this->mContentType= 'text/html; charset=utf-8';
48 if ( $text ) {
49 $this->addText( $text );
53 function setCacheDuration( $duration ) {
54 $this->mCacheDuration = $duration;
57 function setVary( $vary ) {
58 $this->mVary = $vary;
61 function setResponseCode( $code ) {
62 $this->mResponseCode = $code;
65 function setContentType( $type ) {
66 $this->mContentType = $type;
69 function disable() {
70 $this->mDisabled = true;
73 /** Add content to the response */
74 function addText( $text ) {
75 if ( ! $this->mDisabled && $text ) {
76 $this->mText .= $text;
80 /** Output text */
81 function printText() {
82 if ( ! $this->mDisabled ) {
83 print $this->mText;
87 /** Construct the header and output it */
88 function sendHeaders() {
89 global $wgUseSquid, $wgUseESI;
91 if ( $this->mResponseCode ) {
92 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
93 header( "Status: " . $this->mResponseCode, true, (int)$n );
96 header ("Content-Type: " . $this->mContentType );
98 if ( $this->mLastModified ) {
99 header ("Last-Modified: " . $this->mLastModified );
101 else {
102 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
105 if ( $this->mCacheDuration ) {
107 # If squid caches are configured, tell them to cache the response,
108 # and tell the client to always check with the squid. Otherwise,
109 # tell the client to use a cached copy, without a way to purge it.
111 if( $wgUseSquid ) {
113 # Expect explicite purge of the proxy cache, but require end user agents
114 # to revalidate against the proxy on each visit.
115 # Surrogate-Control controls our Squid, Cache-Control downstream caches
117 if ( $wgUseESI ) {
118 header( 'Surrogate-Control: max-age='.$this->mCacheDuration.', content="ESI/1.0"');
119 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
120 } else {
121 header( 'Cache-Control: s-maxage='.$this->mCacheDuration.', must-revalidate, max-age=0' );
124 } else {
126 # Let the client do the caching. Cache is not purged.
127 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT");
128 header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
131 } else {
132 # always expired, always modified
133 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
134 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
135 header ("Pragma: no-cache"); // HTTP/1.0
138 if ( $this->mVary ) {
139 header ( "Vary: " . $this->mVary );
144 * checkLastModified tells the client to use the client-cached response if
145 * possible. If sucessful, the AjaxResponse is disabled so that
146 * any future call to AjaxResponse::printText() have no effect. The method
147 * returns true iff the response code was set to 304 Not Modified.
149 function checkLastModified ( $timestamp ) {
150 global $wgCachePages, $wgCacheEpoch, $wgUser;
151 $fname = 'AjaxResponse::checkLastModified';
153 if ( !$timestamp || $timestamp == '19700101000000' ) {
154 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
155 return;
157 if( !$wgCachePages ) {
158 wfDebug( "$fname: CACHE DISABLED\n", false );
159 return;
161 if( $wgUser->getOption( 'nocache' ) ) {
162 wfDebug( "$fname: USER DISABLED CACHE\n", false );
163 return;
166 $timestamp = wfTimestamp( TS_MW, $timestamp );
167 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
169 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
170 # IE sends sizes after the date like this:
171 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
172 # this breaks strtotime().
173 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
174 $modsinceTime = strtotime( $modsince );
175 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
176 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
177 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
178 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
179 $this->setResponseCode( "304 Not Modified" );
180 $this->disable();
181 $this->mLastModified = $lastmod;
183 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
185 return true;
186 } else {
187 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
188 $this->mLastModified = $lastmod;
190 } else {
191 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
192 $this->mLastModified = $lastmod;
196 function loadFromMemcached( $mckey, $touched ) {
197 global $wgMemc;
198 if ( !$touched ) return false;
200 $mcvalue = $wgMemc->get( $mckey );
201 if ( $mcvalue ) {
202 # Check to see if the value has been invalidated
203 if ( $touched <= $mcvalue['timestamp'] ) {
204 wfDebug( "Got $mckey from cache\n" );
205 $this->mText = $mcvalue['value'];
206 return true;
207 } else {
208 wfDebug( "$mckey has expired\n" );
212 return false;
215 function storeInMemcached( $mckey, $expiry = 86400 ) {
216 global $wgMemc;
218 $wgMemc->set( $mckey,
219 array(
220 'timestamp' => wfTimestampNow(),
221 'value' => $this->mText
222 ), $expiry
225 return true;