View source
<?php
class XMLSitemapException extends Exception {
}
class XMLSitemapGenerationException extends XMLSitemapException {
}
class XMLSitemapWriter extends XMLWriter {
protected $uri = NULL;
protected $sitemapElementCount = 0;
protected $linkCountFlush = 500;
protected $sitemap = NULL;
protected $sitemap_page = NULL;
protected $rootElement = 'urlset';
public function __construct(stdClass $sitemap, $page) {
$this->sitemap = $sitemap;
$this->sitemap_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(t('Could not open file @file for writing.', array(
'@file' => $uri,
)));
}
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(t('Unknown error occurred while writing to file @file.', array(
'@file' => $this->uri,
)));
}
if (variable_get('xmlsitemap_xsl', 1)) {
$this
->writeXSL();
}
$this
->startElement($this->rootElement, TRUE);
return $result;
}
public function getSitemapUrl($path, array $options = array()) {
global $base_url;
$options += $this->sitemap->uri['options'];
$options += array(
'absolute' => TRUE,
'base_url' => variable_get('xmlsitemap_base_url', $base_url),
'language' => language_default(),
'alias' => TRUE,
);
if (!empty($options['protocol_relative'])) {
$options['base_url'] = preg_replace('~^https?:~', '', $options['base_url']);
}
return url($path, $options);
}
public function writeXSL() {
$this
->writePi('xml-stylesheet', 'type="text/xsl" href="' . $this
->getSitemapUrl('sitemap.xsl', array(
'absolute' => FALSE,
)) . '"');
$this
->writeRaw(PHP_EOL);
}
public function getRootAttributes() {
$attributes['xmlns'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
if (variable_get('xmlsitemap_developer_mode', 0)) {
$attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
$attributes['xsi:schemaLocation'] = 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd';
}
drupal_alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
return $attributes;
}
public function generateXML() {
return xmlsitemap_generate_chunk($this->sitemap, $this, $this->sitemap_page);
}
public function startElement($name, $root = FALSE) {
parent::startElement($name);
if ($root) {
foreach ($this
->getRootAttributes() as $name => $value) {
$this
->writeAttribute($name, $value);
}
$this
->writeRaw(PHP_EOL);
}
}
public function writeSitemapElement($name, array &$element) {
$this
->writeElement($name, $element);
$this
->writeRaw(PHP_EOL);
$this->sitemapElementCount++;
if ($this->sitemapElementCount % $this->linkCountFlush == 0) {
$this
->flush();
}
}
public function writeElement($name, $content = NULL) {
if (is_array($content)) {
$this
->startElement($name);
$xml_content = format_xml_elements($content);
$xml_content = str_replace(array(
" <",
">\n",
), array(
"<",
">",
), $xml_content);
$this
->writeRaw($xml_content);
$this
->endElement();
}
else {
parent::writeElement($name, check_plain((string) $content));
}
}
public function getURI() {
return $this->uri;
}
public function getSitemapElementCount() {
return $this->sitemapElementCount;
}
public function endDocument() {
$return = parent::endDocument();
if (!$return) {
throw new XMLSitemapGenerationException(t('Unknown error occurred while writing to file @file.', array(
'@file' => $this->uri,
)));
}
return $return;
}
}
class XMLSitemapIndexWriter extends XMLSitemapWriter {
protected $rootElement = 'sitemapindex';
public function __construct(stdClass $sitemap, $page = 'index') {
parent::__construct($sitemap, 'index');
}
public function getRootAttributes() {
$attributes['xmlns'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
if (variable_get('xmlsitemap_developer_mode', 0)) {
$attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
$attributes['xsi:schemaLocation'] = 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd';
}
drupal_alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
return $attributes;
}
public function generateXML() {
$lastmod_format = variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM);
for ($i = 1; $i <= $this->sitemap->chunks; $i++) {
$element = array(
'loc' => $this
->getSitemapUrl('sitemap.xml', array(
'query' => array(
'page' => $i,
),
)),
'lastmod' => gmdate($lastmod_format, REQUEST_TIME),
);
$this
->writeSitemapElement('sitemap', $element);
}
}
}
function xmlsitemap_xmlsitemap_link_info() {
return array(
'frontpage' => array(
'label' => t('Frontpage'),
'xmlsitemap' => array(
'settings callback' => 'xmlsitemap_link_frontpage_settings',
),
),
);
}
function xmlsitemap_link_frontpage_settings(&$form) {
module_load_include('admin.inc', 'xmlsitemap');
if (user_access('administer site configuration')) {
$form['#description'] = t('The front page path can be changed in the <a href="@url-frontpage">site information configuration</a>.', array(
'@url-frontpage' => url('admin/config/system/site-information'),
));
}
$form['xmlsitemap_frontpage_priority'] = array(
'#type' => 'select',
'#title' => t('Priority'),
'#options' => xmlsitemap_get_priority_options(),
'#default_value' => variable_get('xmlsitemap_frontpage_priority', 1.0),
);
$form['xmlsitemap_frontpage_changefreq'] = array(
'#type' => 'select',
'#title' => t('Change frequency'),
'#options' => xmlsitemap_get_changefreq_options(),
'#default_value' => variable_get('xmlsitemap_frontpage_changefreq', XMLSITEMAP_FREQUENCY_DAILY),
);
return $form;
}
function xmlsitemap_xmlsitemap_link_alter(&$link) {
if ($link['type'] == 'frontpage' || $link['loc'] == '' || $link['loc'] == variable_get('site_frontpage', 'node')) {
$link['priority'] = variable_get('xmlsitemap_frontpage_priority', 1.0);
$link['changefreq'] = variable_get('xmlsitemap_frontpage_changefreq', XMLSITEMAP_FREQUENCY_DAILY);
}
}
function xmlsitemap_xmlsitemap_links() {
$links[] = array(
'type' => 'frontpage',
'id' => 0,
'loc' => '',
);
return $links;
}
function xmlsitemap_xmlsitemap_sitemap_operations() {
$operations['update'] = array(
'label' => t('Update cached files'),
'action past' => t('Updated'),
'callback' => 'xmlsitemap_sitemap_multiple_update',
);
return $operations;
}
function xmlsitemap_sitemap_multiple_update(array $smids) {
module_load_include('generate.inc', 'xmlsitemap');
$batch = xmlsitemap_regenerate_batch($smids);
batch_set($batch);
}
function xmlsitemap_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInterface $query) {
if ($query instanceof EntityFieldQuery && ($entity = $query
->getMetaData('entity'))) {
$info = $query
->getMetaData('entity_info');
$bundle = $query
->getMetaData('bundle');
if (empty($bundle)) {
$bundle = xmlsitemap_get_link_type_enabled_bundles($entity);
}
$query
->entityCondition('bundle', $bundle, is_array($bundle) ? 'IN' : '=');
}
}