Commit 65a5da69 authored by gbyte.co's avatar gbyte.co

Fix empty variants not being deleted on generate and clean up

parent eee77e49
...@@ -119,7 +119,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -119,7 +119,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
/** /**
* @return bool * @return bool
* @todo: Variant cannot be null
*/ */
protected function isDefaultVariant() { protected function isDefaultVariant() {
return $this->sitemapVariant === $this->settings['default_variant']; return $this->sitemapVariant === $this->settings['default_variant'];
...@@ -128,7 +127,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -128,7 +127,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
/** /**
* @param array $links * @param array $links
* @return string * @return string
* @todo: Variant cannot be null
*/ */
abstract protected function getXml(array $links); abstract protected function getXml(array $links);
...@@ -146,7 +144,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -146,7 +144,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
* Returns the sitemap index for all sitemap chunks of this type. * Returns the sitemap index for all sitemap chunks of this type.
* *
* @return string * @return string
* @todo: Variant cannot be null
*/ */
protected function getIndexXml(array $chunk_info) { protected function getIndexXml(array $chunk_info) {
$this->writer->openMemory(); $this->writer->openMemory();
...@@ -183,12 +180,12 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -183,12 +180,12 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
* @return $this * @return $this
*/ */
public function remove($mode = 'all') { public function remove($mode = 'all') {
self::removeSitemapVariants($this->sitemapVariant, $mode); self::purgeSitemapVariants($this->sitemapVariant, $mode);
return $this; return $this;
} }
public static function removeSitemapVariants($variants = NULL, $mode = 'all') { public static function purgeSitemapVariants($variants = NULL, $mode = 'all') {
if (NULL === $variants || !empty((array) $variants)) { if (NULL === $variants || !empty((array) $variants)) {
$delete_query = \Drupal::database()->delete('simple_sitemap'); $delete_query = \Drupal::database()->delete('simple_sitemap');
...@@ -223,7 +220,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -223,7 +220,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
* @param array $links * @param array $links
* All links with their multilingual versions and settings. * All links with their multilingual versions and settings.
* *
* @todo Variant cannot be null
*/ */
public function generate(array $links) { public function generate(array $links) {
$highest_id = $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField(); $highest_id = $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField();
...@@ -244,7 +240,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -244,7 +240,6 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
/** /**
* @throws \Exception * @throws \Exception
* @todo: Variant cannot be null
*/ */
public function generateIndex() { public function generateIndex() {
if (!empty($chunk_info = $this->getChunkInfo()) && count($chunk_info) > 1) { if (!empty($chunk_info = $this->getChunkInfo()) && count($chunk_info) > 1) {
......
...@@ -115,7 +115,7 @@ class QueueWorker { ...@@ -115,7 +115,7 @@ class QueueWorker {
*/ */
public function deleteQueue() { public function deleteQueue() {
$this->queue->deleteQueue(); $this->queue->deleteQueue();
SitemapGeneratorBase::removeSitemapVariants(NULL, 'unpublished'); //todo should call the remove() method of every plugin instead? SitemapGeneratorBase::purgeSitemapVariants(NULL, 'unpublished');
$this->variantProcessedNow = NULL; $this->variantProcessedNow = NULL;
$this->generatorProcessedNow = NULL; $this->generatorProcessedNow = NULL;
$this->results = []; $this->results = [];
...@@ -136,7 +136,7 @@ class QueueWorker { ...@@ -136,7 +136,7 @@ class QueueWorker {
* @todo Lock functionality * @todo Lock functionality
*/ */
public function rebuildQueue($variants = NULL) { public function rebuildQueue($variants = NULL) {
$data_sets = []; $all_data_sets = [];
$sitemap_variants = $this->manager->getSitemapVariants(); $sitemap_variants = $this->manager->getSitemapVariants();
$this->moduleHandler->alter('simple_sitemap_variants', $sitemap_variants); $this->moduleHandler->alter('simple_sitemap_variants', $sitemap_variants);
...@@ -155,30 +155,38 @@ class QueueWorker { ...@@ -155,30 +155,38 @@ class QueueWorker {
// Adding generate_sitemap operations for all data sets. // Adding generate_sitemap operations for all data sets.
foreach ($type_definitions[$type]['urlGenerators'] as $url_generator_id) { foreach ($type_definitions[$type]['urlGenerators'] as $url_generator_id) {
foreach ($this->manager->getUrlGenerator($url_generator_id) $data_sets = $this->manager->getUrlGenerator($url_generator_id)
->setSitemapVariant($variant_name) ->setSitemapVariant($variant_name)
->getDataSets() as $data_set) { ->getDataSets();
$data_sets[] = [ if (!empty($data_sets)) {
'data' => $data_set, $sitemap_variants[$variant_name]['data'] = TRUE;
'sitemap_variant' => $variant_name, foreach ($data_sets as $data_set) {
'url_generator' => $url_generator_id, $all_data_sets[] = [
'sitemap_generator' => $type_definitions[$type]['sitemapGenerator'], 'data' => $data_set,
]; 'sitemap_variant' => $variant_name,
'url_generator' => $url_generator_id,
if (count($data_sets) === self::REBUILD_QUEUE_CHUNK_ITEM_SIZE) { 'sitemap_generator' => $type_definitions[$type]['sitemapGenerator'],
$this->queueElements($data_sets); ];
$data_sets = [];
if (count($all_data_sets) === self::REBUILD_QUEUE_CHUNK_ITEM_SIZE) {
$this->queueElements($all_data_sets);
$all_data_sets = [];
}
} }
} }
} }
} }
if (!empty($data_sets)) { if (!empty($all_data_sets)) {
$this->queueElements($data_sets); $this->queueElements($all_data_sets);
} }
$this->getQueuedElementCount(TRUE); $this->getQueuedElementCount(TRUE);
// todo: May not be clean to remove sitemap variants data when queuing elements.
// Remove all sitemap variant instances where no results have been queued.
$this->manager->removeSitemap(array_keys(array_filter($sitemap_variants, function($e) { return empty($e['data']); })));
return $this; return $this;
} }
...@@ -193,6 +201,7 @@ class QueueWorker { ...@@ -193,6 +201,7 @@ class QueueWorker {
* @throws \Drupal\Component\Plugin\Exception\PluginException * @throws \Drupal\Component\Plugin\Exception\PluginException
* *
* @todo Lock functionality * @todo Lock functionality
* @todo Does not unpublish sitemaps were variant does not have links anymore.
*/ */
public function generateSitemap($from = 'form') { public function generateSitemap($from = 'form') {
......
...@@ -299,25 +299,9 @@ class Simplesitemap { ...@@ -299,25 +299,9 @@ class Simplesitemap {
/** /**
* @return $this * @return $this
* @throws \Drupal\Component\Plugin\Exception\PluginException * @throws \Drupal\Component\Plugin\Exception\PluginException
*
* @todo document
*/ */
public function removeSitemap() { public function removeSitemap() {
$saved_variants = $this->manager->getSitemapVariants(); $this->manager->removeSitemap($this->getVariants(FALSE));
$this->moduleHandler->alter('simple_sitemap_variants', $saved_variants);
$remove_variants = NULL !== ($variants = $this->getVariants(FALSE))
? array_intersect_key($saved_variants, array_flip((array) $variants))
: $saved_variants;
if (!empty($remove_variants)) {
$type_definitions = $this->manager->getSitemapTypes();
$this->moduleHandler->alter('simple_sitemap_types', $type_definitions);
foreach ($remove_variants as $variant_name => $variant_definition) {
$this->manager->getSitemapGenerator($type_definitions[$variant_definition['type']]['sitemapGenerator'])
->setSitemapVariant($variant_name)
->remove();
}
}
return $this; return $this;
} }
...@@ -326,6 +310,8 @@ class Simplesitemap { ...@@ -326,6 +310,8 @@ class Simplesitemap {
* @param string $from * @param string $from
* @return $this * @return $this
* @throws \Drupal\Component\Plugin\Exception\PluginException * @throws \Drupal\Component\Plugin\Exception\PluginException
*
* @todo variants
*/ */
public function generateSitemap($from = 'form') { public function generateSitemap($from = 'form') {
switch($from) { switch($from) {
......
...@@ -133,7 +133,6 @@ class SimplesitemapManager { ...@@ -133,7 +133,6 @@ class SimplesitemapManager {
* @param null $sitemap_type * @param null $sitemap_type
* @return array * @return array
* *
* @todo document
* @todo translate label * @todo translate label
*/ */
public function getSitemapVariants($sitemap_type = NULL, $attach_type_info = TRUE) { public function getSitemapVariants($sitemap_type = NULL, $attach_type_info = TRUE) {
...@@ -210,9 +209,29 @@ class SimplesitemapManager { ...@@ -210,9 +209,29 @@ class SimplesitemapManager {
return $this; return $this;
} }
public function removeSitemap($variant_names = NULL) {
$saved_variants = $this->getSitemapVariants();
$remove_variants = NULL !== $variant_names && !empty((array) $variant_names)
? array_intersect_key($saved_variants, array_flip((array) $variant_names))
: $saved_variants;
if (!empty($remove_variants)) {
$type_definitions = $this->getSitemapTypes();
foreach ($remove_variants as $variant_name => $variant_definition) {
$this->getSitemapGenerator($type_definitions[$variant_definition['type']]['sitemapGenerator'])
->setSitemapVariant($variant_name)
->remove();
}
}
return $this;
}
public function removeSitemapVariants($variant_names = NULL) { public function removeSitemapVariants($variant_names = NULL) {
if (NULL === $variant_names || !empty((array) $variant_names)) { if (NULL === $variant_names || !empty((array) $variant_names)) {
SitemapGeneratorBase::removeSitemapVariants($variant_names); //todo should call the remove() method of every plugin instead?
// Remove sitemap instances.
$this->removeSitemap($variant_names);
if (NULL === $variant_names) { if (NULL === $variant_names) {
// Remove all variants and their bundle settings. // Remove all variants and their bundle settings.
......
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