X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FAjaxResponse.php;h=23c31bf9da7ae70b31e8c3803f0661ea0856d7ec;hb=703752db95eeb841661602b9e17772885396fc29;hp=de6b3c56a434eea356c1d58b9f93e9710509f46c;hpb=d38a31222006c2ce17bc39cf4db35e1467c37625;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/AjaxResponse.php b/includes/AjaxResponse.php index de6b3c56a4..23c31bf9da 100644 --- a/includes/AjaxResponse.php +++ b/includes/AjaxResponse.php @@ -1,92 +1,155 @@ disable() */ + /** + * Disables output. Can be set by calling $AjaxResponse->disable() + * @var bool $mDisabled + */ private $mDisabled; - /** Date for the HTTP header Last-modified */ + /** + * Date for the HTTP header Last-modified + * @var string|false $mLastModified + */ private $mLastModified; - /** HTTP response code */ + /** + * HTTP response code + * @var string $mResponseCode + */ private $mResponseCode; - /** HTTP Vary header */ + /** + * HTTP Vary header + * @var string $mVary + */ private $mVary; - /** Content of our HTTP response */ + /** + * Content of our HTTP response + * @var string $mText + */ private $mText; - function __construct( $text = NULL ) { - $this->mCacheDuration = NULL; - $this->mVary = NULL; + /** + * @param $text string|null + */ + function __construct( $text = null ) { + $this->mCacheDuration = null; + $this->mVary = null; $this->mDisabled = false; $this->mText = ''; $this->mResponseCode = '200 OK'; $this->mLastModified = false; - $this->mContentType= 'application/x-wiki'; + $this->mContentType = 'application/x-wiki'; if ( $text ) { $this->addText( $text ); } } + /** + * Set the number of seconds to get the response cached by a proxy + * @param $duration int + */ function setCacheDuration( $duration ) { $this->mCacheDuration = $duration; } + /** + * Set the HTTP Vary header + * @param $vary string + */ function setVary( $vary ) { $this->mVary = $vary; } + /** + * Set the HTTP response code + * @param $code string + */ function setResponseCode( $code ) { $this->mResponseCode = $code; } + /** + * Set the HTTP header Content-Type + * @param $type string + */ function setContentType( $type ) { $this->mContentType = $type; } + /** + * Disable output. + */ function disable() { $this->mDisabled = true; } - /** Add content to the response */ + /** + * Add content to the response + * @param $text string + */ function addText( $text ) { if ( ! $this->mDisabled && $text ) { $this->mText .= $text; } } - /** Output text */ + /** + * Output text + */ function printText() { if ( ! $this->mDisabled ) { print $this->mText; } } - /** Construct the header and output it */ + /** + * Construct the header and output it + */ function sendHeaders() { global $wgUseSquid, $wgUseESI; @@ -95,46 +158,43 @@ class AjaxResponse { header( "Status: " . $this->mResponseCode, true, (int)$n ); } - header ("Content-Type: " . $this->mContentType ); + header ( "Content-Type: " . $this->mContentType ); if ( $this->mLastModified ) { - header ("Last-Modified: " . $this->mLastModified ); - } - else { - header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); + header ( "Last-Modified: " . $this->mLastModified ); + } else { + header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" ); } if ( $this->mCacheDuration ) { - # If squid caches are configured, tell them to cache the response, # 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. - if( $wgUseSquid ) { - + if ( $wgUseSquid ) { # 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 if ( $wgUseESI ) { - header( 'Surrogate-Control: max-age='.$this->mCacheDuration.', content="ESI/1.0"'); + 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' ); + header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' ); } } else { # 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}"); + header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" ); + header ( "Cache-Control: s-maxage={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" ); } } 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 + 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 } if ( $this->mVary ) { @@ -144,9 +204,11 @@ class AjaxResponse { /** * 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. + * possible. If successful, the AjaxResponse is disabled so that + * any future call to AjaxResponse::printText() have no effect. + * + * @param $timestamp string + * @return bool Returns true if the response code was set to 304 Not Modified. */ function checkLastModified ( $timestamp ) { global $wgCachePages, $wgCacheEpoch, $wgUser; @@ -154,21 +216,23 @@ class AjaxResponse { if ( !$timestamp || $timestamp == '19700101000000' ) { wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" ); - return; + return false; } - if( !$wgCachePages ) { + + if ( !$wgCachePages ) { wfDebug( "$fname: CACHE DISABLED\n", false ); - return; + return false; } - if( $wgUser->getOption( 'nocache' ) ) { + + if ( $wgUser->getOption( 'nocache' ) ) { wfDebug( "$fname: USER DISABLED CACHE\n", false ); - return; + return false; } $timestamp = wfTimestamp( TS_MW, $timestamp ); - $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) ); + $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) ); - if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { + 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(). @@ -177,27 +241,38 @@ class AjaxResponse { $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 ) { + + if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) { + ini_set( 'zlib.output_compression', 0 ); $this->setResponseCode( "304 Not Modified" ); $this->disable(); $this->mLastModified = $lastmod; - wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false ); + wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false ); return true; } else { - wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false ); + wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; 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; } + return false; } + /** + * @param $mckey string + * @param $touched int + * @return bool + */ function loadFromMemcached( $mckey, $touched ) { global $wgMemc; - if ( !$touched ) return false; + + if ( !$touched ) { + return false; + } $mcvalue = $wgMemc->get( $mckey ); if ( $mcvalue ) { @@ -205,6 +280,7 @@ class AjaxResponse { if ( $touched <= $mcvalue['timestamp'] ) { wfDebug( "Got $mckey from cache\n" ); $this->mText = $mcvalue['value']; + return true; } else { wfDebug( "$mckey has expired\n" ); @@ -214,6 +290,11 @@ class AjaxResponse { return false; } + /** + * @param $mckey string + * @param $expiry int + * @return bool + */ function storeInMemcached( $mckey, $expiry = 86400 ) { global $wgMemc;