Commit e963e288 authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #2924080 by gbyte.co: Use queue API for cron generation instead of batch API

parent fb255e3b
max_links: 2000 max_links: 2000
cron_generate: true cron_generate: true
cron_generate_interval: 0 cron_generate_interval: 0
generate_duration: 10000
remove_duplicates: true remove_duplicates: true
skip_untranslated: true skip_untranslated: true
batch_process_limit: 1500
base_url: '' base_url: ''
default_variant: 'default' default_variant: 'default'
custom_links_include_images: false custom_links_include_images: false
......
...@@ -5,10 +5,13 @@ simple_sitemap.settings: ...@@ -5,10 +5,13 @@ simple_sitemap.settings:
label: 'Max links' label: 'Max links'
type: integer type: integer
cron_generate: cron_generate:
label: 'Cron generate' label: 'Cron generation'
type: boolean type: boolean
cron_generate_interval: cron_generate_interval:
label: 'Cron generate interval' label: 'Cron generation interval'
type: integer
generate_duration:
label: 'Generation duration'
type: integer type: integer
remove_duplicates: remove_duplicates:
label: 'Remove duplicates' label: 'Remove duplicates'
...@@ -16,9 +19,6 @@ simple_sitemap.settings: ...@@ -16,9 +19,6 @@ simple_sitemap.settings:
skip_untranslated: skip_untranslated:
label: 'Skip untranslated' label: 'Skip untranslated'
type: boolean type: boolean
batch_process_limit:
label: 'Batch process limit'
type: integer
base_url: base_url:
label: 'Base URL' label: 'Base URL'
type: string type: string
...@@ -89,3 +89,6 @@ simple_sitemap.variants.*: ...@@ -89,3 +89,6 @@ simple_sitemap.variants.*:
label: label:
label: 'Variant label' label: 'Variant label'
type: string type: string
weight:
label: 'Weight'
type: integer
...@@ -14,6 +14,13 @@ function simple_sitemap_drush_command() { ...@@ -14,6 +14,13 @@ function simple_sitemap_drush_command() {
'callback' => 'drush_simple_sitemap_generate', 'callback' => 'drush_simple_sitemap_generate',
'drupal dependencies' => ['simple_sitemap'], 'drupal dependencies' => ['simple_sitemap'],
]; ];
$items['simple_sitemap-rebuild-queue'] = [
'description' => 'Rebuild the sitemap queue for all sitemap variants.',
'callback' => 'drush_simple_sitemap_rebuild_queue',
'drupal dependencies' => ['simple_sitemap'],
];
return $items; return $items;
} }
...@@ -25,3 +32,12 @@ function simple_sitemap_drush_command() { ...@@ -25,3 +32,12 @@ function simple_sitemap_drush_command() {
function drush_simple_sitemap_generate() { function drush_simple_sitemap_generate() {
\Drupal::service('simple_sitemap.generator')->generateSitemap('drush'); \Drupal::service('simple_sitemap.generator')->generateSitemap('drush');
} }
/**
* Callback function for hook_drush_command().
*
* Rebuild the sitemap queue for all sitemap variants.
*/
function drush_simple_sitemap_rebuild_queue() {
\Drupal::service('simple_sitemap.generator')->rebuildQueue();
}
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
* Module install and update procedures. * Module install and update procedures.
*/ */
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
/** /**
* Implements hook_requirements(). * Implements hook_requirements().
* *
...@@ -30,7 +28,7 @@ function simple_sitemap_requirements($phase) { ...@@ -30,7 +28,7 @@ function simple_sitemap_requirements($phase) {
case 'runtime': case 'runtime':
$generator = \Drupal::service('simple_sitemap.generator'); $generator = \Drupal::service('simple_sitemap.generator');
$generated_ago = $generator->getGeneratedAgo(\Drupal\simple_sitemap\Simplesitemap::DEFAULT_SITEMAP_VARIANT); //todo $generated_ago = $generator->getGeneratedAgo(\Drupal\simple_sitemap\SimplesitemapManager::DEFAULT_SITEMAP_VARIANT); //todo
$cron_generation = $generator->getSetting('cron_generate'); $cron_generation = $generator->getSetting('cron_generate');
if (!$generated_ago) { if (!$generated_ago) {
...@@ -104,9 +102,17 @@ function simple_sitemap_schema() { ...@@ -104,9 +102,17 @@ function simple_sitemap_schema() {
'sitemap_created' => [ 'sitemap_created' => [
'description' => 'Timestamp of sitemap chunk generation.', 'description' => 'Timestamp of sitemap chunk generation.',
'type' => 'int', 'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => 0, 'default' => 0,
],
'status' => [
'description' => "Flag indicating the publishing status of the chunk.",
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE, 'not null' => TRUE,
'unsigned' => TRUE, 'unsigned' => TRUE,
'default' => 0,
], ],
], ],
'primary key' => ['id'], 'primary key' => ['id'],
...@@ -118,8 +124,8 @@ function simple_sitemap_schema() { ...@@ -118,8 +124,8 @@ function simple_sitemap_schema() {
'id' => [ 'id' => [
'description' => 'Override unique identifier.', 'description' => 'Override unique identifier.',
'type' => 'serial', 'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE, 'not null' => TRUE,
'unsigned' => TRUE,
], ],
'entity_type' => [ 'entity_type' => [
'description' => 'Entity type of the overriding entity.', 'description' => 'Entity type of the overriding entity.',
...@@ -562,3 +568,39 @@ function simple_sitemap_update_8214() { ...@@ -562,3 +568,39 @@ function simple_sitemap_update_8214() {
$config_factory->getEditable($type)->delete(); $config_factory->getEditable($type)->delete();
} }
} }
/**
* Adding 'status' field to simple_sitemap table and weight to variants.
*/
function simple_sitemap_update_8215() {
$database = \Drupal::database();
$database->truncate('simple_sitemap')->execute();
if (!$database->schema()->fieldExists('simple_sitemap', 'status')) {
$database->schema()->addField(
'simple_sitemap',
'status', [
'description' => "Flag indicating the publishing status of the chunk.",
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => 0,
]
);
}
$config_factory = \Drupal::service('config.factory');
foreach ($config_factory->listAll('simple_sitemap.variants.') as $type) {
/** @var \Drupal\Core\Config\Config $type */
$type = $config_factory->getEditable($type);
$variants = $type->get('variants');
foreach($variants as $i => $variant) {
$variants[$i]['weight'] = 0;
}
$type->set('variants', $variants)->save();
}
return t('The XML sitemaps need to be regenerated.');
}
...@@ -148,7 +148,7 @@ function simple_sitemap_cron() { ...@@ -148,7 +148,7 @@ function simple_sitemap_cron() {
$requestTime = \Drupal::service('datetime.time')->getRequestTime(); $requestTime = \Drupal::service('datetime.time')->getRequestTime();
if ($interval === 0 || ((\Drupal::state()->get('simple_sitemap.last_cron_generate', 0) + $interval) <= $requestTime)) { if ($interval === 0 || ((\Drupal::state()->get('simple_sitemap.last_cron_generate', 0) + $interval) <= $requestTime)) {
\Drupal::state()->set('simple_sitemap.last_cron_generate', $requestTime); \Drupal::state()->set('simple_sitemap.last_cron_generate', $requestTime);
$generator->generateSitemap('backend'); $generator->generateSitemap('cron');
} }
} }
} }
......
...@@ -4,6 +4,8 @@ services: ...@@ -4,6 +4,8 @@ services:
public: true public: true
arguments: arguments:
- '@simple_sitemap.entity_helper' - '@simple_sitemap.entity_helper'
- '@simple_sitemap.settings'
- '@simple_sitemap.manager'
- '@config.factory' - '@config.factory'
- '@database' - '@database'
- '@entity_type.manager' - '@entity_type.manager'
...@@ -12,10 +14,40 @@ services: ...@@ -12,10 +14,40 @@ services:
- '@date.formatter' - '@date.formatter'
- '@datetime.time' - '@datetime.time'
- '@module_handler' - '@module_handler'
- '@simple_sitemap.batch' - '@simple_sitemap.queue_worker'
- '@plugin.manager.simple_sitemap.url_generator'
- '@plugin.manager.simple_sitemap.sitemap_generator' simple_sitemap.manager:
- '@plugin.manager.simple_sitemap.sitemap_type' class: Drupal\simple_sitemap\SimplesitemapManager
public: true
arguments:
- '@config.factory'
- '@plugin.manager.simple_sitemap.sitemap_type'
- '@plugin.manager.simple_sitemap.url_generator'
- '@plugin.manager.simple_sitemap.sitemap_generator'
simple_sitemap.settings:
class: Drupal\simple_sitemap\SimplesitemapSettings
public: true
arguments:
- '@config.factory'
simple_sitemap.queue_worker:
class: Drupal\simple_sitemap\Queue\QueueWorker
public: true
arguments:
- '@simple_sitemap.settings'
- '@simple_sitemap.manager'
- '@state'
- '@module_handler'
- '@simple_sitemap.element_queue'
- '@lock'
simple_sitemap.element_queue:
class: Drupal\simple_sitemap\Queue\ElementQueue
public: false
arguments:
- 'simple_sitemap_elements'
- '@database'
simple_sitemap.sitemap_writer: simple_sitemap.sitemap_writer:
class: Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter class: Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter
...@@ -36,10 +68,6 @@ services: ...@@ -36,10 +68,6 @@ services:
- '@simple_sitemap.entity_helper' - '@simple_sitemap.entity_helper'
- '@current_user' - '@current_user'
simple_sitemap.batch:
class: Drupal\simple_sitemap\Batch
public: false
simple_sitemap.logger: simple_sitemap.logger:
class: Drupal\simple_sitemap\Logger class: Drupal\simple_sitemap\Logger
public: true public: true
......
<?php
namespace Drupal\simple_sitemap;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase;
/**
* Class Batch
* @package Drupal\simple_sitemap\Batch
*
* The services of this class are not injected, as this class looses its state
* on every method call because of how the batch API works.
*/
class Batch {
use StringTranslationTrait;
/**
* @var array
*/
protected $batch;
/**
* @var array
*/
protected $batchMeta;
const BATCH_TITLE = 'Generating XML sitemap';
const BATCH_INIT_MESSAGE = 'Initializing batch...';
const BATCH_ERROR_MESSAGE = 'An error has occurred. This may result in an incomplete XML sitemap.';
const BATCH_PROGRESS_MESSAGE = 'Running @current out of @total operations.';
const REGENERATION_FINISHED_MESSAGE = 'The XML sitemaps have been regenerated.';
const REGENERATION_FINISHED_ERROR_MESSAGE = 'The sitemap generation finished with an error.';
/**
* Batch constructor.
*/
public function __construct() {
$this->batch = [
'title' => $this->t(self::BATCH_TITLE),
'init_message' => $this->t(self::BATCH_INIT_MESSAGE),
'error_message' => $this->t(self::BATCH_ERROR_MESSAGE),
'progress_message' => $this->t(self::BATCH_PROGRESS_MESSAGE),
'operations' => [],
'finished' => [__CLASS__, 'finishGeneration'],
];
}
/**
* @param array $batch_meta
*/
public function setBatchMeta(array $batch_meta) {
$this->batchMeta = $batch_meta;
}
/**
* Adds an operation to the batch.
*
* @param string $operation_name
* @param array $arguments
*/
public function addOperation($operation_name, $arguments = []) {
$this->batch['operations'][] = [
__CLASS__ . '::' . $operation_name, [$arguments, $this->batchMeta]
];
}
/**
* Batch callback function which generates URLs.
*
* @param array $arguments
* @param array $batch_meta
* @param $context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateSitemap(array $arguments, array $batch_meta, &$context) {
/** @var UrlGeneratorBase $url_generator*/
$url_generator = \Drupal::service('plugin.manager.simple_sitemap.url_generator')
->createInstance($arguments['url_generator']);
$url_generator
->setContext($context)
->setSettings($arguments['settings'])
->setBatchMeta($batch_meta)
->setSitemapVariant($arguments['variant'])
->setSitemapGeneratorId($arguments['sitemap_generator'])
->generate($arguments['data_set']);
}
/**
* Batch callback function which generates URLs.
*
* @param array $arguments
* @param array $batch_meta
* @param $context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateIndex(array $arguments, array $batch_meta, &$context) {
/** @var SitemapGeneratorBase $sitemap_generator*/
$sitemap_generator = \Drupal::service('plugin.manager.simple_sitemap.sitemap_generator')
->createInstance($arguments['sitemap_generator']);
$sitemap_generator
->setSettings($arguments['settings'])
->setSitemapVariant($arguments['variant'])
->generateIndex();
}
/**
* Batch callback function which generates URLs.
*
* @param array $arguments
* @param array $batch_meta
* @param $context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function removeSitemap(array $arguments, array $batch_meta, &$context) {
/** @var SitemapGeneratorBase $sitemap_generator*/
$sitemap_generator = \Drupal::service('plugin.manager.simple_sitemap.sitemap_generator')
->createInstance($arguments['sitemap_generator']);
$sitemap_generator
->setSitemapVariant($arguments['variant'])
->remove()
->invalidateCache();
}
/**
* Callback function called by the batch API when all operations are finished.
*
* @param $success
* @param $results
* @param $operations
*
* @return bool
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*
* @todo Display success/failure message in Drush > 9.
*/
public static function finishGeneration($success, $results, $operations) {
if ($success) {
\Drupal::service('simple_sitemap.logger')
->m(self::REGENERATION_FINISHED_MESSAGE)
// ['@url' => $this->sitemapGenerator->getCustomBaseUrl() . '/sitemap.xml']) //todo: Use actual base URL for message.
->display('status')
->log('info');
}
else {
\Drupal::service('simple_sitemap.logger')
->m(self::REGENERATION_FINISHED_ERROR_MESSAGE)
->display('error', 'administer sitemap settings')
->log('error');
}
return $success;
}
/**
* Starts the batch process depending on where it was requested from.
*
* @return bool
*/
public function start() {
// Update last operation info for each operation.
$this->addAdditionalMetaInfo();
switch ($this->batchMeta['from']) {
case 'form':
// Start batch process.
batch_set($this->batch);
return TRUE;
case 'drush':
// Start drush batch process.
batch_set($this->batch);
// See https://www.drupal.org/node/638712
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
drush_log($this->t(self::BATCH_INIT_MESSAGE), 'status');
drush_backend_batch_process();
return TRUE;
case 'backend':
// Start backend batch process.
batch_set($this->batch);
// See https://www.drupal.org/node/638712
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
// todo: Does not take advantage of batch API and eventually runs out of memory on very large sites. Use queue API instead?
batch_process();
return TRUE;
case 'nobatch':
// Call each batch operation the way the Drupal batch API would do, but
// within one process (so in fact not using batch API here, just
// mimicking it to avoid code duplication).
$context = [];
foreach ($this->batch['operations'] as $i => $operation) {
$operation[1][] = &$context;
call_user_func_array($operation[0], $operation[1]);
}
return $this->finishGeneration(TRUE, !empty($context['results']) ? $context['results'] : [], []);
}
return FALSE;
}
protected function addAdditionalMetaInfo() {
$last_operation_no = 0;
foreach ($this->batch['operations'] as $i => $operation) {
if ($operation[0] === __CLASS__ . '::generateSitemap') {
$this->batch['operations'][$i][1][1]['current_generate_sitemap_operation_no'] = $i;
$last_operation_no = $i;
}
}
foreach ($this->batch['operations'] as $i => $operation) {
if ($operation[0] === __CLASS__ . '::generateSitemap') {
$this->batch['operations'][$i][1][1]['last_generate_sitemap_operation_no'] = $last_operation_no;
}
}
}
}
...@@ -35,4 +35,15 @@ class SimplesitemapCommands extends DrushCommands { ...@@ -35,4 +35,15 @@ class SimplesitemapCommands extends DrushCommands {
$this->generator->generateSitemap('drush'); $this->generator->generateSitemap('drush');
} }
/**
* Rebuild the sitemap queue for all sitemap variants.
*
* @command simple-sitemap:rebuild-queue
* @validate-module-enabled simple_sitemap
* @aliases ss:rebuild-queue, ssr, simple_sitemap:rebuild-queue, simple_sitemap-rebuild-queue
*/
public function rebuildQueue() {
$this->generator->rebuildQueue();
}
} }
...@@ -55,7 +55,7 @@ class SimplesitemapController extends ControllerBase { ...@@ -55,7 +55,7 @@ class SimplesitemapController extends ControllerBase {
* @param string $variant * @param string $variant
* Optional name of sitemap variant. * Optional name of sitemap variant.
* @see \hook_simple_sitemap_variants_alter() * @see \hook_simple_sitemap_variants_alter()
* @see Simplesitemap::getSitemapVariants() * @see SimplesitemapManager::getSitemapVariants()
* *
* @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\HttpFoundation\Request $request
* The request object. * The request object.
......
This diff is collapsed.
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
namespace Drupal\simple_sitemap\Form; namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_sitemap\Simplesitemap; use Drupal\simple_sitemap\SimplesitemapManager;
/** /**
* Class SimplesitemapVariantsForm * Class SimplesitemapVariantsForm
...@@ -33,11 +33,11 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase { ...@@ -33,11 +33,11 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
$form['simple_sitemap_variants']['variants'] = [ $form['simple_sitemap_variants']['variants'] = [
'#type' => 'textarea', '#type' => 'textarea',
'#title' => $this->t('Variants'), '#title' => $this->t('Variants'),
'#default_value' => $this->variantsToString($this->generator->getSitemapVariants(NULL, TRUE)), '#default_value' => $this->variantsToString($this->generator->getSitemapManager()->getSitemapVariants(NULL, TRUE)),
'#description' => $this->t("Please specify sitemap variants, one per line.<br/>A variant definition consists of the variant name (used as the variant's path), the sitemap type it belongs to (optional) and the variant label (optional).<br/>These three values have to be separated by the | pipe | symbol.<br/><br/><strong>Examples:</strong><br/><em>default | default_hreflang | Default</em> -> variant of the <em>default_hreflang</em> sitemap type and <em>Default</em> as label; accessible under <em>/default/sitemap.xml</em><br/><em>test</em> -> variant of the <em>@default_sitemap_type</em> sitemap type and <em>test</em> as label; accessible under <em>/test/sitemap.xml</em><br/><br/><strong>Available sitemap types:</strong>", ['@default_sitemap_type' => Simplesitemap::DEFAULT_SITEMAP_TYPE]), '#description' => $this->t("Please specify sitemap variants, one per line.<br/>A variant definition consists of the variant name (used as the variant's path), the sitemap type it belongs to (optional) and the variant label (optional).<br/>These three values have to be separated by the | pipe | symbol.<br/><br/><strong>Examples:</strong><br/><em>default | default_hreflang | Default</em> -> variant of the <em>default_hreflang</em> sitemap type and <em>Default</em> as label; accessible under <em>/default/sitemap.xml</em><br/><em>test</em> -> variant of the <em>@default_sitemap_type</em> sitemap type and <em>test</em> as label; accessible under <em>/test/sitemap.xml</em><br/><br/><strong>Available sitemap types:</strong>", ['@default_sitemap_type' => SimplesitemapManager::DEFAULT_SITEMAP_TYPE]),
]; ];
foreach ($this->generator->getSitemapTypes() as $sitemap_type => $definition) { foreach ($this->generator->getSitemapManager()->getSitemapTypes() as $sitemap_type => $definition) {
$form['simple_sitemap_variants']['variants']['#description'] .= '<br/>' . '<em>' . $sitemap_type . '</em>' . (!empty($definition['description']) ? (': ' . $definition['description']) : ''); $form['simple_sitemap_variants']['variants']['#description'] .= '<br/>' . '<em>' . $sitemap_type . '</em>' . (!empty($definition['description']) ? (': ' . $definition['description']) : '');
} }
...@@ -53,7 +53,7 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase { ...@@ -53,7 +53,7 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
*/ */
public function validateForm(array &$form, FormStateInterface $form_state) { public function validateForm(array &$form, FormStateInterface $form_state) {
$line = 0; $line = 0;
$sitemap_types = $this->generator->getSitemapTypes(); $sitemap_types = $this->generator->getSitemapManager()->getSitemapTypes();
foreach ($this->stringToVariants($form_state->getValue('variants')) as $variant_name => $variant_definition) { foreach ($this->stringToVariants($form_state->getValue('variants')) as $variant_name => $variant_definition) {
$placeholders = [ $placeholders = [
'@line' => ++$line, '@line' => ++$line,
...@@ -80,9 +80,11 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase { ...@@ -80,9 +80,11 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$this->generator->removeSitemapVariants(); $this->generator->getSitemapManager()->removeSitemapVariants();
$weight = 0;
foreach ($this->stringToVariants($form_state->getValue('variants')) as $variant_name => $variant_definition) { foreach ($this->stringToVariants($form_state->getValue('variants')) as $variant_name => $variant_definition) {
$this->generator->addSitemapVariant($variant_name, $variant_definition); $this->generator->getSitemapManager()->addSitemapVariant($variant_name, $variant_definition + ['weight' => $weight]);
$weight++;
} }
parent::submitForm($form, $form_state); parent::submitForm($form, $form_state);
...@@ -108,7 +110,7 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase { ...@@ -108,7 +110,7 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
foreach ($variants_string_lines as $i => &$line) { foreach ($variants_string_lines as $i => &$line) {
$variant_settings = explode('|', $line); $variant_settings = explode('|', $line);
$name = strtolower(trim($variant_settings[0])); $name = strtolower(trim($variant_settings[0]));
$variants[$name]['type'] = !empty($variant_settings[1]) ? trim($variant_settings[1]) : Simplesitemap::DEFAULT_SITEMAP_TYPE; $variants[$name]['type'] = !empty($variant_settings[1]) ? trim($variant_settings[1]) : SimplesitemapManager::DEFAULT_SITEMAP_TYPE;
$variants[$name]['label'] = !empty($variant_settings[2]) ? trim($variant_settings[2]) : $name; $variants[$name]['label'] = !empty($variant_settings[2]) ? trim($variant_settings[2]) : $name;
} }
return $variants; return $variants;
......
...@@ -139,6 +139,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -139,6 +139,7 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
->fields('s', ['delta', 'sitemap_created', 'type']) ->fields('s', ['delta', 'sitemap_created', 'type'])
->condition('s.type', $this->sitemapVariant) ->condition('s.type', $this->sitemapVariant)
->condition('s.delta', self::INDEX_DELTA, '<>') ->condition('s.delta', self::INDEX_DELTA, '<>')
->condition('s.status', 0)
->execute() ->execute()
->fetchAllAssoc('delta'); ->fetchAllAssoc('delta');
} }
...@@ -182,10 +183,23 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -182,10 +183,23 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
/** /**
* @return $this * @return $this
*/ */
public function remove() { public function remove($mode = 'all') {
$this->db->delete('simple_sitemap') $query = $this->db->delete('simple_sitemap')
->condition('type', $this->sitemapVariant) ->condition('type', $this->sitemapVariant);
->execute();
switch($mode) {
case 'published':
$query->condition('status', 1);
break;
case 'unpublished':
$query->condition('status', 0);
break;
case 'all':
break;
default:
//todo: throw error
}
$query->execute();
return $this; return $this;
} }
...@@ -201,18 +215,17 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -201,18 +215,17 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
*/ */
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();
$highest_delta = $this->db->query('SELECT MAX(delta) FROM {simple_sitemap} WHERE type = :type', [':type' => $this->sitemapVariant]) $highest_delta = $this->db->query('SELECT MAX(delta) FROM {simple_sitemap} WHERE type = :type AND status = :status', [':type' => $this->sitemapVariant, ':status' => 0])
->fetchField(); ->fetchField();
$values = [ $this->db->insert('simple_sitemap')->fields([
'id' => NULL === $highest_id ? 0 : $highest_id + 1, 'id' => NULL === $highest_id ? 0 : $highest_id + 1,
'delta' => NULL === $highest_delta ? self::FIRST_CHUNK_DELTA : $highest_delta + 1, 'delta' => NULL === $highest_delta ? self::FIRST_CHUNK_DELTA : $highest_delta + 1,
'type' => $this->sitemapVariant, 'type' => $this->sitemapVariant,
'sitemap_string' => $this->getXml($links), 'sitemap_string' => $this->getXml($links),
'sitemap_created' => $this->time->getRequestTime(), 'sitemap_created' => $this->time->getRequestTime(),
]; 'status' => 0,
])->execute();
$this->db->insert('simple_sitemap')->fields($values)->execute();
return $this; return $this;
} }
...@@ -226,16 +239,20 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -226,16 +239,20 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
$index_xml = $this->getIndexXml($chunk_info); $index_xml = $this->getIndexXml($chunk_info);
$highest_id = $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField(); $highest_id = $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField();