SitemapGenerator.php 5.99 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
    // 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']) {
96 97
            $operation['info'] = $instance->getInfo();
            $operation['query'] = $instance->getQuery($bundle);
Pawel G's avatar
Pawel G committed
98 99 100 101
            $operation['info']['bundle_settings'] = $bundle_settings;
            $operations[] = $operation;
          }
        }
102 103
      }
    }
Pawel G's avatar
Pawel G committed
104 105 106 107
    return $operations;
  }

  /**
108 109
   * Wrapper method which takes links along with their options and then
   * generates and saves the sitemap.
Pawel G's avatar
Pawel G committed
110
   *
111 112
   * @param array $links
   *  All links with their multilingual versions and settings.
Pawel G's avatar
Pawel G committed
113 114
   */
  public static function generate_sitemap($links) {
Pawel G's avatar
Pawel G committed
115 116 117 118
    $values = array(
      'id' => db_query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
      'sitemap_string' => self::generate_sitemap_chunk($links),
      'sitemap_created' => REQUEST_TIME,
Pawel G's avatar
Pawel G committed
119
    );
Pawel G's avatar
Pawel G committed
120
    db_insert('simple_sitemap')->fields($values)->execute();
121 122
  }

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

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

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

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

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

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

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

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

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