SitemapGenerator.php 6.08 KB
Newer Older
1 2 3
<?php
/**
 * @file
Pawel G's avatar
Pawel G committed
4
 * Contains \Drupal\simple_sitemap\SitemapGenerator.
5 6 7 8
 *
 * Generates a sitemap for entities and custom links.
 */

Pawel G's avatar
Pawel G committed
9
namespace Drupal\simple_sitemap;
10

11
use \XMLWriter;
12 13 14 15 16 17 18 19

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

  const XML_VERSION = '1.0';
  const ENCODING = 'UTF-8';
20 21
  const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
22

23
  private $entityTypes;
24 25
  private $custom;
  private $links;
26
  private $generatingFrom;
27

Pawel G's avatar
Pawel G committed
28
  function __construct($from = 'form') {
29
    $this->links = array();
30
    $this->generatingFrom = $from;
31
  }
32

33 34
  public function setEntityTypes($entityTypes) {
    $this->entityTypes = is_array($entityTypes) ? $entityTypes : array();
35 36
  }

37
  public function setCustomLinks($custom) {
38
    $this->custom = is_array($custom) ? $custom : array();
39 40
  }

41
  /**
42
   * Adds all operations to the batch and starts it.
43
   */
44 45 46 47
  public function startBatch() {
    $batch = new Batch($this->generatingFrom);
    $batch->addOperations('custom_paths', $this->batchAddCustomPaths());
    $batch->addOperations('entity_types', $this->batchAddEntityTypePaths());
Pawel G's avatar
Pawel G committed
48 49 50 51
    $batch->start();
  }

  /**
52 53 54
   * Returns the custom path generating operation.
   *
   * @return array $operation.
Pawel G's avatar
Pawel G committed
55
   */
56
  private function batchAddCustomPaths() {
Pawel G's avatar
Pawel G committed
57
    $link_generator = new CustomLinkGenerator();
58
    return $link_generator->getCustomPaths($this->custom);
Pawel G's avatar
Pawel G committed
59
  }
60

Pawel G's avatar
Pawel G committed
61
  /**
62 63 64 65
   * Collects the entity path generating information from all simeple_sitemap
   * plugins to be added to the batch.
   *
   * @return array $operations.
Pawel G's avatar
Pawel G committed
66
   */
67
  private function batchAddEntityTypePaths() {
68

Pawel G's avatar
Pawel G committed
69 70 71
    $manager = \Drupal::service('plugin.manager.simple_sitemap');
    $plugins = $manager->getDefinitions();
    $operations = array();
72

Pawel G's avatar
Pawel G committed
73 74
    // Let all simple_sitemap plugins add their links to the sitemap.
    foreach ($plugins as $link_generator_plugin) {
75
      if (isset($this->entityTypes[$link_generator_plugin['id']])) {
Pawel G's avatar
Pawel G committed
76
        $instance = $manager->createInstance($link_generator_plugin['id']);
77
        foreach($this->entityTypes[$link_generator_plugin['id']] as $bundle => $bundle_settings) {
Pawel G's avatar
Pawel G committed
78
          if ($bundle_settings['index']) {
79 80 81 82 83 84
            $operation['query']['query'] = $instance->getQuery($bundle);
            $operation['query']['field_info'] = $instance->getQueryInfo()['field_info'];
            $operation['entity_info']['bundle_settings'] = $bundle_settings;
            $operation['entity_info']['bundle_name'] = $bundle;
            $operation['entity_info']['bundle_entity_type'] = $link_generator_plugin['id'];
            $operation['entity_info']['entity_type_name'] = !empty($link_generator_plugin['entity_type_name']) ? $link_generator_plugin['entity_type_name'] : '';
Pawel G's avatar
Pawel G committed
85 86 87
            $operations[] = $operation;
          }
        }
88 89
      }
    }
Pawel G's avatar
Pawel G committed
90 91 92 93
    return $operations;
  }

  /**
94 95
   * 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
96
   *
97 98
   * @param array $links
   *  All links with their multilingual versions and settings.
Pawel G's avatar
Pawel G committed
99
   */
100
  public static function generateSitemap($links, $remove_sitemap = FALSE) {
101 102
    // Invoke alter hook.
    \Drupal::moduleHandler()->alter('simple_sitemap_links', $links);
Pawel G's avatar
Pawel G committed
103
    $values = array(
104
      'id' => $remove_sitemap ? 1 : db_query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
105
      'sitemap_string' => self::generateSitemapChunk($links),
Pawel G's avatar
Pawel G committed
106
      'sitemap_created' => REQUEST_TIME,
Pawel G's avatar
Pawel G committed
107
    );
108 109
    if ($remove_sitemap)
      db_truncate('simple_sitemap')->execute();
Pawel G's avatar
Pawel G committed
110
    db_insert('simple_sitemap')->fields($values)->execute();
111 112
  }

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

129
    foreach ($sitemap as $chunk_id => $chunk_data) {
130
      $writer->startElement('sitemap');
131 132
      $writer->writeElement('loc', $GLOBALS['base_url'] . '/sitemaps/'
        . $chunk_id . '/' . 'sitemap.xml');
133
      $writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
134 135 136 137 138 139 140
      $writer->endElement();
    }
    $writer->endElement();
    $writer->endDocument();
    return $writer->outputMemory();
  }

141 142 143 144
  /**
   * Generates and returns a sitemap chunk.
   *
   * @param array $sitemap_links
145
   *  All links with their multilingual versions and settings.
146 147 148
   *
   * @return string sitemap chunk
   */
149 150
  private static function generateSitemapChunk($sitemap_links) {
    $default_language_id = Simplesitemap::getDefaultLangId();
151 152 153 154 155 156 157 158 159

    $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);

160
    foreach ($sitemap_links as $link) {
161 162 163
      $writer->startElement('url');

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

166
      // Adding alternate urls (other languages) if any.
Pawel G's avatar
Pawel G committed
167 168
      if (count($link['urls']) > 1) {
        foreach($link['urls'] as $language_id => $localised_url) {
169 170 171 172 173
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $localised_url);
          $writer->endElement();
174 175 176
        }
      }

177
      // Add priority if any.
Pawel G's avatar
Pawel G committed
178
      if (isset($link['priority'])) {
179 180 181
        $writer->writeElement('priority', $link['priority']);
      }

182
      // Add lastmod if any.
Pawel G's avatar
Pawel G committed
183
      if (isset($link['lastmod'])) {
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