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