MediaWiki  1.23.14
AjaxResponse.php
Go to the documentation of this file.
1 <?php
30 class AjaxResponse {
35  private $mCacheDuration;
36 
41  private $mContentType;
42 
47  private $mDisabled;
48 
53  private $mLastModified;
54 
59  private $mResponseCode;
60 
65  private $mVary;
66 
71  private $mText;
72 
76  function __construct( $text = null ) {
77  $this->mCacheDuration = null;
78  $this->mVary = null;
79 
80  $this->mDisabled = false;
81  $this->mText = '';
82  $this->mResponseCode = '200 OK';
83  $this->mLastModified = false;
84  $this->mContentType = 'application/x-wiki';
85 
86  if ( $text ) {
87  $this->addText( $text );
88  }
89  }
90 
95  function setCacheDuration( $duration ) {
96  $this->mCacheDuration = $duration;
97  }
98 
103  function setVary( $vary ) {
104  $this->mVary = $vary;
105  }
106 
111  function setResponseCode( $code ) {
112  $this->mResponseCode = $code;
113  }
114 
119  function setContentType( $type ) {
120  $this->mContentType = $type;
121  }
122 
126  function disable() {
127  $this->mDisabled = true;
128  }
129 
134  function addText( $text ) {
135  if ( ! $this->mDisabled && $text ) {
136  $this->mText .= $text;
137  }
138  }
139 
143  function printText() {
144  if ( ! $this->mDisabled ) {
146  }
147  }
148 
152  function sendHeaders() {
153  global $wgUseSquid, $wgUseESI;
154 
155  if ( $this->mResponseCode ) {
156  $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
157  header( "Status: " . $this->mResponseCode, true, (int)$n );
158  }
159 
160  header ( "Content-Type: " . $this->mContentType );
161 
162  if ( $this->mLastModified ) {
163  header ( "Last-Modified: " . $this->mLastModified );
164  } else {
165  header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
166  }
167 
168  if ( $this->mCacheDuration ) {
169  # If squid caches are configured, tell them to cache the response,
170  # and tell the client to always check with the squid. Otherwise,
171  # tell the client to use a cached copy, without a way to purge it.
172 
173  if ( $wgUseSquid ) {
174  # Expect explicit purge of the proxy cache, but require end user agents
175  # to revalidate against the proxy on each visit.
176  # Surrogate-Control controls our Squid, Cache-Control downstream caches
177 
178  if ( $wgUseESI ) {
179  header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' );
180  header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
181  } else {
182  header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
183  }
184 
185  } else {
186  # Let the client do the caching. Cache is not purged.
187  header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" );
188  header ( "Cache-Control: s-maxage={$this->mCacheDuration}," .
189  "public,max-age={$this->mCacheDuration}" );
190  }
191 
192  } else {
193  # always expired, always modified
194  header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
195  header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
196  header ( "Pragma: no-cache" ); // HTTP/1.0
197  }
198 
199  if ( $this->mVary ) {
200  header ( "Vary: " . $this->mVary );
201  }
202  }
203 
212  function checkLastModified( $timestamp ) {
213  global $wgCachePages, $wgCacheEpoch, $wgUser;
214  $fname = 'AjaxResponse::checkLastModified';
215 
216  if ( !$timestamp || $timestamp == '19700101000000' ) {
217  wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n", 'log' );
218  return false;
219  }
220 
221  if ( !$wgCachePages ) {
222  wfDebug( "$fname: CACHE DISABLED\n", 'log' );
223  return false;
224  }
225 
227  $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
228 
229  if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
230  # IE sends sizes after the date like this:
231  # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
232  # this breaks strtotime().
233  $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
234  $modsinceTime = strtotime( $modsince );
235  $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
236  wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", 'log' );
237  wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", 'log' );
238 
239  if ( ( $ismodsince >= $timestamp )
240  && $wgUser->validateCache( $ismodsince ) &&
241  $ismodsince >= $wgCacheEpoch
242  ) {
243  ini_set( 'zlib.output_compression', 0 );
244  $this->setResponseCode( "304 Not Modified" );
245  $this->disable();
246  $this->mLastModified = $lastmod;
247 
248  wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
249  "page: $timestamp ; site $wgCacheEpoch\n", 'log' );
250 
251  return true;
252  } else {
253  wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
254  "page: $timestamp ; site $wgCacheEpoch\n", 'log' );
255  $this->mLastModified = $lastmod;
256  }
257  } else {
258  wfDebug( "$fname: client did not send If-Modified-Since header\n", 'log' );
259  $this->mLastModified = $lastmod;
260  }
261  return false;
262  }
263 
269  function loadFromMemcached( $mckey, $touched ) {
270  global $wgMemc;
271 
272  if ( !$touched ) {
273  return false;
274  }
275 
276  $mcvalue = $wgMemc->get( $mckey );
277  if ( $mcvalue ) {
278  # Check to see if the value has been invalidated
279  if ( $touched <= $mcvalue['timestamp'] ) {
280  wfDebug( "Got $mckey from cache\n" );
281  $this->mText = $mcvalue['value'];
282 
283  return true;
284  } else {
285  wfDebug( "$mckey has expired\n" );
286  }
287  }
288 
289  return false;
290  }
291 
297  function storeInMemcached( $mckey, $expiry = 86400 ) {
298  global $wgMemc;
299 
300  $wgMemc->set( $mckey,
301  array(
302  'timestamp' => wfTimestampNow(),
303  'value' => $this->mText
304  ), $expiry
305  );
306 
307  return true;
308  }
309 }
$wgUser
$wgUser
Definition: Setup.php:572
AjaxResponse\$mText
string $mText
Content of our HTTP response $mText.
Definition: AjaxResponse.php:64
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
$timestamp
if( $limit) $timestamp
Definition: importImages.php:104
AjaxResponse\setResponseCode
setResponseCode( $code)
Set the HTTP response code.
Definition: AjaxResponse.php:104
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2530
$n
$n
Definition: RandomTest.php:76
AjaxResponse\checkLastModified
checkLastModified( $timestamp)
checkLastModified tells the client to use the client-cached response if possible.
Definition: AjaxResponse.php:205
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:35
AjaxResponse\$mContentType
string $mContentType
HTTP header Content-Type $mContentType.
Definition: AjaxResponse.php:39
AjaxResponse\loadFromMemcached
loadFromMemcached( $mckey, $touched)
Definition: AjaxResponse.php:262
AjaxResponse\$mResponseCode
string $mResponseCode
HTTP response code $mResponseCode.
Definition: AjaxResponse.php:54
AjaxResponse\setContentType
setContentType( $type)
Set the HTTP header Content-Type.
Definition: AjaxResponse.php:112
AjaxResponse\$mVary
string $mVary
HTTP Vary header $mVary.
Definition: AjaxResponse.php:59
AjaxResponse\setVary
setVary( $vary)
Set the HTTP Vary header.
Definition: AjaxResponse.php:96
AjaxResponse\__construct
__construct( $text=null)
Definition: AjaxResponse.php:69
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
AjaxResponse\disable
disable()
Disable output.
Definition: AjaxResponse.php:119
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:2561
AjaxResponse\addText
addText( $text)
Add content to the response.
Definition: AjaxResponse.php:127
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2478
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:980
AjaxResponse\$mLastModified
string false $mLastModified
Date for the HTTP header Last-modified $mLastModified.
Definition: AjaxResponse.php:49
AjaxResponse\printText
printText()
Output text.
Definition: AjaxResponse.php:136
AjaxResponse
Handle responses for Ajax requests (send headers, print content, that sort of thing)
Definition: AjaxResponse.php:30
AjaxResponse\storeInMemcached
storeInMemcached( $mckey, $expiry=86400)
Definition: AjaxResponse.php:290
AjaxResponse\setCacheDuration
setCacheDuration( $duration)
Set the number of seconds to get the response cached by a proxy.
Definition: AjaxResponse.php:88
AjaxResponse\sendHeaders
sendHeaders()
Construct the header and output it.
Definition: AjaxResponse.php:145
TS_RFC2822
const TS_RFC2822
RFC 2822 format, for E-mail and HTTP headers.
Definition: GlobalFunctions.php:2488
AjaxResponse\$mDisabled
bool $mDisabled
Disables output.
Definition: AjaxResponse.php:44
AjaxResponse\$mCacheDuration
int $mCacheDuration
Number of seconds to get the response cached by a proxy $mCacheDuration.
Definition: AjaxResponse.php:34
$type
$type
Definition: testCompression.php:46