Loading src/Controller/SimpleSitemapController.php +8 −1 Changes for src/Controller/SimpleSitemapController.php: 8 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -3,6 +3,8 @@ namespace Drupal\simple_sitemap\Controller; use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableResponse; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Response; Loading Loading @@ -55,15 +57,20 @@ class SimpleSitemapController extends ControllerBase { * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function getSitemap(Request $request, ?string $variant = NULL): Response { $variant = $variant ?? $this->generator->getDefaultVariant(); $output = $this->generator->setVariants($variant)->getContent($request->query->get('page')); if ($output === NULL) { throw new NotFoundHttpException(); } return new Response($output, Response::HTTP_OK, [ $response = new CacheableResponse($output, Response::HTTP_OK, [ 'Content-type' => 'application/xml; charset=utf-8', 'X-Robots-Tag' => 'noindex, follow', ]); $response->getCacheableMetadata() ->addCacheTags(Cache::buildTags('simple_sitemap', (array) $variant)) ->addCacheContexts(['url.query_args']); return $response; } /** Loading src/Entity/SimpleSitemapStorage.php +22 Changes for src/Entity/SimpleSitemapStorage.php: 22 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\simple_sitemap\Entity; use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; use Drupal\Core\Config\Entity\ConfigEntityStorage; use Drupal\Component\Uuid\UuidInterface; Loading Loading @@ -205,6 +206,11 @@ class SimpleSitemapStorage extends ConfigEntityStorage { if (!$entity->isEnabled() && $entity->fromPublishedAndUnpublished()->getChunkCount()) { $this->deleteContent($entity); } // We need the else since we don't want to thrash cache invalidation and // deleting content already invalidates cache. else { $this->invalidateCache([$entity->id()]); } return parent::doSave($id, $entity); } Loading Loading @@ -256,6 +262,7 @@ class SimpleSitemapStorage extends ConfigEntityStorage { ':type' => $entity->id(), ':status' => self::SITEMAP_PUBLISHED, ]); $this->invalidateCache([$entity->id()]); } } Loading Loading @@ -299,6 +306,7 @@ class SimpleSitemapStorage extends ConfigEntityStorage { 'status' => 0, 'link_count' => $link_count, ])->execute(); $this->invalidateCache([$entity->id()]); } /** Loading Loading @@ -330,6 +338,7 @@ class SimpleSitemapStorage extends ConfigEntityStorage { 'sitemap_created' => $this->time->getRequestTime(), ]) ->execute(); $this->invalidateCache([$entity->id()]); } /** Loading Loading @@ -564,6 +573,19 @@ class SimpleSitemapStorage extends ConfigEntityStorage { $query->condition('type', $variants, 'IN'); } $query->execute(); $this->invalidateCache($variants); } /** * Invalidates cache for all or specified sitemaps. * * @param array|null $variants * An array of sitemap IDs, or NULL for all sitemaps. */ public function invalidateCache(?array $variants = NULL): void { $variants = $variants ?? array_keys(SimpleSitemap::loadMultiple()); $tags = Cache::buildTags('simple_sitemap', (array) $variants); Cache::invalidateTags($tags); } } src/Manager/Generator.php +25 −10 Changes for src/Manager/Generator.php: 25 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -102,6 +102,28 @@ class Generator { return $this; } /** * Gets the default variant from the currently set variants. * * @return string|null * The default variant or NULL if there are no variants. */ public function getDefaultVariant(): ?string { if (empty($variants = $this->getVariants())) { return NULL; } if (count($variants) > 1) { $variant = $this->getSetting('default_variant'); if ($variant && in_array($variant, $variants)) { return $variant; } } return reset($variants); } /** * Returns a sitemap variant, its index, or its requested chunk. * Loading @@ -115,17 +137,10 @@ class Generator { * Returns null if the content is not retrievable from the database. */ public function getContent(?int $delta = NULL): ?string { /** @var \Drupal\simple_sitemap\Entity\SimpleSitemapInterface $sitemap */ if (empty($variants = $this->getVariants())) { return NULL; } $variant = $this->getDefaultVariant(); $variant = count($variants) > 1 && !empty($default_variant = $this->getSetting('default_variant', '')) ? $default_variant : reset($variants); if (($sitemap = SimpleSitemap::load($variant)) && $sitemap->isEnabled() /** @var \Drupal\simple_sitemap\Entity\SimpleSitemapInterface $sitemap */ if ($variant && ($sitemap = SimpleSitemap::load($variant)) && $sitemap->isEnabled() && ($sitemap_string = $sitemap->fromPublished()->toString($delta))) { return $sitemap_string; } Loading tests/src/Functional/SimplesitemapTest.php +19 Changes for tests/src/Functional/SimplesitemapTest.php: 19 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -84,6 +84,25 @@ class SimplesitemapTest extends SimplesitemapTestBase { $this->assertSession()->statusCodeEquals(404); } /** * Test cached sitemap. * * @throws \Drupal\Component\Plugin\Exception\PluginException * @throws \Behat\Mink\Exception\ExpectationException */ public function testCachedSitemap() { $this->generator->customLinkManager()->add( '/node/' . $this->node->id(), ['priority' => 0.2, 'changefreq' => 'monthly'] ); $this->generator->generate(QueueWorker::GENERATE_TYPE_BACKEND); $this->drupalGet($this->defaultSitemapUrl); $assert = $this->assertSession(); $assert->statusCodeEquals(200); $assert->responseHeaderContains('X-Drupal-Cache-Tags', 'sitemap'); } /** * Test custom link. * Loading Loading
src/Controller/SimpleSitemapController.php +8 −1 Changes for src/Controller/SimpleSitemapController.php: 8 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -3,6 +3,8 @@ namespace Drupal\simple_sitemap\Controller; use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableResponse; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Response; Loading Loading @@ -55,15 +57,20 @@ class SimpleSitemapController extends ControllerBase { * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function getSitemap(Request $request, ?string $variant = NULL): Response { $variant = $variant ?? $this->generator->getDefaultVariant(); $output = $this->generator->setVariants($variant)->getContent($request->query->get('page')); if ($output === NULL) { throw new NotFoundHttpException(); } return new Response($output, Response::HTTP_OK, [ $response = new CacheableResponse($output, Response::HTTP_OK, [ 'Content-type' => 'application/xml; charset=utf-8', 'X-Robots-Tag' => 'noindex, follow', ]); $response->getCacheableMetadata() ->addCacheTags(Cache::buildTags('simple_sitemap', (array) $variant)) ->addCacheContexts(['url.query_args']); return $response; } /** Loading
src/Entity/SimpleSitemapStorage.php +22 Changes for src/Entity/SimpleSitemapStorage.php: 22 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\simple_sitemap\Entity; use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; use Drupal\Core\Config\Entity\ConfigEntityStorage; use Drupal\Component\Uuid\UuidInterface; Loading Loading @@ -205,6 +206,11 @@ class SimpleSitemapStorage extends ConfigEntityStorage { if (!$entity->isEnabled() && $entity->fromPublishedAndUnpublished()->getChunkCount()) { $this->deleteContent($entity); } // We need the else since we don't want to thrash cache invalidation and // deleting content already invalidates cache. else { $this->invalidateCache([$entity->id()]); } return parent::doSave($id, $entity); } Loading Loading @@ -256,6 +262,7 @@ class SimpleSitemapStorage extends ConfigEntityStorage { ':type' => $entity->id(), ':status' => self::SITEMAP_PUBLISHED, ]); $this->invalidateCache([$entity->id()]); } } Loading Loading @@ -299,6 +306,7 @@ class SimpleSitemapStorage extends ConfigEntityStorage { 'status' => 0, 'link_count' => $link_count, ])->execute(); $this->invalidateCache([$entity->id()]); } /** Loading Loading @@ -330,6 +338,7 @@ class SimpleSitemapStorage extends ConfigEntityStorage { 'sitemap_created' => $this->time->getRequestTime(), ]) ->execute(); $this->invalidateCache([$entity->id()]); } /** Loading Loading @@ -564,6 +573,19 @@ class SimpleSitemapStorage extends ConfigEntityStorage { $query->condition('type', $variants, 'IN'); } $query->execute(); $this->invalidateCache($variants); } /** * Invalidates cache for all or specified sitemaps. * * @param array|null $variants * An array of sitemap IDs, or NULL for all sitemaps. */ public function invalidateCache(?array $variants = NULL): void { $variants = $variants ?? array_keys(SimpleSitemap::loadMultiple()); $tags = Cache::buildTags('simple_sitemap', (array) $variants); Cache::invalidateTags($tags); } }
src/Manager/Generator.php +25 −10 Changes for src/Manager/Generator.php: 25 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -102,6 +102,28 @@ class Generator { return $this; } /** * Gets the default variant from the currently set variants. * * @return string|null * The default variant or NULL if there are no variants. */ public function getDefaultVariant(): ?string { if (empty($variants = $this->getVariants())) { return NULL; } if (count($variants) > 1) { $variant = $this->getSetting('default_variant'); if ($variant && in_array($variant, $variants)) { return $variant; } } return reset($variants); } /** * Returns a sitemap variant, its index, or its requested chunk. * Loading @@ -115,17 +137,10 @@ class Generator { * Returns null if the content is not retrievable from the database. */ public function getContent(?int $delta = NULL): ?string { /** @var \Drupal\simple_sitemap\Entity\SimpleSitemapInterface $sitemap */ if (empty($variants = $this->getVariants())) { return NULL; } $variant = $this->getDefaultVariant(); $variant = count($variants) > 1 && !empty($default_variant = $this->getSetting('default_variant', '')) ? $default_variant : reset($variants); if (($sitemap = SimpleSitemap::load($variant)) && $sitemap->isEnabled() /** @var \Drupal\simple_sitemap\Entity\SimpleSitemapInterface $sitemap */ if ($variant && ($sitemap = SimpleSitemap::load($variant)) && $sitemap->isEnabled() && ($sitemap_string = $sitemap->fromPublished()->toString($delta))) { return $sitemap_string; } Loading
tests/src/Functional/SimplesitemapTest.php +19 Changes for tests/src/Functional/SimplesitemapTest.php: 19 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -84,6 +84,25 @@ class SimplesitemapTest extends SimplesitemapTestBase { $this->assertSession()->statusCodeEquals(404); } /** * Test cached sitemap. * * @throws \Drupal\Component\Plugin\Exception\PluginException * @throws \Behat\Mink\Exception\ExpectationException */ public function testCachedSitemap() { $this->generator->customLinkManager()->add( '/node/' . $this->node->id(), ['priority' => 0.2, 'changefreq' => 'monthly'] ); $this->generator->generate(QueueWorker::GENERATE_TYPE_BACKEND); $this->drupalGet($this->defaultSitemapUrl); $assert = $this->assertSession(); $assert->statusCodeEquals(200); $assert->responseHeaderContains('X-Drupal-Cache-Tags', 'sitemap'); } /** * Test custom link. * Loading