View source
<?php
namespace Drupal\simple_sitemap;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Cache\Cache;
class Batch {
use StringTranslationTrait;
protected $batch;
protected $batchSettings;
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 = 'Processing @current out of @total link types.';
const REGENERATION_FINISHED_MESSAGE = 'The <a href="@url" target="_blank">XML sitemap</a> has been regenerated.';
const REGENERATION_FINISHED_ERROR_MESSAGE = 'The sitemap generation finished with an error.';
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',
],
];
}
public function setBatchSettings(array $batch_settings) {
$this->batchSettings = $batch_settings;
}
public function start() {
switch ($this->batchSettings['from']) {
case 'form':
batch_set($this->batch);
return TRUE;
case 'drush':
batch_set($this->batch);
$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':
batch_set($this->batch);
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
batch_process();
return TRUE;
case 'nobatch':
$context = [];
foreach ($this->batch['operations'] as $i => $operation) {
$operation[1][] =& $context;
call_user_func_array($operation[0], $operation[1]);
}
$this
->finishGeneration(TRUE, !empty($context['results']) ? $context['results'] : [], []);
return TRUE;
}
return FALSE;
}
public function addOperation($plugin_id, $data_sets = NULL) {
$this->batch['operations'][] = [
__CLASS__ . '::generate',
[
$plugin_id,
$data_sets,
$this->batchSettings,
],
];
}
public static function generate($plugin_id, $data_sets, array $batch_settings, &$context) {
\Drupal::service('plugin.manager.simple_sitemap.url_generator')
->createInstance($plugin_id)
->setContext($context)
->setBatchSettings($batch_settings)
->generate($data_sets);
}
public static function finishGeneration($success, $results, $operations) {
if ($success) {
$remove_sitemap = empty($results['chunk_count']);
if (!empty($results['generate']) || $remove_sitemap) {
\Drupal::service('simple_sitemap.sitemap_generator')
->setSettings([
'excluded_languages' => \Drupal::service('simple_sitemap.generator')
->getSetting('excluded_languages', []),
])
->generateSitemap(!empty($results['generate']) ? $results['generate'] : [], $remove_sitemap);
}
Cache::invalidateTags([
'simple_sitemap',
]);
\Drupal::service('simple_sitemap.logger')
->m(self::REGENERATION_FINISHED_MESSAGE, [
'@url' => $GLOBALS['base_url'] . '/sitemap.xml',
])
->display('status')
->log('info');
}
else {
\Drupal::service('simple_sitemap.logger')
->m(self::REGENERATION_FINISHED_ERROR_MESSAGE)
->display('error', 'administer sitemap settings')
->log('error');
}
}
}