2007-09-14 02:27:15 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Initial implementation of Sitemap support.
|
2007-11-15 23:40:20 +01:00
|
|
|
* GoogleSitemap should handle requests to 'sitemap.xml'
|
2008-11-24 23:04:32 +01:00
|
|
|
* the other two classes are used to render the sitemap.
|
|
|
|
*
|
|
|
|
* You can notify ("ping") Google about a changed sitemap
|
|
|
|
* automatically whenever a new page is published or unpublished.
|
|
|
|
* By default, Google is not notified, and will pick up your new
|
|
|
|
* sitemap whenever the GoogleBot visits your website.
|
|
|
|
*
|
|
|
|
* Enabling notification of Google after every publish (in your _config.php):
|
|
|
|
* <example
|
|
|
|
* GoogleSitemap::enable_google_notificaton();
|
|
|
|
* </example>
|
|
|
|
*
|
|
|
|
* @see http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=34609
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage misc
|
2007-09-14 02:27:15 +02:00
|
|
|
*/
|
2007-11-15 23:40:20 +01:00
|
|
|
class GoogleSitemap extends Controller {
|
2008-09-16 22:38:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected static $enabled = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DataObjectSet
|
|
|
|
*/
|
2007-09-14 02:27:15 +02:00
|
|
|
protected $Pages;
|
2007-10-25 03:10:31 +02:00
|
|
|
|
2008-09-16 22:38:20 +02:00
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-11-24 23:04:32 +01:00
|
|
|
protected static $google_notification_enabled = false;
|
2008-09-16 22:38:20 +02:00
|
|
|
|
2007-12-13 23:32:53 +01:00
|
|
|
public function Items() {
|
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-11-15 23:32:52 +01:00
|
|
|
$newPages = new DataObjectSet();
|
|
|
|
|
2007-10-25 03:10:31 +02:00
|
|
|
foreach($this->Pages as $page) {
|
2008-06-25 08:08:27 +02:00
|
|
|
// Only include pages from this host and pages which are not an instance of ErrorPage
|
|
|
|
if(parse_url($page->AbsoluteLink(), PHP_URL_HOST) == $_SERVER['HTTP_HOST'] && !($page instanceof ErrorPage)) {
|
2007-11-15 23:32:52 +01:00
|
|
|
|
|
|
|
// If the page has been set to 0 priority, we set a flag so it won't be included
|
2008-09-23 05:16:23 +02:00
|
|
|
if(!isset($page->Priority) || $page->Priority > 0) {
|
|
|
|
// The one field that isn't easy to deal with in the template is
|
|
|
|
// Change frequency, so we set that here.
|
|
|
|
$properties = $page->toMap();
|
2008-10-16 10:59:40 +02:00
|
|
|
$created = new SSDatetime($properties['Created']);
|
|
|
|
$now = new SSDatetime();
|
2008-09-23 05:16:23 +02:00
|
|
|
$versions = $properties['Version'];
|
|
|
|
$timediff = $now->format('U') - $created->format('U');
|
2007-09-14 02:27:15 +02:00
|
|
|
|
2008-09-23 05:16:23 +02:00
|
|
|
// Check how many revisions have been made over the lifetime of the
|
|
|
|
// Page for a rough estimate of it's changing frequency.
|
2007-09-14 02:27:15 +02:00
|
|
|
|
2008-09-23 05:16:23 +02:00
|
|
|
$period = $timediff / ($versions + 1);
|
2007-09-14 02:27:15 +02:00
|
|
|
|
2008-09-23 05:16:23 +02:00
|
|
|
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';
|
|
|
|
}
|
2007-11-15 23:32:52 +01:00
|
|
|
|
2008-09-23 05:16:23 +02:00
|
|
|
$newPages->push($page);
|
|
|
|
}
|
2007-09-14 02:27:15 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-15 23:32:52 +01:00
|
|
|
return $newPages;
|
2007-09-14 02:27:15 +02:00
|
|
|
}
|
|
|
|
|
2008-09-16 22:38:20 +02:00
|
|
|
/**
|
2008-11-24 23:04:32 +01:00
|
|
|
* Notifies Google about changes to your sitemap.
|
|
|
|
* Triggered automatically on every publish/unpublish of a page.
|
|
|
|
* This behaviour is disabled by default, enable with:
|
|
|
|
* GoogleSitemap::enable_google_notificaton();
|
|
|
|
*
|
|
|
|
* If the site is in "dev-mode", no ping will be sent regardless wether
|
|
|
|
* the Google notification is enabled.
|
|
|
|
*
|
2008-09-16 22:38:20 +02:00
|
|
|
* @return string Response text
|
|
|
|
*/
|
2007-10-25 03:10:31 +02:00
|
|
|
static function ping() {
|
2008-09-16 22:38:20 +02:00
|
|
|
if(!self::$enabled) return false;
|
|
|
|
|
2007-11-12 03:09:31 +01:00
|
|
|
//Don't ping if the site has disabled it, or if the site is in dev mode
|
2008-11-24 23:04:32 +01:00
|
|
|
if(!GoogleSitemap::$google_notification_enabled || Director::isDev())
|
2007-09-14 02:47:00 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-24 23:04:32 +01:00
|
|
|
/**
|
|
|
|
* Enable pings to google.com whenever sitemap changes.
|
|
|
|
*/
|
2008-09-16 22:38:20 +02:00
|
|
|
public static function enable_google_notification() {
|
2008-11-24 23:04:32 +01:00
|
|
|
self::$google_notification_enabled = true;
|
2008-09-16 22:38:20 +02:00
|
|
|
}
|
2007-09-14 02:47:00 +02:00
|
|
|
|
|
|
|
/**
|
2008-09-16 22:38:20 +02:00
|
|
|
* Disables pings to google when the sitemap changes.
|
2007-09-14 02:47:00 +02:00
|
|
|
*/
|
2008-09-16 22:38:20 +02:00
|
|
|
public static function disable_google_notification() {
|
2008-11-24 23:04:32 +01:00
|
|
|
self::$google_notification_enabled = false;
|
2007-09-14 02:47:00 +02:00
|
|
|
}
|
|
|
|
|
2007-10-25 03:10:31 +02:00
|
|
|
function index($url) {
|
2008-09-16 22:38:20 +02:00
|
|
|
if(self::$enabled) {
|
|
|
|
// 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();
|
|
|
|
} else {
|
|
|
|
return new HTTPResponse('Not allowed', 405);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function enable() {
|
|
|
|
self::$enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function disable() {
|
|
|
|
self::$enabled = false;
|
2007-09-14 02:27:15 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-24 23:04:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage misc
|
|
|
|
*/
|
|
|
|
class GoogleSitemapDecorator extends SiteTreeDecorator {
|
|
|
|
|
|
|
|
function extraStatics() {
|
|
|
|
return array(
|
|
|
|
'db' => array(
|
|
|
|
"Priority" => "Float",
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateCMSFields(&$fields) {
|
|
|
|
$pagePriorities = 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
|
|
|
|
'1.0' => '1 - ' . _t('SiteTree.PRIORITYMOSTIMPORTANT', "Most important"),
|
|
|
|
'0.9' => '2',
|
|
|
|
'0.8' => '3',
|
|
|
|
'0.7' => '4',
|
|
|
|
'0.6' => '5',
|
|
|
|
'0.5' => '6',
|
|
|
|
'0.4' => '7',
|
|
|
|
'0.3' => '8',
|
|
|
|
'0.2' => '9',
|
|
|
|
'0.1' => '10 - ' . _t('SiteTree.PRIORITYLEASTIMPORTANT', "Least important")
|
|
|
|
);
|
|
|
|
|
|
|
|
$tabset = $fields->findOrMakeTab('Root.Content');
|
|
|
|
$tabset->push(
|
|
|
|
$addTab = new Tab(
|
|
|
|
'GoogleSitemap',
|
|
|
|
_t('SiteTree.TABGOOGLESITEMAP', 'Google Sitemap'),
|
|
|
|
new LiteralField(
|
|
|
|
"GoogleSitemapIntro",
|
|
|
|
"<p>" .
|
|
|
|
sprintf(
|
|
|
|
_t(
|
|
|
|
'SiteTree.METANOTEPRIORITY',
|
|
|
|
"Manually specify a Google Sitemaps priority for this page (%s)"
|
|
|
|
),
|
|
|
|
'<a href="https://www.google.com/webmasters/tools/docs/en/protocol.html#prioritydef">?</a>'
|
|
|
|
) .
|
|
|
|
"</p>"
|
|
|
|
),
|
|
|
|
new DropdownField("Priority", $this->owner->fieldLabel('Priority'), $pagePriorities)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateFieldLabels(&$labels) {
|
|
|
|
parent::updateFieldLabels($labels);
|
|
|
|
|
|
|
|
$labels['Priority'] = _t('SiteTree.METAPAGEPRIO', "Page Priority");
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAfterPublish() {
|
|
|
|
GoogleSiteMap::ping();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAfterUnpublish() {
|
|
|
|
GoogleSiteMap::ping();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default value of the priority field depends on the depth of the page in
|
|
|
|
* the site tree, so it must be calculated dynamically.
|
|
|
|
*/
|
|
|
|
function getPriority() {
|
|
|
|
if(!$this->owner->getField('Priority')) {
|
|
|
|
$parentStack = $this->owner->parentStack();
|
|
|
|
$numParents = is_array($parentStack) ? count($parentStack) - 1: 0;
|
|
|
|
return max(0.1, 1.0 - ($numParents / 10));
|
|
|
|
} else if($this->owner->getField('Priority') == -1) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return $this->owner->getField('Priority');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object::add_extension('SiteTree', 'GoogleSitemapDecorator');
|
|
|
|
?>
|