wscott: Added base code for a working Google Sitemaps support.

Changes in Files: 
* Sitemap.php - controller for the sitemap, new 
* Sitemap.ss - template for the sitemap, new 

* main.php - added director rule 
* SiteTree?.php - added 'priority' database field, cms code to override priority 

TODO: ping google when the sitemap is updated, 

namely when pages are added / removed, or when a priority is overridden 


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41689 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-14 00:27:15 +00:00
parent 285e6aa8f9
commit 5857c4cab6
4 changed files with 101 additions and 3 deletions

80
core/control/Sitemap.php Executable file
View File

@ -0,0 +1,80 @@
<?php
/**
* Initial implementation of Sitemap support.
* SitemapController should handle requests to 'sitemap.xml'
* the other two classes are used to render the sitemap
*/
class Sitemap extends Controller {
protected $Pages;
function __construct()
{
$this->Pages=DataObject::get('SiteTree');
}
public function Items()
{
foreach($this->Pages as $page)
{
// The one field that isn't easy to deal with in the template is
// Change frequency, so we set that here.
$properties = $page->toMap();
$created = new Datetime($properties['Created']);
$now = new Datetime();
$versions = $properties['Version'];
$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
{
$page->ChangeFreq='yearly';
}
else if($period > 60*60*24*30) // > ~1 month
{
$page->ChangeFreq='monthly';
}
else if($period > 60*60*24*7) // > 1 week
{
$page->ChangeFreq='weekly';
}
else if($period > 60*60*24) // > 1 day
{
$page->ChangeFreq='daily';
}
else if($period > 60*60) // > 1 hour
{
$page->ChangeFreq='hourly';
}
else // < 1 hour
{
$page->ChangeFreq='always';
}
}
return $this->Pages;
}
static function Ping()
{
$location = urlencode(Director::absoluteBaseURL() . '/sitemap.xml');
$response = HTTP::sendRequest("www.google.com", "/webmasters/sitemaps/ping",
"sitemap=" . $location);
return $response;
}
function index($url)
{
// We need to override the default content-type
ContentNegotiator::disable();
header('Content-type: application/xml; charset="utf-8"');
// But we want to still render.
return array();
}
}
?>

View File

@ -671,7 +671,9 @@ class SiteTree extends DataObject {
new HeaderField("Search Engine Meta-tags"),
new TextField("MetaTitle", "Title"),
new TextareaField("MetaDescription", "Description"),
new TextareaField("MetaKeywords", "Keywords")
new TextareaField("MetaKeywords", "Keywords"),
new LiteralField("", "<p>Manually specify a Priority for this page:: (valid values are from 0 to 1)</p>"),
new NumericField("Priority","Page Priority")
)
),
new Tab("Behaviour",
@ -990,7 +992,8 @@ class SiteTree extends DataObject {
"Viewers" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')",
"Editors" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')",
"ViewersGroup" => "Int",
"EditorsGroup" => "Int"
"EditorsGroup" => "Int",
"Priority" => "Float"
);
static $indexes = array(
@ -1028,7 +1031,8 @@ class SiteTree extends DataObject {
"Status" => "New page",
"CanCreateChildren" => array(10),
"Viewers" => "Anyone",
"Editors" => "LoggedInUsers"
"Editors" => "LoggedInUsers",
"Priority" => .5
);
static $has_one = array(

View File

@ -63,6 +63,7 @@ Director::addRules(10, array(
'$Controller/$Action/$ID/$OtherID' => '*',
'images/$Action/$Class/$ID/$Field' => 'Image_Uploader',
'' => 'RootURLController',
'sitemap.xml' => 'Sitemap',
));
Director::addRules(1, array(

13
templates/Sitemap.ss Executable file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84">
<% control Items %>
<url>
<loc>$AbsoluteLink</loc>
<lastmod>$LastEdited.Format(c)</lastmod>
<% if ChangeFreq %><changefreq>$ChangeFreq</changefreq><% end_if %>
<% if Priority %><priority>$Priority</priority><% end_if %>
</url>
<% end_control %>
</urlset>