SitemapGenerator.php 8.25 KB
Newer Older
1
2
<?php

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

Pawel G's avatar
Pawel G committed
5
use XMLWriter;
Pawel G's avatar
Pawel G committed
6
7
8
9
use Drupal\simple_sitemap\Batch\Batch;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Language\LanguageManagerInterface;
10
11

/**
Pawel G's avatar
Pawel G committed
12
13
 * Class SitemapGenerator.
 *
Pawel G's avatar
Pawel G committed
14
 * @package Drupal\simple_sitemap
15
16
17
18
19
 */
class SitemapGenerator {

  const XML_VERSION = '1.0';
  const ENCODING = 'UTF-8';
20
21
  const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
22
  const GENERATED_BY = 'Generated by the Simple XML sitemap Drupal module: https://drupal.org/project/simple_sitemap.';
23

24
  private $batch;
25
26
27
28
  private $db;
  private $moduleHandler;
  private $defaultLanguageId;
  private $generateFrom = 'form';
29
  private $isHreflangSitemap;
30
  private $generator;
31

32
33
  /**
   * SitemapGenerator constructor.
Pawel G's avatar
Pawel G committed
34
35
36
37
   * @param \Drupal\simple_sitemap\Batch\Batch $batch
   * @param \Drupal\Core\Database\Connection $database
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
38
   */
Pawel G's avatar
Pawel G committed
39
40
41
42
  public function __construct(
    Batch $batch,
    Connection $database,
    ModuleHandler $module_handler,
43
    LanguageManagerInterface $language_manager
Pawel G's avatar
Pawel G committed
44
  ) {
45
    $this->batch = $batch;
46
    $this->db = $database;
47
    $this->moduleHandler = $module_handler;
48
    $this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
49
    $this->isHreflangSitemap = count($language_manager->getLanguages()) > 1;
50
51
52
  }

  /**
Pawel G's avatar
Pawel G committed
53
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
54
55
   * @return $this
   */
Pawel G's avatar
Pawel G committed
56
  public function setGenerator(Simplesitemap $generator) {
57
58
    $this->generator = $generator;
    return $this;
59
60
  }

Pawel G's avatar
Pawel G committed
61
  /**
Pawel G's avatar
Pawel G committed
62
   * @param string $from
63
   * @return $this
Pawel G's avatar
Pawel G committed
64
   */
Pawel G's avatar
Pawel G committed
65
66
  public function setGenerateFrom($from) {
    $this->generateFrom = $from;
Pawel G's avatar
Pawel G committed
67
    return $this;
68
69
  }

70
  /**
71
   * Adds all operations to the batch and starts it.
72
   */
73
  public function startGeneration() {
74
    $this->batch->setBatchInfo([
Pawel G's avatar
Pawel G committed
75
      'from' => $this->generateFrom,
Pawel G's avatar
Pawel G committed
76
      'batch_process_limit' => !empty($this->generator->getSetting('batch_process_limit'))
Pawel G's avatar
Pawel G committed
77
      ? $this->generator->getSetting('batch_process_limit') : NULL,
78
79
80
      'max_links' => $this->generator->getSetting('max_links', 2000),
      'skip_untranslated' => $this->generator->getSetting('skip_untranslated', FALSE),
      'remove_duplicates' => $this->generator->getSetting('remove_duplicates', TRUE),
81
      'entity_types' => $this->generator->getBundleSettings(),
82
      'base_url' => $this->generator->getSetting('base_url', ''),
83
    ]);
Pawel G's avatar
Pawel G committed
84
    // Add custom link generating operation.
85
    $this->batch->addOperation('generateCustomUrls', $this->getCustomUrlsData());
Pawel G's avatar
Pawel G committed
86
87

    // Add entity link generating operations.
Pawel G's avatar
Pawel G committed
88
    foreach ($this->getEntityTypeData() as $data) {
89
      $this->batch->addOperation('generateBundleUrls', $data);
Pawel G's avatar
Pawel G committed
90
    }
91
    $this->batch->start();
Pawel G's avatar
Pawel G committed
92
93
94
  }

  /**
Pawel G's avatar
Pawel G committed
95
   * Returns a batch-ready data array for custom link generation.
96
   *
Pawel G's avatar
Pawel G committed
97
   * @return array
Pawel G's avatar
Pawel G committed
98
   *   Data to be processed.
Pawel G's avatar
Pawel G committed
99
   */
Pawel G's avatar
Pawel G committed
100
  private function getCustomUrlsData() {
101
    $paths = [];
102
    foreach ($this->generator->getCustomLinks() as $i => $custom_path) {
103
104
      $paths[$i]['path'] = $custom_path['path'];
      $paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
Pawel G's avatar
Pawel G committed
105
106
      // todo: implement lastmod.
      $paths[$i]['lastmod'] = NULL;
107
108
    }
    return $paths;
Pawel G's avatar
Pawel G committed
109
  }
110

Pawel G's avatar
Pawel G committed
111
  /**
112
   * Collects entity metadata for entities that are set to be indexed
Pawel G's avatar
Pawel G committed
113
   * and returns an array of batch-ready data sets for entity link generation.
114
   *
Pawel G's avatar
Pawel G committed
115
   * @return array
Pawel G's avatar
Pawel G committed
116
   */
Pawel G's avatar
Pawel G committed
117
118
  private function getEntityTypeData() {
    $data_sets = [];
119
    $sitemap_entity_types = $this->generator->getSitemapEntityTypes();
120
    $entity_types = $this->generator->getBundleSettings();
Pawel G's avatar
Pawel G committed
121
    foreach ($entity_types as $entity_type_name => $bundles) {
122
123
      if (isset($sitemap_entity_types[$entity_type_name])) {
        $keys = $sitemap_entity_types[$entity_type_name]->getKeys();
Pawel G's avatar
Pawel G committed
124

Pawel G's avatar
Pawel G committed
125
126
        // Menu fix.
        $keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle'];
Pawel G's avatar
Pawel G committed
127

Pawel G's avatar
Pawel G committed
128
        foreach ($bundles as $bundle_name => $bundle_settings) {
129
          if ($bundle_settings['index']) {
Pawel G's avatar
Pawel G committed
130
131
132
133
134
            $data_sets[] = [
              'bundle_settings' => $bundle_settings,
              'bundle_name' => $bundle_name,
              'entity_type_name' => $entity_type_name,
              'keys' => $keys,
135
136
            ];
          }
Pawel G's avatar
Pawel G committed
137
        }
138
139
      }
    }
Pawel G's avatar
Pawel G committed
140
    return $data_sets;
Pawel G's avatar
Pawel G committed
141
142
143
  }

  /**
144
145
   * Wrapper method which takes links along with their options, lets other
   * modules alter the links and then generates and saves the sitemap.
Pawel G's avatar
Pawel G committed
146
   *
147
   * @param array $links
Pawel G's avatar
Pawel G committed
148
   *   All links with their multilingual versions and settings.
149
   * @param bool $remove_sitemap
Pawel G's avatar
Pawel G committed
150
   *   Remove old sitemap from database before inserting the new one.
Pawel G's avatar
Pawel G committed
151
   */
Pawel G's avatar
Pawel G committed
152
  public function generateSitemap(array $links, $remove_sitemap = FALSE) {
153
    // Invoke alter hook.
154
    $this->moduleHandler->alter('simple_sitemap_links', $links);
Pawel G's avatar
Pawel G committed
155

Pawel G's avatar
Pawel G committed
156
    $values = [
157
158
      'id' => $remove_sitemap ? 1 : $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
      'sitemap_string' => $this->generateSitemapChunk($links),
159
      'sitemap_created' => REQUEST_TIME,
Pawel G's avatar
Pawel G committed
160
    ];
161
    if ($remove_sitemap) {
162
      $this->db->truncate('simple_sitemap')->execute();
163
    }
164
    $this->db->insert('simple_sitemap')->fields($values)->execute();
165
166
  }

167
  /**
168
   * Generates and returns the sitemap index for all sitemap chunks.
169
   *
Pawel G's avatar
Pawel G committed
170
   * @param array $chunks
Pawel G's avatar
Pawel G committed
171
   *   All sitemap chunks keyed by the chunk ID.
172
173
174
   *
   * @return string sitemap index
   */
Pawel G's avatar
Pawel G committed
175
  public function generateSitemapIndex(array $chunks) {
176
177
178
179
    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
180
    $writer->writeComment(self::GENERATED_BY);
181
182
183
    $writer->startElement('sitemapindex');
    $writer->writeAttribute('xmlns', self::XMLNS);

Pawel G's avatar
Pawel G committed
184
    foreach ($chunks as $chunk_id => $chunk_data) {
185
      $writer->startElement('sitemap');
186
      $writer->writeElement('loc', $this->getCustomBaseUrl() . '/sitemaps/' . $chunk_id . '/' . 'sitemap.xml');
187
      $writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
188
189
190
191
192
193
194
      $writer->endElement();
    }
    $writer->endElement();
    $writer->endDocument();
    return $writer->outputMemory();
  }

195
196
197
198
199
  public function getCustomBaseUrl() {
    $customBaseUrl = $this->generator->getSetting('base_url', '');
    return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url'];
  }

200
201
202
  /**
   * Generates and returns a sitemap chunk.
   *
Pawel G's avatar
Pawel G committed
203
   * @param array $links
Pawel G's avatar
Pawel G committed
204
   *   All links with their multilingual versions and settings.
205
   *
Pawel G's avatar
Pawel G committed
206
   * @return string
Pawel G's avatar
Pawel G committed
207
   *   Sitemap chunk
208
   */
Pawel G's avatar
Pawel G committed
209
  private function generateSitemapChunk(array $links) {
210
211
212
213
    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
214
    $writer->writeComment(self::GENERATED_BY);
215
216
    $writer->startElement('urlset');
    $writer->writeAttribute('xmlns', self::XMLNS);
217
218
219
    if ($this->isHreflangSitemap) {
      $writer->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);
    }
220

Pawel G's avatar
Pawel G committed
221
    foreach ($links as $link) {
222

223
      // Add each translation variant URL as location to the sitemap.
224
225
226
      $writer->startElement('url');
      $writer->writeElement('loc', $link['url']);

227
228
229
230
      // If more than one language is enabled, add all translation variant URLs
      // as alternate links to this location turning the sitemap into a hreflang
      // sitemap.
      if ($this->isHreflangSitemap) {
Pawel G's avatar
Pawel G committed
231
        foreach ($link['alternate_urls'] as $language_id => $alternate_url) {
232
233
234
235
236
237
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $alternate_url);
          $writer->endElement();
        }
238
      }
239

240
241
      // Add lastmod if any.
      if (isset($link['lastmod'])) {
242
243
        $writer->writeElement('lastmod', $link['lastmod']);
      }
244
245
246
247
248
249
250
251

      //todo: Implement changefreq here.

      // Add priority if any.
      if (isset($link['priority'])) {
        $writer->writeElement('priority', $link['priority']);
      }

252
253
      $writer->endElement();
    }
Pawel G's avatar
Pawel G committed
254
    $writer->endElement();
255
256
    $writer->endDocument();
    return $writer->outputMemory();
257
  }
Pawel G's avatar
Pawel G committed
258

259
}