diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index 3df76768c..477a14894 100755 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -170,6 +170,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid */ public static $breadcrumbs_delimiter = " » "; + /** + * Whether or not to write the homepage map for static publisher + */ + public static $write_homepage_map = true; + static $searchable_fields = array( 'Title', 'Content', @@ -1948,13 +1953,34 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid } } + // Check to write CMS homepage map. + $usingStaticPublishing = false; + foreach(ClassInfo::subclassesFor('StaticPublisher') as $class) if ($this->hasExtension($class)) $usingStaticPublishing = true; + // NOTE: if you change the path here, you must also change it in sapphire/static-main.php + if (self::$write_homepage_map) { + if ($usingStaticPublishing && $map = self::generate_homemage_domain_map()) { + @file_put_contents(BASE_PATH.'/'.ASSETS_DIR.'/_homepage-map.php', ""); + } else { if (file_exists(BASE_PATH.'/'.ASSETS_DIR.'/_homepage-map.php')) unlink(BASE_PATH.'/'.ASSETS_DIR.'/_homepage-map.php'); } + } + // Handle activities undertaken by decorators $this->extend('onAfterPublish', $original); return true; } + static function generate_homepage_domain_map() { + $domainSpecificHomepages = Versioned::get_by_stage('Page', 'Live', "HomepageForDomain != ''", 'URLSegment ASC'); + if (!$domainSpecificHomepages) return false; + + $map = array(); + foreach($domainSpecificHomepages->map('URLSegment', 'HomepageForDomain') as $url => $domains) { + foreach(explode(',', $domains) as $domain) $map[$domain] = $url; + } + return $map; + } + /** * Unpublish this page - remove it from the live site * diff --git a/static-main.php b/static-main.php index 45d403817..bcde77f2e 100644 --- a/static-main.php +++ b/static-main.php @@ -16,13 +16,14 @@ $cacheOn = true; $cacheDebug = false; $hostmapLocation = '../subsites/host-map.php'; +$homepageMapLocation = '../assets/_homepage-map.php'; date_default_timezone_set('Pacific/Auckland'); if ($cacheOn && empty($_COOKIE['bypassStaticCache'])) { if (file_exists($hostmapLocation)) { include_once $hostmapLocation; $subsiteHostmap['default'] = isset($subsiteHostmap['default']) ? $subsiteHostmap['default'] : ''; - + // Look for the host, and find the cache dir $host = str_replace('www.', '', $_SERVER['HTTP_HOST']); $cacheDir = (isset($subsiteHostmap[$host]) ? $subsiteHostmap[$host] : $subsiteHostmap['default']) . '/'; @@ -31,16 +32,24 @@ if ($cacheOn && empty($_COOKIE['bypassStaticCache'])) { } // Look for the file in the cachedir - $file = preg_replace('/[^a-zA-Z0-9]/si', '_', trim($_SERVER['REQUEST_URI'], '/')); + $file = trim($_SERVER['REQUEST_URI'], '/'); $file = $file ? $file : 'index'; - + + // Route to the 'correct' index file (if applicable) + if ($file == 'index' && file_exists($homepageMapLocation)) { + include_once $homepageMapLocation; + $file = isset($homepageMap[$_SERVER['HTTP_HOST']]) ? $homepageMap[$_SERVER['HTTP_HOST']] : $file; + } + + $file = preg_replace('/[^a-zA-Z0-9]/si', '_', $file); + if (file_exists('../cache/'.$cacheDir.$file.'.html')) { header('X-cache: hit at '.@date('r')); echo file_get_contents('../cache/'.$cacheDir.$file.'.html'); } elseif (file_exists('../cache/'.$cacheDir.$file.'.php')) { header('X-cache: hit at '.@date('r')); include_once '../cache/'.$cacheDir.$file.'.php'; - if ($cacheDebug) echo "

File was cached

"; + if ($cacheDebug) echo "

File was cached

"; } else { header('X-cache: miss at '.@date('r') . ' on ' . $cacheDir . $file); // No cache hit... fallback!!!