Merge "Make room for preloadFileStat() call in FileBackend::doOperationsInternal"
[lhc/web/wiklou.git] / includes / cache / ResourceFileCache.php
1 <?php
2 /**
3 * Resource loader request result 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 * Resource loader request result caching in the file system.
26 *
27 * @ingroup Cache
28 */
29 class ResourceFileCache extends FileCacheBase {
30 protected $mCacheWorthy;
31
32 /* @todo configurable? */
33 const MISS_THRESHOLD = 360; // 6/min * 60 min
34
35 /**
36 * Construct an ResourceFileCache from a context
37 * @param ResourceLoaderContext $context
38 * @return ResourceFileCache
39 */
40 public static function newFromContext( ResourceLoaderContext $context ) {
41 $cache = new self();
42
43 if ( $context->getOnly() === 'styles' ) {
44 $cache->mType = 'css';
45 } else {
46 $cache->mType = 'js';
47 }
48 $modules = array_unique( $context->getModules() ); // remove duplicates
49 sort( $modules ); // normalize the order (permutation => combination)
50 $cache->mKey = sha1( $context->getHash() . implode( '|', $modules ) );
51 if ( count( $modules ) == 1 ) {
52 $cache->mCacheWorthy = true; // won't take up much space
53 }
54
55 return $cache;
56 }
57
58 /**
59 * Check if an RL request can be cached.
60 * Caller is responsible for checking if any modules are private.
61 * @param ResourceLoaderContext $context
62 * @return bool
63 */
64 public static function useFileCache( ResourceLoaderContext $context ) {
65 global $wgUseFileCache, $wgDefaultSkin, $wgLanguageCode;
66 if ( !$wgUseFileCache ) {
67 return false;
68 }
69 // Get all query values
70 $queryVals = $context->getRequest()->getValues();
71 foreach ( $queryVals as $query => $val ) {
72 if ( $query === 'modules' || $query === 'version' || $query === '*' ) {
73 continue; // note: &* added as IE fix
74 } elseif ( $query === 'skin' && $val === $wgDefaultSkin ) {
75 continue;
76 } elseif ( $query === 'lang' && $val === $wgLanguageCode ) {
77 continue;
78 } elseif ( $query === 'only' && in_array( $val, array( 'styles', 'scripts' ) ) ) {
79 continue;
80 } elseif ( $query === 'debug' && $val === 'false' ) {
81 continue;
82 }
83
84 return false;
85 }
86
87 return true; // cacheable
88 }
89
90 /**
91 * Get the base file cache directory
92 * @return string
93 */
94 protected function cacheDirectory() {
95 return $this->baseCacheDirectory() . '/resources';
96 }
97
98 /**
99 * Item has many recent cache misses
100 * @return bool
101 */
102 public function isCacheWorthy() {
103 if ( $this->mCacheWorthy === null ) {
104 $this->mCacheWorthy = (
105 $this->isCached() || // even stale cache indicates it was cache worthy
106 $this->getMissesRecent() >= self::MISS_THRESHOLD // many misses
107 );
108 }
109
110 return $this->mCacheWorthy;
111 }
112 }