5e9d8127b3e8ab1e28a3cccc1898fa172d801519
[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 * @return string
49 */
50 protected function typeSubdirectory() {
51 if ( $this->mType === 'view' ) {
52 return ''; // b/c to not skip existing cache
53 } else {
54 return $this->mType . '/';
55 }
56 }
57
58 /**
59 * Check if pages can be cached for this request/user
60 * @param $context IContextSource
61 * @return bool
62 */
63 public static function useFileCache( IContextSource $context ) {
64 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
65 if ( !$wgUseFileCache ) {
66 return false;
67 }
68 if ( $wgShowIPinHeader || $wgDebugToolbar ) {
69 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
70 return false;
71 }
72
73 // Get all query values
74 $queryVals = $context->getRequest()->getValues();
75 foreach ( $queryVals as $query => $val ) {
76 if ( $query === 'title' || $query === 'curid' ) {
77 continue; // note: curid sets title
78 // Normal page view in query form can have action=view.
79 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
80 continue;
81 // Below are header setting params
82 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
83 continue;
84 }
85 return false;
86 }
87 $user = $context->getUser();
88 // Check for non-standard user language; this covers uselang,
89 // and extensions for auto-detecting user language.
90 $ulang = $context->getLanguage()->getCode();
91 $clang = $wgContLang->getCode();
92 // Check that there are no other sources of variation
93 return !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
94 }
95
96 /**
97 * Read from cache to context output
98 * @param $context IContextSource
99 * @return void
100 */
101 public function loadFromFileCache( IContextSource $context ) {
102 global $wgMimeType, $wgLanguageCode;
103
104 wfDebug( __METHOD__ . "()\n");
105 $filename = $this->cachePath();
106 $context->getOutput()->sendCacheControl();
107 header( "Content-Type: $wgMimeType; charset=UTF-8" );
108 header( "Content-Language: $wgLanguageCode" );
109 if ( $this->useGzip() ) {
110 if ( wfClientAcceptsGzip() ) {
111 header( 'Content-Encoding: gzip' );
112 } else {
113 /* Send uncompressed */
114 readgzfile( $filename );
115 return;
116 }
117 }
118 readfile( $filename );
119 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
120 }
121
122 /**
123 * Save this cache object with the given text.
124 * Use this as an ob_start() handler.
125 * @param $text string
126 * @return bool Whether $wgUseFileCache is enabled
127 */
128 public function saveToFileCache( $text ) {
129 global $wgUseFileCache;
130
131 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
132 // Disabled or empty/broken output (OOM and PHP errors)
133 return $text;
134 }
135
136 wfDebug( __METHOD__ . "()\n", false);
137
138 $now = wfTimestampNow();
139 if ( $this->useGzip() ) {
140 $text = str_replace(
141 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
142 } else {
143 $text = str_replace(
144 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
145 }
146
147 // Store text to FS...
148 $compressed = $this->saveText( $text );
149 if ( $compressed === false ) {
150 return $text; // error
151 }
152
153 // gzip output to buffer as needed and set headers...
154 if ( $this->useGzip() ) {
155 // @TODO: ugly wfClientAcceptsGzip() function - use context!
156 if ( wfClientAcceptsGzip() ) {
157 header( 'Content-Encoding: gzip' );
158 return $compressed;
159 } else {
160 return $text;
161 }
162 } else {
163 return $text;
164 }
165 }
166
167 /**
168 * Clear the file caches for a page for all actions
169 * @param $title Title
170 * @return bool Whether $wgUseFileCache is enabled
171 */
172 public static function clearFileCache( Title $title ) {
173 global $wgUseFileCache;
174
175 if ( !$wgUseFileCache ) {
176 return false;
177 }
178
179 foreach ( self::cacheablePageActions() as $type ) {
180 $fc = self::newFromTitle( $title, $type );
181 $fc->clearCache();
182 }
183
184 return true;
185 }
186 }