diff --git a/README.md b/README.md index 490ee68..ec13ad4 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ## Maintainer Contact - * Will Rossiter (Nickname: wrossiter, willr) +* Will Rossiter (Nickname: wrossiter, willr) ## Requirements - * SilverStripe 3.0 +* SilverStripe 3.0 ## Documentation @@ -18,7 +18,7 @@ information on your site to be discovered by Google quickly. Therefore, all Silverstripe websites contain a special controller which can be visited: http://yoursite.com/sitemap.xml -See http://en.wikipedia.org/wiki/Sitemaps for info on this format +See http://en.wikipedia.org/wiki/Sitemaps for info on this format. ## Usage Overview diff --git a/code/GoogleSitemap.php b/code/GoogleSitemap.php index 668a97d..3e418bc 100644 --- a/code/GoogleSitemap.php +++ b/code/GoogleSitemap.php @@ -1,6 +1,12 @@ ($changeFreq) ? $changeFreq : 'monthly', + 'priority' => ($priority) ? $priority : '0.6' + ); } } @@ -102,31 +90,31 @@ class GoogleSitemap extends Controller { * @return bool */ public static function is_registered($className) { - return in_array($className, self::$google_sitemap_dataobjects); + return isset(self::$dataobjects[$className]); } /** - * Adds DataObjects to the existing DataObjectSet with pages from the - * site tree + * Returns a list containing each viewable {@link DataObject} instance of + * the registered class names. * * @return ArrayList */ - protected function addRegisteredDataObjects() { + protected function getDataObjects() { $output = new ArrayList(); - foreach(self::$google_sitemap_dataobjects as $index => $className) { - $dataObjectSet = DataObject::get($className); + foreach(self::$dataobjects as $class => $config) { + $instances = new DataList($class); - if($dataObjectSet) { - foreach($dataObjectSet as $dataObject) { - if($dataObject->canView()) { - $dataObject->ChangeFreq = self::$google_sitemap_dataobjects_changefreq[$index]; + if($instances) { + foreach($instances as $obj) { + if($obj->canView()) { + $obj->ChangeFreq = $config['frequency']; - if(!isset($dataObject->Priority)) { - $dataObject->Priority = self::$google_sitemap_dataobjects_priority[$index]; + if(!isset($obj->Priority)) { + $obj->Priority = $config['priority']; } - $output->push($dataObject); + $output->push($obj); } } } @@ -136,76 +124,77 @@ class GoogleSitemap extends Controller { } /** - * Returns all the links to {@link SiteTree} pages and - * {@link DataObject} urls on the page. + * Returns a list containing each viewable {@link SiteTree} instance. If + * you wish to exclude a particular class from the sitemap, simply set + * the priority of the class to -1. * - * @return DataObjectSet + * @return ArrayList */ - public function Items() { - $filter = ''; + protected function getPages() { + if(!class_exists('SiteTree')) return new ArrayList(); - $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; - - if(self::$use_show_in_search) { - $filter = "{$bt}ShowInSearch{$bt} = 1"; - } - - $pages = class_exists('SiteTree') ? Versioned::get_by_stage('SiteTree', 'Live', $filter) : false; - - $newPages = new ArrayList(); + $filter = (self::$use_show_in_search) ? "\"ShowInSearch\" = 1" : ""; + $pages = Versioned::get_by_stage('SiteTree', 'Live', $filter); + $output = new ArrayList(); 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 - // host name component; we could use another protocol (like ftp - // as the prefix and the code would work the same. $pageHttp = parse_url($page->AbsoluteLink(), PHP_URL_HOST); $hostHttp = parse_url('http://' . $_SERVER['HTTP_HOST'], PHP_URL_HOST); if(($pageHttp == $hostHttp) && !($page instanceof ErrorPage)) { - - // If the page has been set to 0 priority, we set a flag so - // it won't be included if($page->canView() && (!isset($page->Priority) || $page->Priority > 0)) { - $page->setChangeFrequency(); - $newPages->push($page); + $ + $output->push($page); } } } - } - $newPages->merge($this->addRegisteredDataObjects()); + return $output; + } - $this->extend('updateItems', $newPages); + /** + * Constructs the list of data to include in the rendered sitemap. Links + * can include pages from the website, dataobjects (such as forum posts) + * as well as custom registered paths. + * + * @return ArrayList + */ + public function Items() { + $output = new ArrayList(); + $output->merge($this->getPages()); + $output->merge($this->getDataObjects()); + + $this->extend('updateItems', $output); - return $newPages; + return $output; } /** - * Notifies Google about changes to your sitemap. - * - * Triggered automatically on every publish/unpublish of a page. - * This behaviour is disabled by default, enable with: + * Notifies Google about changes to your sitemap. This behavior is disabled + * by default, enable with: * * * GoogleSitemap::enable_google_notificaton(); * * - * If the site is in "dev-mode", no ping will be sent regardless wether + * After notifications have been enabled, every publish / unpublish of a page. + * will notify Google of the update. + * + * If the site is in development mode no ping will be sent regardless whether * the Google notification is enabled. * * @return string Response text */ - static function ping() { + public static function ping() { if(!self::$enabled) return false; - //Don't ping if the site has disabled it, or if the site is in dev mode - if(!GoogleSitemap::$google_notification_enabled || Director::isDev()) + // Don't ping if the site has disabled it, or if the site is in dev mode + if(!GoogleSitemap::$google_notification_enabled || Director::isDev()) { return; - + } + $location = urlencode(Controller::join_links( Director::absoluteBaseURL(), 'sitemap.xml' @@ -241,7 +230,7 @@ class GoogleSitemap extends Controller { /** * Default controller handler for the sitemap.xml file */ - function index($url) { + public function index($url) { if(self::$enabled) { SSViewer::set_source_file_comments(false); @@ -255,7 +244,8 @@ class GoogleSitemap extends Controller { } /** - * Enable the sitemap.xml file + * Enable Google Sitemap support. Requests to the sitemap.xml route will + * result in an XML sitemap being provided. * * @return void */ @@ -264,7 +254,8 @@ class GoogleSitemap extends Controller { } /** - * Disable the sitemap.xml file + * Disable Google Sitemap support. Any requests to the sitemap.xml route + * will produce a 404 response. * * @return void */ diff --git a/code/GoogleSitemapDecorator.php b/code/GoogleSitemapDecorator.php index 4211b7c..b42fb58 100644 --- a/code/GoogleSitemapDecorator.php +++ b/code/GoogleSitemapDecorator.php @@ -15,15 +15,17 @@ class GoogleSitemapDecorator extends DataExtension { */ class GoogleSitemapSiteTreeDecorator extends DataExtension { - function extraStatics($class = null, $extension = null) { - return array( - 'db' => array( - "Priority" => "Varchar(5)", - ), - ); - } + /** + * @var array + */ + public static $db = array( + "Priority" => "Varchar(5)" + ); - function updateSettingsFields(&$fields) { + /** + * @param FieldList + */ + public function updateSettingsFields(&$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 @@ -53,17 +55,23 @@ class GoogleSitemapSiteTreeDecorator extends DataExtension { )); } - function updateFieldLabels(&$labels) { + public function updateFieldLabels(&$labels) { parent::updateFieldLabels($labels); $labels['Priority'] = _t('SiteTree.METAPAGEPRIO', "Page Priority"); } - function onAfterPublish() { + /** + * @return void + */ + public function onAfterPublish() { GoogleSitemap::ping(); } - function onAfterUnpublish() { + /** + * @return void + */ + public function onAfterUnpublish() { GoogleSitemap::ping(); } @@ -73,7 +81,7 @@ class GoogleSitemapSiteTreeDecorator extends DataExtension { * * @return float */ - function getPriority() { + public function getPriority() { if(!$this->owner->getField('Priority')) { $parentStack = $this->owner->parentStack(); $numParents = is_array($parentStack) ? count($parentStack) - 1 : 0; @@ -91,24 +99,25 @@ class GoogleSitemapSiteTreeDecorator extends DataExtension { } /** - * 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. + * Returns a pages change frequency calculated by pages age and number of + * versions. Google expects always, hourly, daily, weekly, monthly, yearly + * or never as values. * + * @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=183668&topic=8476&ctx=topic + * * @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. + public function getChangeFrequency() { $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 @@ -116,17 +125,19 @@ class GoogleSitemapSiteTreeDecorator extends DataExtension { $period = $timediff / ($versions + 1); if ($period > 60 * 60 * 24 * 365) { - $this->owner->ChangeFreq = 'yearly'; + $freq = 'yearly'; } elseif ($period > 60 * 60 * 24 * 30) { - $this->owner->ChangeFreq = 'monthly'; + $freq = 'monthly'; } elseif ($period > 60 * 60 * 24 * 7) { - $this->owner->ChangeFreq = 'weekly'; + $freq = 'weekly'; } elseif ($period > 60 * 60 * 24) { - $this->owner->ChangeFreq = 'daily'; + $freq = 'daily'; } elseif ($period > 60 * 60) { - $this->owner->ChangeFreq = 'hourly'; + $freq = 'hourly'; } else { - $this->owner->ChangeFreq = 'always'; + $freq = 'always'; } + + return $freq; } } \ No newline at end of file diff --git a/docs/en/index.md b/docs/en/index.md index 1545069..770f82a 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -5,48 +5,46 @@ Google and other search engines to see all pages on your site. This helps your SilverStripe website rank well in search engines, and to encourage the information on your site to be discovered by Google quickly. -Therefore, all Silverstripe websites contain a special controller which can -be visited: http://yoursite.com/sitemap.xml +Therefore, all Silverstripe websites contain a special controller which can be +visited: http://yoursite.com/sitemap.xml. This is not a file directly, but +rather a custom route which points to the GoogleSitemap controller. -See http://en.wikipedia.org/wiki/Sitemaps for info on this format +See http://en.wikipedia.org/wiki/Sitemaps for info on the Google Sitemap +format. -In addition, whenever you publish a new or republish an existing page, -SilverStripe automatically informs Google of the change, encouraging a Google -to take notice. If you install the SilverStripe Google Analytics module, you -can see if Google has updated your page as a result. +Whenever you publish a new or republish an existing page, SilverStripe can +automatically inform Google of the change, encouraging a Google to take notice. +If you install the SilverStripe Google Analytics module, you can see if Google +has updated your page as a result. By default, SilverStripe informs Google that the importance of a page depends on its position of in the sitemap. "Top level" pages are most important, and the deeper a page is nested, the less important it is. (For each level, Importance drops from 1.0, to 0.9, to 0.8, and so on, until 0.1 is reached). -In the CMS, in the "Content/GoogleSitemap" tab, you can set the page importance -manually, including requesting to have the page excluded from the google sitemap. +In the CMS, in the Settings tab for each page, you can set the importance +manually, including requesting to have the page excluded from the sitemap. ## Setup automatic pinging GoogleSitemap::enable_google_notificaton(); -### Include Dataobjects in listing +### Including DataObjects -The module provides support for including DataObject subclasses as pages in -the SiteTree such as comments, forum posts and other pages which are created -by DataObjects. +The module provides support for including DataObject subclasses as pages in the +SiteTree such as comments, forum posts and other pages which are stored in your +database as DataObject subclasses. -To include a DataObject in the Sitemap it requires that your subclass defines -two functions. +To include a DataObject instance in the Sitemap it requires that your subclass +defines two functions: * AbsoluteLink() function which returns the URL for this DataObject * canView() function which returns a boolean value. -The SilverStripe convention is to use a Link function to define the AbsoluteLink. -This enables $Link to work for relative links (while in templates) and $AbsoluteLink -to work for RSS Feeds and the Sitemap Links. - -The following is a barebones example of a DataObject called 'MyDataObject'. It assumes -that you have a controller called 'MyController' which has a show method to show the -DataObject by it's ID. +The following is a barebones example of a DataObject called 'MyDataObject'. It +assumes that you have a controller called 'MyController' which has a show method +to show the DataObject by its ID. push(new ArrayData(array( + 'AbsoluteLink' => Controller::join_links($base, $route) + ))); + } + } + } + +Before we can see the updates we first must add this extension to our built in +class. Inside your mysite/_config.php file add the following: + + Object::add_extension('GoogleSitemap', 'GoogleSitemapExtension'); \ No newline at end of file diff --git a/templates/GoogleSitemap.ss b/templates/GoogleSitemap.ss index b681336..de17034 100644 --- a/templates/GoogleSitemap.ss +++ b/templates/GoogleSitemap.ss @@ -3,7 +3,7 @@ <% loop $Items %> $AbsoluteLink - $LastEdited.Format(c) + <% if $LastEdited %>$LastEdited.Format(c)<% end_if %> <% if $ChangeFreq %>$ChangeFreq<% end_if %> <% if $Priority %>$Priority<% end_if %> diff --git a/tests/GoogleSitemapTest.php b/tests/GoogleSitemapTest.php index bdd204f..97d2846 100644 --- a/tests/GoogleSitemapTest.php +++ b/tests/GoogleSitemapTest.php @@ -14,7 +14,7 @@ class GoogleSitemapTest extends FunctionalTest { 'GoogleSitemapTest_UnviewableDataObject' ); - function setUp() { + public function setUp() { parent::setUp(); if(class_exists('Page')) { @@ -22,7 +22,7 @@ class GoogleSitemapTest extends FunctionalTest { } } - function testItems() { + public function testItems() { $sitemap = new GoogleSitemap(); // register a DataObject and see if its aded to the sitemap @@ -37,7 +37,7 @@ class GoogleSitemapTest extends FunctionalTest { $this->assertEquals(3, $sitemap->Items()->Count()); } - function testItemsWithPages() { + public function testItemsWithPages() { if(!class_exists('Page')) { $this->markTestIncomplete('No cms module installed, page related test skipped'); } @@ -84,7 +84,7 @@ class GoogleSitemapTest extends FunctionalTest { $this->assertEquals(4, $sitemap->Items()->Count()); } - function testAccess() { + public function testAccess() { GoogleSitemap::enable(); $response = $this->get('sitemap.xml'); @@ -98,7 +98,7 @@ class GoogleSitemapTest extends FunctionalTest { $this->assertEquals(404, $response->getStatusCode(), 'Sitemap returns a 404 when disabled'); } - function testDecoratorAddsFields() { + public function testDecoratorAddsFields() { if(!class_exists("Page")) { $this->markTestIncomplete('No cms module installed, page related test skipped'); } @@ -113,7 +113,7 @@ class GoogleSitemapTest extends FunctionalTest { $this->assertInstanceOf('LiteralField', $tab->fieldByName('GoogleSitemapIntro')); } - function testGetPriority() { + public function testGetPriority() { if(!class_exists("Page")) { $this->markTestIncomplete('No cms module installed, page related test skipped'); }