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

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

5
use \XMLWriter;
6 7

/**
gbyte.co's avatar
gbyte.co committed
8 9
 * Class SitemapGenerator
 * @package Drupal\simple_sitemap
10 11 12 13 14
 */
class SitemapGenerator {

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

19
  private $generator;
20 21 22 23
  private $db;
  private $moduleHandler;
  private $defaultLanguageId;
  private $generateFrom = 'form';
24
  private $isHreflangSitemap;
25

26
  public function __construct($generator, $database, $language_manager, $module_handler) {
27
    $this->generator = $generator;
28 29
    $this->db = $database;
    $this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
30
    $this->isHreflangSitemap = count($language_manager->getLanguages()) > 1;
31
    $this->moduleHandler = $module_handler;
32 33
  }

34 35
  public function setGenerateFrom($from) {
    $this->generateFrom = $from;
gbyte.co's avatar
gbyte.co committed
36
    return $this;
37 38
  }

39
  /**
40
   * Adds all operations to the batch and starts it.
41
   */
42 43 44
  public function startGeneration() {
    $batch = new Batch();
    $batch->setBatchInfo([
45
      'from' => $this->generateFrom,
46 47
      'batch_process_limit' => !empty($this->generator->getSetting('batch_process_limit'))
        ? $this->generator->getSetting('batch_process_limit') : NULL,
48 49 50
      'max_links' => $this->generator->getSetting('max_links', 2000),
      'skip_untranslated' => $this->generator->getSetting('skip_untranslated', FALSE),
      'remove_duplicates' => $this->generator->getSetting('remove_duplicates', TRUE),
51
      'entity_types' => $this->generator->getConfig('entity_types'),
52
    ]);
53 54 55 56 57 58 59
    // Add custom link generating operation.
    $batch->addOperation('generateCustomUrls', $this->getCustomUrlsData());

    // Add entity link generating operations.
    foreach($this->getEntityTypeData() as $data) {
      $batch->addOperation('generateBundleUrls', $data);
    }
gbyte.co's avatar
gbyte.co committed
60 61 62 63
    $batch->start();
  }

  /**
64
   * Returns a batch-ready data array for custom link generation.
65
   *
gbyte.co's avatar
gbyte.co committed
66
   * @return array
67
   *  Data to be processed.
gbyte.co's avatar
gbyte.co committed
68
   */
69
  private function getCustomUrlsData() {
70 71 72 73 74 75 76
    $paths = [];
    foreach($this->generator->getConfig('custom') as $i => $custom_path) {
      $paths[$i]['path'] = $custom_path['path'];
      $paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
      $paths[$i]['lastmod'] = NULL; //todo: implement lastmod
    }
    return $paths;
gbyte.co's avatar
gbyte.co committed
77
  }
78

gbyte.co's avatar
gbyte.co committed
79
  /**
80
   * Collects entity metadata for entities that are set to be indexed
81
   * and returns an array of batch-ready data sets for entity link generation.
82
   *
gbyte.co's avatar
gbyte.co committed
83
   * @return array
gbyte.co's avatar
gbyte.co committed
84
   */
85 86
  private function getEntityTypeData() {
    $data_sets = [];
87
    $sitemap_entity_types = $this->generator->getSitemapEntityTypes();
88
    $entity_types = $this->generator->getConfig('entity_types');
89
    foreach($entity_types as $entity_type_name => $bundles) {
90 91 92 93
      if (isset($sitemap_entity_types[$entity_type_name])) {
        $keys = $sitemap_entity_types[$entity_type_name]->getKeys();
        $keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle']; // Menu fix.
        foreach($bundles as $bundle_name => $bundle_settings) {
94
          if ($bundle_settings['index']) {
95 96 97 98 99
            $data_sets[] = [
              'bundle_settings' => $bundle_settings,
              'bundle_name' => $bundle_name,
              'entity_type_name' => $entity_type_name,
              'keys' => $keys,
100 101
            ];
          }
gbyte.co's avatar
gbyte.co committed
102
        }
103 104
      }
    }
105
    return $data_sets;
gbyte.co's avatar
gbyte.co committed
106 107 108
  }

  /**
109 110
   * 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
111
   *
112 113
   * @param array $links
   *  All links with their multilingual versions and settings.
114 115
   * @param bool $remove_sitemap
   *  Remove old sitemap from database before inserting the new one.
gbyte.co's avatar
gbyte.co committed
116
   */
117
  public function generateSitemap($links, $remove_sitemap = FALSE) {
118
    // Invoke alter hook.
119
    $this->moduleHandler->alter('simple_sitemap_links', $links);
120
    $values = [
121 122
      'id' => $remove_sitemap ? 1 : $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
      'sitemap_string' => $this->generateSitemapChunk($links),
gbyte.co's avatar
gbyte.co committed
123
      'sitemap_created' => REQUEST_TIME,
124
    ];
125
    if ($remove_sitemap) {
126
      $this->db->truncate('simple_sitemap')->execute();
127
    }
128
    $this->db->insert('simple_sitemap')->fields($values)->execute();
129 130
  }

131
  /**
132
   * Generates and returns the sitemap index for all sitemap chunks.
133
   *
134
   * @param array $chunks
135 136 137 138
   *  All sitemap chunks keyed by the chunk ID.
   *
   * @return string sitemap index
   */
139
  public function generateSitemapIndex($chunks) {
140 141 142 143
    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
144
    $writer->writeComment(self::GENERATED_BY);
145 146 147
    $writer->startElement('sitemapindex');
    $writer->writeAttribute('xmlns', self::XMLNS);

148
    foreach ($chunks as $chunk_id => $chunk_data) {
149
      $writer->startElement('sitemap');
150 151
      $writer->writeElement('loc', $GLOBALS['base_url'] . '/sitemaps/'
        . $chunk_id . '/' . 'sitemap.xml');
152
      $writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
153 154 155 156 157 158 159
      $writer->endElement();
    }
    $writer->endElement();
    $writer->endDocument();
    return $writer->outputMemory();
  }

160 161 162
  /**
   * Generates and returns a sitemap chunk.
   *
163
   * @param array $links
164
   *  All links with their multilingual versions and settings.
165
   *
gbyte.co's avatar
gbyte.co committed
166 167
   * @return string
   *  Sitemap chunk
168
   */
169
  private function generateSitemapChunk($links) {
170 171 172 173
    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
174
    $writer->writeComment(self::GENERATED_BY);
175 176
    $writer->startElement('urlset');
    $writer->writeAttribute('xmlns', self::XMLNS);
177 178 179
    if ($this->isHreflangSitemap) {
      $writer->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);
    }
180

181
    foreach ($links as $link) {
182

183
      // Add each translation variant URL as location to the sitemap.
184 185 186
      $writer->startElement('url');
      $writer->writeElement('loc', $link['url']);

187 188 189 190 191 192 193 194 195 196 197
      // 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) {
        foreach($link['alternate_urls'] as $language_id => $alternate_url) {
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $alternate_url);
          $writer->endElement();
        }
198
      }
199 200
      // Add priority if any.
      if (isset($link['priority'])) {
201 202
        $writer->writeElement('priority', $link['priority']);
      }
203 204
      // Add lastmod if any.
      if (isset($link['lastmod'])) {
205 206 207 208
        $writer->writeElement('lastmod', $link['lastmod']);
      }
      $writer->endElement();
    }
gbyte.co's avatar
gbyte.co committed
209
    $writer->endElement();
210 211
    $writer->endDocument();
    return $writer->outputMemory();
212 213
  }
}