MediaWiki  master
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  public 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  public function setResponseCode( $code ) {
104  $this->mResponseCode = $code;
105  }
106 
111  public function setContentType( $type ) {
112  $this->mContentType = $type;
113  }
114 
118  public function disable() {
119  $this->mDisabled = true;
120  }
121 
126  public function addText( $text ) {
127  if ( !$this->mDisabled && $text ) {
128  $this->mText .= $text;
129  }
130  }
131 
135  public function printText() {
136  if ( !$this->mDisabled ) {
138  }
139  }
140 
144  public function sendHeaders() {
145  if ( $this->mResponseCode ) {
146  // For back-compat, it is supported that mResponseCode be a string like " 200 OK"
147  // (with leading space and the status message after). Cast response code to an integer
148  // to take advantage of PHP's conversion rules which will turn " 200 OK" into 200.
149  // https://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
150  $n = intval( trim( $this->mResponseCode ) );
151  HttpStatus::header( $n );
152  }
153 
154  header( "Content-Type: " . $this->mContentType );
155 
156  if ( $this->mLastModified ) {
157  header( "Last-Modified: " . $this->mLastModified );
158  } else {
159  header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
160  }
161 
162  if ( $this->mCacheDuration ) {
163  # If CDN caches are configured, tell them to cache the response,
164  # and tell the client to always check with the CDN. Otherwise,
165  # tell the client to use a cached copy, without a way to purge it.
166  if ( $this->mConfig->get( 'UseCdn' ) ) {
167  # Expect explicit purge of the proxy cache, but require end user agents
168  # to revalidate against the proxy on each visit.
169  header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
170  } else {
171  # Let the client do the caching. Cache is not purged.
172  header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" );
173  header( "Cache-Control: s-maxage={$this->mCacheDuration}," .
174  "public,max-age={$this->mCacheDuration}" );
175  }
176 
177  } else {
178  # always expired, always modified
179  header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
180  header( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
181  header( "Pragma: no-cache" ); // HTTP/1.0
182  }
183 
184  if ( $this->mVary ) {
185  header( "Vary: " . $this->mVary );
186  }
187  }
188 }
AjaxResponse\$mText
string $mText
Content of our HTTP response.
Definition: AjaxResponse.php:72
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:149
AjaxResponse\setResponseCode
setResponseCode( $code)
Set the HTTP response code.
Definition: AjaxResponse.php:103
AjaxResponse\$mContentType
string $mContentType
HTTP header Content-Type.
Definition: AjaxResponse.php:42
AjaxResponse\setContentType
setContentType( $type)
Set the HTTP header Content-Type.
Definition: AjaxResponse.php:111
Config
Interface for configuration instances.
Definition: Config.php:28
AjaxResponse\__construct
__construct( $text=null, Config $config=null)
Definition: AjaxResponse.php:83
AjaxResponse\$mConfig
Config $mConfig
Definition: AjaxResponse.php:77
AjaxResponse\$mVary
string $mVary
HTTP Vary header.
Definition: AjaxResponse.php:66
AjaxResponse\disable
disable()
Disable output.
Definition: AjaxResponse.php:118
AjaxResponse\$mResponseCode
int string $mResponseCode
HTTP response code.
Definition: AjaxResponse.php:60
AjaxResponse\addText
addText( $text)
Add content to the response.
Definition: AjaxResponse.php:126
AjaxResponse\printText
printText()
Output text.
Definition: AjaxResponse.php:135
AjaxResponse
Handle responses for Ajax requests (send headers, print content, that sort of thing)
Definition: AjaxResponse.php:31
AjaxResponse\$mLastModified
string bool $mLastModified
Date for the HTTP header Last-modified.
Definition: AjaxResponse.php:54
print
print
Definition: opensearch_desc.php:53
HttpStatus\header
static header( $code)
Output an HTTP status code header.
Definition: HttpStatus.php:96
AjaxResponse\sendHeaders
sendHeaders()
Construct the header and output it.
Definition: AjaxResponse.php:144
AjaxResponse\$mDisabled
bool $mDisabled
Disables output.
Definition: AjaxResponse.php:48
AjaxResponse\$mCacheDuration
int $mCacheDuration
Number of seconds to get the response cached by a proxy.
Definition: AjaxResponse.php:36
$type
$type
Definition: testCompression.php:52