diff --git a/src/Controller/XmlSitemapController.php b/src/Controller/XmlSitemapController.php index 9bf37482c2b98567fa29c61285d04ab09851051f..2df3e4dc5b8aa011440797aa4a8aeedefc61b9f7 100644 --- a/src/Controller/XmlSitemapController.php +++ b/src/Controller/XmlSitemapController.php @@ -2,13 +2,14 @@ namespace Drupal\xmlsitemap\Controller; +use Drupal\Component\Serialization\Json; use Drupal\Core\Controller\ControllerBase; use Drupal\xmlsitemap\Entity\XmlSitemap; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\State\StateInterface; -use Drupal\Core\Template\TwigEnvironment; +use Symfony\Component\HttpFoundation\Request; /** * Class for Xml Sitemap Controller. @@ -25,24 +26,14 @@ class XmlSitemapController extends ControllerBase { */ protected $state; - /** - * The twig loader object. - * - * @var \Drupal\Core\Template\TwigEnvironment - */ - protected $twig; - /** * Constructs a new XmlSitemapController object. * * @param \Drupal\Core\State\StateInterface $state * The state service. - * @param Drupal\Core\Template\TwigEnvironment $twig - * Twig environment for Drupal. */ - public function __construct(StateInterface $state, TwigEnvironment $twig) { + public function __construct(StateInterface $state) { $this->state = $state; - $this->twig = $twig; } /** @@ -50,43 +41,54 @@ class XmlSitemapController extends ControllerBase { */ public static function create(ContainerInterface $container) { return new static( - $container->get('state'), - $container->get('twig') + $container->get('state') ); } /** * Provides the sitemap in XML format. * + * @param \Symfony\Component\HttpFoundation\Request $request + * The request object. + * * @throws NotFoundHttpException * * @return \Symfony\Component\HttpFoundation\Response * The sitemap in XML format or plain text if xmlsitemap_developer_mode flag * is set. */ - public function renderSitemapXml() { + 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) { - throw new NotFoundHttpException(); + $exception = new NotFoundHttpException(); + $exception->setHeaders($headers); + throw $exception; } - $chunk = xmlsitemap_get_current_chunk($sitemap); + + $chunk = xmlsitemap_get_current_chunk($sitemap, $request); $file = xmlsitemap_sitemap_get_file($sitemap, $chunk); - // Provide debugging information if enabled. + // Provide debugging information via headers. if ($this->state->get('xmlsitemap_developer_mode')) { - $module_path = drupal_get_path('module', 'xmlsitemap'); - $template = $this->twig->loadTemplate($module_path . '/templates/sitemap-developer-mode.html.twig'); - $elements = [ - 'current_context' => print_r(xmlsitemap_get_current_context(), TRUE), - 'sitemap' => print_r($sitemap, TRUE), - 'chunk' => $chunk, - 'cache_file_location' => $file, - 'cache_file_exists' => file_exists($file) ? 'Yes' : 'No', - ]; - return new Response($template->render($elements)); + $headers['X-XmlSitemap'] = Json::encode($sitemap->toArray()); + $headers['X-XmlSitemap-Cache-File'] = $file; + $headers['X-XmlSitemap-Cache-Hit'] = file_exists($file) ? 'HIT' : 'MISS'; } - $response = new Response(); - return xmlsitemap_output_file($response, $file); + + if (!is_file($file) || !is_readable($file)) { + $exception = new NotFoundHttpException(); + $exception->setHeaders($headers); + throw $exception; + } + + return xmlsitemap_output_file(new Response(), $file, $headers); } /** @@ -122,7 +124,7 @@ class XmlSitemapController extends ControllerBase { // Output the XSL content. $response = new Response($xsl_content); $response->headers->set('Content-type', 'application/xml; charset=utf-8'); - $response->headers->set('X-Robots-Tag', 'noindex, follow'); + $response->headers->set('X-Robots-Tag', 'noindex, nofollow'); return $response; } diff --git a/templates/sitemap-developer-mode.html.twig b/templates/sitemap-developer-mode.html.twig deleted file mode 100644 index a263d6e1e3764d28faa7b94e4bf26c4ebea42849..0000000000000000000000000000000000000000 --- a/templates/sitemap-developer-mode.html.twig +++ /dev/null @@ -1,43 +0,0 @@ -{# -/** - * @file - * Sitemap template output when developer_mode flag is set. - * - * Available variables: - * - current_context: The current context. - * - language: The langcode of the context. - * - sitemap: The sitemap object to be displayed. - * - uri: Sitemap uri. - * - id: Entity id. - * - label: Entity label. - * - chunks: Number of chunks of the sitemap. - * - max_filesize: Maximum filesize allowed for sitemap XML file. - * - context: Context of the sitemap. - * - updated: Last time when sitemap was updated. - * - originalId: The original ID of the configuration entity. - * - pluginConfigKey: The name of the property that is used to store plugin - * configuration. - * - status: The enabled/disabled status of the configuration entity. - * - isSyncing: Whether the config is being created, updated or deleted - * through the import process. - * - isUninstalling: Whether the config is being deleted by the uninstall - * process. - * - uuid: Unique identified for the sitemap entity. - * - langcode: The language code of the entity's default language. - * - entityTypeId: The entity type. - * - enforceIsNew: Boolean indicating whether the entity should be forced to - * be new. - * - _serviceIds: Services that are used for this sitemap entity. - * - dependencies: Dependencies for the sitemap. - * - chunk: Current chunk of the sitemap. - * - cache_file_location: Path for the sitemap cache file. - * - cache_file_exists: Boolean indicating if the sitemap cache file exists. - * - * @see Drupal\xmlsitemap\Controller\XmlSitemapController::renderSitemapXml() - */ -#} -Current context: {{ current_context }}
-Sitemap: {{ sitemap }}
-Chunk: {{ chunk }}
-Cache file location: {{ cache_file_location }}
-Cache file exists: {{ cache_file_exists }}
diff --git a/xmlsitemap.module b/xmlsitemap.module index 7b2230e0d813acb34ae04629f934b3c8d5e3f009..d48975439a94420021ec11a85219d5e1d5448df1 100644 --- a/xmlsitemap.module +++ b/xmlsitemap.module @@ -28,6 +28,7 @@ use Drupal\Core\Url; use Drupal\xmlsitemap\Entity\XmlSitemap; use Drupal\xmlsitemap\XmlSitemapInterface; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -2198,13 +2199,19 @@ function xmlsitemap_get_status_options($default = NULL) { * * @param \Drupal\xmlsitemap\XmlSitemapInterface $sitemap * Sitemap entity. + * @param \Symfony\Component\HttpFoundation\Request $request + * The request to use if provided, otherwise \Drupal::request() will be used. * * @return int|string * Returns current chunk of the sitemap. */ -function xmlsitemap_get_current_chunk(XmlSitemapInterface $sitemap) { - // Check if we should be displaing the index. - $query = \Drupal::request()->query; +function xmlsitemap_get_current_chunk(XmlSitemapInterface $sitemap, Request $request = NULL) { + if (!isset($request)) { + $request = \Drupal::request(); + } + + // Check if we should display the index. + $query = $request->query; $query_page = $query->get('page'); if (!isset($query_page) || !is_numeric($query_page)) { if ($sitemap->getChunks() > 1) { @@ -2237,8 +2244,11 @@ function xmlsitemap_get_current_chunk(XmlSitemapInterface $sitemap) { */ function xmlsitemap_output_file(Response $response, $file, array $headers = []) { if (!file_exists($file) || !is_readable($file)) { - throw new NotFoundHttpException(); + $exception = new NotFoundHttpException(); + $exception->setHeaders($headers); + throw $exception; } + $mtime = filemtime($file); $last_modified = gmdate(DATE_RFC1123, $mtime); $etag = '"' . md5($last_modified) . '"'; @@ -2248,6 +2258,7 @@ function xmlsitemap_output_file(Response $response, $file, array $headers = []) $if_modified_since = $request->server->has('HTTP_IF_MODIFIED_SINCE') ? stripslashes($request->server->get('HTTP_IF_MODIFIED_SINCE')) : FALSE; $if_none_match = $request->server->has('HTTP_IF_NONE_MATCH') ? stripslashes($request->server->get('HTTP_IF_NONE_MATCH')) : FALSE; if ($if_modified_since && $if_none_match && $if_none_match == $etag && $if_modified_since == $last_modified) { + $response->headers->add($headers); $response->setNotModified(); // All 304 responses must send an etag if the 200 response for the same // object contained an etag.