diff --git a/core/control/Sitemap.php b/core/control/Sitemap.php new file mode 100755 index 000000000..eacdfdd12 --- /dev/null +++ b/core/control/Sitemap.php @@ -0,0 +1,80 @@ +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(); + } +} +?> \ No newline at end of file diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index 6d54aec6e..1c1a2fdf4 100644 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -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("", "
Manually specify a Priority for this page:: (valid values are from 0 to 1)
"), + 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( diff --git a/main.php b/main.php index 283d9efb8..1e2c77f10 100644 --- a/main.php +++ b/main.php @@ -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( diff --git a/templates/Sitemap.ss b/templates/Sitemap.ss new file mode 100755 index 000000000..cb1102f36 --- /dev/null +++ b/templates/Sitemap.ss @@ -0,0 +1,13 @@ + +