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
* Will Rossiter (Nickname: wrossiter, willr) <will@fullscreen.io>
* Will Rossiter (Nickname: wrossiter, willr) <will@fullscreen.io>
## 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

View File

@ -1,6 +1,12 @@
<?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'
* 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;
/**
* 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
*/
public static $google_sitemap_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();
private static $dataobjects = array();
/**
* Decorates the given DataObject with {@link GoogleSitemapDecorator}
@ -82,15 +75,10 @@ class GoogleSitemap extends Controller {
if (!self::is_registered($className)) {
Object::add_extension($className, 'GoogleSitemapDecorator');
self::$google_sitemap_dataobjects[] = $className;
if (!$changeFreq) {
self::$google_sitemap_dataobjects_changefreq[] = "monthly";
} else {
self::$google_sitemap_dataobjects_changefreq[] = $changeFreq;
}
self::$google_sitemap_dataobjects_priority[] = $priority;
self::$dataobjects[$className] = array(
'frequency' => ($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,75 +124,76 @@ 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());
$this->extend('updateItems', $newPages);
return $newPages;
return $output;
}
/**
* 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.
* This behaviour is disabled by default, enable with:
* @return ArrayList
*/
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>
* GoogleSitemap::enable_google_notificaton();
* </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.
*
* @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(),
@ -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
*/

View File

@ -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;
$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;
}
}

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
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.
<?php
@ -67,18 +65,52 @@ DataObject by it's ID.
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
following in your _config.php file.
the Google Sitemaps module that it should be listed in the sitemap.xml file. To
do that, include the following in your _config.php file.
GoogleSitemap::register_dataobject('MyDataObject');
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().
So instead of the previous code you would write:
frequency (daily, weekly, monthly) as a second parameter to register(), So
instead of the previous code you would write:
GoogleSitemap::register('MyDataObject', 'daily');
See the following blog post for more information:
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 %>
<url>
<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 $Priority %><priority>$Priority</priority><% end_if %>
</url>

View File

@ -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');
}