View source
<?php
namespace Drupal\simple_sitemap;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Component\Datetime\Time;
class SitemapGenerator {
const XML_VERSION = '1.0';
const ENCODING = 'UTF-8';
const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
const GENERATED_BY = 'Generated by the Simple XML sitemap Drupal module: https://drupal.org/project/simple_sitemap.';
const FIRST_CHUNK_INDEX = 1;
const XMLNS_IMAGE = 'http://www.google.com/schemas/sitemap-image/1.1';
protected $entityHelper;
protected $db;
protected $languageManager;
protected $moduleHandler;
protected $isHreflangSitemap;
protected $time;
protected $settings;
protected $writer;
protected static $attributes = [
'xmlns' => self::XMLNS,
'xmlns:xhtml' => self::XMLNS_XHTML,
'xmlns:image' => self::XMLNS_IMAGE,
];
protected static $indexAttributes = [
'xmlns' => self::XMLNS,
];
public function __construct(EntityHelper $entityHelper, Connection $database, ModuleHandler $module_handler, LanguageManagerInterface $language_manager, Time $time, SitemapWriter $sitemapWriter) {
$this->entityHelper = $entityHelper;
$this->db = $database;
$this->moduleHandler = $module_handler;
$this->languageManager = $language_manager;
$this->time = $time;
$this->writer = $sitemapWriter;
}
protected function isHreflangSitemap() {
if (NULL === $this->isHreflangSitemap) {
$this->isHreflangSitemap = count(array_diff_key($this->languageManager
->getLanguages(), $this->settings['excluded_languages'])) > 1;
}
return $this->isHreflangSitemap;
}
public function setSettings(array $settings) {
$this->settings = $settings;
return $this;
}
public function generateSitemap(array $links, $remove_sitemap = FALSE) {
$values = [
'id' => $remove_sitemap ? self::FIRST_CHUNK_INDEX : $this->db
->query('SELECT MAX(id) FROM {simple_sitemap}')
->fetchField() + 1,
'sitemap_string' => $this
->generateSitemapChunk($links),
'sitemap_created' => $this->time
->getRequestTime(),
];
if ($remove_sitemap) {
$this->db
->truncate('simple_sitemap')
->execute();
}
$this->db
->insert('simple_sitemap')
->fields($values)
->execute();
}
public function generateSitemapIndex(array $chunk_info) {
$this->writer
->openMemory();
$this->writer
->setIndent(TRUE);
$this->writer
->startDocument(self::XML_VERSION, self::ENCODING);
$this->writer
->writeComment(self::GENERATED_BY);
$this->writer
->startElement('sitemapindex');
$this->moduleHandler
->alter('simple_sitemap_index_attributes', self::$indexAttributes);
foreach (self::$indexAttributes as $name => $value) {
$this->writer
->writeAttribute($name, $value);
}
foreach ($chunk_info as $chunk_id => $chunk_data) {
$this->writer
->startElement('sitemap');
$this->writer
->writeElement('loc', $this
->getCustomBaseUrl() . '/sitemaps/' . $chunk_id . '/' . 'sitemap.xml');
$this->writer
->writeElement('lastmod', date_iso8601($chunk_data->sitemap_created));
$this->writer
->endElement();
}
$this->writer
->endElement();
$this->writer
->endDocument();
return $this->writer
->outputMemory();
}
public function getCustomBaseUrl() {
$customBaseUrl = $this->settings['base_url'];
return !empty($customBaseUrl) ? $customBaseUrl : $GLOBALS['base_url'];
}
protected function generateSitemapChunk(array $links) {
$this->writer
->openMemory();
$this->writer
->setIndent(TRUE);
$this->writer
->startDocument(self::XML_VERSION, self::ENCODING);
$this->writer
->writeComment(self::GENERATED_BY);
$this->writer
->startElement('urlset');
if (!$this
->isHreflangSitemap()) {
unset(self::$attributes['xmlns:xhtml']);
}
$this->moduleHandler
->alter('simple_sitemap_attributes', self::$attributes);
foreach (self::$attributes as $name => $value) {
$this->writer
->writeAttribute($name, $value);
}
$this->moduleHandler
->alter('simple_sitemap_links', $links);
foreach ($links as $link) {
$this->writer
->startElement('url');
$this->writer
->writeElement('loc', $link['url']);
if (isset($link['alternate_urls']) && $this
->isHreflangSitemap()) {
foreach ($link['alternate_urls'] as $language_id => $alternate_url) {
$this->writer
->startElement('xhtml:link');
$this->writer
->writeAttribute('rel', 'alternate');
$this->writer
->writeAttribute('hreflang', $language_id);
$this->writer
->writeAttribute('href', $alternate_url);
$this->writer
->endElement();
}
}
if (isset($link['lastmod'])) {
$this->writer
->writeElement('lastmod', $link['lastmod']);
}
if (isset($link['changefreq'])) {
$this->writer
->writeElement('changefreq', $link['changefreq']);
}
if (isset($link['priority'])) {
$this->writer
->writeElement('priority', $link['priority']);
}
if (!empty($link['images'])) {
foreach ($link['images'] as $image) {
$this->writer
->startElement('image:image');
$this->writer
->writeElement('image:loc', $image['path']);
$this->writer
->endElement();
}
}
$this->writer
->endElement();
}
$this->writer
->endElement();
$this->writer
->endDocument();
return $this->writer
->outputMemory();
}
}