silverstripe-googlesitemaps/code/controllers/GoogleSitemapController.php
UndefinedOffset cc0c20b3a8 Changed is_registered to check both the current case and lower case version of the class
Fixed test failure resulting from GoogleSitemapRoute
2016-06-29 14:49:06 -03:00

89 lines
2.5 KiB
PHP

<?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
*/
class GoogleSitemapController extends Controller
{
/**
* @var array
*/
private static $allowed_actions = array(
'index',
'sitemap'
);
/**
* 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');
$sitemaps = GoogleSitemap::inst()->getSitemaps();
$this->extend('updateGoogleSitemaps', $sitemaps);
return array(
'Sitemaps' => $sitemaps
);
} else {
return new SS_HTTPResponse('Page not found', 404);
}
}
/**
* 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'));
$page = $this->request->param('OtherID');
if (GoogleSitemap::enabled() && $class && $page && ($class == 'SiteTree' || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))) {
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');
$items = GoogleSitemap::inst()->getItems($class, $page);
$this->extend('updateGoogleSitemapItems', $items, $class, $page);
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);
}
}