Merge "merge msg script now detects extensions main files"
[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 * @return HTMLFileCache
37 */
38 public static function newFromTitle( $title, $action ) {
39 $cache = new self();
40
41 $allowedTypes = self::cacheablePageActions();
42 if ( !in_array( $action, $allowedTypes ) ) {
43 throw new MWException( "Invalid filecache type given." );
44 }
45 $cache->mKey = ( $title instanceof Title )
46 ? $title->getPrefixedDBkey()
47 : (string)$title;
48 $cache->mType = (string)$action;
49 $cache->mExt = 'html';
50
51 return $cache;
52 }
53
54 /**
55 * Cacheable actions
56 * @return array
57 */
58 protected static function cacheablePageActions() {
59 return array( 'view', 'history' );
60 }
61
62 /**
63 * Get the base file cache directory
64 * @return string
65 */
66 protected function cacheDirectory() {
67 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
68 }
69
70 /**
71 * Get the cache type subdirectory (with the trailing slash) or the empty string
72 * Alter the type -> directory mapping to put action=view cache at the root.
73 *
74 * @return string
75 */
76 protected function typeSubdirectory() {
77 if ( $this->mType === 'view' ) {
78 return ''; // b/c to not skip existing cache
79 } else {
80 return $this->mType . '/';
81 }
82 }
83
84 /**
85 * Check if pages can be cached for this request/user
86 * @param $context IContextSource
87 * @return bool
88 */
89 public static function useFileCache( IContextSource $context ) {
90 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
91 if ( !$wgUseFileCache ) {
92 return false;
93 }
94 if ( $wgShowIPinHeader || $wgDebugToolbar ) {
95 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
96 return false;
97 }
98
99 // Get all query values
100 $queryVals = $context->getRequest()->getValues();
101 foreach ( $queryVals as $query => $val ) {
102 if ( $query === 'title' || $query === 'curid' ) {
103 continue; // note: curid sets title
104 // Normal page view in query form can have action=view.
105 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) {
106 continue;
107 // Below are header setting params
108 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
109 continue;
110 }
111 return false;
112 }
113 $user = $context->getUser();
114 // Check for non-standard user language; this covers uselang,
115 // and extensions for auto-detecting user language.
116 $ulang = $context->getLanguage()->getCode();
117 $clang = $wgContLang->getCode();
118 // Check that there are no other sources of variation
119 return !$user->getId() && !$user->getNewtalk() && $ulang == $clang;
120 }
121
122 /**
123 * Read from cache to context output
124 * @param $context IContextSource
125 * @return void
126 */
127 public function loadFromFileCache( IContextSource $context ) {
128 global $wgMimeType, $wgLanguageCode;
129
130 wfDebug( __METHOD__ . "()\n");
131 $filename = $this->cachePath();
132
133 $context->getOutput()->sendCacheControl();
134 header( "Content-Type: $wgMimeType; charset=UTF-8" );
135 header( "Content-Language: $wgLanguageCode" );
136 if ( $this->useGzip() ) {
137 if ( wfClientAcceptsGzip() ) {
138 header( 'Content-Encoding: gzip' );
139 readfile( $filename );
140 } else {
141 /* Send uncompressed */
142 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
143 readgzfile( $filename );
144 }
145 } else {
146 readfile( $filename );
147 }
148 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
149 }
150
151 /**
152 * Save this cache object with the given text.
153 * Use this as an ob_start() handler.
154 * @param $text string
155 * @return bool Whether $wgUseFileCache is enabled
156 */
157 public function saveToFileCache( $text ) {
158 global $wgUseFileCache;
159
160 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
161 // Disabled or empty/broken output (OOM and PHP errors)
162 return $text;
163 }
164
165 wfDebug( __METHOD__ . "()\n", false);
166
167 $now = wfTimestampNow();
168 if ( $this->useGzip() ) {
169 $text = str_replace(
170 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
171 } else {
172 $text = str_replace(
173 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
174 }
175
176 // Store text to FS...
177 $compressed = $this->saveText( $text );
178 if ( $compressed === false ) {
179 return $text; // error
180 }
181
182 // gzip output to buffer as needed and set headers...
183 if ( $this->useGzip() ) {
184 // @TODO: ugly wfClientAcceptsGzip() function - use context!
185 if ( wfClientAcceptsGzip() ) {
186 header( 'Content-Encoding: gzip' );
187 return $compressed;
188 } else {
189 return $text;
190 }
191 } else {
192 return $text;
193 }
194 }
195
196 /**
197 * Clear the file caches for a page for all actions
198 * @param $title Title
199 * @return bool Whether $wgUseFileCache is enabled
200 */
201 public static function clearFileCache( Title $title ) {
202 global $wgUseFileCache;
203
204 if ( !$wgUseFileCache ) {
205 return false;
206 }
207
208 foreach ( self::cacheablePageActions() as $type ) {
209 $fc = self::newFromTitle( $title, $type );
210 $fc->clearCache();
211 }
212
213 return true;
214 }
215 }