X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FAjaxResponse.php;h=6c2782c0fab892fda18a8bf9eedc45dc122c3288;hb=3a15ecebea7c560e01f81b7145f2cc32bb167c5f;hp=41cbd24ca79e536caf577cce5c5ce1c1b8b7dddf;hpb=a15fe7dd93cfcd9510b54b8481b4c3aa891ac380;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/AjaxResponse.php b/includes/AjaxResponse.php index 41cbd24ca7..6c2782c0fa 100644 --- a/includes/AjaxResponse.php +++ b/includes/AjaxResponse.php @@ -70,16 +70,23 @@ class AjaxResponse { */ private $mText; + /** + * @var Config + */ + private $mConfig; + /** * @param string|null $text + * @param Config|null $config */ - function __construct( $text = null ) { + function __construct( $text = null, Config $config = null ) { $this->mCacheDuration = null; $this->mVary = null; + $this->mConfig = $config ?: ConfigFactory::getDefaultInstance()->makeConfig( 'main' ); $this->mDisabled = false; $this->mText = ''; - $this->mResponseCode = '200 OK'; + $this->mResponseCode = 200; $this->mLastModified = false; $this->mContentType = 'application/x-wiki'; @@ -150,32 +157,34 @@ class AjaxResponse { * Construct the header and output it */ function sendHeaders() { - global $wgUseSquid, $wgUseESI; - if ( $this->mResponseCode ) { - $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode ); - header( "Status: " . $this->mResponseCode, true, (int)$n ); + // 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. + // http://php.net/string#language.types.string.conversion + $n = intval( trim( $this->mResponseCode ) ); + HttpStatus::header( $n ); } - header ( "Content-Type: " . $this->mContentType ); + header( "Content-Type: " . $this->mContentType ); if ( $this->mLastModified ) { - header ( "Last-Modified: " . $this->mLastModified ); + header( "Last-Modified: " . $this->mLastModified ); } else { - header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" ); + 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, + # If CDN caches are configured, tell them to cache the response, + # and tell the client to always check with the CDN. Otherwise, # tell the client to use a cached copy, without a way to purge it. - if ( $wgUseSquid ) { + if ( $this->mConfig->get( 'UseSquid' ) ) { # Expect explicit 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 + # Surrogate-Control controls our CDN, Cache-Control downstream caches - if ( $wgUseESI ) { + if ( $this->mConfig->get( 'UseESI' ) ) { header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' ); header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' ); } else { @@ -184,20 +193,20 @@ class AjaxResponse { } 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-maxage={$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 ) { - header ( "Vary: " . $this->mVary ); + header( "Vary: " . $this->mVary ); } } @@ -214,12 +223,12 @@ class AjaxResponse { $fname = 'AjaxResponse::checkLastModified'; if ( !$timestamp || $timestamp == '19700101000000' ) { - wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n", 'log' ); + wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP", 'private' ); return false; } if ( !$wgCachePages ) { - wfDebug( "$fname: CACHE DISABLED\n", 'log' ); + wfDebug( "$fname: CACHE DISABLED", 'private' ); return false; } @@ -233,29 +242,29 @@ class AjaxResponse { $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", 'log' ); - wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", 'log' ); + wfDebug( "$fname: -- client send If-Modified-Since: $modsince", 'private' ); + wfDebug( "$fname: -- we might send Last-Modified : $lastmod", 'private' ); if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) { ini_set( 'zlib.output_compression', 0 ); - $this->setResponseCode( "304 Not Modified" ); + $this->setResponseCode( 304 ); $this->disable(); $this->mLastModified = $lastmod; wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; " . - "page: $timestamp ; site $wgCacheEpoch\n", 'log' ); + "page: $timestamp ; site $wgCacheEpoch", 'private' ); return true; } else { wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; " . - "page: $timestamp ; site $wgCacheEpoch\n", 'log' ); + "page: $timestamp ; site $wgCacheEpoch", 'private' ); $this->mLastModified = $lastmod; } } else { - wfDebug( "$fname: client did not send If-Modified-Since header\n", 'log' ); + wfDebug( "$fname: client did not send If-Modified-Since header", 'private' ); $this->mLastModified = $lastmod; } return false; @@ -267,22 +276,20 @@ class AjaxResponse { * @return bool */ function loadFromMemcached( $mckey, $touched ) { - global $wgMemc; - if ( !$touched ) { return false; } - $mcvalue = $wgMemc->get( $mckey ); + $mcvalue = ObjectCache::getMainWANInstance()->get( $mckey ); if ( $mcvalue ) { # Check to see if the value has been invalidated if ( $touched <= $mcvalue['timestamp'] ) { - wfDebug( "Got $mckey from cache\n" ); + wfDebug( "Got $mckey from cache" ); $this->mText = $mcvalue['value']; return true; } else { - wfDebug( "$mckey has expired\n" ); + wfDebug( "$mckey has expired" ); } } @@ -295,9 +302,7 @@ class AjaxResponse { * @return bool */ function storeInMemcached( $mckey, $expiry = 86400 ) { - global $wgMemc; - - $wgMemc->set( $mckey, + ObjectCache::getMainWANInstance()->set( $mckey, array( 'timestamp' => wfTimestampNow(), 'value' => $this->mText