You are here

trait BatchTrait in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 src/Queue/BatchTrait.php \Drupal\simple_sitemap\Queue\BatchTrait

Hierarchy

1 file declares its use of BatchTrait
performance_test.php in tests/scripts/performance_test.php

File

src/Queue/BatchTrait.php, line 7

Namespace

Drupal\simple_sitemap\Queue
View source
trait BatchTrait {
  use StringTranslationTrait;

  /**
   * @var array
   */
  protected $batch;
  protected static $batchErrorMessage = 'The generation failed to finish. It can be continued manually on the module\'s settings page, or via drush.';

  /**
   * @param string $from
   * @param array|null $variants
   *
   * @return bool
   */
  public function batchGenerateSitemap(string $from = self::GENERATE_TYPE_FORM, ?array $variants = NULL) : bool {
    $this->batch = [
      'title' => $this
        ->t('Generating XML sitemaps'),
      'init_message' => $this
        ->t('Initializing...'),
      'error_message' => $this
        ->t(self::$batchErrorMessage),
      'progress_message' => $this
        ->t('Processing items from the queue.<br>Each sitemap variant gets published after all of its items have been processed.'),
      'operations' => [
        [
          __CLASS__ . '::' . 'doBatchGenerateSitemap',
          [],
        ],
      ],
      'finished' => [
        __CLASS__,
        'finishGeneration',
      ],
    ];
    switch ($from) {
      case self::GENERATE_TYPE_FORM:

        // Start batch process.
        batch_set($this->batch);
        return TRUE;
      case self::GENERATE_TYPE_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_backend_batch_process();
        return TRUE;
    }
    return FALSE;
  }

  /**
   * @param $context
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   *
   * @todo Variants into generateSitemap().
   */
  public static function doBatchGenerateSitemap(&$context) : void {

    /** @var \Drupal\simple_sitemap\Queue\QueueWorker $queue_worker */
    $queue_worker = \Drupal::service('simple_sitemap.queue_worker');
    $queue_worker
      ->generateSitemap();
    $processed_element_count = $queue_worker
      ->getProcessedElementCount();
    $original_element_count = $queue_worker
      ->getInitialElementCount();
    $context['message'] = t('@indexed out of @total total queue items have been processed.', [
      '@indexed' => $processed_element_count,
      '@total' => $original_element_count,
    ]);
    $context['finished'] = $original_element_count > 0 ? $processed_element_count / $original_element_count : 1;
  }

  /**
   * Callback function called by the batch API when all operations are finished.
   *
   * @param bool $success
   * @param array $results
   * @param array $operations
   *
   * @return bool
   * @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
   */
  public static function finishGeneration(bool $success, array $results, array $operations) : bool {
    if ($success) {
      \Drupal::service('simple_sitemap.logger')
        ->m('The XML sitemaps have been regenerated.')
        ->log('info');
    }
    else {
      \Drupal::service('simple_sitemap.logger')
        ->m(self::$batchErrorMessage)
        ->display('error', 'administer sitemap settings')
        ->log('error');
    }
    return $success;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BatchTrait::$batch protected property
BatchTrait::$batchErrorMessage protected static property
BatchTrait::batchGenerateSitemap public function
BatchTrait::doBatchGenerateSitemap public static function @todo Variants into generateSitemap().
BatchTrait::finishGeneration public static function Callback function called by the batch API when all operations are finished.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.