add up comment for FileCache rewrite (r98698)
[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|string Title object or prefixed DB key string
11 * @param $action string
12 * @return HTMLFileCache
13 */
14 public static function newFromTitle( $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 instanceof Title )
22 ? $title->getPrefixedDBkey()
23 : (string)$title;
24 $cache->mType = (string)$action;
25 $cache->mExt = 'html';
26
27 return $cache;
28 }
29
30 /**
31 * Cacheable actions
32 * @return array
33 */
34 protected static function cacheablePageActions() {
35 return array( 'view', 'history' );
36 }
37
38 /**
39 * Get the base file cache directory
40 * @return string
41 */
42 protected function cacheDirectory() {
43 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
44 }
45
46 /**
47 * Get the cache type subdirectory (with the trailing slash) or the empty string
48 * Alter the type -> directory mapping to put action=view cache at the root.
49 *
50 * @return string
51 */
52 protected function typeSubdirectory() {
53 if ( $this->mType === 'view' ) {
54 return ''; // b/c to not skip existing cache
55 } else {
56 return $this->mType . '/';
57 }
58 }
59
60 /**
61 * Check if pages can be cached for this request/user
62 * @param $context IContextSource
63 * @return bool
64 */
65 public static function useFileCache( IContextSource $context ) {
66 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
67 if ( !$wgUseFileCache ) {
68 return false;
69 }
70 if ( $wgShowIPinHeader || $wgDebugToolbar ) {
71 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
72 return false;
73 }
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 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
82 continue;
83 // Below are header setting params
84 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
85 continue;
86 }
87 return false;
88 }
89 $user = $context->getUser();
90 // Check for non-standard user language; this covers uselang,
91 // and extensions for auto-detecting user language.
92 $ulang = $context->getLanguage()->getCode();
93 $clang = $wgContLang->getCode();
94 // Check that there are no other sources of variation
95 return !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
96 }
97
98 /**
99 * Read from cache to context output
100 * @param $context IContextSource
101 * @return void
102 */
103 public function loadFromFileCache( IContextSource $context ) {
104 global $wgMimeType, $wgLanguageCode;
105
106 wfDebug( __METHOD__ . "()\n");
107 $filename = $this->cachePath();
108 $context->getOutput()->sendCacheControl();
109 header( "Content-Type: $wgMimeType; charset=UTF-8" );
110 header( "Content-Language: $wgLanguageCode" );
111 if ( $this->useGzip() ) {
112 if ( wfClientAcceptsGzip() ) {
113 header( 'Content-Encoding: gzip' );
114 } else {
115 /* Send uncompressed */
116 readgzfile( $filename );
117 return;
118 }
119 }
120 readfile( $filename );
121 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
122 }
123
124 /**
125 * Save this cache object with the given text.
126 * Use this as an ob_start() handler.
127 * @param $text string
128 * @return bool Whether $wgUseFileCache is enabled
129 */
130 public function saveToFileCache( $text ) {
131 global $wgUseFileCache;
132
133 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
134 // Disabled or empty/broken output (OOM and PHP errors)
135 return $text;
136 }
137
138 wfDebug( __METHOD__ . "()\n", false);
139
140 $now = wfTimestampNow();
141 if ( $this->useGzip() ) {
142 $text = str_replace(
143 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
144 } else {
145 $text = str_replace(
146 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
147 }
148
149 // Store text to FS...
150 $compressed = $this->saveText( $text );
151 if ( $compressed === false ) {
152 return $text; // error
153 }
154
155 // gzip output to buffer as needed and set headers...
156 if ( $this->useGzip() ) {
157 // @TODO: ugly wfClientAcceptsGzip() function - use context!
158 if ( wfClientAcceptsGzip() ) {
159 header( 'Content-Encoding: gzip' );
160 return $compressed;
161 } else {
162 return $text;
163 }
164 } else {
165 return $text;
166 }
167 }
168
169 /**
170 * Clear the file caches for a page for all actions
171 * @param $title Title
172 * @return bool Whether $wgUseFileCache is enabled
173 */
174 public static function clearFileCache( Title $title ) {
175 global $wgUseFileCache;
176
177 if ( !$wgUseFileCache ) {
178 return false;
179 }
180
181 foreach ( self::cacheablePageActions() as $type ) {
182 $fc = self::newFromTitle( $title, $type );
183 $fc->clearCache();
184 }
185
186 return true;
187 }
188 }