SitemapGenerator.php 6.27 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

Pawel G's avatar
Pawel G committed
17
  private $generator;
18
  private $links;
Pawel G's avatar
Pawel G committed
19
  private $generateFrom;
20

Pawel G's avatar
Pawel G committed
21 22
  function __construct($generator) {
    $this->generator = $generator;
Pawel G's avatar
Pawel G committed
23 24
    $this->links = [];
    $this->generateFrom = 'form';
25 26
  }

Pawel G's avatar
Pawel G committed
27 28
  public function setGenerateFrom($from) {
    $this->generateFrom = $from;
29 30
  }

31
  /**
32
   * Adds all operations to the batch and starts it.
33
   */
34 35 36
  public function startGeneration() {
    $batch = new Batch();
    $batch->setBatchInfo([
Pawel G's avatar
Pawel G committed
37
      'from' => $this->generateFrom,
Pawel G's avatar
Pawel G committed
38 39 40 41 42
      'batch_process_limit' => !empty($this->generator->getSetting('batch_process_limit'))
        ? $this->generator->getSetting('batch_process_limit') : NULL,
      'max_links' => $this->generator->getSetting('max_links'),
      'remove_duplicates' => $this->generator->getSetting('remove_duplicates'),
      'entity_types' => $this->generator->getConfig('entity_types'),
43
    ]);
Pawel G's avatar
Pawel G committed
44 45 46 47 48 49 50
    // 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
51 52 53 54
    $batch->start();
  }

  /**
Pawel G's avatar
Pawel G committed
55
   * Returns a batch-ready data array for custom link generation.
56
   *
Pawel G's avatar
Pawel G committed
57 58
   * @return array $data
   *  Data to be processed.
Pawel G's avatar
Pawel G committed
59
   */
Pawel G's avatar
Pawel G committed
60
  private function getCustomUrlsData() {
Pawel G's avatar
Pawel G committed
61
    $link_generator = new CustomLinkGenerator();
Pawel G's avatar
Pawel G committed
62
    return $link_generator->getCustomPaths($this->generator->getConfig('custom'));
Pawel G's avatar
Pawel G committed
63
  }
64

Pawel G's avatar
Pawel G committed
65
  /**
66
   * Collects entity metadata for entities that are set to be indexed
Pawel G's avatar
Pawel G committed
67
   * and returns an array of batch-ready data sets for entity link generation.
68 69
   *
   * @return array $operations.
Pawel G's avatar
Pawel G committed
70
   */
Pawel G's avatar
Pawel G committed
71 72
  private function getEntityTypeData() {
    $data_sets = [];
73
    $sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
Pawel G's avatar
Pawel G committed
74
    $entity_types = $this->generator->getConfig('entity_types');
75
    foreach($entity_types as $entity_type_name => $bundles) {
76 77 78 79
      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) {
80
          if ($bundle_settings['index']) {
Pawel G's avatar
Pawel G committed
81 82 83 84 85
            $data_sets[] = [
              'bundle_settings' => $bundle_settings,
              'bundle_name' => $bundle_name,
              'entity_type_name' => $entity_type_name,
              'keys' => $keys,
86 87
            ];
          }
Pawel G's avatar
Pawel G committed
88
        }
89 90
      }
    }
Pawel G's avatar
Pawel G committed
91
    return $data_sets;
Pawel G's avatar
Pawel G committed
92 93 94
  }

  /**
95 96
   * 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
97
   *
98 99
   * @param array $links
   *  All links with their multilingual versions and settings.
100 101
   * @param bool $remove_sitemap
   *  Remove old sitemap from database before inserting the new one.
Pawel G's avatar
Pawel G committed
102
   */
103
  public static function generateSitemap($links, $remove_sitemap = FALSE) {
104
    // Invoke alter hook.
Pawel G's avatar
Pawel G committed
105
        \Drupal::moduleHandler()->alter('simple_sitemap_links', $links);
Pawel G's avatar
Pawel G committed
106
    $values = [
107
      'id' => $remove_sitemap ? 1 : \Drupal::service('database')->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
108
      'sitemap_string' => self::generateSitemapChunk($links),
Pawel G's avatar
Pawel G committed
109
      'sitemap_created' => REQUEST_TIME,
Pawel G's avatar
Pawel G committed
110
    ];
111 112 113 114
    if ($remove_sitemap) {
      \Drupal::service('database')->truncate('simple_sitemap')->execute();
    }
    \Drupal::service('database')->insert('simple_sitemap')->fields($values)->execute();
115 116
  }

117
  /**
118
   * Generates and returns the sitemap index for all sitemap chunks.
119
   *
Pawel G's avatar
Pawel G committed
120
   * @param array $chunks
121 122 123 124
   *  All sitemap chunks keyed by the chunk ID.
   *
   * @return string sitemap index
   */
Pawel G's avatar
Pawel G committed
125
  public function generateSitemapIndex($chunks) {
126 127 128 129 130 131 132
    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
    $writer->startElement('sitemapindex');
    $writer->writeAttribute('xmlns', self::XMLNS);

Pawel G's avatar
Pawel G committed
133
    foreach ($chunks as $chunk_id => $chunk_data) {
134
      $writer->startElement('sitemap');
135 136
      $writer->writeElement('loc', $GLOBALS['base_url'] . '/sitemaps/'
        . $chunk_id . '/' . 'sitemap.xml');
137
      $writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
138 139 140 141 142 143 144
      $writer->endElement();
    }
    $writer->endElement();
    $writer->endDocument();
    return $writer->outputMemory();
  }

145 146 147
  /**
   * Generates and returns a sitemap chunk.
   *
Pawel G's avatar
Pawel G committed
148
   * @param array $links
149
   *  All links with their multilingual versions and settings.
150 151 152
   *
   * @return string sitemap chunk
   */
Pawel G's avatar
Pawel G committed
153
  private static function generateSitemapChunk($links) {
Pawel G's avatar
Pawel G committed
154
    $default_language_id = \Drupal::languageManager()->getDefaultLanguage()->getId();
155 156 157 158 159 160 161 162 163

    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
    $writer->startElement('urlset');
    $writer->writeAttribute('xmlns', self::XMLNS);
    $writer->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);

Pawel G's avatar
Pawel G committed
164
    foreach ($links as $link) {
165 166 167
      $writer->startElement('url');

      // Adding url to standard language.
Pawel G's avatar
Pawel G committed
168
      $writer->writeElement('loc', $link['urls'][$default_language_id]);
169

170
      // Adding alternate urls (other languages) if any.
Pawel G's avatar
Pawel G committed
171 172
      if (count($link['urls']) > 1) {
        foreach($link['urls'] as $language_id => $localised_url) {
173 174 175 176 177
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $localised_url);
          $writer->endElement();
178 179
        }
      }
180
      if (isset($link['priority'])) { // Add priority if any.
181 182
        $writer->writeElement('priority', $link['priority']);
      }
183
      if (isset($link['lastmod'])) { // Add lastmod if any.
184 185 186 187
        $writer->writeElement('lastmod', $link['lastmod']);
      }
      $writer->endElement();
    }
Pawel G's avatar
Pawel G committed
188
    $writer->endElement();
189 190
    $writer->endDocument();
    return $writer->outputMemory();
191 192
  }
}
193