]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/AjaxResponse.php
MediaWiki 1.11.0-scripts
[autoinstallsdev/mediawiki.git] / includes / AjaxResponse.php
1 <?php
2 if( !defined( 'MEDIAWIKI' ) ) {
3         die( 1 );
4 }
5
6 /**
7  * @todo document
8  * @addtogroup Ajax
9  */
10 class AjaxResponse {
11
12         /** Number of seconds to get the response cached by a proxy */
13         private $mCacheDuration;
14
15         /** HTTP header Content-Type */
16         private $mContentType;
17
18         /** @todo document */
19         private $mDisabled;
20
21         /** Date for the HTTP header Last-modified */
22         private $mLastModified;
23
24         /** HTTP response code */
25         private $mResponseCode;
26
27         /** HTTP Vary header */
28         private $mVary;
29
30         /** Content of our HTTP response */
31         private $mText;
32
33         function __construct( $text = NULL ) {
34                 $this->mCacheDuration = NULL;
35                 $this->mVary = NULL;
36
37                 $this->mDisabled = false;
38                 $this->mText = '';
39                 $this->mResponseCode = '200 OK';
40                 $this->mLastModified = false;
41                 $this->mContentType= 'text/html; charset=utf-8';
42
43                 if ( $text ) {
44                         $this->addText( $text );
45                 }
46         }
47
48         function setCacheDuration( $duration ) {
49                 $this->mCacheDuration = $duration;
50         }
51
52         function setVary( $vary ) {
53                 $this->mVary = $vary;
54         }
55
56         function setResponseCode( $code ) {
57                 $this->mResponseCode = $code;
58         }
59
60         function setContentType( $type ) {
61                 $this->mContentType = $type;
62         }
63
64         function disable() {
65                 $this->mDisabled = true;
66         }
67
68         /** Add content to the response */
69         function addText( $text ) {
70                 if ( ! $this->mDisabled && $text ) {
71                         $this->mText .= $text;
72                 }
73         }
74
75         /** Output text */
76         function printText() {
77                 if ( ! $this->mDisabled ) {
78                         print $this->mText;
79                 }
80         }
81
82         /** Construct the header and output it */
83         function sendHeaders() {
84                 global $wgUseSquid, $wgUseESI;
85
86                 if ( $this->mResponseCode ) {
87                         $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
88                         header( "Status: " . $this->mResponseCode, true, (int)$n );
89                 }
90
91                 header ("Content-Type: " . $this->mContentType );
92
93                 if ( $this->mLastModified ) {
94                         header ("Last-Modified: " . $this->mLastModified );
95                 }
96                 else {
97                         header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
98                 }
99
100                 if ( $this->mCacheDuration ) {
101
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.
105
106                         if( $wgUseSquid ) {
107
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
111
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' );
117                                 }
118
119                         } else {
120
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}");
124                         }
125
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
131                 }
132
133                 if ( $this->mVary ) {
134                         header ( "Vary: " . $this->mVary );
135                 }
136         }
137
138         /**
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.
143          */
144         function checkLastModified ( $timestamp ) {
145                 global $wgCachePages, $wgCacheEpoch, $wgUser;
146                 $fname = 'AjaxResponse::checkLastModified';
147
148                 if ( !$timestamp || $timestamp == '19700101000000' ) {
149                         wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
150                         return;
151                 }
152                 if( !$wgCachePages ) {
153                         wfDebug( "$fname: CACHE DISABLED\n", false );
154                         return;
155                 }
156                 if( $wgUser->getOption( 'nocache' ) ) {
157                         wfDebug( "$fname: USER DISABLED CACHE\n", false );
158                         return;
159                 }
160
161                 $timestamp = wfTimestamp( TS_MW, $timestamp );
162                 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
163
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;
177
178                                 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
179
180                                 return true;
181                         } else {
182                                 wfDebug( "$fname: READY  client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
183                                 $this->mLastModified = $lastmod;
184                         }
185                 } else {
186                         wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
187                         $this->mLastModified = $lastmod;
188                 }
189         }
190
191         function loadFromMemcached( $mckey, $touched ) {
192                 global $wgMemc;
193                 if ( !$touched ) return false;
194
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" );
204                         }
205                 }
206
207                 return false;
208         }
209
210         function storeInMemcached( $mckey, $expiry = 86400 ) {
211                 global $wgMemc;
212
213                 $wgMemc->set( $mckey,
214                         array(
215                                 'timestamp' => wfTimestampNow(),
216                                 'value' => $this->mText
217                         ), $expiry
218                 );
219
220                 return true;
221         }
222 }
223