3 namespace Drupal\simple_sitemap;
6 use Drupal\simple_sitemap\Batch\Batch;
7 use Drupal\Core\Database\Connection;
8 use Drupal\Core\Extension\ModuleHandler;
9 use Drupal\Core\Language\LanguageManagerInterface;
12 * Class SitemapGenerator
13 * @package Drupal\simple_sitemap
15 class SitemapGenerator {
17 const XML_VERSION = '1.0';
18 const ENCODING = 'UTF-8';
19 const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
20 const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
21 const GENERATED_BY = 'Generated by the Simple XML sitemap Drupal module: https://drupal.org/project/simple_sitemap.';
22 const FIRST_CHUNK_INDEX = 1;
25 * @var \Drupal\simple_sitemap\Batch\Batch
30 * @var \Drupal\simple_sitemap\EntityHelper
32 protected $entityHelper;
35 * @var \Drupal\Core\Database\Connection
41 * @var \Drupal\Core\Language\LanguageManagerInterface
43 protected $languageManager;
46 * @var \Drupal\Core\Extension\ModuleHandler
48 protected $moduleHandler;
53 protected $generateFrom = 'form';
58 protected $isHreflangSitemap;
61 * @var \Drupal\simple_sitemap\Simplesitemap
66 * SitemapGenerator constructor.
67 * @param \Drupal\simple_sitemap\Batch\Batch $batch
68 * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
69 * @param \Drupal\Core\Database\Connection $database
70 * @param \Drupal\Core\Extension\ModuleHandler $module_handler
71 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
73 public function __construct(
75 EntityHelper $entityHelper,
77 ModuleHandler $module_handler,
78 LanguageManagerInterface $language_manager
80 $this->batch = $batch;
81 $this->entityHelper = $entityHelper;
82 $this->db = $database;
83 $this->moduleHandler = $module_handler;
84 $this->languageManager = $language_manager;
85 $this->setIsHreflangSitemap();
88 protected function setIsHreflangSitemap() {
89 $this->isHreflangSitemap = count($this->languageManager->getLanguages()) > 1;
95 public function isHreflangSitemap() {
96 return $this->isHreflangSitemap;
100 * @param \Drupal\simple_sitemap\Simplesitemap $generator
103 public function setGenerator(Simplesitemap $generator) {
104 $this->generator = $generator;
109 * @param string $from
112 public function setGenerateFrom($from) {
113 $this->generateFrom = $from;
118 * Adds all operations to the batch and starts it.
120 public function startGeneration() {
121 $this->batch->setBatchInfo([
122 'from' => $this->generateFrom,
123 'batch_process_limit' => !empty($this->generator->getSetting('batch_process_limit'))
124 ? $this->generator->getSetting('batch_process_limit') : NULL,
125 'max_links' => $this->generator->getSetting('max_links', 2000),
126 'skip_untranslated' => $this->generator->getSetting('skip_untranslated', FALSE),
127 'remove_duplicates' => $this->generator->getSetting('remove_duplicates', TRUE),
128 'entity_types' => $this->generator->getBundleSettings(),
129 'base_url' => $this->generator->getSetting('base_url', ''),
131 // Add custom link generating operation.
132 $this->batch->addOperation('generateCustomUrls', $this->getCustomUrlsData());
134 // Add entity link generating operations.
135 foreach ($this->getEntityTypeData() as $data) {
136 $this->batch->addOperation('generateBundleUrls', $data);
138 $this->batch->start();
142 * Returns a batch-ready data array for custom link generation.
145 * Data to be processed.
147 protected function getCustomUrlsData() {
149 foreach ($this->generator->getCustomLinks() as $i => $custom_path) {
150 $paths[$i]['path'] = $custom_path['path'];
151 $paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL;
152 // todo: implement lastmod.
153 $paths[$i]['lastmod'] = NULL;
159 * Collects entity metadata for entities that are set to be indexed
160 * and returns an array of batch-ready data sets for entity link generation.
164 protected function getEntityTypeData() {
166 $sitemap_entity_types = $this->entityHelper->getSitemapEntityTypes();
167 $entity_types = $this->generator->getBundleSettings();
168 foreach ($entity_types as $entity_type_name => $bundles) {
169 if (isset($sitemap_entity_types[$entity_type_name])) {
170 $keys = $sitemap_entity_types[$entity_type_name]->getKeys();
173 $keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle'];
175 foreach ($bundles as $bundle_name => $bundle_settings) {
176 if ($bundle_settings['index']) {
178 'bundle_settings' => $bundle_settings,
179 'bundle_name' => $bundle_name,
180 'entity_type_name' => $entity_type_name,
191 * Wrapper method which takes links along with their options, lets other
192 * modules alter the links and then generates and saves the sitemap.
194 * @param array $links
195 * All links with their multilingual versions and settings.
196 * @param bool $remove_sitemap
197 * Remove old sitemap from database before inserting the new one.
199 public function generateSitemap(array $links, $remove_sitemap = FALSE) {
200 // Invoke alter hook.
201 $this->moduleHandler->alter('simple_sitemap_links', $links);
204 'id' => $remove_sitemap ? self::FIRST_CHUNK_INDEX
205 : $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')
207 'sitemap_string' => $this->generateSitemapChunk($links),
208 'sitemap_created' => REQUEST_TIME,
210 if ($remove_sitemap) {
211 $this->db->truncate('simple_sitemap')->execute();
213 $this->db->insert('simple_sitemap')->fields($values)->execute();
217 * Generates and returns the sitemap index for all sitemap chunks.
219 * @param array $chunk_info
220 * Array containing chunk creation timestamps keyed by chunk ID.
222 * @return string sitemap index
224 public function generateSitemapIndex(array $chunk_info) {
225 $writer = new XMLWriter();
226 $writer->openMemory();
227 $writer->setIndent(TRUE);
228 $writer->startDocument(self::XML_VERSION, self::ENCODING);
229 $writer->writeComment(self::GENERATED_BY);
230 $writer->startElement('sitemapindex');
231 $writer->writeAttribute('xmlns', self::XMLNS);
233 foreach ($chunk_info as $chunk_id => $chunk_data) {
234 $writer->startElement('sitemap');
235 $writer->writeElement('loc', $this->getCustomBaseUrl() . '/sitemaps/' . $chunk_id . '/' . 'sitemap.xml');
236 $writer->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
237 $writer->endElement();
239 $writer->endElement();
240 $writer->endDocument();
241 return $writer->outputMemory();
244 public function getCustomBaseUrl() {
245 $customBaseUrl = $this->generator->getSetting('base_url', '');
246 return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url'];
250 * Generates and returns a sitemap chunk.
252 * @param array $links
253 * All links with their multilingual versions and settings.
258 protected function generateSitemapChunk(array $links) {
259 $writer = new XMLWriter();
260 $writer->openMemory();
261 $writer->setIndent(TRUE);
262 $writer->startDocument(self::XML_VERSION, self::ENCODING);
263 $writer->writeComment(self::GENERATED_BY);
264 $writer->startElement('urlset');
265 $writer->writeAttribute('xmlns', self::XMLNS);
267 if ($this->isHreflangSitemap()) {
268 $writer->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);
271 foreach ($links as $link) {
273 // Add each translation variant URL as location to the sitemap.
274 $writer->startElement('url');
275 $writer->writeElement('loc', $link['url']);
277 // If more than one language is enabled, add all translation variant URLs
278 // as alternate links to this location turning the sitemap into a hreflang
280 if ($this->isHreflangSitemap()) {
281 foreach ($link['alternate_urls'] as $language_id => $alternate_url) {
282 $writer->startElement('xhtml:link');
283 $writer->writeAttribute('rel', 'alternate');
284 $writer->writeAttribute('hreflang', $language_id);
285 $writer->writeAttribute('href', $alternate_url);
286 $writer->endElement();
290 // Add lastmod if any.
291 if (isset($link['lastmod'])) {
292 $writer->writeElement('lastmod', $link['lastmod']);
295 //todo: Implement changefreq here.
297 // Add priority if any.
298 if (isset($link['priority'])) {
299 $writer->writeElement('priority', $link['priority']);
302 $writer->endElement();
304 $writer->endElement();
305 $writer->endDocument();
306 return $writer->outputMemory();