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