Merge "[FileBackend] Changed copy script to use batches for concurrency."
[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 Title|string Title object or prefixed DB key string
35 * @param $action string
36 * @return HTMLFileCache
37 */
38 public static function newFromTitle( $title, $action ) {
39 $cache = new self();
40
41 $allowedTypes = self::cacheablePageActions();
42 if ( !in_array( $action, $allowedTypes ) ) {
43 throw new MWException( "Invalid filecache type given." );
44 }
45 $cache->mKey = ( $title instanceof Title )
46 ? $title->getPrefixedDBkey()
47 : (string)$title;
48 $cache->mType = (string)$action;
49 $cache->mExt = 'html';
50
51 return $cache;
52 }
53
54 /**
55 * Cacheable actions
56 * @return array
57 */
58 protected static function cacheablePageActions() {
59 return array( 'view', 'history' );
60 }
61
62 /**
63 * Get the base file cache directory
64 * @return string
65 */
66 protected function cacheDirectory() {
67 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files
68 }
69
70 /**
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.
73 *
74 * @return string
75 */
76 protected function typeSubdirectory() {
77 if ( $this->mType === 'view' ) {
78 return ''; // b/c to not skip existing cache
79 } else {
80 return $this->mType . '/';
81 }
82 }
83
84 /**
85 * Check if pages can be cached for this request/user
86 * @param $context IContextSource
87 * @return bool
88 */
89 public static function useFileCache( IContextSource $context ) {
90 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang;
91 if ( !$wgUseFileCache ) {
92 return false;
93 }
94 if ( $wgShowIPinHeader || $wgDebugToolbar ) {
95 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" );
96 return false;
97 }
98
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() ) ) {
106 continue;
107 // Below are header setting params
108 } elseif ( $query === 'maxage' || $query === 'smaxage' ) {
109 continue;
110 }
111 return false;
112 }
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;
120 }
121
122 /**
123 * Read from cache to context output
124 * @param $context IContextSource
125 * @return void
126 */
127 public function loadFromFileCache( IContextSource $context ) {
128 global $wgMimeType, $wgLanguageCode;
129
130 wfDebug( __METHOD__ . "()\n");
131 $filename = $this->cachePath();
132
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 );
140 } else {
141 /* Send uncompressed */
142 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" );
143 readgzfile( $filename );
144 }
145 }
146 $context->getOutput()->disable(); // tell $wgOut that output is taken care of
147 }
148
149 /**
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
154 */
155 public function saveToFileCache( $text ) {
156 global $wgUseFileCache;
157
158 if ( !$wgUseFileCache || strlen( $text ) < 512 ) {
159 // Disabled or empty/broken output (OOM and PHP errors)
160 return $text;
161 }
162
163 wfDebug( __METHOD__ . "()\n", false);
164
165 $now = wfTimestampNow();
166 if ( $this->useGzip() ) {
167 $text = str_replace(
168 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
169 } else {
170 $text = str_replace(
171 '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
172 }
173
174 // Store text to FS...
175 $compressed = $this->saveText( $text );
176 if ( $compressed === false ) {
177 return $text; // error
178 }
179
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' );
185 return $compressed;
186 } else {
187 return $text;
188 }
189 } else {
190 return $text;
191 }
192 }
193
194 /**
195 * Clear the file caches for a page for all actions
196 * @param $title Title
197 * @return bool Whether $wgUseFileCache is enabled
198 */
199 public static function clearFileCache( Title $title ) {
200 global $wgUseFileCache;
201
202 if ( !$wgUseFileCache ) {
203 return false;
204 }
205
206 foreach ( self::cacheablePageActions() as $type ) {
207 $fc = self::newFromTitle( $title, $type );
208 $fc->clearCache();
209 }
210
211 return true;
212 }
213 }