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