532850a59648e1e5348d23bb3af5e31b3289c396
[lhc/web/wiklou.git] / includes / cache / HTMLFileCache.php
1 <?php
2 /**
3 * Contain the HTMLFileCache class
4 * @file
5 * @ingroup Cache
6 */
7 class HTMLFileCache extends FileCacheBase {
8 /**
9 * Construct an ObjectFileCache from a Title and an action
10 * @param $title Title
11 * @param $action string
12 * @return HTMLFileCache
13 */
14 public static function newFromTitle( Title $title, $action ) {
15 $cache = new self();
16
17 $allowedTypes = self::cacheablePageActions();
18 if ( !in_array( $action, $allowedTypes ) ) {
19 throw new MWException( "Invalid filecache type given." );
20 }
21 $cache->mKey = $title->getPrefixedDBkey();
22 $cache->mType = (string)$action;
23 $cache->mExt = 'html';
24
25 return $cache;
26 }
27
28 /**
29 * Cacheable actions
30 * @return array
31 */
32 protected static function cacheablePageActions() {
33 return array( 'view', 'history' );
34 }
35
36 /**
37 * Get the base file cache directory
38 * Note: avoids baseCacheDirectory() for b/c to not skip existing cache
39 * @return string
40 */
41 protected function cacheDirectory() {
42 global $wgCacheDirectory, $wgFileCacheDirectory, $wgFileCacheDepth;
43 if ( $wgFileCacheDirectory ) {
44 $dir = $wgFileCacheDirectory;
45 } elseif ( $wgCacheDirectory ) {
46 $dir = "$wgCacheDirectory/html";
47 } else {
48 throw new MWException( 'Please set $wgCacheDirectory in LocalSettings.php if you wish to use the HTML file cache' );
49 }
50 return $dir;
51 }
52
53 /**
54 * Get the cache type subdirectory (with the trailing slash) or the empty string
55 * @return string
56 */
57 protected function typeSubdirectory() {
58 if ( $this->mType === 'view' ) {
59 return ''; // b/c to not skip existing cache
60 } else {
61 return $this->mType . '/';
62 }
63 }
64
65 /**
66 * Check if pages can be cached for this request/user
67 * @param $context IContextSource
68 * @return bool
69 */
70 public static function useFileCache( IContextSource $context ) {
71 global $wgUseFileCache, $wgShowIPinHeader, $wgContLang;
72 if ( !$wgUseFileCache ) {
73 return false;
74 }
75 // Get all query values
76 $queryVals = $context->getRequest()->getValues();
77 foreach ( $queryVals as $query => $val ) {
78 if ( $query === 'title' || $query === 'curid' ) {
79 continue; // note: curid sets title
80 // Normal page view in query form can have action=view.
81 // Raw hits for pages also stored, like .css pages for example.
82 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
83 continue;
84 // Below are header setting params
85 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
86 continue;
87 }
88 return false;
89 }
90 $user = $context->getUser();
91 // Check for non-standard user language; this covers uselang,
92 // and extensions for auto-detecting user language.
93 $ulang = $context->getLang()->getCode();
94 $clang = $wgContLang->getCode();
95 // Check that there are no other sources of variation
96 return !$wgShowIPinHeader && !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
97 }
98
99 /**
100 * Read from cache to context output
101 * @param $context IContextSource
102 * @return void
103 */
104 public function loadFromFileCache( IContextSource $context ) {
105 global $wgMimeType, $wgLanguageCode;
106
107 wfDebug( __METHOD__ . "()\n");
108 $filename = $this->cachePath();
109 $context->getOutput()->sendCacheControl();
110 header( "Content-Type: $wgMimeType; charset=UTF-8" );
111 header( "Content-Language: $wgLanguageCode" );
112 if ( $this->useGzip() ) {
113 if ( wfClientAcceptsGzip() ) {
114 header( 'Content-Encoding: gzip' );
115 } else {
116 /* Send uncompressed */
117 readgzfile( $filename );
118 return;
119 }
120 }
121 readfile( $filename );
122 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
123 }
124
125 /**
126 * Save this cache object with the given text.
127 * Use this as an ob_start() handler.
128 * @param $text string
129 * @return bool Whether $wgUseFileCache is enabled
130 */
131 public function saveToFileCache( $text ) {
132 global $wgUseFileCache;
133
134 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
135 // Disabled or empty/broken output (OOM and PHP errors)
136 return $text;
137 }
138
139 wfDebug( __METHOD__ . "()\n", false);
140
141 $now = wfTimestampNow();
142 if ( $this->useGzip() ) {
143 $text = str_replace(
144 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
145 } else {
146 $text = str_replace(
147 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
148 }
149
150 // Store text to FS...
151 $compressed = $this->saveText( $text );
152 if ( $compressed === false ) {
153 return $text; // error
154 }
155
156 // gzip output to buffer as needed and set headers...
157 if ( $this->useGzip() ) {
158 // @TODO: ugly wfClientAcceptsGzip() function - use context!
159 if ( wfClientAcceptsGzip() ) {
160 header( 'Content-Encoding: gzip' );
161 return $compressed;
162 } else {
163 return $text;
164 }
165 } else {
166 return $text;
167 }
168 }
169
170 /**
171 * Clear the file caches for a page for all actions
172 * @param $title Title
173 * @return bool Whether $wgUseFileCache is enabled
174 */
175 public static function clearFileCache( Title $title ) {
176 global $wgUseFileCache;
177
178 if ( !$wgUseFileCache ) {
179 return false;
180 }
181
182 foreach ( self::cacheablePageActions() as $type ) {
183 $fc = self::newFromTitle( $title, $type );
184 $fc->clearCache();
185 }
186
187 return true;
188 }
189 }