3 namespace Drupal\simple_sitemap\Batch;
5 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use StringTranslationTrait;
24 const BATCH_TITLE = 'Generating XML sitemap';
25 const BATCH_INIT_MESSAGE = 'Initializing batch...';
26 const BATCH_ERROR_MESSAGE = 'An error has occurred. This may result in an incomplete XML sitemap.';
27 const BATCH_PROGRESS_MESSAGE = 'Processing @current out of @total link types.';
32 public function __construct() {
34 'title' => $this->t(self::BATCH_TITLE),
35 'init_message' => $this->t(self::BATCH_INIT_MESSAGE),
36 'error_message' => $this->t(self::BATCH_ERROR_MESSAGE),
37 'progress_message' => $this->t(self::BATCH_PROGRESS_MESSAGE),
39 // __CLASS__ . '::finishGeneration' not working possibly due to a drush error.
40 'finished' => [__CLASS__, 'finishGeneration'],
45 * @param array $batch_info
47 public function setBatchInfo(array $batch_info) {
48 $this->batchInfo = $batch_info;
52 * Starts the batch process depending on where it was requested from.
54 public function start() {
55 switch ($this->batchInfo['from']) {
58 // Start batch process.
59 batch_set($this->batch);
63 // Start drush batch process.
64 batch_set($this->batch);
65 $this->batch =& batch_get();
66 $this->batch['progressive'] = FALSE;
67 drush_log($this->t(self::BATCH_INIT_MESSAGE), 'status');
68 drush_backend_batch_process();
72 // Start backend batch process.
73 batch_set($this->batch);
74 $this->batch =& batch_get();
75 $this->batch['progressive'] = FALSE;
76 // todo: Does not take advantage of batch API and eventually runs out of memory on very large sites. Use queue API instead?
81 // Call each batch operation the way the Drupal batch API would do, but
82 // within one process (so in fact not using batch API here, just
83 // mimicking it to avoid code duplication).
85 foreach ($this->batch['operations'] as $i => $operation) {
86 $operation[1][] = &$context;
87 call_user_func_array($operation[0], $operation[1]);
89 $this->finishGeneration(TRUE, $context['results'], []);
95 * Adds an operation to the batch.
97 * @param string $processing_method
100 public function addOperation($processing_method, array $data) {
101 $this->batch['operations'][] = [
102 __CLASS__ . '::' . $processing_method, [$data, $this->batchInfo],
107 * Batch callback function which generates urls to entity paths.
109 * @param array $entity_info
110 * @param array $batch_info
111 * @param array &$context
113 * @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
115 public static function generateBundleUrls(array $entity_info, array $batch_info, &$context) {
116 \Drupal::service('simple_sitemap.batch_url_generator')
117 ->setContext($context)
118 ->setBatchInfo($batch_info)
119 ->generateBundleUrls($entity_info);
123 * Batch callback function which generates urls to custom paths.
125 * @param array $custom_paths
126 * @param array $batch_info
127 * @param array &$context
129 * @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
131 public static function generateCustomUrls(array $custom_paths, array $batch_info, &$context) {
132 \Drupal::service('simple_sitemap.batch_url_generator')
133 ->setContext($context)
134 ->setBatchInfo($batch_info)
135 ->generateCustomUrls($custom_paths);
139 * Callback function called by the batch API when all operations are finished.
145 * @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
147 public static function finishGeneration($success, $results, $operations) {
148 \Drupal::service('simple_sitemap.batch_url_generator')
149 ->finishGeneration($success, $results, $operations);