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