SitemapGenerator.php 5.83 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 20 21 22

/**
 * 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';
23 24
  const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
25 26 27 28

  private $entity_types;
  private $custom;
  private $links;
Pawel G's avatar
Pawel G committed
29
  private $generating_from;
30

Pawel G's avatar
Pawel G committed
31
  function __construct($from = 'form') {
32
    $this->links = array();
Pawel G's avatar
Pawel G committed
33
    $this->generating_from = $from;
34
  }
35

36 37 38 39 40
  /**
   * Gets the values needed to display the priority dropdown setting.
   *
   * @return array $options
   */
41 42 43 44 45 46 47 48 49 50
  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) {
51
    $this->entity_types = is_array($entity_types) ? $entity_types : array();
52 53 54
  }

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

58 59 60 61 62 63 64 65
  /**
   * Generates and returns the sitemap.
   *
   * @param int $max_links
   *  This number dictates how many sitemap chunks are to be created.
   *
   * @return array $sitemaps.
   */
Pawel G's avatar
Pawel G committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79
  public function start_batch() {
    $batch = new Batch($this->generating_from);
    $batch->add_operations('custom_paths', $this->batch_add_custom_paths());
    $batch->add_operations('entity_types', $this->batch_add_entity_type_paths());
    $batch->start();
  }

  /**
   * Generates custom internal paths.
   */
  private function batch_add_custom_paths() {
    $link_generator = new CustomLinkGenerator();
    return $link_generator->get_custom_paths($this->custom);
  }
80

Pawel G's avatar
Pawel G committed
81 82 83 84
  /**
   * Lets all simple sitemap plugins add their paths to the sitemap.
   */
  private function batch_add_entity_type_paths() {
85

Pawel G's avatar
Pawel G committed
86 87 88
    $manager = \Drupal::service('plugin.manager.simple_sitemap');
    $plugins = $manager->getDefinitions();
    $operations = array();
89

Pawel G's avatar
Pawel G committed
90 91 92 93 94 95 96 97 98 99 100
    // Let all simple_sitemap plugins add their links to the sitemap.
    foreach ($plugins as $link_generator_plugin) {
      if (isset($this->entity_types[$link_generator_plugin['id']])) {
        $instance = $manager->createInstance($link_generator_plugin['id']);
        foreach($this->entity_types[$link_generator_plugin['id']] as $bundle => $bundle_settings) {
          if ($bundle_settings['index']) {
            $operation = $instance->get_entities_of_bundle($bundle);
            $operation['info']['bundle_settings'] = $bundle_settings;
            $operations[] = $operation;
          }
        }
101 102
      }
    }
Pawel G's avatar
Pawel G committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    return $operations;
  }

  /**
   * Generates and returns the sitemap.
   *
   * @param int $max_links
   *  This number dictates how many sitemap chunks are to be created.
   *
   * @return array $sitemaps.
   */
  public static function generate_sitemap($links) {
    Simplesitemap::save_sitemap(array(
        'id' => db_query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
        'sitemap_string' => self::generate_sitemap_chunk($links),
        'sitemap_created' => REQUEST_TIME)
    );
120 121
  }

122 123 124 125 126 127 128 129
  /**
   * Generates and returns the sitemap index.
   *
   * @param array $sitemap
   *  All sitemap chunks keyed by the chunk ID.
   *
   * @return string sitemap index
   */
130 131 132 133 134 135 136 137
  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);

138
    foreach ($sitemap as $chunk_id => $chunk_data) {
139
      $writer->startElement('sitemap');
140 141
      $writer->writeElement('loc', $GLOBALS['base_url'] . '/sitemaps/'
        . $chunk_id . '/' . 'sitemap.xml');
142
      $writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
143 144 145 146 147 148 149
      $writer->endElement();
    }
    $writer->endElement();
    $writer->endDocument();
    return $writer->outputMemory();
  }

150 151 152 153 154 155 156 157
  /**
   * Generates and returns a sitemap chunk.
   *
   * @param array $sitemap_links
   *  All links with their translation and settings.
   *
   * @return string sitemap chunk
   */
Pawel G's avatar
Pawel G committed
158 159
  private static function generate_sitemap_chunk($sitemap_links) {
    $default_language_id = Simplesitemap::get_default_lang_id();
160 161 162 163 164 165 166 167 168

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

169
    foreach ($sitemap_links as $link) {
170 171 172
      $writer->startElement('url');

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

175
      // Adding alternate urls (other languages) if any.
Pawel G's avatar
Pawel G committed
176 177
      if (count($link['urls']) > 1) {
        foreach($link['urls'] as $language_id => $localised_url) {
178 179 180 181 182
          $writer->startElement('xhtml:link');
          $writer->writeAttribute('rel', 'alternate');
          $writer->writeAttribute('hreflang', $language_id);
          $writer->writeAttribute('href', $localised_url);
          $writer->endElement();
183 184 185
        }
      }

186
      // Add priority if any.
Pawel G's avatar
Pawel G committed
187
      if (isset($link['priority'])) {
188 189 190
        $writer->writeElement('priority', $link['priority']);
      }

191
      // Add lastmod if any.
Pawel G's avatar
Pawel G committed
192
      if (isset($link['lastmod'])) {
193 194 195 196
        $writer->writeElement('lastmod', $link['lastmod']);
      }
      $writer->endElement();
    }
Pawel G's avatar
Pawel G committed
197
    $writer->endElement();
198 199
    $writer->endDocument();
    return $writer->outputMemory();
200 201
  }
}