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