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
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Ajax
|
|
|
|
|
*/
|
|
|
|
|
|
2007-01-20 13:34:31 +00:00
|
|
|
if( !defined( 'MEDIAWIKI' ) ) {
|
|
|
|
|
die( 1 );
|
|
|
|
|
}
|
2006-08-29 15:43:34 +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)
|
|
|
|
|
*
|
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 {
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2007-05-05 00:06:20 +00:00
|
|
|
/** Number of seconds to get the response cached by a proxy */
|
|
|
|
|
private $mCacheDuration;
|
|
|
|
|
|
|
|
|
|
/** HTTP header Content-Type */
|
|
|
|
|
private $mContentType;
|
|
|
|
|
|
2008-08-20 22:22:29 +00:00
|
|
|
/** Disables output. Can be set by calling $AjaxResponse->disable() */
|
2007-05-05 00:06:20 +00:00
|
|
|
private $mDisabled;
|
|
|
|
|
|
|
|
|
|
/** Date for the HTTP header Last-modified */
|
|
|
|
|
private $mLastModified;
|
|
|
|
|
|
|
|
|
|
/** HTTP response code */
|
|
|
|
|
private $mResponseCode;
|
|
|
|
|
|
|
|
|
|
/** HTTP Vary header */
|
|
|
|
|
private $mVary;
|
|
|
|
|
|
|
|
|
|
/** Content of our HTTP response */
|
|
|
|
|
private $mText;
|
2006-08-29 15:43:34 +00:00
|
|
|
|
2009-12-11 21:07:27 +00:00
|
|
|
function __construct( $text = null ) {
|
|
|
|
|
$this->mCacheDuration = null;
|
|
|
|
|
$this->mVary = null;
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
$this->mDisabled = false;
|
|
|
|
|
$this->mText = '';
|
|
|
|
|
$this->mResponseCode = '200 OK';
|
|
|
|
|
$this->mLastModified = false;
|
2009-03-18 20:20:10 +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 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setCacheDuration( $duration ) {
|
|
|
|
|
$this->mCacheDuration = $duration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setVary( $vary ) {
|
|
|
|
|
$this->mVary = $vary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setResponseCode( $code ) {
|
|
|
|
|
$this->mResponseCode = $code;
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
function setContentType( $type ) {
|
|
|
|
|
$this->mContentType = $type;
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
function disable() {
|
|
|
|
|
$this->mDisabled = true;
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2007-05-05 00:06:20 +00:00
|
|
|
/** Add content to the response */
|
2006-08-29 15:43:34 +00:00
|
|
|
function addText( $text ) {
|
|
|
|
|
if ( ! $this->mDisabled && $text ) {
|
|
|
|
|
$this->mText .= $text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-05 00:06:20 +00:00
|
|
|
/** Output text */
|
2006-08-29 15:43:34 +00:00
|
|
|
function printText() {
|
|
|
|
|
if ( ! $this->mDisabled ) {
|
|
|
|
|
print $this->mText;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2007-05-05 00:06:20 +00:00
|
|
|
/** Construct the header and output it */
|
2006-08-29 15:43:34 +00:00
|
|
|
function sendHeaders() {
|
2006-11-29 11:43:58 +00:00
|
|
|
global $wgUseSquid, $wgUseESI;
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $this->mResponseCode ) {
|
|
|
|
|
$n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
|
|
|
|
|
header( "Status: " . $this->mResponseCode, true, (int)$n );
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +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 ) {
|
|
|
|
|
header ("Last-Modified: " . $this->mLastModified );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $this->mCacheDuration ) {
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2008-04-14 07:45:50 +00:00
|
|
|
# If squid caches are configured, tell them to cache the response,
|
2006-08-29 15:43:34 +00:00
|
|
|
# and tell the client to always check with the squid. Otherwise,
|
|
|
|
|
# tell the client to use a cached copy, without a way to purge it.
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if( $wgUseSquid ) {
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
# Expect explicite purge of the proxy cache, but require end user agents
|
|
|
|
|
# to revalidate against the proxy on each visit.
|
|
|
|
|
# Surrogate-Control controls our Squid, Cache-Control downstream caches
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $wgUseESI ) {
|
|
|
|
|
header( 'Surrogate-Control: max-age='.$this->mCacheDuration.', content="ESI/1.0"');
|
|
|
|
|
header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
|
|
|
|
|
} else {
|
|
|
|
|
header( 'Cache-Control: s-maxage='.$this->mCacheDuration.', must-revalidate, max-age=0' );
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
} else {
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
# Let the client do the caching. Cache is not purged.
|
|
|
|
|
header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT");
|
|
|
|
|
header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
} else {
|
|
|
|
|
# always expired, always modified
|
|
|
|
|
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
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
if ( $this->mVary ) {
|
|
|
|
|
header ( "Vary: " . $this->mVary );
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
/**
|
|
|
|
|
* checkLastModified tells the client to use the client-cached response if
|
|
|
|
|
* possible. If sucessful, the AjaxResponse is disabled so that
|
|
|
|
|
* any future call to AjaxResponse::printText() have no effect. The method
|
|
|
|
|
* returns true iff the response code was set to 304 Not Modified.
|
|
|
|
|
*/
|
|
|
|
|
function checkLastModified ( $timestamp ) {
|
2006-11-29 11:43:58 +00:00
|
|
|
global $wgCachePages, $wgCacheEpoch, $wgUser;
|
2006-08-29 15:43:34 +00:00
|
|
|
$fname = 'AjaxResponse::checkLastModified';
|
|
|
|
|
|
|
|
|
|
if ( !$timestamp || $timestamp == '19700101000000' ) {
|
|
|
|
|
wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if( !$wgCachePages ) {
|
|
|
|
|
wfDebug( "$fname: CACHE DISABLED\n", false );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if( $wgUser->getOption( 'nocache' ) ) {
|
|
|
|
|
wfDebug( "$fname: USER DISABLED CACHE\n", false );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$timestamp = wfTimestamp( TS_MW, $timestamp );
|
|
|
|
|
$lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
|
|
|
|
|
|
|
|
|
|
if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
|
|
|
|
|
# IE sends sizes after the date like this:
|
|
|
|
|
# Wed, 20 Aug 2003 06:51:19 GMT; length=5202
|
|
|
|
|
# this breaks strtotime().
|
|
|
|
|
$modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
|
|
|
|
|
$modsinceTime = strtotime( $modsince );
|
|
|
|
|
$ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
|
|
|
|
|
wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
|
|
|
|
|
wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
|
|
|
|
|
if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
|
2009-03-18 21:26:57 +00:00
|
|
|
ini_set('zlib.output_compression', 0);
|
2006-08-29 15:43:34 +00:00
|
|
|
$this->setResponseCode( "304 Not Modified" );
|
|
|
|
|
$this->disable();
|
|
|
|
|
$this->mLastModified = $lastmod;
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
|
|
|
|
|
$this->mLastModified = $lastmod;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
|
|
|
|
|
$this->mLastModified = $lastmod;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
function loadFromMemcached( $mckey, $touched ) {
|
|
|
|
|
global $wgMemc;
|
|
|
|
|
if ( !$touched ) return false;
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
$mcvalue = $wgMemc->get( $mckey );
|
|
|
|
|
if ( $mcvalue ) {
|
|
|
|
|
# Check to see if the value has been invalidated
|
|
|
|
|
if ( $touched <= $mcvalue['timestamp'] ) {
|
|
|
|
|
wfDebug( "Got $mckey from cache\n" );
|
|
|
|
|
$this->mText = $mcvalue['value'];
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( "$mckey has expired\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
function storeInMemcached( $mckey, $expiry = 86400 ) {
|
|
|
|
|
global $wgMemc;
|
2007-01-09 20:25:28 +00:00
|
|
|
|
|
|
|
|
$wgMemc->set( $mckey,
|
2006-08-29 15:43:34 +00:00
|
|
|
array(
|
|
|
|
|
'timestamp' => wfTimestampNow(),
|
|
|
|
|
'value' => $this->mText
|
|
|
|
|
), $expiry
|
|
|
|
|
);
|
2007-01-09 20:25:28 +00:00
|
|
|
|
2006-08-29 15:43:34 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|