MINOR merged from branches/2.3 (googlesitemaps modularization)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@66517 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-11-24 22:04:32 +00:00
parent 75a237cd01
commit aa7b5f4ee3
2 changed files with 136 additions and 80 deletions

View File

@ -78,8 +78,6 @@ class SiteTree extends DataObject {
"HasBrokenLink" => "Boolean", "HasBrokenLink" => "Boolean",
"Status" => "Varchar", "Status" => "Varchar",
"ReportClass" => "Varchar", "ReportClass" => "Varchar",
"Priority" => "Float",
"CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", "CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')",
"CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')", "CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')",
@ -87,10 +85,10 @@ class SiteTree extends DataObject {
"ToDo" => "Text", "ToDo" => "Text",
); );
static $indexes = array( static $indexes = array(
"SearchFields" => "fulltext (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords)", "SearchFields" => "fulltext (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords)",
"TitleSearchFields" => "fulltext (Title)", "TitleSearchFields" => "fulltext (Title)",
"URLSegment" => true, "URLSegment" => true,
); );
static $has_many = array( static $has_many = array(
@ -555,7 +553,7 @@ class SiteTree extends DataObject {
* - canEdit() is not granted * - canEdit() is not granted
* - There are no classes defined in {@link $allowed_children} * - There are no classes defined in {@link $allowed_children}
* *
* @uses DataObjectDecorator->canAddChildren() * @uses SiteTreeDecorator->canAddChildren()
* @uses canEdit() * @uses canEdit()
* @uses $allowed_children * @uses $allowed_children
* *
@ -644,6 +642,7 @@ class SiteTree extends DataObject {
* - any descendant page returns FALSE for canDelete() * - any descendant page returns FALSE for canDelete()
* *
* @uses canDelete() * @uses canDelete()
* @uses DataObjectDecorator->canDelete()
* @uses canEdit() * @uses canEdit()
* *
* @param Member $member * @param Member $member
@ -686,6 +685,7 @@ class SiteTree extends DataObject {
* Use {@link canAddChildren()} to control behaviour of creating children under this page. * Use {@link canAddChildren()} to control behaviour of creating children under this page.
* *
* @uses $can_create * @uses $can_create
* @uses DataObjectDecorator->canCreate()
* *
* @param Member $member * @param Member $member
* @return boolean True if the current user can create pages on this class. * @return boolean True if the current user can create pages on this class.
@ -721,6 +721,7 @@ class SiteTree extends DataObject {
* *
* @uses canView() * @uses canView()
* @uses EditorGroups() * @uses EditorGroups()
* @uses DataObjectDecorator->canEdit()
* *
* @param Member $member * @param Member $member
* @return boolean True if the current user can edit this page. * @return boolean True if the current user can edit this page.
@ -768,7 +769,7 @@ class SiteTree extends DataObject {
* - canPublish() on any decorator returns FALSE * - canPublish() on any decorator returns FALSE
* - canEdit() returns FALSE * - canEdit() returns FALSE
* *
* @uses DataObjectDecorator->canPublish() * @uses SiteTreeDecorator->canPublish()
* *
* @param Member $member * @param Member $member
* @return boolean True if the current user can publish this page. * @return boolean True if the current user can publish this page.
@ -988,12 +989,6 @@ class SiteTree extends DataObject {
} }
} }
// If priority is empty or invalid, set it to the default value
if(!is_numeric($this->Priority) ||
(($this->Priority < 0) || ($this->Priority > 1)))
$this->Priority = self::$defaults['Priority'];
parent::onBeforeWrite(); parent::onBeforeWrite();
} }
@ -1045,22 +1040,6 @@ class SiteTree extends DataObject {
} }
} }
/**
* The default value of the priority field depends on the depth of the page in
* the site tree, so it must be calculated dynamically.
*/
function getPriority() {
if(!$this->getField('Priority')) {
$parentStack = $this->parentStack();
$numParents = is_array($parentStack) ? count($parentStack) - 1: 0;
return max(0.1, 1.0 - ($numParents / 10));
} else if($this->getField('Priority') == -1) {
return 0;
} else {
return $this->getField('Priority');
}
}
/** /**
* Returns a FieldSet with which to create the CMS editing form. * Returns a FieldSet with which to create the CMS editing form.
* *
@ -1137,22 +1116,6 @@ class SiteTree extends DataObject {
if(isset($statusMessage)) { if(isset($statusMessage)) {
$message .= "NOTE: " . implode("<br />", $statusMessage); $message .= "NOTE: " . implode("<br />", $statusMessage);
} }
$pagePriorities = array(
'' => _t('SiteTree.PRIORITYAUTOSET','Auto-set based on page depth'),
'-1' => _t('SiteTree.PRIORITYNOTINDEXED', "Not indexed"), // We set this to -ve one because a blank value implies auto-generation of Priority
'1.0' => '1 - ' . _t('SiteTree.PRIORITYMOSTIMPORTANT', "Most important"),
'0.9' => '2',
'0.8' => '3',
'0.7' => '4',
'0.6' => '5',
'0.5' => '6',
'0.4' => '7',
'0.3' => '8',
'0.2' => '9',
'0.1' => '10 - ' . _t('SiteTree.PRIORITYLEASTIMPORTANT', "Least important")
);
// Lay out the fields // Lay out the fields
$fields = new FieldSet( $fields = new FieldSet(
@ -1190,27 +1153,7 @@ class SiteTree extends DataObject {
new TextField("MetaTitle", $this->fieldLabel('MetaTitle')), new TextField("MetaTitle", $this->fieldLabel('MetaTitle')),
new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription')), new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription')),
new TextareaField("MetaKeywords", $this->fieldLabel('MetaKeywords')), new TextareaField("MetaKeywords", $this->fieldLabel('MetaKeywords')),
new ToggleCompositeField( new TextareaField("ExtraMeta",$this->fieldLabel('ExtraMeta'))
'AdvancedOptions',
_t('SiteTree.METAADVANCEDHEADER', "Advanced Options..."),
array(
new TextareaField("ExtraMeta",$this->fieldLabel('ExtraMeta')),
new LiteralField(
"",
"<p>" .
sprintf(
_t(
'SiteTree.METANOTEPRIORITY',
"Manually specify a Google Sitemaps priority for this page (%s)"
),
'<a href="https://www.google.com/webmasters/tools/docs/en/protocol.html#prioritydef">?</a>'
) .
"</p>"
),
new DropdownField("Priority", $this->fieldLabel('Priority'), $pagePriorities)
),
true
)
) )
), ),
$tabBehaviour = new Tab('Behaviour', $tabBehaviour = new Tab('Behaviour',
@ -1301,7 +1244,6 @@ class SiteTree extends DataObject {
$labels['MetaDescription'] = _t('SiteTree.METADESC', "Description"); $labels['MetaDescription'] = _t('SiteTree.METADESC', "Description");
$labels['MetaKeywords'] = _t('SiteTree.METAKEYWORDS', "Keywords"); $labels['MetaKeywords'] = _t('SiteTree.METAKEYWORDS', "Keywords");
$labels['ExtraMeta'] = _t('SiteTree.METAEXTRA', "Custom Meta Tags"); $labels['ExtraMeta'] = _t('SiteTree.METAEXTRA', "Custom Meta Tags");
$labels['Priority'] = _t('SiteTree.METAPAGEPRIO', "Page Priority");
$labels['ClassName'] = _t('SiteTree.PAGETYPE', "Page type", PR_MEDIUM, 'Classname of a page object'); $labels['ClassName'] = _t('SiteTree.PAGETYPE', "Page type", PR_MEDIUM, 'Classname of a page object');
$labels['ShowInMenus'] =_t('SiteTree.SHOWINMENUS', "Show in menus?"); $labels['ShowInMenus'] =_t('SiteTree.SHOWINMENUS', "Show in menus?");
$labels['ShowInSearch'] = _t('SiteTree.SHOWINSEARCH', "Show in search?"); $labels['ShowInSearch'] = _t('SiteTree.SHOWINSEARCH', "Show in search?");
@ -1360,7 +1302,10 @@ class SiteTree extends DataObject {
} }
/** /**
* Publish this page * Publish this page.
*
* @uses SiteTreeDecorator->onBeforePublish()
* @uses SiteTreeDecorator->onAfterPublish()
*/ */
function doPublish() { function doPublish() {
$original = Versioned::get_one_by_stage("SiteTree", "Live", "\"SiteTree\".\"ID\" = $this->ID"); $original = Versioned::get_one_by_stage("SiteTree", "Live", "\"SiteTree\".\"ID\" = $this->ID");
@ -1373,8 +1318,6 @@ class SiteTree extends DataObject {
//$this->PublishedByID = Member::currentUser()->ID; //$this->PublishedByID = Member::currentUser()->ID;
$this->write(); $this->write();
$this->publish("Stage", "Live"); $this->publish("Stage", "Live");
GoogleSitemap::ping();
if(DB::getConn() instanceof MySQLDatabase) { if(DB::getConn() instanceof MySQLDatabase) {
// Special syntax for MySQL (grr!) // Special syntax for MySQL (grr!)
@ -1399,8 +1342,13 @@ class SiteTree extends DataObject {
/** /**
* Unpublish this page - remove it from the live site * Unpublish this page - remove it from the live site
*
* @uses SiteTreeDecorator->onBeforeUnpublish()
* @uses SiteTreeDecorator->onAfterUnpublish()
*/ */
function doUnpublish() { function doUnpublish() {
$this->extend('onBeforeUnpublish');
// Call delete on a cloned object so that this one doesn't lose its ID // Call delete on a cloned object so that this one doesn't lose its ID
$this->flushCache(); $this->flushCache();
$clone = DataObject::get_by_id("SiteTree", $this->ID); $clone = DataObject::get_by_id("SiteTree", $this->ID);
@ -1408,8 +1356,8 @@ class SiteTree extends DataObject {
$this->Status = "Unpublished"; $this->Status = "Unpublished";
$this->write(); $this->write();
GoogleSitemap::ping(); $this->extend('onAfterUnpublish');
} }
/** /**

View File

@ -1,9 +1,21 @@
<?php <?php
/** /**
* Initial implementation of Sitemap support. * Initial implementation of Sitemap support.
* GoogleSitemap should handle requests to 'sitemap.xml' * GoogleSitemap should handle requests to 'sitemap.xml'
* the other two classes are used to render the sitemap * the other two classes are used to render the sitemap.
*
* You can notify ("ping") Google about a changed sitemap
* automatically whenever a new page is published or unpublished.
* By default, Google is not notified, and will pick up your new
* sitemap whenever the GoogleBot visits your website.
*
* Enabling notification of Google after every publish (in your _config.php):
* <example
* GoogleSitemap::enable_google_notificaton();
* </example>
*
* @see http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=34609
*
* @package sapphire * @package sapphire
* @subpackage misc * @subpackage misc
*/ */
@ -22,7 +34,7 @@ class GoogleSitemap extends Controller {
/** /**
* @var boolean * @var boolean
*/ */
protected static $pings = true; protected static $google_notification_enabled = false;
public function Items() { public function Items() {
$this->Pages = Versioned::get_by_stage('SiteTree', 'Live'); $this->Pages = Versioned::get_by_stage('SiteTree', 'Live');
@ -70,13 +82,21 @@ class GoogleSitemap extends Controller {
} }
/** /**
* Notifies Google about changes to your sitemap.
* Triggered automatically on every publish/unpublish of a page.
* This behaviour is disabled by default, enable with:
* GoogleSitemap::enable_google_notificaton();
*
* If the site is in "dev-mode", no ping will be sent regardless wether
* the Google notification is enabled.
*
* @return string Response text * @return string Response text
*/ */
static function ping() { static function ping() {
if(!self::$enabled) return false; if(!self::$enabled) return false;
//Don't ping if the site has disabled it, or if the site is in dev mode //Don't ping if the site has disabled it, or if the site is in dev mode
if(!GoogleSitemap::$pings || Director::isDev()) if(!GoogleSitemap::$google_notification_enabled || Director::isDev())
return; return;
$location = urlencode(Director::absoluteBaseURL() . '/sitemap.xml'); $location = urlencode(Director::absoluteBaseURL() . '/sitemap.xml');
@ -87,15 +107,18 @@ class GoogleSitemap extends Controller {
return $response; return $response;
} }
/**
* Enable pings to google.com whenever sitemap changes.
*/
public static function enable_google_notification() { public static function enable_google_notification() {
self::$pings = true; self::$google_notification_enabled = true;
} }
/** /**
* Disables pings to google when the sitemap changes. * Disables pings to google when the sitemap changes.
*/ */
public static function disable_google_notification() { public static function disable_google_notification() {
self::$pings = false; self::$google_notification_enabled = false;
} }
function index($url) { function index($url) {
@ -120,4 +143,89 @@ class GoogleSitemap extends Controller {
self::$enabled = false; self::$enabled = false;
} }
} }
?>
/**
* @package sapphire
* @subpackage misc
*/
class GoogleSitemapDecorator extends SiteTreeDecorator {
function extraStatics() {
return array(
'db' => array(
"Priority" => "Float",
)
);
}
function updateCMSFields(&$fields) {
$pagePriorities = array(
'' => _t('SiteTree.PRIORITYAUTOSET','Auto-set based on page depth'),
'-1' => _t('SiteTree.PRIORITYNOTINDEXED', "Not indexed"), // We set this to -ve one because a blank value implies auto-generation of Priority
'1.0' => '1 - ' . _t('SiteTree.PRIORITYMOSTIMPORTANT', "Most important"),
'0.9' => '2',
'0.8' => '3',
'0.7' => '4',
'0.6' => '5',
'0.5' => '6',
'0.4' => '7',
'0.3' => '8',
'0.2' => '9',
'0.1' => '10 - ' . _t('SiteTree.PRIORITYLEASTIMPORTANT', "Least important")
);
$tabset = $fields->findOrMakeTab('Root.Content');
$tabset->push(
$addTab = new Tab(
'GoogleSitemap',
_t('SiteTree.TABGOOGLESITEMAP', 'Google Sitemap'),
new LiteralField(
"GoogleSitemapIntro",
"<p>" .
sprintf(
_t(
'SiteTree.METANOTEPRIORITY',
"Manually specify a Google Sitemaps priority for this page (%s)"
),
'<a href="https://www.google.com/webmasters/tools/docs/en/protocol.html#prioritydef">?</a>'
) .
"</p>"
),
new DropdownField("Priority", $this->owner->fieldLabel('Priority'), $pagePriorities)
)
);
}
function updateFieldLabels(&$labels) {
parent::updateFieldLabels($labels);
$labels['Priority'] = _t('SiteTree.METAPAGEPRIO', "Page Priority");
}
function onAfterPublish() {
GoogleSiteMap::ping();
}
function onAfterUnpublish() {
GoogleSiteMap::ping();
}
/**
* The default value of the priority field depends on the depth of the page in
* the site tree, so it must be calculated dynamically.
*/
function getPriority() {
if(!$this->owner->getField('Priority')) {
$parentStack = $this->owner->parentStack();
$numParents = is_array($parentStack) ? count($parentStack) - 1: 0;
return max(0.1, 1.0 - ($numParents / 10));
} else if($this->owner->getField('Priority') == -1) {
return 0;
} else {
return $this->owner->getField('Priority');
}
}
}
Object::add_extension('SiteTree', 'GoogleSitemapDecorator');
?>