MediaWiki REL1_35
AjaxResponse.php
Go to the documentation of this file.
1<?php
24
37
43
48 private $mDisabled;
49
55
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}
Handle responses for Ajax requests (send headers, print content, that sort of thing)
sendHeaders()
Construct the header and output it.
printText()
Output text.
string $mContentType
HTTP header Content-Type.
string $mVary
HTTP Vary header.
string bool $mLastModified
Date for the HTTP header Last-modified.
setResponseCode( $code)
Set the HTTP response code.
addText( $text)
Add content to the response.
int string $mResponseCode
HTTP response code.
__construct( $text=null, Config $config=null)
bool $mDisabled
Disables output.
string $mText
Content of our HTTP response.
setContentType( $type)
Set the HTTP header Content-Type.
disable()
Disable output.
int $mCacheDuration
Number of seconds to get the response cached by a proxy.
MediaWikiServices is the service locator for the application scope of MediaWiki.
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:64
Interface for configuration instances.
Definition Config.php:30