AjaxResponse.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup Ajax
  5. */
  6. if( !defined( 'MEDIAWIKI' ) ) {
  7. die( 1 );
  8. }
  9. /**
  10. * Handle responses for Ajax requests (send headers, print
  11. * content, that sort of thing)
  12. *
  13. * @ingroup Ajax
  14. */
  15. class AjaxResponse {
  16. /** Number of seconds to get the response cached by a proxy */
  17. private $mCacheDuration;
  18. /** HTTP header Content-Type */
  19. private $mContentType;
  20. /** Disables output. Can be set by calling $AjaxResponse->disable() */
  21. private $mDisabled;
  22. /** Date for the HTTP header Last-modified */
  23. private $mLastModified;
  24. /** HTTP response code */
  25. private $mResponseCode;
  26. /** HTTP Vary header */
  27. private $mVary;
  28. /** Content of our HTTP response */
  29. private $mText;
  30. function __construct( $text = NULL ) {
  31. $this->mCacheDuration = NULL;
  32. $this->mVary = NULL;
  33. $this->mDisabled = false;
  34. $this->mText = '';
  35. $this->mResponseCode = '200 OK';
  36. $this->mLastModified = false;
  37. $this->mContentType= 'application/x-wiki';
  38. if ( $text ) {
  39. $this->addText( $text );
  40. }
  41. }
  42. function setCacheDuration( $duration ) {
  43. $this->mCacheDuration = $duration;
  44. }
  45. function setVary( $vary ) {
  46. $this->mVary = $vary;
  47. }
  48. function setResponseCode( $code ) {
  49. $this->mResponseCode = $code;
  50. }
  51. function setContentType( $type ) {
  52. $this->mContentType = $type;
  53. }
  54. function disable() {
  55. $this->mDisabled = true;
  56. }
  57. /** Add content to the response */
  58. function addText( $text ) {
  59. if ( ! $this->mDisabled && $text ) {
  60. $this->mText .= $text;
  61. }
  62. }
  63. /** Output text */
  64. function printText() {
  65. if ( ! $this->mDisabled ) {
  66. print $this->mText;
  67. }
  68. }
  69. /** Construct the header and output it */
  70. function sendHeaders() {
  71. global $wgUseSquid, $wgUseESI;
  72. if ( $this->mResponseCode ) {
  73. $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
  74. header( "Status: " . $this->mResponseCode, true, (int)$n );
  75. }
  76. header ("Content-Type: " . $this->mContentType );
  77. if ( $this->mLastModified ) {
  78. header ("Last-Modified: " . $this->mLastModified );
  79. }
  80. else {
  81. header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  82. }
  83. if ( $this->mCacheDuration ) {
  84. # If squid caches are configured, tell them to cache the response,
  85. # and tell the client to always check with the squid. Otherwise,
  86. # tell the client to use a cached copy, without a way to purge it.
  87. if( $wgUseSquid ) {
  88. # Expect explicite purge of the proxy cache, but require end user agents
  89. # to revalidate against the proxy on each visit.
  90. # Surrogate-Control controls our Squid, Cache-Control downstream caches
  91. if ( $wgUseESI ) {
  92. header( 'Surrogate-Control: max-age='.$this->mCacheDuration.', content="ESI/1.0"');
  93. header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
  94. } else {
  95. header( 'Cache-Control: s-maxage='.$this->mCacheDuration.', must-revalidate, max-age=0' );
  96. }
  97. } else {
  98. # Let the client do the caching. Cache is not purged.
  99. header ("Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT");
  100. header ("Cache-Control: s-max-age={$this->mCacheDuration},public,max-age={$this->mCacheDuration}");
  101. }
  102. } else {
  103. # always expired, always modified
  104. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  105. header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  106. header ("Pragma: no-cache"); // HTTP/1.0
  107. }
  108. if ( $this->mVary ) {
  109. header ( "Vary: " . $this->mVary );
  110. }
  111. }
  112. /**
  113. * checkLastModified tells the client to use the client-cached response if
  114. * possible. If sucessful, the AjaxResponse is disabled so that
  115. * any future call to AjaxResponse::printText() have no effect. The method
  116. * returns true iff the response code was set to 304 Not Modified.
  117. */
  118. function checkLastModified ( $timestamp ) {
  119. global $wgCachePages, $wgCacheEpoch, $wgUser;
  120. $fname = 'AjaxResponse::checkLastModified';
  121. if ( !$timestamp || $timestamp == '19700101000000' ) {
  122. wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
  123. return;
  124. }
  125. if( !$wgCachePages ) {
  126. wfDebug( "$fname: CACHE DISABLED\n", false );
  127. return;
  128. }
  129. if( $wgUser->getOption( 'nocache' ) ) {
  130. wfDebug( "$fname: USER DISABLED CACHE\n", false );
  131. return;
  132. }
  133. $timestamp = wfTimestamp( TS_MW, $timestamp );
  134. $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
  135. if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
  136. # IE sends sizes after the date like this:
  137. # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
  138. # this breaks strtotime().
  139. $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
  140. $modsinceTime = strtotime( $modsince );
  141. $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
  142. wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
  143. wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
  144. if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
  145. ini_set('zlib.output_compression', 0);
  146. $this->setResponseCode( "304 Not Modified" );
  147. $this->disable();
  148. $this->mLastModified = $lastmod;
  149. wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
  150. return true;
  151. } else {
  152. wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
  153. $this->mLastModified = $lastmod;
  154. }
  155. } else {
  156. wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
  157. $this->mLastModified = $lastmod;
  158. }
  159. }
  160. function loadFromMemcached( $mckey, $touched ) {
  161. global $wgMemc;
  162. if ( !$touched ) return false;
  163. $mcvalue = $wgMemc->get( $mckey );
  164. if ( $mcvalue ) {
  165. # Check to see if the value has been invalidated
  166. if ( $touched <= $mcvalue['timestamp'] ) {
  167. wfDebug( "Got $mckey from cache\n" );
  168. $this->mText = $mcvalue['value'];
  169. return true;
  170. } else {
  171. wfDebug( "$mckey has expired\n" );
  172. }
  173. }
  174. return false;
  175. }
  176. function storeInMemcached( $mckey, $expiry = 86400 ) {
  177. global $wgMemc;
  178. $wgMemc->set( $mckey,
  179. array(
  180. 'timestamp' => wfTimestampNow(),
  181. 'value' => $this->mText
  182. ), $expiry
  183. );
  184. return true;
  185. }
  186. }