SitemapGenerator.php 5.9 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
   * Adds all operations to the batch and starts it.
60
   */
Pawel G's avatar
Pawel G committed
61 62 63 64 65 66 67 68
  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();
  }

  /**
69 70 71
   * Returns the custom path generating operation.
   *
   * @return array $operation.
Pawel G's avatar
Pawel G committed
72 73 74 75 76
   */
  private function batch_add_custom_paths() {
    $link_generator = new CustomLinkGenerator();
    return $link_generator->get_custom_paths($this->custom);
  }
77

Pawel G's avatar
Pawel G committed
78
  /**
79 80 81 82
   * Collects the entity path generating information from all simeple_sitemap
   * plugins to be added to the batch.
   *
   * @return array $operations.
Pawel G's avatar
Pawel G committed
83 84
   */
  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
    return $operations;
  }

  /**
107 108
   * Wrapper method which takes links along with their options and then
   * generates and saves the sitemap.
Pawel G's avatar
Pawel G committed
109
   *
110 111
   * @param array $links
   *  All links with their multilingual versions and settings.
Pawel G's avatar
Pawel G committed
112 113 114 115 116 117 118
   */
  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)
    );
119 120
  }

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

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

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

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

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

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

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

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

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