SitemapGenerator.php 4.67 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 44 45 46 47 48

  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) {
49
    $this->entity_types = is_array($entity_types) ? $entity_types : array();
50 51 52
  }

  public function set_custom_links($custom) {
53
    $this->custom = is_array($custom) ? $custom : array();
54 55
  }

56
  public function generate_sitemap($max_links = NULL) {
57 58 59

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

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    $sitemaps = array();
    if (!empty($max_links) && count($this->links) > 0) {
      foreach(array_chunk($this->links, $max_links) as $sitemap_id => $sitemap_links) {
        $sitemaps[] = $this->generate_sitemap_chunk($sitemap_links);
      }
    }
    else {
      $sitemaps[] = $this->generate_sitemap_chunk($this->links);
    }
    return $sitemaps;
  }

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

    foreach ($sitemap as $sitemap_id => $sitemap_string) {
      $writer->startElement('sitemap');
      $writer->writeElement('loc', $GLOBALS['base_url'] . '/sitemaps/' . $sitemap_id . '/' . 'sitemap.xml');
      //todo: lastmod
      $writer->endElement();
    }
    $writer->endElement();
    $writer->endDocument();
    return $writer->outputMemory();
  }

  private function generate_sitemap_chunk($sitemap_links) {
93 94 95 96 97 98 99 100 101

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

102
    foreach ($sitemap_links as $link) {
103 104 105
      $writer->startElement('url');

      // Adding url to standard language.
106
      $writer->writeElement('loc', $link['url'][$this->default_language_id]);
107 108 109 110

      // Adding alternate urls (other languages).
      if (count($link['url']) > 1) {
        foreach($link['url'] as $language_id => $localised_url) {
111 112 113 114 115
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $localised_url);
          $writer->endElement();
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        }
      }

      // Add priority.
      if (!is_null($link['priority'])) {
        $writer->writeElement('priority', $link['priority']);
      }

      // Add lastmod.
      if (!is_null($link['lastmod'])) {
        $writer->writeElement('lastmod', $link['lastmod']);
      }
      $writer->endElement();
    }
    $writer->endDocument();
    return $writer->outputMemory();
132 133 134 135 136
  }

  // Add custom links.
  private function generate_custom_links() {
    $link_generator = new CustomLinkGenerator();
137
    $links = $link_generator->get_custom_links($this->custom , $this->languages);
Pawel G's avatar
Pawel G committed
138
    $this->links = array_merge($this->links, $links);
139 140 141 142 143 144 145 146 147 148
  }

  // Add entity type links.
  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();
149
        $links = $link_generator->get_entity_links($entity_type, $bundles, $this->languages);
Pawel G's avatar
Pawel G committed
150
        $this->links = array_merge($this->links, $links);
151 152 153 154
      }
    }
  }
}