Enable filecache for raw page hits if $wgUseFileCache is on
authorAaron Schulz <aaron@users.mediawiki.org>
Sun, 28 Dec 2008 13:32:32 +0000 (13:32 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Sun, 28 Dec 2008 13:32:32 +0000 (13:32 +0000)
includes/HTMLFileCache.php
includes/RawPage.php

index 299235a..15037cc 100644 (file)
@@ -28,15 +28,18 @@ class HTMLFileCache {
        }
 
        public function fileCacheName() {
-               global $wgFileCacheDirectory;
                if( !$this->mFileCache ) {
+                       global $wgFileCacheDirectory, $wgRequest;
                        $key = $this->mTitle->getPrefixedDbkey();
                        $hash = md5( $key );
+                       # Avoid extension confusion
                        $key = str_replace( '.', '%2E', urlencode( $key ) );
-
+                       # Store raw pages (like CSS hits) elsewhere
+                       $subdir = $wgRequest->getVal('action') == 'raw' ? 'raw/' : '';
+       
                        $hash1 = substr( $hash, 0, 1 );
                        $hash2 = substr( $hash, 0, 2 );
-                       $this->mFileCache = "{$wgFileCacheDirectory}/{$hash1}/{$hash2}/{$key}.html";
+                       $this->mFileCache = "{$wgFileCacheDirectory}/{$subdir}{$hash1}/{$hash2}/{$key}.html";
 
                        if( $this->useGzip() )
                                $this->mFileCache .= '.gz';
@@ -60,21 +63,24 @@ class HTMLFileCache {
         */
        public static function useFileCache() {
                global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest, $wgLang, $wgContLang;
-               if( !$wgUseFileCache )
-                       return false;
+               if( !$wgUseFileCache ) return false;
                // Get all query values
                $queryVals = $wgRequest->getValues();
                foreach( $queryVals as $query => $val ) {
-                       // Normal page view in query form can have action=view
-                       if( $query !== 'title' && $query !== 'curid' && !($query == 'action' && $val == 'view') ) {
-                               return false;
-                       }
+                       if( $query == 'title' || $query == 'curid' ) continue;
+                       // Normal page view in query form can have action=view.
+                       // Raw hits for pages also stored, like .css pages for example.
+                       if( $query == 'action' && ($val == 'view' || $val == 'raw') ) continue;
+                       if( $query == 'usemsgcache' && $val == 'yes' ) continue;
+                       // Below are header setting params
+                       if( $query == 'maxage' || $query == 'smaxage' || $query == 'ctype' || $query == 'gen' )
+                               continue;
                }
                // Check for non-standard user language; this covers uselang,
                // and extensions for auto-detecting user language.
                $ulang = $wgLang->getCode();
                $clang = $wgContLang->getCode();
-
+               // Check that there are no other sources of variation
                return !$wgShowIPinHeader && !$wgUser->getId() && !$wgUser->getNewtalk() && $ulang == $clang;
        }
 
index 6c0c458..9640cee 100644 (file)
@@ -149,6 +149,20 @@ class RawPage {
                # allow the client to cache this for 24 hours
                $mode = $this->mPrivateCache ? 'private' : 'public';
                header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
+               
+               if( HTMLFileCache::useFileCache() ) {
+                       $cache = new HTMLFileCache( $this->mTitle );
+                       if( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
+                               /* Check incoming headers to see if client has this cached */
+                               if( !$wgOut->checkLastModified( $cache->fileCacheTime() ) ) {
+                                       $cache->loadFromFileCache();
+                               }
+                               return;
+                       } else {
+                               ob_start( array(&$cache, 'saveToFileCache' ) );
+                       }
+               }
+               
                $text = $this->getRawText();
 
                if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {