SitemapGenerator.php 2.88 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;
Pawel G's avatar
Pawel G committed
12
use \Drupal\Component\Utility\Html;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

/**
 * 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';
  const XMLNS_URL = 'http://www.sitemaps.org/schemas/sitemap/0.9';

  private $entity_types;
  private $custom;
  private $language;
  private $links;

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

40
  public static function add_xml_link_markup($url, $priority = NULL, $lastmod = NULL) {
Pawel G's avatar
Pawel G committed
41
    return "<url><loc>" . Html::escape($url) . "</loc>"
42 43 44
    . (!is_null($priority) ? ("<priority>" . $priority . "</priority>") : '')
    . (!is_null($lastmod) ? ("<lastmod>" . $lastmod . "</lastmod>") : '')
    . "</url>";
45 46 47
  }

  public function set_entity_types($entity_types) {
48
    $this->entity_types = is_array($entity_types) ? $entity_types : array();
49 50 51
  }

  public function set_custom_links($custom) {
52
    $this->custom = is_array($custom) ? $custom : array();
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  }

  public function set_sitemap_lang($language) {

    // Reset links array to make space for a sitemap with a different language.
    $this->links = array();

    $this->language = $language;
  }

  public function generate_sitemap() {

    $this->generate_custom_links();
    $this->generate_entity_links();
    $sitemap = implode($this->links);
    return $this->add_xml_sitemap_markup($sitemap);
  }

  // Add custom links.
  private function generate_custom_links() {
    $link_generator = new CustomLinkGenerator();
    $links = $link_generator->get_custom_links($this->custom , $this->language);
Pawel G's avatar
Pawel G committed
75
    $this->links = array_merge($this->links, $links);
76 77 78 79 80 81 82 83 84 85 86
  }

  // 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();
        $links = $link_generator->get_entity_links($entity_type, $bundles, $this->language);
Pawel G's avatar
Pawel G committed
87
        $this->links = array_merge($this->links, $links);
88 89 90 91 92 93 94 95 96
      }
    }
  }

  // Add sitemap markup.
  private function add_xml_sitemap_markup($sitemap) {
    return "<?xml version=\"" . self::XML_VERSION . "\" encoding=\"" . self::ENCODING . "\"?><urlset xmlns=\"" . self::XMLNS_URL . "\">" . $sitemap  . "</urlset>";
  }
}