MediaWiki
REL1_23
|
00001 <?php 00031 class HTMLFileCache extends FileCacheBase { 00039 public static function newFromTitle( $title, $action ) { 00040 $cache = new self(); 00041 00042 $allowedTypes = self::cacheablePageActions(); 00043 if ( !in_array( $action, $allowedTypes ) ) { 00044 throw new MWException( "Invalid filecache type given." ); 00045 } 00046 $cache->mKey = ( $title instanceof Title ) 00047 ? $title->getPrefixedDBkey() 00048 : (string)$title; 00049 $cache->mType = (string)$action; 00050 $cache->mExt = 'html'; 00051 00052 return $cache; 00053 } 00054 00059 protected static function cacheablePageActions() { 00060 return array( 'view', 'history' ); 00061 } 00062 00067 protected function cacheDirectory() { 00068 return $this->baseCacheDirectory(); // no subdir for b/c with old cache files 00069 } 00070 00077 protected function typeSubdirectory() { 00078 if ( $this->mType === 'view' ) { 00079 return ''; // b/c to not skip existing cache 00080 } else { 00081 return $this->mType . '/'; 00082 } 00083 } 00084 00090 public static function useFileCache( IContextSource $context ) { 00091 global $wgUseFileCache, $wgShowIPinHeader, $wgDebugToolbar, $wgContLang; 00092 if ( !$wgUseFileCache ) { 00093 return false; 00094 } 00095 if ( $wgShowIPinHeader || $wgDebugToolbar ) { 00096 wfDebug( "HTML file cache skipped. Either \$wgShowIPinHeader and/or \$wgDebugToolbar on\n" ); 00097 00098 return false; 00099 } 00100 00101 // Get all query values 00102 $queryVals = $context->getRequest()->getValues(); 00103 foreach ( $queryVals as $query => $val ) { 00104 if ( $query === 'title' || $query === 'curid' ) { 00105 continue; // note: curid sets title 00106 // Normal page view in query form can have action=view. 00107 } elseif ( $query === 'action' && in_array( $val, self::cacheablePageActions() ) ) { 00108 continue; 00109 // Below are header setting params 00110 } elseif ( $query === 'maxage' || $query === 'smaxage' ) { 00111 continue; 00112 } 00113 00114 return false; 00115 } 00116 $user = $context->getUser(); 00117 // Check for non-standard user language; this covers uselang, 00118 // and extensions for auto-detecting user language. 00119 $ulang = $context->getLanguage()->getCode(); 00120 $clang = $wgContLang->getCode(); 00121 00122 // Check that there are no other sources of variation 00123 return !$user->getId() && !$user->getNewtalk() && $ulang == $clang; 00124 } 00125 00131 public function loadFromFileCache( IContextSource $context ) { 00132 global $wgMimeType, $wgLanguageCode; 00133 00134 wfDebug( __METHOD__ . "()\n" ); 00135 $filename = $this->cachePath(); 00136 00137 $context->getOutput()->sendCacheControl(); 00138 header( "Content-Type: $wgMimeType; charset=UTF-8" ); 00139 header( "Content-Language: $wgLanguageCode" ); 00140 if ( $this->useGzip() ) { 00141 if ( wfClientAcceptsGzip() ) { 00142 header( 'Content-Encoding: gzip' ); 00143 readfile( $filename ); 00144 } else { 00145 /* Send uncompressed */ 00146 wfDebug( __METHOD__ . " uncompressing cache file and sending it\n" ); 00147 readgzfile( $filename ); 00148 } 00149 } else { 00150 readfile( $filename ); 00151 } 00152 $context->getOutput()->disable(); // tell $wgOut that output is taken care of 00153 } 00154 00161 public function saveToFileCache( $text ) { 00162 global $wgUseFileCache; 00163 00164 if ( !$wgUseFileCache || strlen( $text ) < 512 ) { 00165 // Disabled or empty/broken output (OOM and PHP errors) 00166 return $text; 00167 } 00168 00169 wfDebug( __METHOD__ . "()\n", 'log' ); 00170 00171 $now = wfTimestampNow(); 00172 if ( $this->useGzip() ) { 00173 $text = str_replace( 00174 '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text ); 00175 } else { 00176 $text = str_replace( 00177 '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text ); 00178 } 00179 00180 // Store text to FS... 00181 $compressed = $this->saveText( $text ); 00182 if ( $compressed === false ) { 00183 return $text; // error 00184 } 00185 00186 // gzip output to buffer as needed and set headers... 00187 if ( $this->useGzip() ) { 00188 // @todo Ugly wfClientAcceptsGzip() function - use context! 00189 if ( wfClientAcceptsGzip() ) { 00190 header( 'Content-Encoding: gzip' ); 00191 00192 return $compressed; 00193 } else { 00194 return $text; 00195 } 00196 } else { 00197 return $text; 00198 } 00199 } 00200 00206 public static function clearFileCache( Title $title ) { 00207 global $wgUseFileCache; 00208 00209 if ( !$wgUseFileCache ) { 00210 return false; 00211 } 00212 00213 foreach ( self::cacheablePageActions() as $type ) { 00214 $fc = self::newFromTitle( $title, $type ); 00215 $fc->clearCache(); 00216 } 00217 00218 return true; 00219 } 00220 }