2013-01-15 10:29:59 +01:00
|
|
|
<?php
|
|
|
|
|
2016-11-02 12:39:25 +01:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
|
2013-01-15 10:29:59 +01:00
|
|
|
/**
|
2016-11-02 12:39:25 +01:00
|
|
|
* Controller for displaying the sitemap.xml. The module displays an index
|
2013-01-15 10:29:59 +01:00
|
|
|
* 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
|
|
|
|
{
|
2013-01-15 10:29:59 +01:00
|
|
|
|
2015-12-17 19:09:19 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'index',
|
|
|
|
'sitemap'
|
|
|
|
);
|
2013-01-15 10:29:59 +01:00
|
|
|
|
2015-10-12 22:59:42 +02:00
|
|
|
|
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()) {
|
|
|
|
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
|
|
|
|
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
|
2013-01-15 10:29:59 +01:00
|
|
|
|
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 {
|
2016-11-02 12:39:25 +01:00
|
|
|
return new HTTPResponse('Page not found', 404);
|
2015-12-17 19:09:19 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-15 10:29:59 +01:00
|
|
|
|
2015-12-17 19:09:19 +01:00
|
|
|
/**
|
2016-11-02 12:39:25 +01:00
|
|
|
* Specific controller action for displaying a particular list of links
|
2015-12-17 19:09:19 +01:00
|
|
|
* for a class
|
2016-11-02 12:39:25 +01:00
|
|
|
*
|
2015-12-17 19:09:19 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function sitemap()
|
|
|
|
{
|
2016-04-12 12:55:34 +02:00
|
|
|
$class = $this->unsanitiseClassName($this->request->param('ID'));
|
2015-12-17 19:09:19 +01:00
|
|
|
$page = $this->request->param('OtherID');
|
2013-01-15 10:29:59 +01:00
|
|
|
|
2016-11-02 12:39:25 +01:00
|
|
|
if (GoogleSitemap::enabled() && $class && $page && ($class == SiteTree::class || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))) {
|
2015-12-17 19:09:19 +01:00
|
|
|
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
|
|
|
|
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
|
2013-01-15 10:29:59 +01:00
|
|
|
|
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 {
|
2016-11-02 12:39:25 +01:00
|
|
|
return new HTTPResponse('Page not found', 404);
|
2015-12-17 19:09:19 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-12 12:55:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsanitise a namespaced class' name from a URL param
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function unsanitiseClassName($class)
|
|
|
|
{
|
|
|
|
return str_replace('-', '\\', $class);
|
|
|
|
}
|
2013-05-31 15:33:38 +02:00
|
|
|
}
|