(bug 7589) Update to Indonesian localisation (id) #38; updating the release notes
[mediawiki.git] / includes / AjaxResponse.php
blob40f508763a162de08dfa12d4c0109f91a8a26cd4
1 <?php
3 if( !defined( 'MEDIAWIKI' ) )
4 die( 1 );
6 class AjaxResponse {
7 var $mCacheDuration;
8 var $mVary;
10 var $mDisabled;
11 var $mText;
12 var $mResponseCode;
13 var $mLastModified;
14 var $mContentType;
16 function AjaxResponse( $text = NULL ) {
17 $this->mCacheDuration = NULL;
18 $this->mVary = NULL;
20 $this->mDisabled = false;
21 $this->mText = '';
22 $this->mResponseCode = '200 OK';
23 $this->mLastModified = false;
24 $this->mContentType= 'text/html; charset=utf-8';
26 if ( $text ) {
27 $this->addText( $text );
31 function setCacheDuration( $duration ) {
32 $this->mCacheDuration = $duration;
35 function setVary( $vary ) {
36 $this->mVary = $vary;
39 function setResponseCode( $code ) {
40 $this->mResponseCode = $code;
43 function setContentType( $type ) {
44 $this->mContentType = $type;
47 function disable() {
48 $this->mDisabled = true;
51 function addText( $text ) {
52 if ( ! $this->mDisabled && $text ) {
53 $this->mText .= $text;
57 function printText() {
58 if ( ! $this->mDisabled ) {
59 print $this->mText;
63 function sendHeaders() {
64 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
66 if ( $this->mResponseCode ) {
67 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
68 header( "Status: " . $this->mResponseCode, true, (int)$n );
71 header ("Content-Type: " . $this->mContentType );
73 if ( $this->mLastModified ) {
74 header ("Last-Modified: " . $this->mLastModified );
76 else {
77 header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
80 if ( $this->mCacheDuration ) {
82 # If squid caches are configured, tell them to cache the response,
83 # and tell the client to always check with the squid. Otherwise,
84 # tell the client to use a cached copy, without a way to purge it.
86 if( $wgUseSquid ) {
88 # Expect explicite purge of the proxy cache, but require end user agents
89 # to revalidate against the proxy on each visit.
90 # Surrogate-Control controls our Squid, Cache-Control downstream caches
92 if ( $wgUseESI ) {
93 header( 'Surrogate-Control: max-age='.$this->mCacheDuration.', content="ESI/1.0"');
94 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
95 } else {
96 header( 'Cache-Control: s-maxage='.$this->mCacheDuration.', must-revalidate, max-age=0' );
99 } else {
101 # Let the client do the caching. Cache is not purged.
102 header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT");
103 header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
106 } else {
107 # always expired, always modified
108 header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
109 header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
110 header ("Pragma: no-cache"); // HTTP/1.0
113 if ( $this->mVary ) {
114 header ( "Vary: " . $this->mVary );
119 * checkLastModified tells the client to use the client-cached response if
120 * possible. If sucessful, the AjaxResponse is disabled so that
121 * any future call to AjaxResponse::printText() have no effect. The method
122 * returns true iff the response code was set to 304 Not Modified.
124 function checkLastModified ( $timestamp ) {
125 global $wgCachePages, $wgCacheEpoch, $wgUser, $wgRequest;
126 $fname = 'AjaxResponse::checkLastModified';
128 if ( !$timestamp || $timestamp == '19700101000000' ) {
129 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
130 return;
132 if( !$wgCachePages ) {
133 wfDebug( "$fname: CACHE DISABLED\n", false );
134 return;
136 if( $wgUser->getOption( 'nocache' ) ) {
137 wfDebug( "$fname: USER DISABLED CACHE\n", false );
138 return;
141 $timestamp = wfTimestamp( TS_MW, $timestamp );
142 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
144 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
145 # IE sends sizes after the date like this:
146 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
147 # this breaks strtotime().
148 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
149 $modsinceTime = strtotime( $modsince );
150 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
151 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
152 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
153 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
154 $this->setResponseCode( "304 Not Modified" );
155 $this->disable();
156 $this->mLastModified = $lastmod;
158 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
160 return true;
161 } else {
162 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
163 $this->mLastModified = $lastmod;
165 } else {
166 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
167 $this->mLastModified = $lastmod;
171 function loadFromMemcached( $mckey, $touched ) {
172 global $wgMemc;
173 if ( !$touched ) return false;
175 $mcvalue = $wgMemc->get( $mckey );
176 if ( $mcvalue ) {
177 # Check to see if the value has been invalidated
178 if ( $touched <= $mcvalue['timestamp'] ) {
179 wfDebug( "Got $mckey from cache\n" );
180 $this->mText = $mcvalue['value'];
181 return true;
182 } else {
183 wfDebug( "$mckey has expired\n" );
187 return false;
190 function storeInMemcached( $mckey, $expiry = 86400 ) {
191 global $wgMemc;
193 $wgMemc->set( $mckey,
194 array(
195 'timestamp' => wfTimestampNow(),
196 'value' => $this->mText
197 ), $expiry
200 return true;