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

namespace Drupal\simplesitemap;

use Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator;
12
use \XMLWriter;
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

    $this->generate_custom_links();
    $this->generate_entity_links();
73

74
    $timestamp = time();
75 76 77
    $sitemaps = array();
    if (!empty($max_links) && count($this->links) > 0) {
      foreach(array_chunk($this->links, $max_links) as $sitemap_id => $sitemap_links) {
78 79 80 81
        $sitemaps[] = (object)[
          'sitemap_string' => $this->generate_sitemap_chunk($sitemap_links),
          'generated' => $timestamp,
        ];
82 83 84
      }
    }
    else {
85 86 87 88
      $sitemaps[] = (object)[
        'sitemap_string' => $this->generate_sitemap_chunk($this->links),
        'generated' => $timestamp,
      ];
89 90 91 92
    }
    return $sitemaps;
  }

93 94 95 96 97 98 99 100
  /**
   * Generates and returns the sitemap index.
   *
   * @param array $sitemap
   *  All sitemap chunks keyed by the chunk ID.
   *
   * @return string sitemap index
   */
101 102 103 104 105 106 107 108
  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);

109
    foreach ($sitemap as $chunk_id => $chunk_data) {
110
      $writer->startElement('sitemap');
111 112 113
      $writer->writeElement('loc', $GLOBALS['base_url'] . '/sitemaps/'
        . $chunk_id . '/' . 'sitemap.xml');
      $writer->writeElement('lastmod', date_iso8601($chunk_data->generated));
114 115 116 117 118 119 120
      $writer->endElement();
    }
    $writer->endElement();
    $writer->endDocument();
    return $writer->outputMemory();
  }

121 122 123 124 125 126 127 128
  /**
   * Generates and returns a sitemap chunk.
   *
   * @param array $sitemap_links
   *  All links with their translation and settings.
   *
   * @return string sitemap chunk
   */
129
  private function generate_sitemap_chunk($sitemap_links) {
130 131 132 133 134 135 136 137 138

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

139
    foreach ($sitemap_links as $link) {
140 141 142
      $writer->startElement('url');

      // Adding url to standard language.
143
      $writer->writeElement('loc', $link['url'][$this->default_language_id]);
144

145
      // Adding alternate urls (other languages) if any.
146 147
      if (count($link['url']) > 1) {
        foreach($link['url'] as $language_id => $localised_url) {
148 149 150 151 152
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $localised_url);
          $writer->endElement();
153 154 155
        }
      }

156
      // Add priority if any.
157 158 159 160
      if (!is_null($link['priority'])) {
        $writer->writeElement('priority', $link['priority']);
      }

161
      // Add lastmod if any.
162 163 164 165 166 167 168
      if (!is_null($link['lastmod'])) {
        $writer->writeElement('lastmod', $link['lastmod']);
      }
      $writer->endElement();
    }
    $writer->endDocument();
    return $writer->outputMemory();
169 170
  }

171 172 173
  /**
   * Gets custom links.
   */
174 175
  private function generate_custom_links() {
    $link_generator = new CustomLinkGenerator();
176
    $links = $link_generator->get_custom_links($this->custom , $this->languages);
177
    $this->links = array_merge($this->links, $links);
178 179
  }

180 181 182
  /**
   * Gets entity type links.
   */
183 184 185 186 187 188 189
  private function generate_entity_links() {
    foreach($this->entity_types as $entity_type => $bundles) {
      $class_path = Simplesitemap::get_plugin_path($entity_type);
      if ($class_path !== FALSE) {
        require_once $class_path;
        $class_name = "Drupal\\simplesitemap\\LinkGenerators\\EntityTypeLinkGenerators\\$entity_type";
        $link_generator = new $class_name();
190
        $links = $link_generator->get_entity_links($entity_type, $bundles, $this->languages);
191
        $this->links = array_merge($this->links, $links);
192 193 194 195
      }
    }
  }
}