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
25 * Page view caching in the file system.
26 * The only cacheable actions are "view" and "history". Also special pages
31 class HTMLFileCache
extends FileCacheBase
{
33 * Construct an ObjectFileCache from a Title and an action
34 * @param Title|string $title Title object or prefixed DB key string
35 * @param string $action
37 * @return HTMLFileCache
39 public static function newFromTitle( $title, $action ) {
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()
49 $cache->mType
= (string)$action;
50 $cache->mExt
= 'html';
59 protected static function cacheablePageActions() {
60 return array( 'view', 'history' );
64 * Get the base file cache directory
67 protected function cacheDirectory() {
68 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
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.
77 protected function typeSubdirectory() {
78 if ( $this->mType
=== 'view' ) {
79 return ''; // b/c to not skip existing cache
81 return $this->mType
. '/';
86 * Check if pages can be cached for this request/user
87 * @param IContextSource $context
90 public static function useFileCache( IContextSource
$context ) {
91 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
92 if ( !$wgUseFileCache ) {
95 if ( $wgShowIPinHeader ||
$wgDebugToolbar ) {
96 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
101 // Get all query values
102 $queryVals = $context->getRequest()->getValues();
103 foreach ( $queryVals as $query => $val ) {
104 if ( $query === 'title' ||
$query === 'curid' ) {
105 continue; // note: curid sets title
106 // Normal page view in query form can have action=view.
107 } elseif ( $query === 'action' && in_array( $val, self
::cacheablePageActions() ) ) {
109 // Below are header setting params
110 } elseif ( $query === 'maxage' ||
$query === 'smaxage' ) {
116 $user = $context->getUser();
117 // Check for non-standard user language; this covers uselang,
118 // and extensions for auto-detecting user language.
119 $ulang = $context->getLanguage()->getCode();
120 $clang = $wgContLang->getCode();
122 // Check that there are no other sources of variation
123 if ( $user->getId() ||
$user->getNewtalk() ||
$ulang != $clang ) {
126 // Allow extensions to disable caching
127 return wfRunHooks( 'HTMLFileCache::useFileCache', array( $context ) );
131 * Read from cache to context output
132 * @param IContextSource $context
135 public function loadFromFileCache( IContextSource
$context ) {
136 global $wgMimeType, $wgLanguageCode;
138 wfDebug( __METHOD__
. "()\n" );
139 $filename = $this->cachePath();
141 $context->getOutput()->sendCacheControl();
142 header( "Content-Type: $wgMimeType; charset=UTF-8" );
143 header( "Content-Language: $wgLanguageCode" );
144 if ( $this->useGzip() ) {
145 if ( wfClientAcceptsGzip() ) {
146 header( 'Content-Encoding: gzip' );
147 readfile( $filename );
149 /* Send uncompressed */
150 wfDebug( __METHOD__
. " uncompressing cache file and sending it\n" );
151 readgzfile( $filename );
154 readfile( $filename );
156 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
160 * Save this cache object with the given text.
161 * Use this as an ob_start() handler.
162 * @param string $text
163 * @return bool Whether $wgUseFileCache is enabled
165 public function saveToFileCache( $text ) {
166 global $wgUseFileCache;
168 if ( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
169 // Disabled or empty/broken output (OOM and PHP errors)
173 wfDebug( __METHOD__
. "()\n", 'log' );
175 $now = wfTimestampNow();
176 if ( $this->useGzip() ) {
178 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
181 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
184 // Store text to FS...
185 $compressed = $this->saveText( $text );
186 if ( $compressed === false ) {
187 return $text; // error
190 // gzip output to buffer as needed and set headers...
191 if ( $this->useGzip() ) {
192 // @todo Ugly wfClientAcceptsGzip() function - use context!
193 if ( wfClientAcceptsGzip() ) {
194 header( 'Content-Encoding: gzip' );
206 * Clear the file caches for a page for all actions
207 * @param Title $title
208 * @return bool Whether $wgUseFileCache is enabled
210 public static function clearFileCache( Title
$title ) {
211 global $wgUseFileCache;
213 if ( !$wgUseFileCache ) {
217 foreach ( self
::cacheablePageActions() as $type ) {
218 $fc = self
::newFromTitle( $title, $type );