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