Merge "Show a tip at the end of the installer to prompt about extensions"
[lhc/web/wiklou.git] / includes / cache / HTMLFileCache.php
1 <?php
2 /**
3 * Page view caching in the file system.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Page view caching in the file system.
26 * The only cacheable actions are "view" and "history". Also special pages
27 * will not be cached.
28 *
29 * @ingroup Cache
30 */
31 class HTMLFileCache extends FileCacheBase {
32 /**
33 * Construct an ObjectFileCache from a Title and an action
34 * @param $title Title|string Title object or prefixed DB key string
35 * @param $action string
36 * @throws MWException
37 * @return HTMLFileCache
38 */
39 public static function newFromTitle( $title, $action ) {
40 $cache = new self();
41
42 $allowedTypes = self::cacheablePageActions();
43 if ( !in_array( $action, $allowedTypes ) ) {
44 throw new MWException( "Invalid filecache type given." );
45 }
46 $cache->mKey = ( $title instanceof Title )
47 ? $title->getPrefixedDBkey()
48 : (string)$title;
49 $cache->mType = (string)$action;
50 $cache->mExt = 'html';
51
52 return $cache;
53 }
54
55 /**
56 * Cacheable actions
57 * @return array
58 */
59 protected static function cacheablePageActions() {
60 return array( 'view', 'history' );
61 }
62
63 /**
64 * Get the base file cache directory
65 * @return string
66 */
67 protected function cacheDirectory() {
68 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
69 }
70
71 /**
72 * Get the cache type subdirectory (with the trailing slash) or the empty string
73 * Alter the type -> directory mapping to put action=view cache at the root.
74 *
75 * @return string
76 */
77 protected function typeSubdirectory() {
78 if ( $this->mType === 'view' ) {
79 return ''; // b/c to not skip existing cache
80 } else {
81 return $this->mType . '/';
82 }
83 }
84
85 /**
86 * Check if pages can be cached for this request/user
87 * @param $context IContextSource
88 * @return bool
89 */
90 public static function useFileCache( IContextSource $context ) {
91 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
92 if ( !$wgUseFileCache ) {
93 return false;
94 }
95 if ( $wgShowIPinHeader || $wgDebugToolbar ) {
96 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
97 return false;
98 }
99
100 // Get all query values
101 $queryVals = $context->getRequest()->getValues();
102 foreach ( $queryVals as $query => $val ) {
103 if ( $query === 'title' || $query === 'curid' ) {
104 continue; // note: curid sets title
105 // Normal page view in query form can have action=view.
106 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
107 continue;
108 // Below are header setting params
109 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
110 continue;
111 }
112 return false;
113 }
114 $user = $context->getUser();
115 // Check for non-standard user language; this covers uselang,
116 // and extensions for auto-detecting user language.
117 $ulang = $context->getLanguage()->getCode();
118 $clang = $wgContLang->getCode();
119 // Check that there are no other sources of variation
120 return !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
121 }
122
123 /**
124 * Read from cache to context output
125 * @param $context IContextSource
126 * @return void
127 */
128 public function loadFromFileCache( IContextSource $context ) {
129 global $wgMimeType, $wgLanguageCode;
130
131 wfDebug( __METHOD__ . "()\n" );
132 $filename = $this->cachePath();
133
134 $context->getOutput()->sendCacheControl();
135 header( "Content-Type: $wgMimeType; charset=UTF-8" );
136 header( "Content-Language: $wgLanguageCode" );
137 if ( $this->useGzip() ) {
138 if ( wfClientAcceptsGzip() ) {
139 header( 'Content-Encoding: gzip' );
140 readfile( $filename );
141 } else {
142 /* Send uncompressed */
143 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
144 readgzfile( $filename );
145 }
146 } else {
147 readfile( $filename );
148 }
149 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
150 }
151
152 /**
153 * Save this cache object with the given text.
154 * Use this as an ob_start() handler.
155 * @param $text string
156 * @return bool Whether $wgUseFileCache is enabled
157 */
158 public function saveToFileCache( $text ) {
159 global $wgUseFileCache;
160
161 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
162 // Disabled or empty/broken output (OOM and PHP errors)
163 return $text;
164 }
165
166 wfDebug( __METHOD__ . "()\n", false );
167
168 $now = wfTimestampNow();
169 if ( $this->useGzip() ) {
170 $text = str_replace(
171 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
172 } else {
173 $text = str_replace(
174 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
175 }
176
177 // Store text to FS...
178 $compressed = $this->saveText( $text );
179 if ( $compressed === false ) {
180 return $text; // error
181 }
182
183 // gzip output to buffer as needed and set headers...
184 if ( $this->useGzip() ) {
185 // @TODO: ugly wfClientAcceptsGzip() function - use context!
186 if ( wfClientAcceptsGzip() ) {
187 header( 'Content-Encoding: gzip' );
188 return $compressed;
189 } else {
190 return $text;
191 }
192 } else {
193 return $text;
194 }
195 }
196
197 /**
198 * Clear the file caches for a page for all actions
199 * @param $title Title
200 * @return bool Whether $wgUseFileCache is enabled
201 */
202 public static function clearFileCache( Title $title ) {
203 global $wgUseFileCache;
204
205 if ( !$wgUseFileCache ) {
206 return false;
207 }
208
209 foreach ( self::cacheablePageActions() as $type ) {
210 $fc = self::newFromTitle( $title, $type );
211 $fc->clearCache();
212 }
213
214 return true;
215 }
216 }