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 Title|string Title object or prefixed DB key string
35 * @param $action string
36 * @return HTMLFileCache
38 public static function newFromTitle( $title, $action ) {
41 $allowedTypes = self
::cacheablePageActions();
42 if ( !in_array( $action, $allowedTypes ) ) {
43 throw new MWException( "Invalid filecache type given." );
45 $cache->mKey
= ( $title instanceof Title
)
46 ?
$title->getPrefixedDBkey()
48 $cache->mType
= (string)$action;
49 $cache->mExt
= 'html';
58 protected static function cacheablePageActions() {
59 return array( 'view', 'history' );
63 * Get the base file cache directory
66 protected function cacheDirectory() {
67 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
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.
76 protected function typeSubdirectory() {
77 if ( $this->mType
=== 'view' ) {
78 return ''; // b/c to not skip existing cache
80 return $this->mType
. '/';
85 * Check if pages can be cached for this request/user
86 * @param $context IContextSource
89 public static function useFileCache( IContextSource
$context ) {
90 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
91 if ( !$wgUseFileCache ) {
94 if ( $wgShowIPinHeader ||
$wgDebugToolbar ) {
95 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
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() ) ) {
107 // Below are header setting params
108 } elseif ( $query === 'maxage' ||
$query === 'smaxage' ) {
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;
123 * Read from cache to context output
124 * @param $context IContextSource
127 public function loadFromFileCache( IContextSource
$context ) {
128 global $wgMimeType, $wgLanguageCode;
130 wfDebug( __METHOD__
. "()\n");
131 $filename = $this->cachePath();
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 );
141 /* Send uncompressed */
142 wfDebug( __METHOD__
. " uncompressing cache file and sending it\n" );
143 readgzfile( $filename );
146 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
150 * Save this cache object with the given text.
151 * Use this as an ob_start() handler.
152 * @param $text string
153 * @return bool Whether $wgUseFileCache is enabled
155 public function saveToFileCache( $text ) {
156 global $wgUseFileCache;
158 if ( !$wgUseFileCache ||
strlen( $text ) < 512 ) {
159 // Disabled or empty/broken output (OOM and PHP errors)
163 wfDebug( __METHOD__
. "()\n", false);
165 $now = wfTimestampNow();
166 if ( $this->useGzip() ) {
168 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
171 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
174 // Store text to FS...
175 $compressed = $this->saveText( $text );
176 if ( $compressed === false ) {
177 return $text; // error
180 // gzip output to buffer as needed and set headers...
181 if ( $this->useGzip() ) {
182 // @TODO: ugly wfClientAcceptsGzip() function - use context!
183 if ( wfClientAcceptsGzip() ) {
184 header( 'Content-Encoding: gzip' );
195 * Clear the file caches for a page for all actions
196 * @param $title Title
197 * @return bool Whether $wgUseFileCache is enabled
199 public static function clearFileCache( Title
$title ) {
200 global $wgUseFileCache;
202 if ( !$wgUseFileCache ) {
206 foreach ( self
::cacheablePageActions() as $type ) {
207 $fc = self
::newFromTitle( $title, $type );