From ac589ba2367cff359d2efbd7f397ccf61eae123b Mon Sep 17 00:00:00 2001 From: Tom Rix Date: Mon, 13 Jul 2009 04:42:02 +0000 Subject: [PATCH] ENHANCEMENT STaticPublisher can now play nice with Subsites, you just need to set FilesystemPublisher::domain_based_caching to true. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@81683 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/staticpublisher/FilesystemPublisher.php | 30 ++++++++++++-------- code/staticpublisher/StaticPublisher.php | 3 +- tasks/RebuildStaticCacheTask.php | 10 ++----- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/code/staticpublisher/FilesystemPublisher.php b/code/staticpublisher/FilesystemPublisher.php index 793db9ab..b1774bdf 100644 --- a/code/staticpublisher/FilesystemPublisher.php +++ b/code/staticpublisher/FilesystemPublisher.php @@ -14,6 +14,12 @@ class FilesystemPublisher extends StaticPublisher { protected static $static_base_url = null; + /** + * Use domain based cacheing (put cache files into a domain subfolder) + * This must be true if you are using this with subsites. + */ + public static $domain_based_caching = false; + /** * Set a different base URL for the static copy of the site. * This can be useful if you are running the CMS on a different domain from the website. @@ -67,16 +73,7 @@ class FilesystemPublisher extends StaticPublisher { Requirements::clear(); DataObject::flush_and_destroy_cache(); - //DataObject::destroy_cached_get_calls(false); - //DataObject::cache_get_calls(false); - - //echo 'Memory: ' . round(memory_get_usage()/100000)/10 . "\n"; - /* - if(!is_object($response)) { - echo "String response for url '$url'\n"; - print_r($response); - }*/ - + // Generate file content // PHP file caching will generate a simple script from a template if($this->fileExtension == 'php') { @@ -103,11 +100,20 @@ class FilesystemPublisher extends StaticPublisher { $content = $response . ''; } } - + + + $urlParts = @parse_url($url); + $urlParts['path'] = isset($urlParts['path']) ? $urlParts['path'] : ''; + $url = preg_replace('/[^a-zA-Z0-9]/si', '_', trim($urlParts['path'], '/')); if($this->fileExtension) $filename = $url ? "$url.$this->fileExtension" : "index.$this->fileExtension"; else $filename = $url ? "$url/index.html" : "index.html"; - + + if (self::$domain_based_caching) { + if (!$urlParts) continue; // seriously malformed url here... + $filename = $urlParts['host'] . '/' . $filename; + } + $files[$filename] = array( 'Content' => $content, 'Folder' => (dirname($filename) == '/') ? '' : (dirname($filename).'/'), diff --git a/code/staticpublisher/StaticPublisher.php b/code/staticpublisher/StaticPublisher.php index 5e0ac4ab..9afc4027 100644 --- a/code/staticpublisher/StaticPublisher.php +++ b/code/staticpublisher/StaticPublisher.php @@ -34,7 +34,7 @@ abstract class StaticPublisher extends DataObjectDecorator { $pages = Versioned::get_by_stage('SiteTree', 'Live', '', '', '', 10); if($pages) { foreach($pages as $page) { - $urls[] = $page->Link(); + $urls[] = $page->AbsoluteLink(); } } } @@ -44,7 +44,6 @@ abstract class StaticPublisher extends DataObjectDecorator { user_error("Bad URL: " . var_export($url, true), E_USER_WARNING); continue; } - $url = Director::makeRelative($url); if(substr($url,-1) == '/') $url = substr($url,0,-1); $urls[$i] = $url; } diff --git a/tasks/RebuildStaticCacheTask.php b/tasks/RebuildStaticCacheTask.php index 42460af0..b0260e23 100644 --- a/tasks/RebuildStaticCacheTask.php +++ b/tasks/RebuildStaticCacheTask.php @@ -49,14 +49,8 @@ class RebuildStaticCacheTask extends Controller { continue; } - $url = Director::makeRelative($url); - // Exclude absolute links - if(preg_match('/^https?:/', $url)) { - unset($urls[$i]); - } else { - if(substr($url,-1) == '/') $url = substr($url,0,-1); - $urls[$i] = $url; - } + if(substr($url,-1) == '/') $url = substr($url,0,-1); + $urls[$i] = $url; } $urls = array_unique($urls); sort($urls);