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

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

5
use \XMLWriter;
6 7 8 9 10 11 12 13

/**
 * SitemapGenerator class.
 */
class SitemapGenerator {

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

Pawel G's avatar
Pawel G committed
18
  private $generator;
19 20 21 22
  private $db;
  private $moduleHandler;
  private $defaultLanguageId;
  private $generateFrom = 'form';
23

24
  function __construct($generator, $database, $language_manager, $module_handler) {
Pawel G's avatar
Pawel G committed
25
    $this->generator = $generator;
26 27 28
    $this->db = $database;
    $this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
    $this->moduleHandler = $module_handler;
29 30
  }

Pawel G's avatar
Pawel G committed
31 32
  public function setGenerateFrom($from) {
    $this->generateFrom = $from;
Pawel G's avatar
Pawel G committed
33
    return $this;
34 35
  }

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

    // Add entity link generating operations.
    foreach($this->getEntityTypeData() as $data) {
      $batch->addOperation('generateBundleUrls', $data);
    }
Pawel G's avatar
Pawel G committed
57 58 59 60
    $batch->start();
  }

  /**
Pawel G's avatar
Pawel G committed
61
   * Returns a batch-ready data array for custom link generation.
62
   *
Pawel G's avatar
Pawel G committed
63
   * @return array
Pawel G's avatar
Pawel G committed
64
   *  Data to be processed.
Pawel G's avatar
Pawel G committed
65
   */
Pawel G's avatar
Pawel G committed
66
  private function getCustomUrlsData() {
67 68 69 70 71 72 73
    $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;
Pawel G's avatar
Pawel G committed
74
  }
75

Pawel G's avatar
Pawel G committed
76
  /**
77
   * Collects entity metadata for entities that are set to be indexed
Pawel G's avatar
Pawel G committed
78
   * and returns an array of batch-ready data sets for entity link generation.
79
   *
Pawel G's avatar
Pawel G committed
80
   * @return array
Pawel G's avatar
Pawel G committed
81
   */
Pawel G's avatar
Pawel G committed
82 83
  private function getEntityTypeData() {
    $data_sets = [];
84
    $sitemap_entity_types = $this->generator->getSitemapEntityTypes();
Pawel G's avatar
Pawel G committed
85
    $entity_types = $this->generator->getConfig('entity_types');
86
    foreach($entity_types as $entity_type_name => $bundles) {
87 88 89 90
      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) {
91
          if ($bundle_settings['index']) {
Pawel G's avatar
Pawel G committed
92 93 94 95 96
            $data_sets[] = [
              'bundle_settings' => $bundle_settings,
              'bundle_name' => $bundle_name,
              'entity_type_name' => $entity_type_name,
              'keys' => $keys,
97 98
            ];
          }
Pawel G's avatar
Pawel G committed
99
        }
100 101
      }
    }
Pawel G's avatar
Pawel G committed
102
    return $data_sets;
Pawel G's avatar
Pawel G committed
103 104 105
  }

  /**
106 107
   * 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
108
   *
109 110
   * @param array $links
   *  All links with their multilingual versions and settings.
111 112
   * @param bool $remove_sitemap
   *  Remove old sitemap from database before inserting the new one.
Pawel G's avatar
Pawel G committed
113
   */
114
  public function generateSitemap($links, $remove_sitemap = FALSE) {
115
    // Invoke alter hook.
116
    $this->moduleHandler->alter('simple_sitemap_links', $links);
Pawel G's avatar
Pawel G committed
117
    $values = [
118 119
      '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
120
      'sitemap_created' => REQUEST_TIME,
Pawel G's avatar
Pawel G committed
121
    ];
122
    if ($remove_sitemap) {
123
      $this->db->truncate('simple_sitemap')->execute();
124
    }
125
    $this->db->insert('simple_sitemap')->fields($values)->execute();
126 127
  }

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

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

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

Pawel G's avatar
Pawel G committed
176
    foreach ($links as $link) {
177

178 179 180 181 182 183 184 185 186 187 188
      // Add each translation variant URL to the sitemap.
      $writer->startElement('url');
      $writer->writeElement('loc', $link['url']);

      // Add all alternate URLs to this translation variant.
      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();
189
      }
190
      if (isset($link['priority'])) { // Add priority if any.
191 192
        $writer->writeElement('priority', $link['priority']);
      }
193
      if (isset($link['lastmod'])) { // Add lastmod if any.
194 195 196 197
        $writer->writeElement('lastmod', $link['lastmod']);
      }
      $writer->endElement();
    }
Pawel G's avatar
Pawel G committed
198
    $writer->endElement();
199 200
    $writer->endDocument();
    return $writer->outputMemory();
201 202
  }
}
203