Merge "Typo fix"
[mediawiki.git] / includes / cache / HTMLFileCache.php
blobab379116e5cb3050d5ece79098956751b2dd9574
1 <?php
2 /**
3 * Page view caching in the file system.
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.
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.
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
20 * @file
21 * @ingroup Cache
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.
29 * @ingroup Cache
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
39 public static function newFromTitle( $title, $action ) {
40 $cache = new self();
42 $allowedTypes = self::cacheablePageActions();
43 if ( !in_array( $action, $allowedTypes ) ) {
44 throw new MWException( "Invalid filecache type given." );
46 $cache->mKey = ( $title instanceof Title )
47 ? $title->getPrefixedDBkey()
48 : (string)$title;
49 $cache->mType = (string)$action;
50 $cache->mExt = 'html';
52 return $cache;
55 /**
56 * Cacheable actions
57 * @return array
59 protected static function cacheablePageActions() {
60 return array( 'view', 'history' );
63 /**
64 * Get the base file cache directory
65 * @return string
67 protected function cacheDirectory() {
68 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
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.
75 * @return string
77 protected function typeSubdirectory() {
78 if ( $this->mType === 'view' ) {
79 return ''; // b/c to not skip existing cache
80 } else {
81 return $this->mType . '/';
85 /**
86 * Check if pages can be cached for this request/user
87 * @param $context IContextSource
88 * @return bool
90 public static function useFileCache( IContextSource $context ) {
91 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
92 if ( !$wgUseFileCache ) {
93 return false;
95 if ( $wgShowIPinHeader || $wgDebugToolbar ) {
96 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
97 return false;
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;
112 return false;
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;
124 * Read from cache to context output
125 * @param $context IContextSource
126 * @return void
128 public function loadFromFileCache( IContextSource $context ) {
129 global $wgMimeType, $wgLanguageCode;
131 wfDebug( __METHOD__ . "()\n" );
132 $filename = $this->cachePath();
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 );
146 } else {
147 readfile( $filename );
149 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
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
158 public function saveToFileCache( $text ) {
159 global $wgUseFileCache;
161 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
162 // Disabled or empty/broken output (OOM and PHP errors)
163 return $text;
166 wfDebug( __METHOD__ . "()\n", false );
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 );
177 // Store text to FS...
178 $compressed = $this->saveText( $text );
179 if ( $compressed === false ) {
180 return $text; // error
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;
192 } else {
193 return $text;
198 * Clear the file caches for a page for all actions
199 * @param $title Title
200 * @return bool Whether $wgUseFileCache is enabled
202 public static function clearFileCache( Title $title ) {
203 global $wgUseFileCache;
205 if ( !$wgUseFileCache ) {
206 return false;
209 foreach ( self::cacheablePageActions() as $type ) {
210 $fc = self::newFromTitle( $title, $type );
211 $fc->clearCache();
214 return true;