MediaWiki  REL1_19
AjaxResponse.php
Go to the documentation of this file.
00001 <?php
00015 class AjaxResponse {
00017         private $mCacheDuration;
00018 
00020         private $mContentType;
00021 
00023         private $mDisabled;
00024 
00026         private $mLastModified;
00027 
00029         private $mResponseCode;
00030 
00032         private $mVary;
00033 
00035         private $mText;
00036 
00037         function __construct( $text = null ) {
00038                 $this->mCacheDuration = null;
00039                 $this->mVary = null;
00040 
00041                 $this->mDisabled = false;
00042                 $this->mText = '';
00043                 $this->mResponseCode = '200 OK';
00044                 $this->mLastModified = false;
00045                 $this->mContentType = 'application/x-wiki';
00046 
00047                 if ( $text ) {
00048                         $this->addText( $text );
00049                 }
00050         }
00051 
00052         function setCacheDuration( $duration ) {
00053                 $this->mCacheDuration = $duration;
00054         }
00055 
00056         function setVary( $vary ) {
00057                 $this->mVary = $vary;
00058         }
00059 
00060         function setResponseCode( $code ) {
00061                 $this->mResponseCode = $code;
00062         }
00063 
00064         function setContentType( $type ) {
00065                 $this->mContentType = $type;
00066         }
00067 
00068         function disable() {
00069                 $this->mDisabled = true;
00070         }
00071 
00073         function addText( $text ) {
00074                 if ( ! $this->mDisabled && $text ) {
00075                         $this->mText .= $text;
00076                 }
00077         }
00078 
00080         function printText() {
00081                 if ( ! $this->mDisabled ) {
00082                         print $this->mText;
00083                 }
00084         }
00085 
00087         function sendHeaders() {
00088                 global $wgUseSquid, $wgUseESI;
00089 
00090                 if ( $this->mResponseCode ) {
00091                         $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
00092                         header( "Status: " . $this->mResponseCode, true, (int)$n );
00093                 }
00094 
00095                 header ( "Content-Type: " . $this->mContentType );
00096 
00097                 if ( $this->mLastModified ) {
00098                         header ( "Last-Modified: " . $this->mLastModified );
00099                 } else {
00100                         header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
00101                 }
00102 
00103                 if ( $this->mCacheDuration ) {
00104                         # If squid caches are configured, tell them to cache the response,
00105                         # and tell the client to always check with the squid. Otherwise,
00106                         # tell the client to use a cached copy, without a way to purge it.
00107 
00108                         if ( $wgUseSquid ) {
00109                                 # Expect explicite purge of the proxy cache, but require end user agents
00110                                 # to revalidate against the proxy on each visit.
00111                                 # Surrogate-Control controls our Squid, Cache-Control downstream caches
00112 
00113                                 if ( $wgUseESI ) {
00114                                         header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' );
00115                                         header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
00116                                 } else {
00117                                         header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
00118                                 }
00119 
00120                         } else {
00121 
00122                                 # Let the client do the caching. Cache is not purged.
00123                                 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" );
00124                                 header ( "Cache-Control: s-maxage={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" );
00125                         }
00126 
00127                 } else {
00128                         # always expired, always modified
00129                         header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );    // Date in the past
00130                         header ( "Cache-Control: no-cache, must-revalidate" );  // HTTP/1.1
00131                         header ( "Pragma: no-cache" );                          // HTTP/1.0
00132                 }
00133 
00134                 if ( $this->mVary ) {
00135                         header ( "Vary: " . $this->mVary );
00136                 }
00137         }
00138 
00145         function checkLastModified ( $timestamp ) {
00146                 global $wgCachePages, $wgCacheEpoch, $wgUser;
00147                 $fname = 'AjaxResponse::checkLastModified';
00148 
00149                 if ( !$timestamp || $timestamp == '19700101000000' ) {
00150                         wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
00151                         return;
00152                 }
00153 
00154                 if ( !$wgCachePages ) {
00155                         wfDebug( "$fname: CACHE DISABLED\n", false );
00156                         return;
00157                 }
00158 
00159                 if ( $wgUser->getOption( 'nocache' ) ) {
00160                         wfDebug( "$fname: USER DISABLED CACHE\n", false );
00161                         return;
00162                 }
00163 
00164                 $timestamp = wfTimestamp( TS_MW, $timestamp );
00165                 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
00166 
00167                 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
00168                         # IE sends sizes after the date like this:
00169                         # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
00170                         # this breaks strtotime().
00171                         $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
00172                         $modsinceTime = strtotime( $modsince );
00173                         $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
00174                         wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
00175                         wfDebug( "$fname: --  we might send Last-Modified : $lastmod\n", false );
00176 
00177                         if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
00178                                 ini_set( 'zlib.output_compression', 0 );
00179                                 $this->setResponseCode( "304 Not Modified" );
00180                                 $this->disable();
00181                                 $this->mLastModified = $lastmod;
00182 
00183                                 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
00184 
00185                                 return true;
00186                         } else {
00187                                 wfDebug( "$fname: READY  client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
00188                                 $this->mLastModified = $lastmod;
00189                         }
00190                 } else {
00191                         wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
00192                         $this->mLastModified = $lastmod;
00193                 }
00194         }
00195 
00201         function loadFromMemcached( $mckey, $touched ) {
00202                 global $wgMemc;
00203 
00204                 if ( !$touched ) {
00205                         return false;
00206                 }
00207 
00208                 $mcvalue = $wgMemc->get( $mckey );
00209                 if ( $mcvalue ) {
00210                         # Check to see if the value has been invalidated
00211                         if ( $touched <= $mcvalue['timestamp'] ) {
00212                                 wfDebug( "Got $mckey from cache\n" );
00213                                 $this->mText = $mcvalue['value'];
00214 
00215                                 return true;
00216                         } else {
00217                                 wfDebug( "$mckey has expired\n" );
00218                         }
00219                 }
00220 
00221                 return false;
00222         }
00223 
00229         function storeInMemcached( $mckey, $expiry = 86400 ) {
00230                 global $wgMemc;
00231 
00232                 $wgMemc->set( $mckey,
00233                         array(
00234                                 'timestamp' => wfTimestampNow(),
00235                                 'value' => $this->mText
00236                         ), $expiry
00237                 );
00238 
00239                 return true;
00240         }
00241 }