Permalink
Browse files

BUGFIX fix for #5076 (from r98957)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102818 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
chillu committed Apr 14, 2010
1 parent d5f2194 commit 45c5163c189d377892102e0e75bd8484b7827a59
Showing with 39 additions and 4 deletions.
  1. +26 −0 core/model/SiteTree.php
  2. +13 −4 static-main.php
View
@@ -168,6 +168,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',
@@ -1982,13 +1987,34 @@ function doPublish() {
}
}
+ // 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', "<?php\n\$homepageMap = ".var_export($map, true)."; ?>");
+ } 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
*
View
@@ -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 @@
}
// 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 "<h1>File was cached</h1>";
+ if ($cacheDebug) echo "<h1>File was cached</h1>";
} else {
header('X-cache: miss at '.@date('r') . ' on ' . $cacheDir . $file);
// No cache hit... fallback!!!

0 comments on commit 45c5163

Please sign in to comment.