Commit 0e322424 authored by gbyte.co's avatar gbyte.co

Invalidate cache on removal of a published sitemap variant

parent 0c034528
...@@ -10,6 +10,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; ...@@ -10,6 +10,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\simple_sitemap\Simplesitemap; use Drupal\simple_sitemap\Simplesitemap;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch; use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Drupal\simple_sitemap\SimplesitemapManager;
/** /**
* Class SimplesitemapController * Class SimplesitemapController
...@@ -66,6 +67,10 @@ class SimplesitemapController extends ControllerBase { ...@@ -66,6 +67,10 @@ class SimplesitemapController extends ControllerBase {
* Returns an XML response. * Returns an XML response.
*/ */
public function getSitemap(Request $request, $variant = NULL) { public function getSitemap(Request $request, $variant = NULL) {
$variant = NULL !== $variant
? $variant
: $this->generator->getSetting('default_variant', SimplesitemapManager::DEFAULT_SITEMAP_VARIANT);
$output = $this->generator->getSitemap($variant, $request->query->getInt('page')); $output = $this->generator->getSitemap($variant, $request->query->getInt('page'));
if (!$output) { if (!$output) {
$this->cacheKillSwitch->trigger(); $this->cacheKillSwitch->trigger();
......
...@@ -181,6 +181,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -181,6 +181,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
} }
/** /**
* @param string $mode
* @return $this * @return $this
*/ */
public function remove($mode = 'all') { public function remove($mode = 'all') {
...@@ -190,25 +191,57 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -190,25 +191,57 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
} }
public static function removeSitemapVariant($variant = NULL, $mode = 'all') { public static function removeSitemapVariant($variant = NULL, $mode = 'all') {
$query = \Drupal::database()->delete('simple_sitemap'); $connection = \Drupal::database();
$delete_query = $connection->delete('simple_sitemap');
if (NULL !== $variant) {
$query->condition('type', $variant);
}
switch($mode) { switch($mode) {
case 'published': case 'published':
$query->condition('status', 1); $status = 1;
break; break;
case 'unpublished': case 'unpublished':
$query->condition('status', 0); $status = 0;
break; break;
case 'all': case 'all':
$status = NULL;
break; break;
default: default:
//todo: throw error //todo: throw error
} }
$query->execute();
if (NULL !== $status) {
$delete_query->condition('status', $status);
}
if (NULL !== $variant) {
$delete_query->condition('type', $variant);
}
elseif ($status !== 0) {
$variant = $connection->query('SELECT DISTINCT type from {simple_sitemap} WHERE status = :status', [':status' => 1])->fetchCol();
}
$delete_query->execute();
if (NULL !== $variant) {
self::invalidateCache($variant);
}
}
/**
* @param string|array $variants
*/
protected static function invalidateCache($variants) {
$variants = is_array($variants) ? $variants : [$variants];
$tags = array_map(function($variant) {
return 'simple_sitemap:' . $variant;
}, $variants);
Cache::invalidateTags($tags);
} }
/** /**
...@@ -284,16 +317,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -284,16 +317,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
return $this; return $this;
} }
/**
* @return $this
* @todo: Variant cannot be null
*/
public function invalidateCache() {
Cache::invalidateTags(['simple_sitemap:' . $this->sitemapVariant]);
return $this;
}
/** /**
* @param array $settings * @param array $settings
* @return $this * @return $this
......
...@@ -19,6 +19,4 @@ interface SitemapGeneratorInterface { ...@@ -19,6 +19,4 @@ interface SitemapGeneratorInterface {
function publish(); function publish();
function remove(); function remove();
function invalidateCache();
} }
...@@ -201,7 +201,10 @@ class Simplesitemap { ...@@ -201,7 +201,10 @@ class Simplesitemap {
* Returns false if the sitemap is not retrievable from the database. * Returns false if the sitemap is not retrievable from the database.
*/ */
public function getSitemap($variant = NULL, $delta = NULL) { public function getSitemap($variant = NULL, $delta = NULL) {
$variant = NULL !== $variant ? $variant : $this->getSetting('default_variant', SimplesitemapManager::DEFAULT_SITEMAP_VARIANT); $variant = NULL !== $variant
? $variant
: $this->getSetting('default_variant', SimplesitemapManager::DEFAULT_SITEMAP_VARIANT);
$chunk_info = $this->fetchSitemapVariantInfo($variant); $chunk_info = $this->fetchSitemapVariantInfo($variant);
if (empty($delta) || !isset($chunk_info[$delta])) { if (empty($delta) || !isset($chunk_info[$delta])) {
...@@ -283,8 +286,7 @@ class Simplesitemap { ...@@ -283,8 +286,7 @@ class Simplesitemap {
foreach ($remove_variants as $variant_name => $variant_definition) { foreach ($remove_variants as $variant_name => $variant_definition) {
$this->manager->getSitemapGenerator($type_definitions[$variant_definition['type']]['sitemapGenerator']) $this->manager->getSitemapGenerator($type_definitions[$variant_definition['type']]['sitemapGenerator'])
->setSitemapVariant($variant_name) ->setSitemapVariant($variant_name)
->remove() ->remove();
->invalidateCache();
} }
} }
......
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