MediaWiki
REL1_19
|
00001 <?php 00007 class HTMLFileCache extends FileCacheBase { 00014 public static function newFromTitle( $title, $action ) { 00015 $cache = new self(); 00016 00017 $allowedTypes = self::cacheablePageActions(); 00018 if ( !in_array( $action, $allowedTypes ) ) { 00019 throw new MWException( "Invalid filecache type given." ); 00020 } 00021 $cache->mKey = ( $title instanceof Title ) 00022 ? $title->getPrefixedDBkey() 00023 : (string)$title; 00024 $cache->mType = (string)$action; 00025 $cache->mExt = 'html'; 00026 00027 return $cache; 00028 } 00029 00034 protected static function cacheablePageActions() { 00035 return array( 'view', 'history' ); 00036 } 00037 00042 protected function cacheDirectory() { 00043 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files 00044 } 00045 00052 protected function typeSubdirectory() { 00053 if ( $this->mType === 'view' ) { 00054 return ''; // b/c to not skip existing cache 00055 } else { 00056 return $this->mType . '/'; 00057 } 00058 } 00059 00065 public static function useFileCache( IContextSource $context ) { 00066 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang; 00067 if ( !$wgUseFileCache ) { 00068 return false; 00069 } 00070 if ( $wgShowIPinHeader || $wgDebugToolbar ) { 00071 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" ); 00072 return false; 00073 } 00074 00075 // Get all query values 00076 $queryVals = $context->getRequest()->getValues(); 00077 foreach ( $queryVals as $query => $val ) { 00078 if ( $query === 'title' || $query === 'curid' ) { 00079 continue; // note: curid sets title 00080 // Normal page view in query form can have action=view. 00081 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) { 00082 continue; 00083 // Below are header setting params 00084 } elseif ( $query === 'maxage' || $query === 'smaxage' ) { 00085 continue; 00086 } 00087 return false; 00088 } 00089 $user = $context->getUser(); 00090 // Check for non-standard user language; this covers uselang, 00091 // and extensions for auto-detecting user language. 00092 $ulang = $context->getLanguage()->getCode(); 00093 $clang = $wgContLang->getCode(); 00094 // Check that there are no other sources of variation 00095 return !$user->getId() && !$user->getNewtalk() && $ulang == $clang; 00096 } 00097 00103 public function loadFromFileCache( IContextSource $context ) { 00104 global $wgMimeType, $wgLanguageCode; 00105 00106 wfDebug( __METHOD__ . "()\n"); 00107 $filename = $this->cachePath(); 00108 $context->getOutput()->sendCacheControl(); 00109 header( "Content-Type: $wgMimeType; charset=UTF-8" ); 00110 header( "Content-Language: $wgLanguageCode" ); 00111 if ( $this->useGzip() ) { 00112 if ( wfClientAcceptsGzip() ) { 00113 header( 'Content-Encoding: gzip' ); 00114 } else { 00115 /* Send uncompressed */ 00116 readgzfile( $filename ); 00117 return; 00118 } 00119 } 00120 readfile( $filename ); 00121 $context->getOutput()->disable(); // tell $wgOut that output is taken care of 00122 } 00123 00130 public function saveToFileCache( $text ) { 00131 global $wgUseFileCache; 00132 00133 if ( !$wgUseFileCache || strlen( $text ) < 512 ) { 00134 // Disabled or empty/broken output (OOM and PHP errors) 00135 return $text; 00136 } 00137 00138 wfDebug( __METHOD__ . "()\n", false); 00139 00140 $now = wfTimestampNow(); 00141 if ( $this->useGzip() ) { 00142 $text = str_replace( 00143 '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text ); 00144 } else { 00145 $text = str_replace( 00146 '</html>', '<!-- Cached '.$now." -->\n</html>", $text ); 00147 } 00148 00149 // Store text to FS... 00150 $compressed = $this->saveText( $text ); 00151 if ( $compressed === false ) { 00152 return $text; // error 00153 } 00154 00155 // gzip output to buffer as needed and set headers... 00156 if ( $this->useGzip() ) { 00157 // @TODO: ugly wfClientAcceptsGzip() function - use context! 00158 if ( wfClientAcceptsGzip() ) { 00159 header( 'Content-Encoding: gzip' ); 00160 return $compressed; 00161 } else { 00162 return $text; 00163 } 00164 } else { 00165 return $text; 00166 } 00167 } 00168 00174 public static function clearFileCache( Title $title ) { 00175 global $wgUseFileCache; 00176 00177 if ( !$wgUseFileCache ) { 00178 return false; 00179 } 00180 00181 foreach ( self::cacheablePageActions() as $type ) { 00182 $fc = self::newFromTitle( $title, $type ); 00183 $fc->clearCache(); 00184 } 00185 00186 return true; 00187 } 00188 }