]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/AjaxResponse.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.git] / includes / AjaxResponse.php
1 <?php
2 /**
3  * @file
4  * @ingroup Ajax
5  */
6
7 if( !defined( 'MEDIAWIKI' ) ) {
8         die( 1 );
9 }
10
11 /**
12  * Handle responses for Ajax requests (send headers, print
13  * content, that sort of thing)
14  * 
15  * @ingroup Ajax
16  */
17 class AjaxResponse {
18
19         /** Number of seconds to get the response cached by a proxy */
20         private $mCacheDuration;
21
22         /** HTTP header Content-Type */
23         private $mContentType;
24
25         /** Disables output. Can be set by calling $AjaxResponse->disable() */
26         private $mDisabled;
27
28         /** Date for the HTTP header Last-modified */
29         private $mLastModified;
30
31         /** HTTP response code */
32         private $mResponseCode;
33
34         /** HTTP Vary header */
35         private $mVary;
36
37         /** Content of our HTTP response */
38         private $mText;
39
40         function __construct( $text = NULL ) {
41                 $this->mCacheDuration = NULL;
42                 $this->mVary = NULL;
43
44                 $this->mDisabled = false;
45                 $this->mText = '';
46                 $this->mResponseCode = '200 OK';
47                 $this->mLastModified = false;
48                 $this->mContentType= 'application/x-wiki';
49
50                 if ( $text ) {
51                         $this->addText( $text );
52                 }
53         }
54
55         function setCacheDuration( $duration ) {
56                 $this->mCacheDuration = $duration;
57         }
58
59         function setVary( $vary ) {
60                 $this->mVary = $vary;
61         }
62
63         function setResponseCode( $code ) {
64                 $this->mResponseCode = $code;
65         }
66
67         function setContentType( $type ) {
68                 $this->mContentType = $type;
69         }
70
71         function disable() {
72                 $this->mDisabled = true;
73         }
74
75         /** Add content to the response */
76         function addText( $text ) {
77                 if ( ! $this->mDisabled && $text ) {
78                         $this->mText .= $text;
79                 }
80         }
81
82         /** Output text */
83         function printText() {
84                 if ( ! $this->mDisabled ) {
85                         print $this->mText;
86                 }
87         }
88
89         /** Construct the header and output it */
90         function sendHeaders() {
91                 global $wgUseSquid, $wgUseESI;
92
93                 if ( $this->mResponseCode ) {
94                         $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
95                         header( "Status: " . $this->mResponseCode, true, (int)$n );
96                 }
97
98                 header ("Content-Type: " . $this->mContentType );
99
100                 if ( $this->mLastModified ) {
101                         header ("Last-Modified: " . $this->mLastModified );
102                 }
103                 else {
104                         header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
105                 }
106
107                 if ( $this->mCacheDuration ) {
108
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.
112
113                         if( $wgUseSquid ) {
114
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
118
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' );
124                                 }
125
126                         } else {
127
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}");
131                         }
132
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
138                 }
139
140                 if ( $this->mVary ) {
141                         header ( "Vary: " . $this->mVary );
142                 }
143         }
144
145         /**
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.
150          */
151         function checkLastModified ( $timestamp ) {
152                 global $wgCachePages, $wgCacheEpoch, $wgUser;
153                 $fname = 'AjaxResponse::checkLastModified';
154
155                 if ( !$timestamp || $timestamp == '19700101000000' ) {
156                         wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
157                         return;
158                 }
159                 if( !$wgCachePages ) {
160                         wfDebug( "$fname: CACHE DISABLED\n", false );
161                         return;
162                 }
163                 if( $wgUser->getOption( 'nocache' ) ) {
164                         wfDebug( "$fname: USER DISABLED CACHE\n", false );
165                         return;
166                 }
167
168                 $timestamp = wfTimestamp( TS_MW, $timestamp );
169                 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
170
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;
185
186                                 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
187
188                                 return true;
189                         } else {
190                                 wfDebug( "$fname: READY  client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
191                                 $this->mLastModified = $lastmod;
192                         }
193                 } else {
194                         wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
195                         $this->mLastModified = $lastmod;
196                 }
197         }
198
199         function loadFromMemcached( $mckey, $touched ) {
200                 global $wgMemc;
201                 if ( !$touched ) return false;
202
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" );
212                         }
213                 }
214
215                 return false;
216         }
217
218         function storeInMemcached( $mckey, $expiry = 86400 ) {
219                 global $wgMemc;
220
221                 $wgMemc->set( $mckey,
222                         array(
223                                 'timestamp' => wfTimestampNow(),
224                                 'value' => $this->mText
225                         ), $expiry
226                 );
227
228                 return true;
229         }
230 }