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 * @deprecated Since 1.24, instantiate this class directly
41 public static function newFromTitle( $title, $action ) {
42 return new self( $title, $action );
46 * @param Title|string $title Title object or prefixed DB key string
47 * @param string $action
50 public function __construct( $title, $action ) {
51 $allowedTypes = self
::cacheablePageActions();
52 if ( !in_array( $action, $allowedTypes ) ) {
53 throw new MWException( 'Invalid file cache type given.' );
55 $this->mKey
= ( $title instanceof Title
)
56 ?
$title->getPrefixedDBkey()
58 $this->mType
= (string)$action;
66 protected static function cacheablePageActions() {
67 return array( 'view', 'history' );
71 * Get the base file cache directory
74 protected function cacheDirectory() {
75 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
79 * Get the cache type subdirectory (with the trailing slash) or the empty string
80 * Alter the type -> directory mapping to put action=view cache at the root.
84 protected function typeSubdirectory() {
85 if ( $this->mType
=== 'view' ) {
86 return ''; // b/c to not skip existing cache
88 return $this->mType
. '/';
93 * Check if pages can be cached for this request/user
94 * @param IContextSource $context
97 public static function useFileCache( IContextSource
$context ) {
98 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
99 if ( !$wgUseFileCache ) {
102 if ( $wgShowIPinHeader ||
$wgDebugToolbar ) {
103 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
108 // Get all query values
109 $queryVals = $context->getRequest()->getValues();
110 foreach ( $queryVals as $query => $val ) {
111 if ( $query === 'title' ||
$query === 'curid' ) {
112 continue; // note: curid sets title
113 // Normal page view in query form can have action=view.
114 } elseif ( $query === 'action' && in_array( $val, self
::cacheablePageActions() ) ) {
116 // Below are header setting params
117 } elseif ( $query === 'maxage' ||
$query === 'smaxage' ) {
123 $user = $context->getUser();
124 // Check for non-standard user language; this covers uselang,
125 // and extensions for auto-detecting user language.
126 $ulang = $context->getLanguage()->getCode();
127 $clang = $wgContLang->getCode();
129 // Check that there are no other sources of variation
130 if ( $user->getId() ||
$user->getNewtalk() ||
$ulang != $clang ) {
133 // Allow extensions to disable caching
134 return Hooks
::run( 'HTMLFileCache::useFileCache', array( $context ) );
138 * Read from cache to context output
139 * @param IContextSource $context
142 public function loadFromFileCache( IContextSource
$context ) {
143 global $wgMimeType, $wgLanguageCode;
145 wfDebug( __METHOD__
. "()\n" );
146 $filename = $this->cachePath();
148 $context->getOutput()->sendCacheControl();
149 header( "Content-Type: $wgMimeType; charset=UTF-8" );
150 header( "Content-Language: $wgLanguageCode" );
151 if ( $this->useGzip() ) {
152 if ( wfClientAcceptsGzip() ) {
153 header( 'Content-Encoding: gzip' );
154 readfile( $filename );
156 /* Send uncompressed */
157 wfDebug( __METHOD__
. " uncompressing cache file and sending it\n" );
158 readgzfile( $filename );
161 readfile( $filename );
163 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
167 * Save this cache object with the given text.
168 * Use this as an ob_start() handler.
169 * @param string $text
170 * @return bool Whether $wgUseFileCache is enabled
172 public function saveToFileCache( $text ) {
173 global $wgUseFileCache;
175 if ( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
176 // Disabled or empty/broken output (OOM and PHP errors)
180 wfDebug( __METHOD__
. "()\n", 'log' );
182 $now = wfTimestampNow();
183 if ( $this->useGzip() ) {
185 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
188 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
191 // Store text to FS...
192 $compressed = $this->saveText( $text );
193 if ( $compressed === false ) {
194 return $text; // error
197 // gzip output to buffer as needed and set headers...
198 if ( $this->useGzip() ) {
199 // @todo Ugly wfClientAcceptsGzip() function - use context!
200 if ( wfClientAcceptsGzip() ) {
201 header( 'Content-Encoding: gzip' );
213 * Clear the file caches for a page for all actions
214 * @param Title $title
215 * @return bool Whether $wgUseFileCache is enabled
217 public static function clearFileCache( Title
$title ) {
218 global $wgUseFileCache;
220 if ( !$wgUseFileCache ) {
224 foreach ( self
::cacheablePageActions() as $type ) {
225 $fc = new self( $title, $type );