View source
<?php
namespace Drupal\xmlsitemap\Controller;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\State\StateInterface;
use Drupal\xmlsitemap\Entity\XmlSitemap;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class XmlSitemapController extends ControllerBase {
protected $state;
protected $configFactory;
public function __construct(StateInterface $state, ConfigFactoryInterface $config_factory) {
$this->state = $state;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('state'), $container
->get('config.factory'));
}
public function renderSitemapXml(Request $request) {
$headers = [];
if ($this->state
->get('xmlsitemap_developer_mode')) {
$headers['X-XmlSitemap-Current-Context'] = Json::encode(xmlsitemap_get_current_context());
$headers['X-XmlSitemap'] = 'NOT FOUND';
}
$sitemap = XmlSitemap::loadByContext();
if (!$sitemap) {
$exception = new NotFoundHttpException();
$exception
->setHeaders($headers);
throw $exception;
}
$chunk = xmlsitemap_get_current_chunk($sitemap, $request);
$file = xmlsitemap_sitemap_get_file($sitemap, $chunk);
if ($this->state
->get('xmlsitemap_developer_mode')) {
$headers['X-XmlSitemap'] = Json::encode($sitemap
->toArray());
$headers['X-XmlSitemap-Cache-File'] = $file;
$headers['X-XmlSitemap-Cache-Hit'] = file_exists($file) ? 'HIT' : 'MISS';
}
return $this
->getSitemapResponse($file, $request, $headers);
}
public function getSitemapResponse($file, Request $request, array $headers = []) {
if (!is_file($file) || !is_readable($file)) {
$exception = new NotFoundHttpException();
$exception
->setHeaders($headers);
throw $exception;
}
$headers += [
'Content-Type' => 'text/xml; charset=utf-8',
'X-Robots-Tag' => 'noindex, follow',
];
$lifetime = $this->configFactory
->get('xmlsitemap.settings')
->get('minimum_lifetime');
$response = new BinaryFileResponse($file, 200, $headers);
$response
->setPrivate();
$response->headers
->addCacheControlDirective('must-revalidate');
$last_modified = $response
->getFile()
->getMTime();
$response
->setEtag(md5($last_modified));
$response
->setExpires(new \DateTime('@' . ($last_modified + $lifetime)));
$response
->isNotModified($request);
return $response;
}
public function renderSitemapXsl() {
$module_path = drupal_get_path('module', 'xmlsitemap');
$xsl_content = file_get_contents($module_path . '/xsl/xmlsitemap.xsl');
$replacements = [
'Sitemap file' => $this
->t('Sitemap file'),
'Generated by the <a href="https://www.drupal.org/project/xmlsitemap">Drupal XML sitemap module</a>.' => $this
->t('Generated by the <a href="@link-xmlsitemap">Drupal XML sitemap module</a>.', [
'@link-xmlsitemap' => 'https://www.drupal.org/project/xmlsitemap',
]),
'Number of sitemaps in this index' => $this
->t('Number of sitemaps in this index'),
'Click on the table headers to change sorting.' => $this
->t('Click on the table headers to change sorting.'),
'Sitemap URL' => $this
->t('Sitemap URL'),
'Last modification date' => $this
->t('Last modification date'),
'Number of URLs in this sitemap' => $this
->t('Number of URLs in this sitemap'),
'URL location' => $this
->t('URL location'),
'Change frequency' => $this
->t('Change frequency'),
'Priority' => $this
->t('Priority'),
'[jquery]' => base_path() . 'core/assets/vendor/jquery/jquery.js',
'[jquery-tablesort]' => base_path() . $module_path . '/xsl/jquery.tablesorter.min.js',
'[xsl-js]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.js',
'[xsl-css]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.css',
];
$xsl_content = strtr($xsl_content, $replacements);
return new Response($xsl_content, 200, [
'Content-Type' => 'application/xml; charset=utf-8',
'X-Robots-Tag' => 'noindex, nofollow',
]);
}
}