API CHANGE Removed SiteTree->HomepageForDomain and related functionality ($write_homepage_map and generate_homepage_domain_map()), use new 'homepagefordomain' module instead (fixes #6902)

This commit is contained in:
Ingo Schommer 2012-03-14 22:43:47 +01:00
parent c6b259e856
commit 1827dc6727
5 changed files with 11 additions and 147 deletions

View File

@ -28,17 +28,20 @@ class RootURLController extends Controller {
*/
public static function get_homepage_link() {
if(!self::$cached_homepage_link) {
$host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
$SQL_host = Convert::raw2sql($host);
$candidates = DataObject::get('SiteTree', "\"HomepageForDomain\" LIKE '%$SQL_host%'");
if($candidates) foreach($candidates as $candidate) {
if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
self::$cached_homepage_link = trim($candidate->RelativeLink(true), '/');
// TODO Move to 'homepagefordomain' module
if(class_exists('HomepageForDomainExtension')) {
$host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
$SQL_host = Convert::raw2sql($host);
$candidates = DataObject::get('SiteTree', "\"HomepageForDomain\" LIKE '%$SQL_host%'");
if($candidates) foreach($candidates as $candidate) {
if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
self::$cached_homepage_link = trim($candidate->RelativeLink(true), '/');
}
}
}
if(!self::$cached_homepage_link) {
// TODO Move to 'translatable' module
if (
class_exists('Translatable')
&& Object::has_extension('SiteTree', 'Translatable')

View File

@ -71,7 +71,6 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
"ExtraMeta" => "HTMLText",
"ShowInMenus" => "Boolean",
"ShowInSearch" => "Boolean",
"HomepageForDomain" => "Varchar(100)",
"Sort" => "Int",
"HasBrokenFile" => "Boolean",
"HasBrokenLink" => "Boolean",
@ -154,11 +153,6 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
"Versioned('Stage', 'Live')",
);
/**
* Whether or not to write the homepage map for static publisher
*/
public static $write_homepage_map = true;
static $searchable_fields = array(
'Title',
'Content',
@ -1908,19 +1902,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
$parentIDField = new TreeDropdownField("ParentID", $this->fieldLabel('ParentID'), 'SiteTree', 'ID', 'MenuTitle')
),
new CheckboxField("ShowInMenus", $this->fieldLabel('ShowInMenus')),
new CheckboxField("ShowInSearch", $this->fieldLabel('ShowInSearch')),
new LiteralField(
"HomepageForDomainInfo",
"<p>" .
_t('SiteTree.NOTEUSEASHOMEPAGE',
"Use this page as the 'home page' for the following domains:
(separate multiple domains with commas)") .
"</p>"
),
new TextField(
"HomepageForDomain",
_t('SiteTree.HOMEPAGEFORDOMAIN', "Domain(s)", PR_MEDIUM, 'Listing domains that should be used as homepage')
)
new CheckboxField("ShowInSearch", $this->fieldLabel('ShowInSearch'))
),
$tabAccess = new Tab('Access',
$viewersOptionsField = new OptionsetField(
@ -2019,7 +2001,6 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
$labels['EditorGroups'] = _t('SiteTree.EDITORGROUPS', "Editor Groups");
$labels['URLSegment'] = _t('SiteTree.URLSegment', 'URL Segment', PR_MEDIUM, 'URL for this page');
$labels['Content'] = _t('SiteTree.Content', 'Content', PR_MEDIUM, 'Main HTML Content for a page');
$labels['HomepageForDomain'] = _t('SiteTree.HomepageForDomain', 'Hompage for this domain');
$labels['CanViewType'] = _t('SiteTree.Viewers', 'Viewers Groups');
$labels['CanEditType'] = _t('SiteTree.Editors', 'Editors Groups');
$labels['Comments'] = _t('SiteTree.Comments', 'Comments');
@ -2165,34 +2146,12 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
}
Versioned::set_reading_mode($origMode);
// 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 = SiteTree::generate_homepage_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 extensions
$this->invokeWithExtensions('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 File

@ -360,17 +360,11 @@ $lang['en_US']['SiteTree']['EDIT_ALL_DESCRIPTION'] = 'Edit any page';
$lang['en_US']['SiteTree']['EDIT_ALL_HELP'] = 'Ability to edit any page on the site, regardless of the settings on the Access tab. Requires the "Access to \'Pages\' section" permission';
$lang['en_US']['SiteTree']['Editors'] = 'Editors Groups';
$lang['en_US']['SiteTree']['HASBROKENLINKS'] = 'This page has broken links.';
$lang['en_US']['SiteTree']['HOMEPAGEFORDOMAIN'] = array(
'Domain(s)',
PR_MEDIUM,
'Listing domains that should be used as homepage'
);
$lang['en_US']['SiteTree']['HTMLEDITORTITLE'] = array(
'Content',
PR_MEDIUM,
'HTML editor title'
);
$lang['en_US']['SiteTree']['HomepageForDomain'] = 'Hompage for this domain';
$lang['en_US']['SiteTree']['INHERIT'] = 'Inherit from parent page';
$lang['en_US']['SiteTree']['LINKCHANGENOTE'] = 'Changing this page\'s link will also affect the links of all child pages.';
$lang['en_US']['SiteTree']['MENUTITLE'] = 'Navigation label';

View File

@ -6,71 +6,13 @@
class RootURLControllerTest extends SapphireTest {
static $fixture_file = 'RootURLControllerTest.yml';
function testHomepageForDomain() {
$originalHost = $_SERVER['HTTP_HOST'];
// Tests matching an HTTP_HOST value to URLSegment homepage values
$tests = array(
'page.co.nz' => 'page1',
'www.page.co.nz' => 'page1',
'help.com' => 'page1',
'www.help.com' => 'page1',
'something.com' => 'page1',
'www.something.com' => 'page1',
'other.co.nz' => 'page2',
'www.other.co.nz' => 'page2',
'right' => 'page2',
'www. right' => 'page2',
'only.com' => 'page3',
'www.only.com' => 'page3',
'www.somethingelse.com' => 'home',
'somethingelse.com' => 'home',
// Test some potential false matches to page2 and page3
'alternate.only.com' => 'home',
'www.alternate.only.com' => 'home',
'alternate.something.com' => 'home',
);
foreach($tests as $domain => $urlSegment) {
RootURLController::reset();
$_SERVER['HTTP_HOST'] = $domain;
$this->assertEquals(
$urlSegment,
RootURLController::get_homepage_link(),
"Testing $domain matches $urlSegment"
);
}
$_SERVER['HTTP_HOST'] = $originalHost;
}
public function testGetHomepageLink() {
$default = $this->objFromFixture('Page', 'home');
$nested = $this->objFromFixture('Page', 'nested');
SiteTree::disable_nested_urls();
$this->assertEquals('home', RootURLController::get_homepage_link());
SiteTree::enable_nested_urls();
$this->assertEquals('home', RootURLController::get_homepage_link());
$nested->HomepageForDomain = str_replace('www.', null, $_SERVER['HTTP_HOST']);
$nested->write();
RootURLController::reset();
SiteTree::disable_nested_urls();
$this->assertEquals('nested-home', RootURLController::get_homepage_link());
RootURLController::reset();
SiteTree::enable_nested_urls();
$this->assertEquals('home/nested-home', RootURLController::get_homepage_link());
$nested->HomepageForDomain = null;
$nested->write();
}
}

View File

@ -15,7 +15,6 @@ class FilesystemPublisherTest extends SapphireTest {
parent::setUp();
Object::add_extension("SiteTree", "FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
SiteTree::$write_homepage_map = false;
$this->orig['domain_based_caching'] = FilesystemPublisher::$domain_based_caching;
FilesystemPublisher::$domain_based_caching = false;
@ -25,7 +24,6 @@ class FilesystemPublisherTest extends SapphireTest {
parent::tearDown();
Object::remove_extension("SiteTree", "FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
SiteTree::$write_homepage_map = true;
FilesystemPublisher::$domain_based_caching = $this->orig['domain_based_caching'];
@ -127,38 +125,6 @@ class FilesystemPublisherTest extends SapphireTest {
$this->assertEquals($fsp->class, 'FilesystemPublisher');
}
function testHomepageMapIsWithStaticPublishing() {
$this->logInWithPermission('ADMIN');
$p1 = new Page();
$p1->URLSegment = strtolower(__CLASS__).'-page-1';
$p1->HomepageForDomain = '';
$p1->write();
$p1->doPublish();
$p2 = new Page();
$p2->URLSegment = strtolower(__CLASS__).'-page-2';
$p2->HomepageForDomain = 'domain1';
$p2->write();
$p2->doPublish();
$p3 = new Page();
$p3->URLSegment = strtolower(__CLASS__).'-page-3';
$p3->HomepageForDomain = 'domain2,domain3';
$p3->write();
$p3->doPublish();
$map = SiteTree::generate_homepage_domain_map();
$this->assertEquals(
$map,
array(
'domain1' => strtolower(__CLASS__).'-page-2',
'domain2' => strtolower(__CLASS__).'-page-3',
'domain3' => strtolower(__CLASS__).'-page-3',
),
'Homepage/domain map is correct when static publishing is enabled'
);
}
/*
* These are a few simple tests to check that we will be retrieving the correct theme when we need it
* StaticPublishing needs to be able to retrieve a non-null theme at the time publishPages() is called.