SitemapGenerator.php 8.41 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;
Pawel G's avatar
Pawel G committed
10
use Drupal\Component\Datetime\Time;
11 12

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

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

25
  private $batch;
26 27 28 29
  private $db;
  private $moduleHandler;
  private $defaultLanguageId;
  private $generateFrom = 'form';
30
  private $isHreflangSitemap;
31
  private $generator;
Pawel G's avatar
Pawel G committed
32
  private $time;
33

34 35
  /**
   * SitemapGenerator constructor.
Pawel G's avatar
Pawel G committed
36 37 38 39 40
   * @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
   * @param \Drupal\Component\Datetime\Time $time
41
   */
Pawel G's avatar
Pawel G committed
42 43 44 45
  public function __construct(
    Batch $batch,
    Connection $database,
    ModuleHandler $module_handler,
Pawel G's avatar
Pawel G committed
46 47
    LanguageManagerInterface $language_manager,
    Time $time
Pawel G's avatar
Pawel G committed
48
  ) {
49
    $this->batch = $batch;
50
    $this->db = $database;
51
    $this->moduleHandler = $module_handler;
52
    $this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
53
    $this->isHreflangSitemap = count($language_manager->getLanguages()) > 1;
Pawel G's avatar
Pawel G committed
54
    $this->time = $time;
55 56 57
  }

  /**
Pawel G's avatar
Pawel G committed
58
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
59 60
   * @return $this
   */
Pawel G's avatar
Pawel G committed
61
  public function setGenerator(Simplesitemap $generator) {
62 63
    $this->generator = $generator;
    return $this;
64 65
  }

Pawel G's avatar
Pawel G committed
66
  /**
Pawel G's avatar
Pawel G committed
67
   * @param string $from
68
   * @return $this
Pawel G's avatar
Pawel G committed
69
   */
Pawel G's avatar
Pawel G committed
70 71
  public function setGenerateFrom($from) {
    $this->generateFrom = $from;
Pawel G's avatar
Pawel G committed
72
    return $this;
73 74
  }

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

    // Add entity link generating operations.
Pawel G's avatar
Pawel G committed
93
    foreach ($this->getEntityTypeData() as $data) {
94
      $this->batch->addOperation('generateBundleUrls', $data);
Pawel G's avatar
Pawel G committed
95
    }
96
    $this->batch->start();
Pawel G's avatar
Pawel G committed
97 98 99
  }

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

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

Pawel G's avatar
Pawel G committed
130 131
        // Menu fix.
        $keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle'];
Pawel G's avatar
Pawel G committed
132

Pawel G's avatar
Pawel G committed
133
        foreach ($bundles as $bundle_name => $bundle_settings) {
134
          if ($bundle_settings['index']) {
Pawel G's avatar
Pawel G committed
135 136 137 138 139
            $data_sets[] = [
              'bundle_settings' => $bundle_settings,
              'bundle_name' => $bundle_name,
              'entity_type_name' => $entity_type_name,
              'keys' => $keys,
140 141
            ];
          }
Pawel G's avatar
Pawel G committed
142
        }
143 144
      }
    }
Pawel G's avatar
Pawel G committed
145
    return $data_sets;
Pawel G's avatar
Pawel G committed
146 147 148
  }

  /**
149 150
   * 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
151
   *
152
   * @param array $links
Pawel G's avatar
Pawel G committed
153
   *   All links with their multilingual versions and settings.
154
   * @param bool $remove_sitemap
Pawel G's avatar
Pawel G committed
155
   *   Remove old sitemap from database before inserting the new one.
Pawel G's avatar
Pawel G committed
156
   */
Pawel G's avatar
Pawel G committed
157
  public function generateSitemap(array $links, $remove_sitemap = FALSE) {
158
    // Invoke alter hook.
159
    $this->moduleHandler->alter('simple_sitemap_links', $links);
Pawel G's avatar
Pawel G committed
160

Pawel G's avatar
Pawel G committed
161
    $values = [
162 163
      'id' => $remove_sitemap ? 1 : $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
      'sitemap_string' => $this->generateSitemapChunk($links),
Pawel G's avatar
Pawel G committed
164
      'sitemap_created' => $this->time->getRequestTime(),
Pawel G's avatar
Pawel G committed
165
    ];
166
    if ($remove_sitemap) {
167
      $this->db->truncate('simple_sitemap')->execute();
168
    }
169
    $this->db->insert('simple_sitemap')->fields($values)->execute();
170 171
  }

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

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

200 201 202 203 204
  public function getCustomBaseUrl() {
    $customBaseUrl = $this->generator->getSetting('base_url', '');
    return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url'];
  }

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

Pawel G's avatar
Pawel G committed
226
    foreach ($links as $link) {
227

228
      // Add each translation variant URL as location to the sitemap.
229 230 231
      $writer->startElement('url');
      $writer->writeElement('loc', $link['url']);

232 233 234 235
      // 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
236
        foreach ($link['alternate_urls'] as $language_id => $alternate_url) {
237 238 239 240 241 242
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $alternate_url);
          $writer->endElement();
        }
243
      }
244

245 246
      // Add lastmod if any.
      if (isset($link['lastmod'])) {
247 248
        $writer->writeElement('lastmod', $link['lastmod']);
      }
249 250 251 252 253 254 255 256

      //todo: Implement changefreq here.

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

257 258
      $writer->endElement();
    }
Pawel G's avatar
Pawel G committed
259
    $writer->endElement();
260 261
    $writer->endDocument();
    return $writer->outputMemory();
262
  }
Pawel G's avatar
Pawel G committed
263

264
}