Permalink
Browse files

ENHANCEMENT: Implemented a basic suite of unit tests. MINOR: Fixed in…

…dentation in GoogleSiteMapDecorator. BUGFIX: fixed potential corrupt XML from invalid Priority fields. Fixed #4
  • Loading branch information...
1 parent 65c59fc commit e801e916361b57f26eb07592c99a77c545e10ce6 @rlehmann rlehmann committed with wilr Jun 20, 2011
Showing with 283 additions and 151 deletions.
  1. +11 −12 code/GoogleSitemap.php
  2. +114 −120 code/GoogleSitemapDecorator.php
  3. +142 −14 tests/GoogleSitemapTest.php
  4. +16 −5 tests/GoogleSitemapTest.yml
View
@@ -27,11 +27,6 @@ class GoogleSitemap extends Controller {
protected static $enabled = true;
/**
- * @var DataObjectSet
- */
- protected $Pages;
-
- /**
* @var boolean
*/
protected static $google_notification_enabled = false;
@@ -148,12 +143,12 @@ public function Items() {
$filter = "{$bt}ShowInSearch{$bt} = 1";
}
- $this->Pages = Versioned::get_by_stage('SiteTree', 'Live', $filter);
-
+ $pages = Versioned::get_by_stage('SiteTree', 'Live', $filter);
+
$newPages = new DataObjectSet();
- if($this->Pages) {
- foreach($this->Pages as $page) {
+ if($pages) {
+ foreach($pages as $page) {
// Only include pages from this host and pages which are not an
// instance of ErrorPage. We prefix $_SERVER['HTTP_HOST'] with
// 'http://' so that parse_url to help parse_url identify the
@@ -182,10 +177,14 @@ public function Items() {
/**
* Notifies Google about changes to your sitemap.
+ *
* Triggered automatically on every publish/unpublish of a page.
* This behaviour is disabled by default, enable with:
+ *
+ * <code>
* GoogleSitemap::enable_google_notificaton();
- *
+ * </code>
+ *
* If the site is in "dev-mode", no ping will be sent regardless wether
* the Google notification is enabled.
*
@@ -241,7 +240,7 @@ function index($url) {
// But we want to still render.
return array();
} else {
- return new SS_HTTPResponse('Not allowed', 405);
+ return new SS_HTTPResponse('Page not found', 404);
}
}
@@ -261,5 +260,5 @@ public static function enable() {
*/
public static function disable() {
self::$enabled = false;
- }
+ }
}
@@ -15,124 +15,118 @@ class GoogleSitemapDecorator extends DataObjectDecorator {
*/
class GoogleSitemapSiteTreeDecorator extends SiteTreeDecorator {
- function extraStatics() {
- return array(
- 'db' => array(
- "Priority" => "Varchar(5)",
- )
- );
- }
-
- 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="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=71936#prioritize" target="_blank">?</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));
- } elseif ($this->owner->getField('Priority') == -1) {
- return 0;
- } else {
- return $this->owner->getField('Priority');
- }
- }
-
- /**
- * Set a pages change frequency calculated by pages age and number of versions.
- * Google expects always, hourly, daily, weekly, monthly, yearly or never as values.
- *
- * @return void
- */
- public function setChangeFrequency() {
- // The one field that isn't easy to deal with in the template is
- // Change frequency, so we set that here.
-
- $date = date('Y-m-d H:i:s');
-
- $prop = $this->owner->toMap();
- $created = new SS_Datetime();
- $created->value = (isset($prop['Created'])) ? $prop['Created'] : $date;
-
- $now = new SS_Datetime();
- $now->value = $date;
- $versions = (isset($prop['Version'])) ? $prop['Version'] : 1;
-
- $timediff = $now->format('U') - $created->format('U');
-
- // Check how many revisions have been made over the lifetime of the
- // Page for a rough estimate of it's changing frequency.
- $period = $timediff / ($versions + 1);
-
- if ($period > 60 * 60 * 24 * 365) {
- // > 1 year
- $this->owner->ChangeFreq = 'yearly';
- } elseif ($period > 60 * 60 * 24 * 30) {
- $this->owner->ChangeFreq = 'monthly';
- } elseif ($period > 60 * 60 * 24 * 7) {
- // > 1 week
- $this->owner->ChangeFreq = 'weekly';
- } elseif ($period > 60 * 60 * 24) {
- // > 1 day
- $this->owner->ChangeFreq = 'daily';
- } elseif ($period > 60 * 60) {
- // > 1 hour
- $this->owner->ChangeFreq = 'hourly';
- } else {
- // < 1 hour
- $this->owner->ChangeFreq = 'always';
- }
- }
-
+ function extraStatics() {
+ return array(
+ 'db' => array(
+ "Priority" => "Varchar(5)",
+ ),
+ );
+ }
+
+ function updateCMSFields(&$fields) {
+ $prorities = 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');
+
+ $message = "<p>";
+ $message .= sprintf(_t('SiteTree.METANOTEPRIORITY', "Manually specify a Google Sitemaps priority for this page (%s)"),
+ '<a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=71936#prioritize" target="_blank">?</a>'
+ );
+ $message .= "</p>";
+
+ $tabset->push(new Tab('GoogleSitemap', _t('SiteTree.TABGOOGLESITEMAP', 'Google Sitemap'),
+ new LiteralField("GoogleSitemapIntro", $message),
+ new DropdownField("Priority", $this->owner->fieldLabel('Priority'), $prorities)
+ ));
+ }
+
+ 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.
+ *
+ * @return float
+ */
+ 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));
+ }
+ elseif ($this->owner->getField('Priority') == -1) {
+ return 0;
+ }
+ else {
+ $priority = abs($this->owner->getField('Priority'));
+
+ return (is_float($priority) && $priority <= 1.0) ? $priority : 0.5;
+ }
+ }
+
+ /**
+ * Set a pages change frequency calculated by pages age and number of versions.
+ * Google expects always, hourly, daily, weekly, monthly, yearly or never as values.
+ *
+ * @return void
+ */
+ public function setChangeFrequency() {
+ // The one field that isn't easy to deal with in the template is
+ // Change frequency, so we set that here.
+ $date = date('Y-m-d H:i:s');
+
+ $prop = $this->owner->toMap();
+ $created = new SS_Datetime();
+ $created->value = (isset($prop['Created'])) ? $prop['Created'] : $date;
+
+ $now = new SS_Datetime();
+ $now->value = $date;
+ $versions = (isset($prop['Version'])) ? $prop['Version'] : 1;
+
+ $timediff = $now->format('U') - $created->format('U');
+
+ // Check how many revisions have been made over the lifetime of the
+ // Page for a rough estimate of it's changing frequency.
+ $period = $timediff / ($versions + 1);
+
+ if ($period > 60 * 60 * 24 * 365) {
+ $this->owner->ChangeFreq = 'yearly';
+ } elseif ($period > 60 * 60 * 24 * 30) {
+ $this->owner->ChangeFreq = 'monthly';
+ } elseif ($period > 60 * 60 * 24 * 7) {
+ $this->owner->ChangeFreq = 'weekly';
+ } elseif ($period > 60 * 60 * 24) {
+ $this->owner->ChangeFreq = 'daily';
+ } elseif ($period > 60 * 60) {
+ $this->owner->ChangeFreq = 'hourly';
+ } else {
+ $this->owner->ChangeFreq = 'always';
+ }
+ }
}
Oops, something went wrong.

0 comments on commit e801e91

Please sign in to comment.