MediaWiki  REL1_19
ResourceFileCache.php
Go to the documentation of this file.
00001 <?php
00007 class ResourceFileCache extends FileCacheBase {
00008         protected $mCacheWorthy;
00009 
00010         /* @TODO: configurable? */
00011         const MISS_THRESHOLD = 360; // 6/min * 60 min
00012 
00018         public static function newFromContext( ResourceLoaderContext $context ) {
00019                 $cache = new self();
00020 
00021                 if ( $context->getOnly() === 'styles' ) {
00022                         $cache->mType = 'css';
00023                 } else {
00024                         $cache->mType = 'js';
00025                 }
00026                 $modules = array_unique( $context->getModules() ); // remove duplicates
00027                 sort( $modules ); // normalize the order (permutation => combination)
00028                 $cache->mKey = sha1( $context->getHash() . implode( '|', $modules ) );
00029                 if ( count( $modules ) == 1 ) {
00030                         $cache->mCacheWorthy = true; // won't take up much space
00031                 }
00032 
00033                 return $cache;
00034         }
00035 
00042         public static function useFileCache( ResourceLoaderContext $context ) {
00043                 global $wgUseFileCache, $wgDefaultSkin, $wgLanguageCode;
00044                 if ( !$wgUseFileCache ) {
00045                         return false;
00046                 }
00047                 // Get all query values
00048                 $queryVals = $context->getRequest()->getValues();
00049                 foreach ( $queryVals as $query => $val ) {
00050                         if ( $query === 'modules' || $query === 'version' || $query === '*' ) {
00051                                 continue; // note: &* added as IE fix
00052                         } elseif ( $query === 'skin' && $val === $wgDefaultSkin ) {
00053                                 continue;
00054                         } elseif ( $query === 'lang' && $val === $wgLanguageCode ) {
00055                                 continue;
00056                         } elseif ( $query === 'only' && in_array( $val, array( 'styles', 'scripts' ) ) ) {
00057                                 continue;
00058                         } elseif ( $query === 'debug' && $val === 'false' ) {
00059                                 continue;
00060                         }
00061                         return false;
00062                 }
00063                 return true; // cacheable
00064         }
00065 
00070         protected function cacheDirectory() {
00071                 return $this->baseCacheDirectory() . '/resources';
00072         }
00073 
00078         public function isCacheWorthy() {
00079                 if ( $this->mCacheWorthy === null ) {
00080                         $this->mCacheWorthy = (
00081                                 $this->isCached() || // even stale cache indicates it was cache worthy
00082                                 $this->getMissesRecent() >= self::MISS_THRESHOLD // many misses
00083                         );
00084                 }
00085                 return $this->mCacheWorthy;
00086         }
00087 }