BatchTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Drupal\simple_sitemap\Queue;
  3. use Drupal\Core\StringTranslation\StringTranslationTrait;
  4. trait BatchTrait {
  5. use StringTranslationTrait;
  6. /**
  7. * @var array
  8. */
  9. protected $batch;
  10. protected static $batchErrorMessage = 'The generation failed to finish. It can be continued manually on the module\'s setting page, or via drush.';
  11. /**
  12. * @param string $from
  13. * @param array|null $variants
  14. * @return bool
  15. */
  16. public function batchGenerateSitemap($from = 'form', $variants = NULL) {
  17. $this->batch = [
  18. 'title' => $this->t('Generating XML sitemaps'),
  19. 'init_message' => $this->t('Initializing...'),
  20. 'error_message' => $this->t(self::$batchErrorMessage),
  21. 'progress_message' => $this->t('Processing items from queue. Each sitemap variant is published as soon as its items have been processed.'),
  22. 'operations' => [[ __CLASS__ . '::' . 'doBatchGenerateSitemap', []]],
  23. 'finished' => [__CLASS__, 'finishGeneration'],
  24. ];
  25. switch ($from) {
  26. case 'form':
  27. // Start batch process.
  28. batch_set($this->batch);
  29. return TRUE;
  30. case 'drush':
  31. // Start drush batch process.
  32. batch_set($this->batch);
  33. // See https://www.drupal.org/node/638712
  34. $this->batch =& batch_get();
  35. $this->batch['progressive'] = FALSE;
  36. drush_backend_batch_process();
  37. return TRUE;
  38. }
  39. return FALSE;
  40. }
  41. /**
  42. * @param $context
  43. * @throws \Drupal\Component\Plugin\Exception\PluginException
  44. *
  45. * @todo Make sure batch does not run at the same time as cron.
  46. * @todo Variants into generateSitemap().
  47. */
  48. public static function doBatchGenerateSitemap(&$context) {
  49. /** @var \Drupal\simple_sitemap\Queue\QueueWorker $queue_worker */
  50. $queue_worker = \Drupal::service('simple_sitemap.queue_worker');
  51. $queue_worker->generateSitemap();
  52. $processed_element_count = $queue_worker->getProcessedElementCount();
  53. $original_element_count = $queue_worker->getInitialElementCount();
  54. $context['message'] = t('@indexed out of @total total items have been processed.', [
  55. '@indexed' => $processed_element_count, '@total' => $original_element_count]);
  56. $context['finished'] = $original_element_count > 0 ? ($processed_element_count / $original_element_count) : 1;
  57. }
  58. /**
  59. * Callback function called by the batch API when all operations are finished.
  60. *
  61. * @param bool $success
  62. * @param array $results
  63. * @param array $operations
  64. *
  65. * @return bool
  66. *
  67. * @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
  68. */
  69. public static function finishGeneration($success, $results, $operations) {
  70. if ($success) {
  71. \Drupal::service('simple_sitemap.logger')
  72. ->m('The XML sitemaps have been regenerated.')
  73. ->log('info');
  74. }
  75. else {
  76. \Drupal::service('simple_sitemap.logger')
  77. ->m(self::$batchErrorMessage)
  78. ->display('error', 'administer sitemap settings')
  79. ->log('error');
  80. }
  81. return $success;
  82. }
  83. }