XmlSitemapWriter.php in XML sitemap 2.x
File
src/XmlSitemapWriter.php
View source
<?php
namespace Drupal\xmlsitemap;
use Drupal\Component\Utility\Html;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
class XmlSitemapWriter extends \XMLWriter {
protected $uri = NULL;
protected $sitemapElementCount = 0;
protected $linkCountFlush = 500;
protected $sitemap;
protected $page;
public function __construct(XmlSitemapInterface $sitemap, $page) {
if ($page !== 'index' && !filter_var($page, FILTER_VALIDATE_INT)) {
throw new \InvalidArgumentException("Invalid XML sitemap page {$page}.");
}
$this->sitemap = $sitemap;
$this->page = $page;
$this->uri = xmlsitemap_sitemap_get_file($sitemap, $page);
$this
->openUri($this->uri);
}
public function openUri($uri) {
$return = parent::openUri($uri);
if (!$return) {
throw new XmlSitemapGenerationException("Could not open file {$uri} for writing.");
}
return $return;
}
public function startDocument($version = '1.0', $encoding = 'UTF-8', $standalone = NULL) {
$this
->setIndent(FALSE);
$result = parent::startDocument($version, $encoding);
if (!$result) {
throw new XmlSitemapGenerationException("Unknown error occurred while writing to file {$this->uri}.");
}
if (\Drupal::config('xmlsitemap.settings')
->get('xsl')) {
$this
->writeXsl();
}
$this
->startElement($this
->isIndex() ? 'sitemapindex' : 'urlset', TRUE);
return $result;
}
public function writeXsl() {
$xls_url = Url::fromRoute('xmlsitemap.sitemap_xsl')
->toString();
$settings = \Drupal::config('language.negotiation');
if ($settings) {
$url_settings = $settings
->get('url');
if (isset($url_settings['source']) && $url_settings['source'] == 'domain') {
$scheme = \Drupal::request()
->getScheme();
$context = $this->sitemap
->getContext();
$base_url = $scheme . '://' . $url_settings['domains'][$context['language']];
$xls_url = Url::fromRoute('xmlsitemap.sitemap_xsl');
$xls_url = $base_url . '/' . $xls_url
->getInternalPath();
}
}
$this
->writePi('xml-stylesheet', 'type="text/xsl" href="' . $xls_url . '"');
$this
->writeRaw(PHP_EOL);
}
public function getRootAttributes() {
$attributes['xmlns'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
$attributes['xmlns:xhtml'] = 'http://www.w3.org/1999/xhtml';
if (\Drupal::state()
->get('xmlsitemap_developer_mode')) {
$attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
$attributes['xsi:schemaLocation'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
if ($this
->isIndex()) {
$attributes['xsi:schemaLocation'] .= ' http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd';
}
else {
$attributes['xsi:schemaLocation'] .= ' http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd';
}
}
\Drupal::moduleHandler()
->alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
return $attributes;
}
public function startElement($name, $root = FALSE) {
parent::startElement($name);
if ($root) {
foreach ($this
->getRootAttributes() as $key => $value) {
$this
->writeAttribute($key, $value);
}
$this
->writeRaw(PHP_EOL);
}
}
public function writeElement($name, $content = NULL) {
if (is_array($content)) {
$this
->startElement($name);
$this
->writeRaw($this
->formatXmlElements($content));
$this
->endElement();
}
else {
parent::writeElement($name, Html::escape(static::toString($content)));
}
$this
->writeRaw(PHP_EOL);
$this->sitemapElementCount++;
if ($this->sitemapElementCount % $this->linkCountFlush == 0) {
$this
->flush();
}
}
public function getUri() {
return $this->uri;
}
public function getSitemapElementCount() {
return $this->sitemapElementCount;
}
public function endDocument() {
$return = parent::endDocument();
if (!$return) {
throw new XmlSitemapGenerationException("Unknown error occurred while writing to file {$this->uri}.");
}
if (xmlsitemap_var('gz')) {
$file_gz = $this->uri . '.gz';
file_put_contents($file_gz, gzencode(file_get_contents($this->uri), 9));
}
return $return;
}
protected function isIndex() {
return $this->page === 'index';
}
public static function formatXmlElements(array $array) {
$output = '';
foreach ($array as $key => $value) {
if (is_numeric($key)) {
if ($value['key']) {
$output .= '<' . $value['key'];
if (isset($value['attributes'])) {
if (is_array($value['attributes'])) {
$value['attributes'] = new Attribute($value['attributes']);
}
$output .= static::toString($value['attributes']);
}
if (isset($value['value']) && $value['value'] != '') {
$output .= '>' . (is_array($value['value']) ? static::formatXmlElements($value['value']) : Html::escape(static::toString($value['value']))) . '</' . $value['key'] . '>';
}
else {
$output .= ' />';
}
}
}
else {
$output .= '<' . $key . '>' . (is_array($value) ? static::formatXmlElements($value) : Html::escape(static::toString($value))) . "</{$key}>";
}
}
return $output;
}
public static function toString($value) {
if (is_object($value)) {
if ($value instanceof Url) {
return $value
->toString();
}
}
return (string) $value;
}
}