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

gbyte.co's avatar
gbyte.co committed
3
namespace Drupal\simple_sitemap;
4

5
use XMLWriter;
gbyte.co's avatar
gbyte.co 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

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

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

gbyte.co's avatar
gbyte.co committed
24 25 26
  /**
   * @var \Drupal\simple_sitemap\Batch\Batch
   */
27
  protected $batch;
gbyte.co's avatar
gbyte.co committed
28 29 30 31

  /**
   * @var \Drupal\simple_sitemap\EntityHelper
   */
32
  protected $entityHelper;
gbyte.co's avatar
gbyte.co committed
33 34 35 36

  /**
   * @var \Drupal\Core\Database\Connection
   */
37
  protected $db;
gbyte.co's avatar
gbyte.co committed
38

gbyte.co's avatar
gbyte.co committed
39

gbyte.co's avatar
gbyte.co committed
40
  /**
gbyte.co's avatar
gbyte.co committed
41
   * @var \Drupal\Core\Language\LanguageManagerInterface
gbyte.co's avatar
gbyte.co committed
42
   */
gbyte.co's avatar
gbyte.co committed
43
  protected $languageManager;
gbyte.co's avatar
gbyte.co committed
44 45

  /**
gbyte.co's avatar
gbyte.co committed
46
   * @var \Drupal\Core\Extension\ModuleHandler
gbyte.co's avatar
gbyte.co committed
47
   */
gbyte.co's avatar
gbyte.co committed
48
  protected $moduleHandler;
gbyte.co's avatar
gbyte.co committed
49 50 51 52

  /**
   * @var string
   */
53
  protected $generateFrom = 'form';
gbyte.co's avatar
gbyte.co committed
54 55 56 57

  /**
   * @var bool
   */
58
  protected $isHreflangSitemap;
gbyte.co's avatar
gbyte.co committed
59 60 61 62

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap
   */
63
  protected $generator;
64

65 66
  /**
   * SitemapGenerator constructor.
67
   * @param \Drupal\simple_sitemap\Batch\Batch $batch
68
   * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
69 70 71
   * @param \Drupal\Core\Database\Connection $database
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
72
   */
gbyte.co's avatar
gbyte.co committed
73 74
  public function __construct(
    Batch $batch,
75
    EntityHelper $entityHelper,
gbyte.co's avatar
gbyte.co committed
76 77
    Connection $database,
    ModuleHandler $module_handler,
78
    LanguageManagerInterface $language_manager
gbyte.co's avatar
gbyte.co committed
79
  ) {
80
    $this->batch = $batch;
81
    $this->entityHelper = $entityHelper;
82
    $this->db = $database;
83
    $this->moduleHandler = $module_handler;
gbyte.co's avatar
gbyte.co committed
84 85 86 87 88 89 90 91 92 93 94 95 96
    $this->languageManager = $language_manager;
    $this->setIsHreflangSitemap();
  }

  protected function setIsHreflangSitemap() {
    $this->isHreflangSitemap = count($this->languageManager->getLanguages()) > 1;
  }

  /**
   * @return bool
   */
  public function isHreflangSitemap() {
    return $this->isHreflangSitemap;
97 98 99
  }

  /**
100
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
101 102
   * @return $this
   */
103
  public function setGenerator(Simplesitemap $generator) {
104 105
    $this->generator = $generator;
    return $this;
106 107
  }

gbyte.co's avatar
gbyte.co committed
108
  /**
109
   * @param string $from
110
   * @return $this
gbyte.co's avatar
gbyte.co committed
111
   */
112 113
  public function setGenerateFrom($from) {
    $this->generateFrom = $from;
gbyte.co's avatar
gbyte.co committed
114
    return $this;
115 116
  }

117
  /**
118
   * Adds all operations to the batch and starts it.
119
   */
120
  public function startGeneration() {
121
    $this->batch->setBatchInfo([
122
      'from' => $this->generateFrom,
123
      'batch_process_limit' => !empty($this->generator->getSetting('batch_process_limit'))
gbyte.co's avatar
gbyte.co committed
124
        ? $this->generator->getSetting('batch_process_limit') : NULL,
125 126 127
      'max_links' => $this->generator->getSetting('max_links', 2000),
      'skip_untranslated' => $this->generator->getSetting('skip_untranslated', FALSE),
      'remove_duplicates' => $this->generator->getSetting('remove_duplicates', TRUE),
128
      'entity_types' => $this->generator->getBundleSettings(),
129
      'base_url' => $this->generator->getSetting('base_url', ''),
130
    ]);
131
    // Add custom link generating operation.
132
    $this->batch->addOperation('generateCustomUrls', $this->getCustomUrlsData());
133 134

    // Add entity link generating operations.
gbyte.co's avatar
gbyte.co committed
135
    foreach ($this->getEntityTypeData() as $data) {
136
      $this->batch->addOperation('generateBundleUrls', $data);
137
    }
138
    $this->batch->start();
gbyte.co's avatar
gbyte.co committed
139 140 141
  }

  /**
142
   * Returns a batch-ready data array for custom link generation.
143
   *
gbyte.co's avatar
gbyte.co committed
144
   * @return array
gbyte.co's avatar
gbyte.co committed
145
   *   Data to be processed.
gbyte.co's avatar
gbyte.co committed
146
   */
147
  protected function getCustomUrlsData() {
148
    $paths = [];
149
    foreach ($this->generator->getCustomLinks() as $i => $custom_path) {
150 151
      $paths[$i]['path'] = $custom_path['path'];
      $paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
gbyte.co's avatar
gbyte.co committed
152 153
      // todo: implement lastmod.
      $paths[$i]['lastmod'] = NULL;
154 155
    }
    return $paths;
gbyte.co's avatar
gbyte.co committed
156
  }
157

gbyte.co's avatar
gbyte.co committed
158
  /**
159
   * Collects entity metadata for entities that are set to be indexed
160
   * and returns an array of batch-ready data sets for entity link generation.
161
   *
gbyte.co's avatar
gbyte.co committed
162
   * @return array
gbyte.co's avatar
gbyte.co committed
163
   */
164
  protected function getEntityTypeData() {
165
    $data_sets = [];
166
    $sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
167
    $entity_types = $this->generator->getBundleSettings();
gbyte.co's avatar
gbyte.co committed
168
    foreach ($entity_types as $entity_type_name => $bundles) {
169 170
      if (isset($sitemap_entity_types[$entity_type_name])) {
        $keys = $sitemap_entity_types[$entity_type_name]->getKeys();
171

gbyte.co's avatar
gbyte.co committed
172 173
        // Menu fix.
        $keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle'];
174

gbyte.co's avatar
gbyte.co committed
175
        foreach ($bundles as $bundle_name => $bundle_settings) {
176
          if ($bundle_settings['index']) {
177 178 179 180 181
            $data_sets[] = [
              'bundle_settings' => $bundle_settings,
              'bundle_name' => $bundle_name,
              'entity_type_name' => $entity_type_name,
              'keys' => $keys,
182 183
            ];
          }
gbyte.co's avatar
gbyte.co committed
184
        }
185 186
      }
    }
187
    return $data_sets;
gbyte.co's avatar
gbyte.co committed
188 189 190
  }

  /**
191 192
   * Wrapper method which takes links along with their options, lets other
   * modules alter the links and then generates and saves the sitemap.
gbyte.co's avatar
gbyte.co committed
193
   *
194
   * @param array $links
gbyte.co's avatar
gbyte.co committed
195
   *   All links with their multilingual versions and settings.
196
   * @param bool $remove_sitemap
gbyte.co's avatar
gbyte.co committed
197
   *   Remove old sitemap from database before inserting the new one.
gbyte.co's avatar
gbyte.co committed
198
   */
199
  public function generateSitemap(array $links, $remove_sitemap = FALSE) {
200
    // Invoke alter hook.
201
    $this->moduleHandler->alter('simple_sitemap_links', $links);
202

203
    $values = [
204 205 206
      'id' => $remove_sitemap ? self::FIRST_CHUNK_INDEX
        : $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')
          ->fetchField() + 1,
207
      'sitemap_string' => $this->generateSitemapChunk($links),
208
      'sitemap_created' => REQUEST_TIME,
209
    ];
210
    if ($remove_sitemap) {
211
      $this->db->truncate('simple_sitemap')->execute();
212
    }
213
    $this->db->insert('simple_sitemap')->fields($values)->execute();
214 215
  }

216
  /**
217
   * Generates and returns the sitemap index for all sitemap chunks.
218
   *
gbyte.co's avatar
gbyte.co committed
219 220
   * @param array $chunk_info
   *   Array containing chunk creation timestamps keyed by chunk ID.
221 222 223
   *
   * @return string sitemap index
   */
gbyte.co's avatar
gbyte.co committed
224
  public function generateSitemapIndex(array $chunk_info) {
225 226 227 228
    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
229
    $writer->writeComment(self::GENERATED_BY);
230 231 232
    $writer->startElement('sitemapindex');
    $writer->writeAttribute('xmlns', self::XMLNS);

gbyte.co's avatar
gbyte.co committed
233
    foreach ($chunk_info as $chunk_id => $chunk_data) {
234
      $writer->startElement('sitemap');
235
      $writer->writeElement('loc', $this->getCustomBaseUrl() . '/sitemaps/' . $chunk_id . '/' . 'sitemap.xml');
236
      $writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
237 238 239 240 241 242 243
      $writer->endElement();
    }
    $writer->endElement();
    $writer->endDocument();
    return $writer->outputMemory();
  }

244 245 246 247 248
  public function getCustomBaseUrl() {
    $customBaseUrl = $this->generator->getSetting('base_url', '');
    return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url'];
  }

249 250 251
  /**
   * Generates and returns a sitemap chunk.
   *
252
   * @param array $links
gbyte.co's avatar
gbyte.co committed
253
   *   All links with their multilingual versions and settings.
254
   *
gbyte.co's avatar
gbyte.co committed
255
   * @return string
gbyte.co's avatar
gbyte.co committed
256
   *   Sitemap chunk
257
   */
258
  protected function generateSitemapChunk(array $links) {
259 260 261 262
    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
263
    $writer->writeComment(self::GENERATED_BY);
264 265
    $writer->startElement('urlset');
    $writer->writeAttribute('xmlns', self::XMLNS);
gbyte.co's avatar
gbyte.co committed
266 267

    if ($this->isHreflangSitemap()) {
268 269
      $writer->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);
    }
270

271
    foreach ($links as $link) {
272

273
      // Add each translation variant URL as location to the sitemap.
274 275 276
      $writer->startElement('url');
      $writer->writeElement('loc', $link['url']);

277 278 279
      // 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.
gbyte.co's avatar
gbyte.co committed
280
      if ($this->isHreflangSitemap()) {
gbyte.co's avatar
gbyte.co committed
281
        foreach ($link['alternate_urls'] as $language_id => $alternate_url) {
282 283 284 285 286 287
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $alternate_url);
          $writer->endElement();
        }
288
      }
289

290 291
      // Add lastmod if any.
      if (isset($link['lastmod'])) {
292 293
        $writer->writeElement('lastmod', $link['lastmod']);
      }
294 295 296 297 298 299 300 301

      //todo: Implement changefreq here.

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

302 303
      $writer->endElement();
    }
gbyte.co's avatar
gbyte.co committed
304
    $writer->endElement();
305 306
    $writer->endDocument();
    return $writer->outputMemory();
307
  }
gbyte.co's avatar
gbyte.co committed
308

309
}