2006-08-29 15:43:34 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2012-05-22 18:06:30 +00:00
|
|
|
* Response handler for Ajax requests.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-08 14:23:14 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Ajax
|
|
|
|
|
*/
|
2016-11-22 23:39:22 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
2008-08-20 22:22:29 +00:00
|
|
|
* Handle responses for Ajax requests (send headers, print
|
|
|
|
|
* content, that sort of thing)
|
2010-02-14 22:07:30 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Ajax
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2006-08-29 15:43:34 +00:00
|
|
|
class AjaxResponse {
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Number of seconds to get the response cached by a proxy
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var int
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2007-05-05 00:06:20 +00:00
|
|
|
private $mCacheDuration;
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* HTTP header Content-Type
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2007-05-05 00:06:20 +00:00
|
|
|
private $mContentType;
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Disables output. Can be set by calling $AjaxResponse->disable()
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var bool
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2007-05-05 00:06:20 +00:00
|
|
|
private $mDisabled;
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Date for the HTTP header Last-modified
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string|bool
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2007-05-05 00:06:20 +00:00
|
|
|
private $mLastModified;
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* HTTP response code
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var int|string
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2007-05-05 00:06:20 +00:00
|
|
|
private $mResponseCode;
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* HTTP Vary header
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2007-05-05 00:06:20 +00:00
|
|
|
private $mVary;
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Content of our HTTP response
|
2019-11-29 13:33:43 +00:00
|
|
|
* @var string
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2007-05-05 00:06:20 +00:00
|
|
|
private $mText;
|
2006-08-29 15:43:34 +00:00
|
|
|
|
2014-08-26 02:14:41 +00:00
|
|
|
/**
|
|
|
|
|
* @var Config
|
|
|
|
|
*/
|
|
|
|
|
private $mConfig;
|
|
|
|
|
|
2012-03-05 15:23:04 +00:00
|
|
|
/**
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string|null $text
|
2014-08-26 02:14:41 +00:00
|
|
|
* @param Config|null $config
|
2012-03-05 15:23:04 +00:00
|
|
|
*/
|
2019-11-30 23:03:59 +00:00
|
|
|
public function __construct( $text = null, Config $config = null ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
$this->mCacheDuration = null;
|
|
|
|
|
$this->mVary = null;
|
2016-11-22 23:39:22 +00:00
|
|
|
$this->mConfig = $config ?: MediaWikiServices::getInstance()->getMainConfig();
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
$this->mDisabled = false;
|
|
|
|
|
$this->mText = '';
|
2015-06-11 15:13:00 +00:00
|
|
|
$this->mResponseCode = 200;
|
2006-08-29 15:43:34 +00:00
|
|
|
$this->mLastModified = false;
|
2010-02-14 22:07:30 +00:00
|
|
|
$this->mContentType = 'application/x-wiki';
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $text ) {
|
|
|
|
|
$this->addText( $text );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Set the HTTP response code
|
2019-09-15 14:40:42 +00:00
|
|
|
* @param int|string $code
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2020-05-09 22:40:20 +00:00
|
|
|
public function setResponseCode( $code ) {
|
2006-08-29 15:43:34 +00:00
|
|
|
$this->mResponseCode = $code;
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Set the HTTP header Content-Type
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $type
|
2012-04-15 00:12:01 +00:00
|
|
|
*/
|
2020-05-09 22:40:20 +00:00
|
|
|
public function setContentType( $type ) {
|
2006-08-29 15:43:34 +00:00
|
|
|
$this->mContentType = $type;
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2012-04-15 00:12:01 +00:00
|
|
|
/**
|
|
|
|
|
* Disable output.
|
|
|
|
|
*/
|
2020-05-09 22:40:20 +00:00
|
|
|
public function disable() {
|
2006-08-29 15:43:34 +00:00
|
|
|
$this->mDisabled = true;
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2012-03-05 15:23:04 +00:00
|
|
|
/**
|
|
|
|
|
* Add content to the response
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $text
|
2012-03-05 15:23:04 +00:00
|
|
|
*/
|
2020-05-09 22:40:20 +00:00
|
|
|
public function addText( $text ) {
|
2014-07-21 12:47:42 +00:00
|
|
|
if ( !$this->mDisabled && $text ) {
|
2006-08-29 15:43:34 +00:00
|
|
|
$this->mText .= $text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-05 15:23:04 +00:00
|
|
|
/**
|
|
|
|
|
* Output text
|
|
|
|
|
*/
|
2020-05-09 22:40:20 +00:00
|
|
|
public function printText() {
|
2014-07-21 12:47:42 +00:00
|
|
|
if ( !$this->mDisabled ) {
|
2006-08-29 15:43:34 +00:00
|
|
|
print $this->mText;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2012-03-05 15:23:04 +00:00
|
|
|
/**
|
|
|
|
|
* Construct the header and output it
|
|
|
|
|
*/
|
2020-05-09 22:40:20 +00:00
|
|
|
public function sendHeaders() {
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $this->mResponseCode ) {
|
2015-06-11 15:13:00 +00:00
|
|
|
// For back-compat, it is supported that mResponseCode be a string like " 200 OK"
|
|
|
|
|
// (with leading space and the status message after). Cast response code to an integer
|
|
|
|
|
// to take advantage of PHP's conversion rules which will turn " 200 OK" into 200.
|
2019-04-12 04:41:14 +00:00
|
|
|
// https://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
|
2015-06-11 15:13:00 +00:00
|
|
|
$n = intval( trim( $this->mResponseCode ) );
|
2015-06-01 14:31:52 +00:00
|
|
|
HttpStatus::header( $n );
|
2006-08-29 15:43:34 +00:00
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
header( "Content-Type: " . $this->mContentType );
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $this->mLastModified ) {
|
2015-06-16 19:06:19 +00:00
|
|
|
header( "Last-Modified: " . $this->mLastModified );
|
2010-05-30 14:28:54 +00:00
|
|
|
} else {
|
2015-06-16 19:06:19 +00:00
|
|
|
header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
|
2006-08-29 15:43:34 +00:00
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $this->mCacheDuration ) {
|
2015-12-10 01:30:47 +00:00
|
|
|
# If CDN caches are configured, tell them to cache the response,
|
|
|
|
|
# and tell the client to always check with the CDN. Otherwise,
|
2006-08-29 15:43:34 +00:00
|
|
|
# tell the client to use a cached copy, without a way to purge it.
|
2017-11-01 20:55:24 +00:00
|
|
|
if ( $this->mConfig->get( 'UseCdn' ) ) {
|
2013-03-13 07:42:41 +00:00
|
|
|
# Expect explicit purge of the proxy cache, but require end user agents
|
2006-08-29 15:43:34 +00:00
|
|
|
# to revalidate against the proxy on each visit.
|
2019-09-08 04:11:22 +00:00
|
|
|
header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
|
2006-08-29 15:43:34 +00:00
|
|
|
} else {
|
|
|
|
|
# Let the client do the caching. Cache is not purged.
|
2015-06-16 19:06:19 +00:00
|
|
|
header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" );
|
|
|
|
|
header( "Cache-Control: s-maxage={$this->mCacheDuration}," .
|
2014-03-23 01:28:57 +00:00
|
|
|
"public,max-age={$this->mCacheDuration}" );
|
2006-08-29 15:43:34 +00:00
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
} else {
|
|
|
|
|
# always expired, always modified
|
2015-06-16 19:06:19 +00:00
|
|
|
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
|
|
|
|
|
header( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
|
|
|
|
|
header( "Pragma: no-cache" ); // HTTP/1.0
|
2006-08-29 15:43:34 +00:00
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $this->mVary ) {
|
2015-06-16 19:06:19 +00:00
|
|
|
header( "Vary: " . $this->mVary );
|
2006-08-29 15:43:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|