Commit 26ef9b04 authored by gbyte.co's avatar gbyte.co

Issue #2930027 by adamzimmermann, gbyte.co, websmithc: Sitemap chunks located...

Issue #2930027 by adamzimmermann, gbyte.co, websmithc: Sitemap chunks located in /sitemaps/ instead of webiste root
parent 9a3f06fc
......@@ -5,33 +5,16 @@ simple_sitemap.sitemap_default:
requirements:
_access: 'TRUE'
# The actual path to a variant is '/{variant}/sitemap.xml'. Because Drupal 8
# cannot handle a parameter as first route argument,
# Drupal\simple_sitemap\PathProcessor\PathProcessorSitemapVariant::processInbound()
# is being used to catch the request and redirect to the following route.
simple_sitemap.sitemap_variant:
path: '/sitemaps/{variant}/sitemap.xml'
defaults:
_controller: '\Drupal\simple_sitemap\Controller\SimplesitemapController::getSitemap'
requirements:
_access: 'TRUE'
simple_sitemap.sitemap_variant_fallback:
path: '/sitemaps/{variant}'
defaults:
_controller: '\Drupal\simple_sitemap\Controller\SimplesitemapController::getSitemap'
requirements:
_access: 'TRUE'
simple_sitemap.sitemap_variant_delta:
path: '/sitemaps/{variant}/{delta}/sitemap.xml'
defaults:
_controller: '\Drupal\simple_sitemap\Controller\SimplesitemapController::getSitemap'
requirements:
_access: 'TRUE'
simple_sitemap.sitemap_variant_delta_fallback:
path: '/sitemaps/{variant}/{delta}'
defaults:
_controller: '\Drupal\simple_sitemap\Controller\SimplesitemapController::getSitemap'
requirements:
_access: 'TRUE'
simple_sitemap.settings:
path: '/admin/config/search/simplesitemap'
......
......@@ -47,6 +47,11 @@ services:
- '@messenger'
- '@current_user'
simple_sitemap.variant:
class: Drupal\simple_sitemap\PathProcessor\PathProcessorSitemapVariant
tags:
- { name: path_processor_inbound, priority: 300 }
logger.channel.simple_sitemap:
parent: logger.channel_base
public: false
......
......@@ -9,6 +9,7 @@ use Drupal\Core\Cache\CacheableResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Symfony\Component\HttpFoundation\Request;
/**
* Class SimplesitemapController
......@@ -56,17 +57,16 @@ class SimplesitemapController extends ControllerBase {
* @see \hook_simple_sitemap_variants_alter()
* @see Simplesitemap::getSitemapVariants()
*
* @param int $delta
* Optional delta of the sitemap chunk. If none provided, the first chunk or
* the sitemap index is fetched.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*
* @throws NotFoundHttpException
*
* @return object
* Returns an XML response.
*/
public function getSitemap($variant = Simplesitemap::DEFAULT_SITEMAP_VARIANT, $delta = NULL) {
$output = $this->generator->getSitemap($variant, $delta);
public function getSitemap(Request $request, $variant = Simplesitemap::DEFAULT_SITEMAP_VARIANT) {
$output = $this->generator->getSitemap($variant, $request->query->getInt('page'));
if (!$output) {
$this->cacheKillSwitch->trigger();
throw new NotFoundHttpException();
......@@ -78,7 +78,9 @@ class SimplesitemapController extends ControllerBase {
]);
// Cache output.
$response->getCacheableMetadata()->addCacheTags(["simple_sitemap:$variant"]);
$response->getCacheableMetadata()
->addCacheTags(["simple_sitemap:$variant"])
->addCacheContexts(['url.query_args']);
return $response;
}
......
<?php
namespace Drupal\simple_sitemap\PathProcessor;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Class PathProcessorSitemapVariant
* @package Drupal\simple_sitemap\PathProcessor
*/
class PathProcessorSitemapVariant implements InboundPathProcessorInterface {
/**
* {@inheritdoc}
*/
public function processInbound($path, Request $request) {
$args = explode('/', $path);
if (count($args) === 3 && $args[0] === '' && !empty($args[1]) && $args[2] === 'sitemap.xml') {
$path = '/sitemaps/' . $args[1] . '/sitemap.xml';
}
return $path;
}
}
......@@ -119,6 +119,13 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
return $this;
}
/**
* @return bool
*/
protected function isDefaultVariant() {
return $this->sitemapVariant === Simplesitemap::DEFAULT_SITEMAP_VARIANT;
}
/**
* @param array $links
* @return string
......@@ -158,7 +165,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
foreach ($chunk_info as $chunk_data) {
$this->writer->startElement('sitemap');
$this->writer->writeElement('loc', $this->getCustomBaseUrl()
. '/sitemaps/' . $chunk_data->type . '/' . $chunk_data->delta . '/sitemap.xml');
. '/' . (!$this->isDefaultVariant() ? ($chunk_data->type . '/') : '') . 'sitemap.xml?page=' . $chunk_data->delta);
$this->writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
$this->writer->endElement();
}
......
......@@ -230,15 +230,15 @@ class SimplesitemapTest extends SimplesitemapTestBase {
->generateSitemap('nobatch');
$this->drupalGet($this->defaultSitemapUrl);
$this->assertSession()->responseContains('sitemaps/default/1/sitemap.xml');
$this->assertSession()->responseContains('sitemaps/default/2/sitemap.xml');
$this->assertSession()->responseContains('sitemap.xml?page=1');
$this->assertSession()->responseContains('sitemap.xml?page=2');
$this->drupalGet('sitemaps/default/1/sitemap.xml');
$this->drupalGet('sitemap.xml?page=1');
$this->assertSession()->responseContains('node/' . $this->node->id());
$this->assertSession()->responseContains('0.5');
$this->assertSession()->responseNotContains('node/' . $this->node2->id());
$this->drupalGet('sitemaps/default/2/sitemap.xml');
$this->drupalGet('sitemap.xml?page=2');
$this->assertSession()->responseContains('node/' . $this->node2->id());
$this->assertSession()->responseContains('0.5');
$this->assertSession()->responseNotContains('node/' . $this->node->id());
......@@ -312,7 +312,7 @@ class SimplesitemapTest extends SimplesitemapTestBase {
->generateSitemap('nobatch');
$this->drupalGet($this->defaultSitemapUrl);
$this->assertSession()->responseContains('http://base_url_test/sitemaps/default/1/sitemap.xml');
$this->assertSession()->responseContains('http://base_url_test/sitemap.xml?page=1');
}
/**
......@@ -468,10 +468,7 @@ class SimplesitemapTest extends SimplesitemapTestBase {
$this->drupalGet($this->defaultSitemapUrl);
$this->assertSession()->responseContains('node/' . $this->node->id());
$this->drupalGet('sitemaps/test');
$this->assertSession()->responseContains('node/' . $this->node->id());
$this->drupalGet('sitemaps/test/sitemap.xml');
$this->drupalGet('test/sitemap.xml');
$this->assertSession()->responseContains('node/' . $this->node->id());
$this->generator->removeSitemapVariants('test');
......@@ -482,7 +479,7 @@ class SimplesitemapTest extends SimplesitemapTestBase {
$this->generator->generateSitemap('nobatch');
// Test if sitemap has been removed along with the variant.
$this->drupalGet('sitemaps/test/sitemap.xml');
$this->drupalGet('test/sitemap.xml');
// $this->assertSession()->responseNotContains('urlset'); //todo
// Test adding a variant of new sitemap type.
......@@ -505,7 +502,7 @@ class SimplesitemapTest extends SimplesitemapTestBase {
// $this->assertSession()->responseNotContains('urlset'); //todo
// Test if custom generator has been successfully included.
$this->drupalGet('sitemaps/test2/sitemap.xml');
$this->drupalGet('test2/sitemap.xml');
$this->assertSession()->responseContains(Url::fromRoute('<front>')->setAbsolute()->toString());
// Test if entity generator has been successfully excluded.
......@@ -540,7 +537,7 @@ class SimplesitemapTest extends SimplesitemapTestBase {
$this->assertEquals('MISS', $this->drupalGetHeader('X-Drupal-Cache'));
$this->assertSession()->responseContains('node/' . $this->node->id());
$this->drupalGet('sitemaps/test/sitemap.xml');
$this->drupalGet('test/sitemap.xml');
$this->assertEquals('MISS', $this->drupalGetHeader('X-Drupal-Cache'));
$this->assertSession()->responseContains('node/' . $this->node->id());
......@@ -548,7 +545,7 @@ class SimplesitemapTest extends SimplesitemapTestBase {
$this->drupalGet('sitemap.xml');
$this->assertEquals('HIT', $this->drupalGetHeader('X-Drupal-Cache'));
$this->drupalGet('sitemaps/test/sitemap.xml');
$this->drupalGet('test/sitemap.xml');
$this->assertEquals('HIT', $this->drupalGetHeader('X-Drupal-Cache'));
$this->generator->generateSitemap('nobatch', 'default');
......@@ -558,13 +555,13 @@ class SimplesitemapTest extends SimplesitemapTestBase {
$this->assertEquals('MISS', $this->drupalGetHeader('X-Drupal-Cache'));
// Verify the cache is still set for test variant.
$this->drupalGet('sitemaps/test/sitemap.xml');
$this->drupalGet('test/sitemap.xml');
// $this->assertEquals('HIT', $this->drupalGetHeader('X-Drupal-Cache')); //todo
$this->generator->removeSitemap('test');
// Verify the cache is unset for test variant after removing it explicitly.
$this->drupalGet('sitemaps/test/sitemap.xml');
$this->drupalGet('test/sitemap.xml');
// $this->assertEquals('MISS', $this->drupalGetHeader('X-Drupal-Cache')); //todo
}
......
......@@ -38,7 +38,7 @@ abstract class SimplesitemapTestBase extends BrowserTestBase {
protected $database;
/**
* An user with all the permissions.
* A user with all the permissions.
*
* @var \Drupal\user\Entity\User
*/
......@@ -73,7 +73,7 @@ abstract class SimplesitemapTestBase extends BrowserTestBase {
$this->node = $this->createNode(['title' => 'Node', 'type' => 'page']);
$this->node2 = $this->createNode(['title' => 'Node2', 'type' => 'page']);
// Create an user with all the permissions.
// Create a user with all the permissions.
$permissions = array_keys($this->container->get('user.permissions')->getPermissions());
$this->privilegedUser = $this->drupalCreateUser($permissions);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment