2007-09-14 02:27:15 +02:00
|
|
|
<?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;
|
2007-10-25 03:10:31 +02:00
|
|
|
|
|
|
|
function __construct() {
|
2007-11-12 03:03:17 +01:00
|
|
|
$this->Pages = Versioned::get_by_stage('SiteTree', 'Live');
|
2007-09-14 02:27:15 +02:00
|
|
|
}
|
|
|
|
|
2007-10-25 03:10:31 +02:00
|
|
|
public function Items() {
|
|
|
|
foreach($this->Pages as $page) {
|
2007-09-14 02:47:00 +02:00
|
|
|
// If the page has been set to 0 priority, we set a flag so it won't be included
|
2007-10-25 03:10:31 +02:00
|
|
|
if(isset($page->Priority) && $page->Priority <= 0) {
|
2007-09-14 02:47:00 +02:00
|
|
|
$page->Include = false;
|
2007-10-25 03:10:31 +02:00
|
|
|
} else {
|
2007-09-14 02:47:00 +02:00
|
|
|
$page->Include = true;
|
2007-10-25 03:10:31 +02:00
|
|
|
}
|
2007-09-14 02:47:00 +02:00
|
|
|
|
2007-09-14 02:27:15 +02:00
|
|
|
// 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);
|
|
|
|
|
2007-10-25 03:10:31 +02:00
|
|
|
if($period > 60*60*24*365) { // > 1 year
|
2007-09-14 02:27:15 +02:00
|
|
|
$page->ChangeFreq='yearly';
|
2007-10-25 03:10:31 +02:00
|
|
|
} else if($period > 60*60*24*30) { // > ~1 month
|
2007-09-14 02:27:15 +02:00
|
|
|
$page->ChangeFreq='monthly';
|
2007-10-25 03:10:31 +02:00
|
|
|
} else if($period > 60*60*24*7) { // > 1 week
|
2007-09-14 02:27:15 +02:00
|
|
|
$page->ChangeFreq='weekly';
|
2007-10-25 03:10:31 +02:00
|
|
|
} else if($period > 60*60*24) { // > 1 day
|
2007-09-14 02:27:15 +02:00
|
|
|
$page->ChangeFreq='daily';
|
2007-10-25 03:10:31 +02:00
|
|
|
} else if($period > 60*60) { // > 1 hour
|
2007-09-14 02:27:15 +02:00
|
|
|
$page->ChangeFreq='hourly';
|
2007-10-25 03:10:31 +02:00
|
|
|
} else { // < 1 hour
|
2007-09-14 02:27:15 +02:00
|
|
|
$page->ChangeFreq='always';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->Pages;
|
|
|
|
}
|
|
|
|
|
2007-10-25 03:10:31 +02:00
|
|
|
static function ping() {
|
2007-09-14 02:47:00 +02:00
|
|
|
//Don't ping if the site has disabled it
|
|
|
|
if(!Sitemap::$pings)
|
|
|
|
return;
|
|
|
|
|
2007-09-14 02:27:15 +02:00
|
|
|
$location = urlencode(Director::absoluteBaseURL() . '/sitemap.xml');
|
|
|
|
|
|
|
|
$response = HTTP::sendRequest("www.google.com", "/webmasters/sitemaps/ping",
|
|
|
|
"sitemap=" . $location);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2007-09-14 02:47:00 +02:00
|
|
|
protected static $pings = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables pings to google when the sitemap changes
|
|
|
|
* To use this, in your _config.php file simply include the line
|
|
|
|
* Sitemap::DisableGoogleNotification();
|
|
|
|
*/
|
2007-10-25 03:10:31 +02:00
|
|
|
static function DisableGoogleNotification() {
|
2007-09-14 02:47:00 +02:00
|
|
|
self::$pings = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-25 03:10:31 +02:00
|
|
|
function index($url) {
|
2007-09-14 02:27:15 +02:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
2007-11-12 03:03:17 +01:00
|
|
|
?>
|