MediaWiki  1.30.0
AjaxResponse.php
Go to the documentation of this file.
1 <?php
24 
31 class AjaxResponse {
36  private $mCacheDuration;
37 
42  private $mContentType;
43 
48  private $mDisabled;
49 
54  private $mLastModified;
55 
60  private $mResponseCode;
61 
66  private $mVary;
67 
72  private $mText;
73 
77  private $mConfig;
78 
83  function __construct( $text = null, Config $config = null ) {
84  $this->mCacheDuration = null;
85  $this->mVary = null;
86  $this->mConfig = $config ?: MediaWikiServices::getInstance()->getMainConfig();
87 
88  $this->mDisabled = false;
89  $this->mText = '';
90  $this->mResponseCode = 200;
91  $this->mLastModified = false;
92  $this->mContentType = 'application/x-wiki';
93 
94  if ( $text ) {
95  $this->addText( $text );
96  }
97  }
98 
103  function setCacheDuration( $duration ) {
104  $this->mCacheDuration = $duration;
105  }
106 
111  function setVary( $vary ) {
112  $this->mVary = $vary;
113  }
114 
119  function setResponseCode( $code ) {
120  $this->mResponseCode = $code;
121  }
122 
127  function setContentType( $type ) {
128  $this->mContentType = $type;
129  }
130 
134  function disable() {
135  $this->mDisabled = true;
136  }
137 
142  function addText( $text ) {
143  if ( !$this->mDisabled && $text ) {
144  $this->mText .= $text;
145  }
146  }
147 
151  function printText() {
152  if ( !$this->mDisabled ) {
153  print $this->mText;
154  }
155  }
156 
160  function sendHeaders() {
161  if ( $this->mResponseCode ) {
162  // For back-compat, it is supported that mResponseCode be a string like " 200 OK"
163  // (with leading space and the status message after). Cast response code to an integer
164  // to take advantage of PHP's conversion rules which will turn " 200 OK" into 200.
165  // https://secure.php.net/manual/en/language.types.string.php#language.types.string.conversion
166  $n = intval( trim( $this->mResponseCode ) );
167  HttpStatus::header( $n );
168  }
169 
170  header( "Content-Type: " . $this->mContentType );
171 
172  if ( $this->mLastModified ) {
173  header( "Last-Modified: " . $this->mLastModified );
174  } else {
175  header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
176  }
177 
178  if ( $this->mCacheDuration ) {
179  # If CDN caches are configured, tell them to cache the response,
180  # and tell the client to always check with the CDN. Otherwise,
181  # tell the client to use a cached copy, without a way to purge it.
182 
183  if ( $this->mConfig->get( 'UseSquid' ) ) {
184  # Expect explicit purge of the proxy cache, but require end user agents
185  # to revalidate against the proxy on each visit.
186  # Surrogate-Control controls our CDN, Cache-Control downstream caches
187 
188  if ( $this->mConfig->get( 'UseESI' ) ) {
189  header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' );
190  header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
191  } else {
192  header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
193  }
194 
195  } else {
196  # Let the client do the caching. Cache is not purged.
197  header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" );
198  header( "Cache-Control: s-maxage={$this->mCacheDuration}," .
199  "public,max-age={$this->mCacheDuration}" );
200  }
201 
202  } else {
203  # always expired, always modified
204  header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
205  header( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
206  header( "Pragma: no-cache" ); // HTTP/1.0
207  }
208 
209  if ( $this->mVary ) {
210  header( "Vary: " . $this->mVary );
211  }
212  }
213 
222  function checkLastModified( $timestamp ) {
224  $fname = 'AjaxResponse::checkLastModified';
225 
226  if ( !$timestamp || $timestamp == '19700101000000' ) {
227  wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP", 'private' );
228  return false;
229  }
230 
231  if ( !$wgCachePages ) {
232  wfDebug( "$fname: CACHE DISABLED", 'private' );
233  return false;
234  }
235 
236  $timestamp = wfTimestamp( TS_MW, $timestamp );
237  $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
238 
239  if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
240  # IE sends sizes after the date like this:
241  # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
242  # this breaks strtotime().
243  $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
244  $modsinceTime = strtotime( $modsince );
245  $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
246  wfDebug( "$fname: -- client send If-Modified-Since: $modsince", 'private' );
247  wfDebug( "$fname: -- we might send Last-Modified : $lastmod", 'private' );
248 
249  if ( ( $ismodsince >= $timestamp )
250  && $wgUser->validateCache( $ismodsince ) &&
251  $ismodsince >= $wgCacheEpoch
252  ) {
253  ini_set( 'zlib.output_compression', 0 );
254  $this->setResponseCode( 304 );
255  $this->disable();
256  $this->mLastModified = $lastmod;
257 
258  wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
259  "page: $timestamp ; site $wgCacheEpoch", 'private' );
260 
261  return true;
262  } else {
263  wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
264  "page: $timestamp ; site $wgCacheEpoch", 'private' );
265  $this->mLastModified = $lastmod;
266  }
267  } else {
268  wfDebug( "$fname: client did not send If-Modified-Since header", 'private' );
269  $this->mLastModified = $lastmod;
270  }
271  return false;
272  }
273 
279  function loadFromMemcached( $mckey, $touched ) {
280  if ( !$touched ) {
281  return false;
282  }
283 
284  $mcvalue = ObjectCache::getMainWANInstance()->get( $mckey );
285  if ( $mcvalue ) {
286  # Check to see if the value has been invalidated
287  if ( $touched <= $mcvalue['timestamp'] ) {
288  wfDebug( "Got $mckey from cache" );
289  $this->mText = $mcvalue['value'];
290 
291  return true;
292  } else {
293  wfDebug( "$mckey has expired" );
294  }
295  }
296 
297  return false;
298  }
299 
305  function storeInMemcached( $mckey, $expiry = 86400 ) {
306  ObjectCache::getMainWANInstance()->set( $mckey,
307  [
308  'timestamp' => wfTimestampNow(),
309  'value' => $this->mText
310  ], $expiry
311  );
312 
313  return true;
314  }
315 }
$wgUser
$wgUser
Definition: Setup.php:809
AjaxResponse\$mText
$mText
Content of our HTTP response.
Definition: AjaxResponse.php:72
AjaxResponse\setResponseCode
setResponseCode( $code)
Set the HTTP response code.
Definition: AjaxResponse.php:119
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2040
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
AjaxResponse\checkLastModified
checkLastModified( $timestamp)
checkLastModified tells the client to use the client-cached response if possible.
Definition: AjaxResponse.php:222
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:36
AjaxResponse\$mContentType
$mContentType
HTTP header Content-Type.
Definition: AjaxResponse.php:42
AjaxResponse\loadFromMemcached
loadFromMemcached( $mckey, $touched)
Definition: AjaxResponse.php:279
AjaxResponse\$mResponseCode
$mResponseCode
HTTP response code.
Definition: AjaxResponse.php:60
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
AjaxResponse\setContentType
setContentType( $type)
Set the HTTP header Content-Type.
Definition: AjaxResponse.php:127
AjaxResponse\__construct
__construct( $text=null, Config $config=null)
Definition: AjaxResponse.php:83
AjaxResponse\$mConfig
Config $mConfig
Definition: AjaxResponse.php:77
AjaxResponse\$mVary
$mVary
HTTP Vary header.
Definition: AjaxResponse.php:66
AjaxResponse\setVary
setVary( $vary)
Set the HTTP Vary header.
Definition: AjaxResponse.php:111
AjaxResponse\disable
disable()
Disable output.
Definition: AjaxResponse.php:134
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:2069
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:1047
AjaxResponse\addText
addText( $text)
Add content to the response.
Definition: AjaxResponse.php:142
$wgCachePages
$wgCachePages
Allow client-side caching of pages.
Definition: DefaultSettings.php:2522
AjaxResponse\printText
printText()
Output text.
Definition: AjaxResponse.php:151
AjaxResponse
Handle responses for Ajax requests (send headers, print content, that sort of thing)
Definition: AjaxResponse.php:31
AjaxResponse\storeInMemcached
storeInMemcached( $mckey, $expiry=86400)
Definition: AjaxResponse.php:305
AjaxResponse\$mLastModified
$mLastModified
Date for the HTTP header Last-modified.
Definition: AjaxResponse.php:54
$wgCacheEpoch
$wgCacheEpoch
Set this to current time to invalidate all prior cached pages.
Definition: DefaultSettings.php:2532
HttpStatus\header
static header( $code)
Output an HTTP status code header.
Definition: HttpStatus.php:96
ObjectCache\getMainWANInstance
static getMainWANInstance()
Get the main WAN cache object.
Definition: ObjectCache.php:370
$code
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:781
AjaxResponse\setCacheDuration
setCacheDuration( $duration)
Set the number of seconds to get the response cached by a proxy.
Definition: AjaxResponse.php:103
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
AjaxResponse\sendHeaders
sendHeaders()
Construct the header and output it.
Definition: AjaxResponse.php:160
AjaxResponse\$mDisabled
$mDisabled
Disables output.
Definition: AjaxResponse.php:48
AjaxResponse\$mCacheDuration
$mCacheDuration
Number of seconds to get the response cached by a proxy.
Definition: AjaxResponse.php:36
$type
$type
Definition: testCompression.php:48