Batch.php 3.44 KB
Newer Older
Pawel G's avatar
Pawel G committed
1
2
<?php

3
namespace Drupal\simple_sitemap\Batch;
Pawel G's avatar
Pawel G committed
4

5
use Drupal\Core\StringTranslation\StringTranslationTrait;
Pawel G's avatar
Pawel G committed
6
7

class Batch {
8

9
  use StringTranslationTrait;
10

Pawel G's avatar
Pawel G committed
11
  private $batch;
12
  private $batchInfo;
Pawel G's avatar
Pawel G committed
13

14
15
16
  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.';
Pawel G's avatar
Pawel G committed
17

18
19
20
  /**
   * Batch constructor.
   */
21
  public function __construct() {
Pawel G's avatar
Pawel G committed
22
    $this->batch = [
23
24
25
26
      'title' => $this->t('Generating XML sitemap'),
      'init_message' => $this->t(self::BATCH_INIT_MESSAGE),
      'error_message' => $this->t(self::BATCH_ERROR_MESSAGE),
      'progress_message' => $this->t(self::BATCH_PROGRESS_MESSAGE),
Pawel G's avatar
Pawel G committed
27
28
29
      'operations' => [],
      'finished' => [__CLASS__, 'finishGeneration'], // __CLASS__ . '::finishGeneration' not working possibly due to a drush error.
    ];
30
31
  }

32
33
34
  /**
   * @param $batch_info
   */
35
36
  public function setBatchInfo($batch_info) {
    $this->batchInfo = $batch_info;
Pawel G's avatar
Pawel G committed
37
38
  }

39
40
41
  /**
   * Starts the batch process depending on where it was requested from.
   */
Pawel G's avatar
Pawel G committed
42
  public function start() {
43
    switch ($this->batchInfo['from']) {
44

Pawel G's avatar
Pawel G committed
45
      case 'form':
46
        batch_set($this->batch);
Pawel G's avatar
Pawel G committed
47
        break;
48

Pawel G's avatar
Pawel G committed
49
      case 'drush':
50
        batch_set($this->batch);
Pawel G's avatar
Pawel G committed
51
        $this->batch =& batch_get();
Pawel G's avatar
Pawel G committed
52
        $this->batch['progressive'] = FALSE;
53
        drush_log(t(self::BATCH_INIT_MESSAGE), 'status');
Pawel G's avatar
Pawel G committed
54
55
        drush_backend_batch_process();
        break;
56

57
      case 'backend':
58
        batch_set($this->batch);
Pawel G's avatar
Pawel G committed
59
        $this->batch =& batch_get();
60
        $this->batch['progressive'] = FALSE;
Pawel G's avatar
Pawel G committed
61
        batch_process(); //todo: Does not take advantage of batch API and eventually runs out of memory on very large sites.
62
        break;
63
64

      case 'nobatch':
Pawel G's avatar
Pawel G committed
65
        $context = [];
66
67
68
69
        foreach($this->batch['operations'] as $i => $operation) {
          $operation[1][] = &$context;
          call_user_func_array($operation[0], $operation[1]);
        }
Pawel G's avatar
Pawel G committed
70
        self::finishGeneration(TRUE, $context['results'], []);
71
        break;
Pawel G's avatar
Pawel G committed
72
73
74
    }
  }

75
  /**
Pawel G's avatar
Pawel G committed
76
   * Adds an operation to the batch.
77
   *
Pawel G's avatar
Pawel G committed
78
79
   * @param string $processing_method
   * @param array $data
80
   */
Pawel G's avatar
Pawel G committed
81
82
83
84
  public function addOperation($processing_method, $data) {
    $this->batch['operations'][] = [
      __CLASS__ . '::' . $processing_method, [$data, $this->batchInfo]
    ];
Pawel G's avatar
Pawel G committed
85
86
87
  }

  /**
88
   * Batch callback function which generates urls to entity paths.
Pawel G's avatar
Pawel G committed
89
   *
90
   * @param array $entity_info
91
92
   * @param array $batch_info
   * @param array &$context
Pawel G's avatar
Pawel G committed
93
   */
94
  public static function generateBundleUrls($entity_info, $batch_info, &$context) {
95
    \Drupal::service('simple_sitemap.batch_url_generator')->generateBundleUrls($entity_info, $batch_info, $context);
Pawel G's avatar
Pawel G committed
96
97
  }

Pawel G's avatar
Pawel G committed
98
  /**
99
   * Batch function which generates urls to custom paths.
Pawel G's avatar
Pawel G committed
100
   *
101
102
103
   * @param array $custom_paths
   * @param array $batch_info
   * @param array &$context
Pawel G's avatar
Pawel G committed
104
   */
105
  public static function generateCustomUrls($custom_paths, $batch_info, &$context) {
106
107
108
109
110
111
112
113
114
115
116
117
    \Drupal::service('simple_sitemap.batch_url_generator')->generateCustomUrls($custom_paths, $batch_info, $context);
  }

  /**
   * Callback function called by the batch API when all operations are finished.
   *
   * @param $success
   * @param $results
   * @param $operations
   */
  public static function finishGeneration($success, $results, $operations) {
    \Drupal::service('simple_sitemap.batch_url_generator')->finishGeneration($success, $results, $operations);
Pawel G's avatar
Pawel G committed
118
119
  }
}