SitemapGenerator.php 6.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @file
 * Contains \Drupal\simplesitemap\SitemapGenerator.
 *
 * Generates a sitemap for entities and custom links.
 */

namespace Drupal\simplesitemap;

11
use \XMLWriter;
12
use Drupal\Core\Url;
13 14 15 16 17 18 19 20 21 22 23

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

  const PRIORITY_DEFAULT = 0.5;
  const PRIORITY_HIGHEST = 10;
  const PRIORITY_DIVIDER = 10;
  const XML_VERSION = '1.0';
  const ENCODING = 'UTF-8';
24 25
  const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
26 27 28 29

  private $entity_types;
  private $custom;
  private $links;
30
  private $languages;
31
  private $default_language_id;
32 33 34

  function __construct() {
    $this->languages = \Drupal::languageManager()->getLanguages();
35
    $this->default_language_id = \Drupal::languageManager()->getDefaultLanguage()->getId();
36 37
    $this->links = array();
  }
38

39 40 41 42 43
  /**
   * Gets the values needed to display the priority dropdown setting.
   *
   * @return array $options
   */
44 45 46 47 48 49 50 51 52 53
  public static function get_priority_select_values() {
    $options = array();
    foreach(range(0, self::PRIORITY_HIGHEST) as $value) {
      $value = $value / self::PRIORITY_DIVIDER;
      $options[(string)$value] = (string)$value;
    }
    return $options;
  }

  public function set_entity_types($entity_types) {
54
    $this->entity_types = is_array($entity_types) ? $entity_types : array();
55 56 57
  }

  public function set_custom_links($custom) {
58
    $this->custom = is_array($custom) ? $custom : array();
59 60
  }

61 62 63 64 65 66 67 68
  /**
   * Generates and returns the sitemap.
   *
   * @param int $max_links
   *  This number dictates how many sitemap chunks are to be created.
   *
   * @return array $sitemaps.
   */
69
  public function generate_sitemap($max_links = NULL) {
70

71 72 73
    $this->generate_custom_paths();
    $this->generate_entity_paths();
    $this->generate_urls_from_paths();
74

75
    $timestamp = time();
76
    $sitemaps = array();
77 78

    // Create sitemap chunks according to the max_links setting.
79 80
    if (!empty($max_links) && count($this->links) > 0) {
      foreach(array_chunk($this->links, $max_links) as $sitemap_id => $sitemap_links) {
81 82
        $sitemaps[] = (object)[
          'sitemap_string' => $this->generate_sitemap_chunk($sitemap_links),
83
          'sitemap_created' => $timestamp,
84
        ];
85 86
      }
    }
87
    // If max_link setting is not set, create just one sitemap.
88
    else {
89 90
      $sitemaps[] = (object)[
        'sitemap_string' => $this->generate_sitemap_chunk($this->links),
91
        'sitemap_created' => $timestamp,
92
      ];
93 94 95 96
    }
    return $sitemaps;
  }

97 98 99 100 101 102 103 104 105

  /**
   * Generates multilingual urls for each path.
   */
  private function generate_urls_from_paths() {
    foreach($this->links as $i => $link) {
      foreach($this->languages as $language) {
        $this->links[$i]['url'][$language->getId()] = Url::fromUserInput('/' . $link['path'], array(
          'language' => $language,
106 107 108
          'absolute' => TRUE,
          'query' => !empty($link['options']['query']) ? $link['options']['query'] : array(),
          'fragment' => !empty($link['options']['fragment']) ? $link['options']['fragment'] : '',
109 110 111 112 113
        ))->toString();
      }
    }
  }

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

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

142 143 144 145 146 147 148 149
  /**
   * Generates and returns a sitemap chunk.
   *
   * @param array $sitemap_links
   *  All links with their translation and settings.
   *
   * @return string sitemap chunk
   */
150
  private function generate_sitemap_chunk($sitemap_links) {
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.
164
      $writer->writeElement('loc', $link['url'][$this->default_language_id]);
165

166
      // Adding alternate urls (other languages) if any.
167 168
      if (count($link['url']) > 1) {
        foreach($link['url'] 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.
178 179 180 181
      if (!is_null($link['priority'])) {
        $writer->writeElement('priority', $link['priority']);
      }

182
      // Add lastmod if any.
183 184 185 186 187 188 189
      if (!is_null($link['lastmod'])) {
        $writer->writeElement('lastmod', $link['lastmod']);
      }
      $writer->endElement();
    }
    $writer->endDocument();
    return $writer->outputMemory();
190 191
  }

192
  /**
193
   * Generates custom internal paths.
194
   */
195
  private function generate_custom_paths() {
196
    $link_generator = new CustomLinkGenerator();
197 198
    $links = $link_generator->get_custom_paths($this->custom);
    $this->add_created_paths($links);
199 200
  }

201
  /**
202
   * Makes all entity type link generating plugins add their paths.
203
   */
204
  private function generate_entity_paths() {
205 206 207 208 209 210 211

    $manager = \Drupal::service('plugin.manager.simplesitemap');
    $plugins = $manager->getDefinitions();

    foreach ($plugins as $link_generator_plugin) {
      if (isset($this->entity_types[$link_generator_plugin['id']])) {
        $instance = $manager->createInstance($link_generator_plugin['id']);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        $links = $instance->get_entity_paths($link_generator_plugin['id'],
          $this->entity_types[$link_generator_plugin['id']]);
        $this->add_created_paths($links);
      }
    }
  }

  /**
   * Adds Drupal internal paths generated by a plugin while removing duplicates.
   *
   * @param array $paths
   *  Drupal internal paths generated by a plugin.
   */
  private function add_created_paths($paths) {
    foreach($paths as $i => $path) {
      foreach($this->links as $existing_path) {
        if ($path['path'] == $existing_path['path']) {
          unset($paths[$i]);
        }
231 232
      }
    }
233
    $this->links = array_merge($this->links, $paths);
234 235
  }
}