PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/cache/HTMLFileCache.php

https://github.com/daevid/MWFork
PHP | 250 lines | 168 code | 34 blank | 48 comment | 51 complexity | a98222f386344233afbfbf83ac5d31ef MD5 | raw file
  1. <?php
  2. /**
  3. * Contain the HTMLFileCache class
  4. * @file
  5. * @ingroup Cache
  6. */
  7. /**
  8. * Handles talking to the file cache, putting stuff in and taking it back out.
  9. * Mostly called from Article.php for the emergency abort/fallback to cache.
  10. *
  11. * Global options that affect this module:
  12. * - $wgCachePages
  13. * - $wgCacheEpoch
  14. * - $wgUseFileCache
  15. * - $wgCacheDirectory
  16. * - $wgFileCacheDirectory
  17. * - $wgUseGzip
  18. *
  19. * @ingroup Cache
  20. */
  21. class HTMLFileCache {
  22. /**
  23. * @var Title
  24. */
  25. var $mTitle;
  26. var $mFileCache, $mType;
  27. public function __construct( $title, $type = 'view' ) {
  28. $this->mTitle = $title;
  29. $this->mType = ($type == 'raw' || $type == 'view' ) ? $type : false;
  30. $this->fileCacheName(); // init name
  31. }
  32. public function fileCacheName() {
  33. if( !$this->mFileCache ) {
  34. global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
  35. if ( $wgFileCacheDirectory ) {
  36. $dir = $wgFileCacheDirectory;
  37. } elseif ( $wgCacheDirectory ) {
  38. $dir = "$wgCacheDirectory/html";
  39. } else {
  40. throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
  41. }
  42. # Store raw pages (like CSS hits) elsewhere
  43. $subdir = ($this->mType === 'raw') ? 'raw/' : '';
  44. $key = $this->mTitle->getPrefixedDbkey();
  45. if ( $wgFileCacheDepth > 0 ) {
  46. $hash = md5( $key );
  47. for ( $i = 1; $i <= $wgFileCacheDepth; $i++ ) {
  48. $subdir .= substr( $hash, 0, $i ) . '/';
  49. }
  50. }
  51. # Avoid extension confusion
  52. $key = str_replace( '.', '%2E', urlencode( $key ) );
  53. $this->mFileCache = "{$dir}/{$subdir}{$key}.html";
  54. if( $this->useGzip() ) {
  55. $this->mFileCache .= '.gz';
  56. }
  57. wfDebug( __METHOD__ . ": {$this->mFileCache}\n" );
  58. }
  59. return $this->mFileCache;
  60. }
  61. public function isFileCached() {
  62. if( $this->mType === false ) {
  63. return false;
  64. }
  65. return file_exists( $this->fileCacheName() );
  66. }
  67. public function fileCacheTime() {
  68. return wfTimestamp( TS_MW, filemtime( $this->fileCacheName() ) );
  69. }
  70. /**
  71. * Check if pages can be cached for this request/user
  72. * @return bool
  73. */
  74. public static function useFileCache() {
  75. global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
  76. if( !$wgUseFileCache ) {
  77. return false;
  78. }
  79. // Get all query values
  80. $queryVals = $wgRequest->getValues();
  81. foreach( $queryVals as $query => $val ) {
  82. if( $query == 'title' || $query == 'curid' ) {
  83. continue;
  84. // Normal page view in query form can have action=view.
  85. // Raw hits for pages also stored, like .css pages for example.
  86. } elseif( $query == 'action' && $val == 'view' ) {
  87. continue;
  88. } elseif( $query == 'usemsgcache' && $val == 'yes' ) {
  89. continue;
  90. // Below are header setting params
  91. } elseif( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' ) {
  92. continue;
  93. } else {
  94. return false;
  95. }
  96. }
  97. // Check for non-standard user language; this covers uselang,
  98. // and extensions for auto-detecting user language.
  99. $ulang = $wgLang->getCode();
  100. $clang = $wgContLang->getCode();
  101. // Check that there are no other sources of variation
  102. return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
  103. }
  104. /**
  105. * Check if up to date cache file exists
  106. * @param $timestamp string
  107. *
  108. * @return bool
  109. */
  110. public function isFileCacheGood( $timestamp = '' ) {
  111. global $wgCacheEpoch;
  112. if( !$this->isFileCached() ) {
  113. return false;
  114. }
  115. $cachetime = $this->fileCacheTime();
  116. $good = $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime;
  117. wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n");
  118. return $good;
  119. }
  120. public function useGzip() {
  121. global $wgUseGzip;
  122. return $wgUseGzip;
  123. }
  124. /* In handy string packages */
  125. public function fetchRawText() {
  126. return file_get_contents( $this->fileCacheName() );
  127. }
  128. public function fetchPageText() {
  129. if( $this->useGzip() ) {
  130. /* Why is there no gzfile_get_contents() or gzdecode()? */
  131. return implode( '', gzfile( $this->fileCacheName() ) );
  132. } else {
  133. return $this->fetchRawText();
  134. }
  135. }
  136. /* Working directory to/from output */
  137. public function loadFromFileCache() {
  138. global $wgOut, $wgMimeType, $wgLanguageCode;
  139. wfDebug( __METHOD__ . "()\n");
  140. $filename = $this->fileCacheName();
  141. // Raw pages should handle cache control on their own,
  142. // even when using file cache. This reduces hits from clients.
  143. if( $this->mType !== 'raw' ) {
  144. $wgOut->sendCacheControl();
  145. header( "Content-Type: $wgMimeType; charset=UTF-8" );
  146. header( "Content-Language: $wgLanguageCode" );
  147. }
  148. if( $this->useGzip() ) {
  149. if( wfClientAcceptsGzip() ) {
  150. header( 'Content-Encoding: gzip' );
  151. } else {
  152. /* Send uncompressed */
  153. readgzfile( $filename );
  154. return;
  155. }
  156. }
  157. readfile( $filename );
  158. $wgOut->disable(); // tell $wgOut that output is taken care of
  159. }
  160. protected function checkCacheDirs() {
  161. $filename = $this->fileCacheName();
  162. $mydir2 = substr($filename,0,strrpos($filename,'/')); # subdirectory level 2
  163. $mydir1 = substr($mydir2,0,strrpos($mydir2,'/')); # subdirectory level 1
  164. wfMkdirParents( $mydir1, null, __METHOD__ );
  165. wfMkdirParents( $mydir2, null, __METHOD__ );
  166. }
  167. public function saveToFileCache( $text ) {
  168. global $wgUseFileCache;
  169. if( !$wgUseFileCache || strlen( $text ) < 512 ) {
  170. // Disabled or empty/broken output (OOM and PHP errors)
  171. return $text;
  172. }
  173. wfDebug( __METHOD__ . "()\n", false);
  174. $this->checkCacheDirs();
  175. $f = fopen( $this->fileCacheName(), 'w' );
  176. if($f) {
  177. $now = wfTimestampNow();
  178. if( $this->useGzip() ) {
  179. $rawtext = str_replace( '</html>',
  180. '<!-- Cached/compressed '.$now." -->\n</html>",
  181. $text );
  182. $text = gzencode( $rawtext );
  183. } else {
  184. $text = str_replace( '</html>',
  185. '<!-- Cached '.$now." -->\n</html>",
  186. $text );
  187. }
  188. fwrite( $f, $text );
  189. fclose( $f );
  190. if( $this->useGzip() ) {
  191. if( wfClientAcceptsGzip() ) {
  192. header( 'Content-Encoding: gzip' );
  193. return $text;
  194. } else {
  195. return $rawtext;
  196. }
  197. } else {
  198. return $text;
  199. }
  200. }
  201. return $text;
  202. }
  203. public static function clearFileCache( $title ) {
  204. global $wgUseFileCache;
  205. if ( !$wgUseFileCache ) {
  206. return false;
  207. }
  208. wfSuppressWarnings();
  209. $fc = new self( $title, 'view' );
  210. unlink( $fc->fileCacheName() );
  211. $fc = new self( $title, 'raw' );
  212. unlink( $fc->fileCacheName() );
  213. wfRestoreWarnings();
  214. return true;
  215. }
  216. }