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