API: Remove public access to frequency and priorities.

Cleaned up related functions and general spring clean of the module. Documented example of manually setting routes through an extension class.
This commit is contained in:
Will Rossiter 2012-07-06 16:44:26 +12:00
parent 5bc3e07be6
commit 929e97accf
6 changed files with 176 additions and 142 deletions

View File

@ -2,11 +2,11 @@
## Maintainer Contact ## Maintainer Contact
* Will Rossiter (Nickname: wrossiter, willr) <will@fullscreen.io> * Will Rossiter (Nickname: wrossiter, willr) <will@fullscreen.io>
## Requirements ## Requirements
* SilverStripe 3.0 * SilverStripe 3.0
## Documentation ## 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 Therefore, all Silverstripe websites contain a special controller which can
be visited: http://yoursite.com/sitemap.xml 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 ## Usage Overview

View File

@ -1,6 +1,12 @@
<?php <?php
/** /**
* Initial implementation of Sitemap support. * Sitemaps are a way to tell Google about pages on your site that they might
* not otherwise discover. In its simplest terms, a XML Sitemap—usually called
* Sitemap, with a capital S—is a list of the pages on your website. Creating
* and submitting a Sitemap helps make sure that Google knows about all the
* pages on your site, including URLs that may not be discoverable by Google's
* normal crawling process.
*
* 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.
* *
@ -44,25 +50,12 @@ class GoogleSitemap extends Controller {
protected static $use_show_in_search = true; protected static $use_show_in_search = true;
/** /**
* List of DataObjects to show in sitemap.xml * List of DataObject class names to include. As well as the change
* frequency and priority of each class.
* *
* @var array * @var array
*/ */
public static $google_sitemap_dataobjects = array(); private static $dataobjects = array();
/**
* List of DataObjects change frequency
*
* @var array
*/
public static $google_sitemap_dataobjects_changefreq = array();
/**
* List of DataObjects priority
*
* @var array
*/
public static $google_sitemap_dataobjects_priority = array();
/** /**
* Decorates the given DataObject with {@link GoogleSitemapDecorator} * Decorates the given DataObject with {@link GoogleSitemapDecorator}
@ -82,15 +75,10 @@ class GoogleSitemap extends Controller {
if (!self::is_registered($className)) { if (!self::is_registered($className)) {
Object::add_extension($className, 'GoogleSitemapDecorator'); Object::add_extension($className, 'GoogleSitemapDecorator');
self::$google_sitemap_dataobjects[] = $className; self::$dataobjects[$className] = array(
'frequency' => ($changeFreq) ? $changeFreq : 'monthly',
if (!$changeFreq) { 'priority' => ($priority) ? $priority : '0.6'
self::$google_sitemap_dataobjects_changefreq[] = "monthly"; );
} else {
self::$google_sitemap_dataobjects_changefreq[] = $changeFreq;
}
self::$google_sitemap_dataobjects_priority[] = $priority;
} }
} }
@ -102,31 +90,31 @@ class GoogleSitemap extends Controller {
* @return bool * @return bool
*/ */
public static function is_registered($className) { 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 * Returns a list containing each viewable {@link DataObject} instance of
* site tree * the registered class names.
* *
* @return ArrayList * @return ArrayList
*/ */
protected function addRegisteredDataObjects() { protected function getDataObjects() {
$output = new ArrayList(); $output = new ArrayList();
foreach(self::$google_sitemap_dataobjects as $index => $className) { foreach(self::$dataobjects as $class => $config) {
$dataObjectSet = DataObject::get($className); $instances = new DataList($class);
if($dataObjectSet) { if($instances) {
foreach($dataObjectSet as $dataObject) { foreach($instances as $obj) {
if($dataObject->canView()) { if($obj->canView()) {
$dataObject->ChangeFreq = self::$google_sitemap_dataobjects_changefreq[$index]; $obj->ChangeFreq = $config['frequency'];
if(!isset($dataObject->Priority)) { if(!isset($obj->Priority)) {
$dataObject->Priority = self::$google_sitemap_dataobjects_priority[$index]; $obj->Priority = $config['priority'];
} }
$output->push($dataObject); $output->push($obj);
} }
} }
} }
@ -136,75 +124,76 @@ class GoogleSitemap extends Controller {
} }
/** /**
* Returns all the links to {@link SiteTree} pages and * Returns a list containing each viewable {@link SiteTree} instance. If
* {@link DataObject} urls on the page. * 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() { protected function getPages() {
$filter = ''; if(!class_exists('SiteTree')) return new ArrayList();
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; $filter = (self::$use_show_in_search) ? "\"ShowInSearch\" = 1" : "";
$pages = Versioned::get_by_stage('SiteTree', 'Live', $filter);
if(self::$use_show_in_search) { $output = new ArrayList();
$filter = "{$bt}ShowInSearch{$bt} = 1";
}
$pages = class_exists('SiteTree') ? Versioned::get_by_stage('SiteTree', 'Live', $filter) : false;
$newPages = new ArrayList();
if($pages) { if($pages) {
foreach($pages as $page) { 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); $pageHttp = parse_url($page->AbsoluteLink(), PHP_URL_HOST);
$hostHttp = parse_url('http://' . $_SERVER['HTTP_HOST'], PHP_URL_HOST); $hostHttp = parse_url('http://' . $_SERVER['HTTP_HOST'], PHP_URL_HOST);
if(($pageHttp == $hostHttp) && !($page instanceof ErrorPage)) { 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)) { if($page->canView() && (!isset($page->Priority) || $page->Priority > 0)) {
$page->setChangeFrequency(); $
$newPages->push($page); $output->push($page);
}
} }
} }
} }
} return $output;
$newPages->merge($this->addRegisteredDataObjects());
$this->extend('updateItems', $newPages);
return $newPages;
} }
/** /**
* Notifies Google about changes to your sitemap. * 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.
* *
* Triggered automatically on every publish/unpublish of a page. * @return ArrayList
* This behaviour is disabled by default, enable with: */
public function Items() {
$output = new ArrayList();
$output->merge($this->getPages());
$output->merge($this->getDataObjects());
$this->extend('updateItems', $output);
return $output;
}
/**
* Notifies Google about changes to your sitemap. This behavior is disabled
* by default, enable with:
* *
* <code> * <code>
* GoogleSitemap::enable_google_notificaton(); * GoogleSitemap::enable_google_notificaton();
* </code> * </code>
* *
* 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. * the Google notification is enabled.
* *
* @return string Response text * @return string Response text
*/ */
static function ping() { public 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::$google_notification_enabled || Director::isDev()) if(!GoogleSitemap::$google_notification_enabled || Director::isDev()) {
return; return;
}
$location = urlencode(Controller::join_links( $location = urlencode(Controller::join_links(
Director::absoluteBaseURL(), Director::absoluteBaseURL(),
@ -241,7 +230,7 @@ class GoogleSitemap extends Controller {
/** /**
* Default controller handler for the sitemap.xml file * Default controller handler for the sitemap.xml file
*/ */
function index($url) { public function index($url) {
if(self::$enabled) { if(self::$enabled) {
SSViewer::set_source_file_comments(false); 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 * @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 * @return void
*/ */

View File

@ -15,15 +15,17 @@ class GoogleSitemapDecorator extends DataExtension {
*/ */
class GoogleSitemapSiteTreeDecorator extends DataExtension { class GoogleSitemapSiteTreeDecorator extends DataExtension {
function extraStatics($class = null, $extension = null) { /**
return array( * @var array
'db' => array( */
"Priority" => "Varchar(5)", public static $db = array(
), "Priority" => "Varchar(5)"
); );
}
function updateSettingsFields(&$fields) { /**
* @param FieldList
*/
public function updateSettingsFields(&$fields) {
$prorities = array( $prorities = array(
'' => _t('SiteTree.PRIORITYAUTOSET', 'Auto-set based on page depth'), '' => _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' => _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); parent::updateFieldLabels($labels);
$labels['Priority'] = _t('SiteTree.METAPAGEPRIO', "Page Priority"); $labels['Priority'] = _t('SiteTree.METAPAGEPRIO', "Page Priority");
} }
function onAfterPublish() { /**
* @return void
*/
public function onAfterPublish() {
GoogleSitemap::ping(); GoogleSitemap::ping();
} }
function onAfterUnpublish() { /**
* @return void
*/
public function onAfterUnpublish() {
GoogleSitemap::ping(); GoogleSitemap::ping();
} }
@ -73,7 +81,7 @@ class GoogleSitemapSiteTreeDecorator extends DataExtension {
* *
* @return float * @return float
*/ */
function getPriority() { public function getPriority() {
if(!$this->owner->getField('Priority')) { if(!$this->owner->getField('Priority')) {
$parentStack = $this->owner->parentStack(); $parentStack = $this->owner->parentStack();
$numParents = is_array($parentStack) ? count($parentStack) - 1 : 0; $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. * Returns a pages change frequency calculated by pages age and number of
* Google expects always, hourly, daily, weekly, monthly, yearly or never as values. * 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 * @return void
*/ */
public function setChangeFrequency() { public function getChangeFrequency() {
// 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'); $date = date('Y-m-d H:i:s');
$prop = $this->owner->toMap(); $prop = $this->owner->toMap();
$created = new SS_Datetime(); $created = new SS_Datetime();
$created->value = (isset($prop['Created'])) ? $prop['Created'] : $date; $created->value = (isset($prop['Created'])) ? $prop['Created'] : $date;
$now = new SS_Datetime(); $now = new SS_Datetime();
$now->value = $date; $now->value = $date;
$versions = (isset($prop['Version'])) ? $prop['Version'] : 1;
$versions = (isset($prop['Version'])) ? $prop['Version'] : 1;
$timediff = $now->format('U') - $created->format('U'); $timediff = $now->format('U') - $created->format('U');
// Check how many revisions have been made over the lifetime of the // Check how many revisions have been made over the lifetime of the
@ -116,17 +125,19 @@ class GoogleSitemapSiteTreeDecorator extends DataExtension {
$period = $timediff / ($versions + 1); $period = $timediff / ($versions + 1);
if ($period > 60 * 60 * 24 * 365) { if ($period > 60 * 60 * 24 * 365) {
$this->owner->ChangeFreq = 'yearly'; $freq = 'yearly';
} elseif ($period > 60 * 60 * 24 * 30) { } elseif ($period > 60 * 60 * 24 * 30) {
$this->owner->ChangeFreq = 'monthly'; $freq = 'monthly';
} elseif ($period > 60 * 60 * 24 * 7) { } elseif ($period > 60 * 60 * 24 * 7) {
$this->owner->ChangeFreq = 'weekly'; $freq = 'weekly';
} elseif ($period > 60 * 60 * 24) { } elseif ($period > 60 * 60 * 24) {
$this->owner->ChangeFreq = 'daily'; $freq = 'daily';
} elseif ($period > 60 * 60) { } elseif ($period > 60 * 60) {
$this->owner->ChangeFreq = 'hourly'; $freq = 'hourly';
} else { } else {
$this->owner->ChangeFreq = 'always'; $freq = 'always';
} }
return $freq;
} }
} }

View File

@ -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 your SilverStripe website rank well in search engines, and to encourage the
information on your site to be discovered by Google quickly. information on your site to be discovered by Google quickly.
Therefore, all Silverstripe websites contain a special controller which can Therefore, all Silverstripe websites contain a special controller which can be
be visited: http://yoursite.com/sitemap.xml 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, Whenever you publish a new or republish an existing page, SilverStripe can
SilverStripe automatically informs Google of the change, encouraging a Google automatically inform Google of the change, encouraging a Google to take notice.
to take notice. If you install the SilverStripe Google Analytics module, you If you install the SilverStripe Google Analytics module, you can see if Google
can see if Google has updated your page as a result. has updated your page as a result.
By default, SilverStripe informs Google that the importance of a page depends 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 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, 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). 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 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 google sitemap. manually, including requesting to have the page excluded from the sitemap.
## Setup automatic pinging ## Setup automatic pinging
GoogleSitemap::enable_google_notificaton(); GoogleSitemap::enable_google_notificaton();
### Include Dataobjects in listing ### Including DataObjects
The module provides support for including DataObject subclasses as pages in The module provides support for including DataObject subclasses as pages in the
the SiteTree such as comments, forum posts and other pages which are created SiteTree such as comments, forum posts and other pages which are stored in your
by DataObjects. database as DataObject subclasses.
To include a DataObject in the Sitemap it requires that your subclass defines To include a DataObject instance in the Sitemap it requires that your subclass
two functions. defines two functions:
* AbsoluteLink() function which returns the URL for this DataObject * AbsoluteLink() function which returns the URL for this DataObject
* canView() function which returns a boolean value. * canView() function which returns a boolean value.
The SilverStripe convention is to use a Link function to define the AbsoluteLink. The following is a barebones example of a DataObject called 'MyDataObject'. It
This enables $Link to work for relative links (while in templates) and $AbsoluteLink assumes that you have a controller called 'MyController' which has a show method
to work for RSS Feeds and the Sitemap Links. to show the DataObject by its 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 it's ID.
<?php <?php
@ -67,18 +65,52 @@ DataObject by it's ID.
After those methods have been defined on your DataObject you now need to tell After those methods have been defined on your DataObject you now need to tell
googlesitemaps that it should be listed in the sitemap.xml file. Include the the Google Sitemaps module that it should be listed in the sitemap.xml file. To
following in your _config.php file. do that, include the following in your _config.php file.
GoogleSitemap::register_dataobject('MyDataObject'); GoogleSitemap::register_dataobject('MyDataObject');
If you need to change the frequency of the indexing, you can pass the change If you need to change the frequency of the indexing, you can pass the change
frequency (daily, weekly, monthly) as a second parameter to register(). frequency (daily, weekly, monthly) as a second parameter to register(), So
instead of the previous code you would write:
So instead of the previous code you would write:
GoogleSitemap::register('MyDataObject', 'daily'); GoogleSitemap::register('MyDataObject', 'daily');
See the following blog post for more information: See the following blog post for more information:
http://www.silvercart.org/blog/dataobjects-and-googlesitemaps/ http://www.silvercart.org/blog/dataobjects-and-googlesitemaps/
### Including other routes
If your project has routes that are not stored in the database such as custom
controllers and actions, the module provides an extension hook called
*updateItems* which allows anyone to write extensions to alter the provided
items.
Here's an example of registering the MyController/about URL which is defined as
an action. First we create our new extension and define the links we wish to
add to the $items list.
<?php
class GoogleSitemapExtension extends Extension {
public function updateItems($items) {
$base = Director::absoluteBaseUrl();
$routes = array(
'/MyController/',
'/MyController/about/'
);
foreach($routes as $route) {
$items->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');

View File

@ -3,7 +3,7 @@
<% loop $Items %> <% loop $Items %>
<url> <url>
<loc>$AbsoluteLink</loc> <loc>$AbsoluteLink</loc>
<lastmod>$LastEdited.Format(c)</lastmod> <% if $LastEdited %><lastmod>$LastEdited.Format(c)</lastmod><% end_if %>
<% if $ChangeFreq %><changefreq>$ChangeFreq</changefreq><% end_if %> <% if $ChangeFreq %><changefreq>$ChangeFreq</changefreq><% end_if %>
<% if $Priority %><priority>$Priority</priority><% end_if %> <% if $Priority %><priority>$Priority</priority><% end_if %>
</url> </url>

View File

@ -14,7 +14,7 @@ class GoogleSitemapTest extends FunctionalTest {
'GoogleSitemapTest_UnviewableDataObject' 'GoogleSitemapTest_UnviewableDataObject'
); );
function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
if(class_exists('Page')) { if(class_exists('Page')) {
@ -22,7 +22,7 @@ class GoogleSitemapTest extends FunctionalTest {
} }
} }
function testItems() { public function testItems() {
$sitemap = new GoogleSitemap(); $sitemap = new GoogleSitemap();
// register a DataObject and see if its aded to the sitemap // 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()); $this->assertEquals(3, $sitemap->Items()->Count());
} }
function testItemsWithPages() { public function testItemsWithPages() {
if(!class_exists('Page')) { if(!class_exists('Page')) {
$this->markTestIncomplete('No cms module installed, page related test skipped'); $this->markTestIncomplete('No cms module installed, page related test skipped');
} }
@ -84,7 +84,7 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertEquals(4, $sitemap->Items()->Count()); $this->assertEquals(4, $sitemap->Items()->Count());
} }
function testAccess() { public function testAccess() {
GoogleSitemap::enable(); GoogleSitemap::enable();
$response = $this->get('sitemap.xml'); $response = $this->get('sitemap.xml');
@ -98,7 +98,7 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertEquals(404, $response->getStatusCode(), 'Sitemap returns a 404 when disabled'); $this->assertEquals(404, $response->getStatusCode(), 'Sitemap returns a 404 when disabled');
} }
function testDecoratorAddsFields() { public function testDecoratorAddsFields() {
if(!class_exists("Page")) { if(!class_exists("Page")) {
$this->markTestIncomplete('No cms module installed, page related test skipped'); $this->markTestIncomplete('No cms module installed, page related test skipped');
} }
@ -113,7 +113,7 @@ class GoogleSitemapTest extends FunctionalTest {
$this->assertInstanceOf('LiteralField', $tab->fieldByName('GoogleSitemapIntro')); $this->assertInstanceOf('LiteralField', $tab->fieldByName('GoogleSitemapIntro'));
} }
function testGetPriority() { public function testGetPriority() {
if(!class_exists("Page")) { if(!class_exists("Page")) {
$this->markTestIncomplete('No cms module installed, page related test skipped'); $this->markTestIncomplete('No cms module installed, page related test skipped');
} }