silverstripe-googlesitemaps/code/controllers/GoogleSitemapController.php

89 lines
2.5 KiB
PHP
Raw Normal View History

<?php
/**
* Controller for displaying the sitemap.xml. The module displays an index
* sitemap at the sitemap.xml level, then outputs the individual objects
* at a second level.
*
* <code>
* http://site.com/sitemap.xml/
* http://site.com/sitemap.xml/sitemap/$ClassName-$Page.xml
* </code>
*
* @package googlesitemaps
*/
2015-12-17 19:09:19 +01:00
class GoogleSitemapController extends Controller
{
2015-12-17 19:09:19 +01:00
/**
* @var array
*/
private static $allowed_actions = array(
'index',
'sitemap'
);
2015-12-17 19:09:19 +01:00
/**
* Default controller action for the sitemap.xml file. Renders a index
* file containing a list of links to sub sitemaps containing the data.
*
* @return mixed
*/
public function index($url)
{
if (GoogleSitemap::enabled()) {
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
2015-12-17 19:09:19 +01:00
$sitemaps = GoogleSitemap::inst()->getSitemaps();
$this->extend('updateGoogleSitemaps', $sitemaps);
2013-01-17 22:49:53 +01:00
2015-12-17 19:09:19 +01:00
return array(
'Sitemaps' => $sitemaps
);
} else {
return new SS_HTTPResponse('Page not found', 404);
}
}
2015-12-17 19:09:19 +01:00
/**
* Specific controller action for displaying a particular list of links
* for a class
*
* @return mixed
*/
public function sitemap()
{
$class = $this->unsanitiseClassName($this->request->param('ID'));
2015-12-17 19:09:19 +01:00
$page = $this->request->param('OtherID');
if (GoogleSitemap::enabled() && $class && $page && ($class == 'SiteTree' || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))) {
2015-12-17 19:09:19 +01:00
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
2015-12-17 19:09:19 +01:00
$items = GoogleSitemap::inst()->getItems($class, $page);
$this->extend('updateGoogleSitemapItems', $items, $class, $page);
2013-01-17 22:49:53 +01:00
2015-12-17 19:09:19 +01:00
return array(
'Items' => $items
);
} else {
return new SS_HTTPResponse('Page not found', 404);
}
}
/**
* Unsanitise a namespaced class' name from a URL param
* @return string
*/
protected function unsanitiseClassName($class)
{
return str_replace('-', '\\', $class);
}
}