Commit d38294fb authored by Dave Reid's avatar Dave Reid

by Dave Reid: Moved XMLSitemapWriter classes to xmlsitemap.xmlsitemap.inc.

parent 7746e96d
......@@ -8,203 +8,6 @@
* @ingroup xmlsitemap
*/
class XMLSitemapIndexWriter extends XMLSitemapWriter {
protected $rootElement = 'sitemapindex';
function __construct(array $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';
}
return $attributes;
}
public function generateXML() {
$lastmod_format = variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM);
$url_options = $this->sitemap['uri']['options'];
$url_options += array(
'absolute' => TRUE,
'base_url' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
'language' => language_default(),
'alias' => TRUE,
);
for ($i = 1; $i <= $this->sitemap['chunks']; $i++) {
$url_options['query']['page'] = $i;
$element = array(
'loc' => url('sitemap.xml', $url_options),
// @todo Use the actual lastmod value of the chunk file.
'lastmod' => gmdate($lastmod_format, REQUEST_TIME),
);
$this->writeSitemapElement('sitemap', $element);
}
}
}
/**
* Extended class for writing XML sitemap files.
*/
class XMLSitemapWriter extends XMLWriter {
protected $status = TRUE;
protected $uri = NULL;
protected $sitemapElementCount = 0;
protected $linkCountFlush = 500;
protected $sitemap = NULL;
protected $sitemap_page = NULL;
protected $rootElement = 'urlset';
/**
* Constructor.
*
* @param $sitemap
* The sitemap array.
* @param $page
* The current page of the sitemap being generated.
*/
function __construct(array $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) {
trigger_error(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);
parent::startDocument($version, $encoding);
if (variable_get('xmlsitemap_xsl', 1)) {
$this->writeXSL();
}
$this->startElement($this->rootElement, TRUE);
}
/**
* Add the XML stylesheet to the XML page.
*/
public function writeXSL() {
$this->writePi('xml-stylesheet', 'type="text/xsl" href="' . url('sitemap.xsl') . '"');
$this->writeRaw(PHP_EOL);
}
/**
* Return an array of attributes for the root element of the XML.
*/
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';
}
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);
}
}
/**
* Write an full XML sitemap element tag.
*
* @param $name
* The element name.
* @param $element
* An array of the elements properties and values.
*/
public function writeSitemapElement($name, array &$element) {
$this->writeElement($name, $element);
$this->writeRaw(PHP_EOL);
// After a certain number of elements have been added, flush the buffer
// to the output file.
$this->sitemapElementCount++;
if (($this->sitemapElementCount % $this->linkCountFlush) == 0) {
$this->flush();
}
}
/**
* Write full element tag including support for nested elements.
*
* @param $name
* The element name.
* @param $content
* The element contents or an array of the elements' sub-elements.
*/
public function writeElement($name, $content) {
if (is_array($content)) {
$this->startElement($name);
foreach ($content as $sub_name => $sub_content) {
$this->writeElement($sub_name, $sub_content);
}
$this->endElement();
}
else {
parent::writeElement($name, $content);
}
}
/**
* Override of XMLWriter::flush() to track file writing status.
*/
public function flush($empty = TRUE) {
$return = parent::flush($empty);
$this->status &= (bool) $return;
return $return;
}
public function getStatus() {
return $this->status;
}
public function getURI() {
return $this->uri;
}
public function getSitemapElementCount() {
return $this->sitemapElementCount;
}
public function endDocument() {
$return = parent::endDocument();
if (!$this->getStatus()) {
trigger_error(t('Unknown error occurred while writing to file @file.', array('@file' => $this->uri)));
return FALSE;
}
//if (xmlsitemap_var('gz')) {
// $file_gz = $file . '.gz';
// file_put_contents($file_gz, gzencode(file_get_contents($file), 9));
//}
return $return;
}
}
/**
* Given an internal Drupal path, return the alias for the path.
*
......
......@@ -6,6 +6,203 @@
* XML sitemap integration functions for xmlsitemap.module.
*/
/**
* Extended class for writing XML sitemap files.
*/
class XMLSitemapWriter extends XMLWriter {
protected $status = TRUE;
protected $uri = NULL;
protected $sitemapElementCount = 0;
protected $linkCountFlush = 500;
protected $sitemap = NULL;
protected $sitemap_page = NULL;
protected $rootElement = 'urlset';
/**
* Constructor.
*
* @param $sitemap
* The sitemap array.
* @param $page
* The current page of the sitemap being generated.
*/
function __construct(array $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) {
trigger_error(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);
parent::startDocument($version, $encoding);
if (variable_get('xmlsitemap_xsl', 1)) {
$this->writeXSL();
}
$this->startElement($this->rootElement, TRUE);
}
/**
* Add the XML stylesheet to the XML page.
*/
public function writeXSL() {
$this->writePi('xml-stylesheet', 'type="text/xsl" href="' . url('sitemap.xsl') . '"');
$this->writeRaw(PHP_EOL);
}
/**
* Return an array of attributes for the root element of the XML.
*/
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';
}
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);
}
}
/**
* Write an full XML sitemap element tag.
*
* @param $name
* The element name.
* @param $element
* An array of the elements properties and values.
*/
public function writeSitemapElement($name, array &$element) {
$this->writeElement($name, $element);
$this->writeRaw(PHP_EOL);
// After a certain number of elements have been added, flush the buffer
// to the output file.
$this->sitemapElementCount++;
if (($this->sitemapElementCount % $this->linkCountFlush) == 0) {
$this->flush();
}
}
/**
* Write full element tag including support for nested elements.
*
* @param $name
* The element name.
* @param $content
* The element contents or an array of the elements' sub-elements.
*/
public function writeElement($name, $content) {
if (is_array($content)) {
$this->startElement($name);
foreach ($content as $sub_name => $sub_content) {
$this->writeElement($sub_name, $sub_content);
}
$this->endElement();
}
else {
parent::writeElement($name, $content);
}
}
/**
* Override of XMLWriter::flush() to track file writing status.
*/
public function flush($empty = TRUE) {
$return = parent::flush($empty);
$this->status &= (bool) $return;
return $return;
}
public function getStatus() {
return $this->status;
}
public function getURI() {
return $this->uri;
}
public function getSitemapElementCount() {
return $this->sitemapElementCount;
}
public function endDocument() {
$return = parent::endDocument();
if (!$this->getStatus()) {
trigger_error(t('Unknown error occurred while writing to file @file.', array('@file' => $this->uri)));
return FALSE;
}
//if (xmlsitemap_var('gz')) {
// $file_gz = $file . '.gz';
// file_put_contents($file_gz, gzencode(file_get_contents($file), 9));
//}
return $return;
}
}
class XMLSitemapIndexWriter extends XMLSitemapWriter {
protected $rootElement = 'sitemapindex';
function __construct(array $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';
}
return $attributes;
}
public function generateXML() {
$lastmod_format = variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM);
$url_options = $this->sitemap['uri']['options'];
$url_options += array(
'absolute' => TRUE,
'base_url' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
'language' => language_default(),
'alias' => TRUE,
);
for ($i = 1; $i <= $this->sitemap['chunks']; $i++) {
$url_options['query']['page'] = $i;
$element = array(
'loc' => url('sitemap.xml', $url_options),
// @todo Use the actual lastmod value of the chunk file.
'lastmod' => gmdate($lastmod_format, REQUEST_TIME),
);
$this->writeSitemapElement('sitemap', $element);
}
}
}
/**
* Implements hook_xmlsitemap_link_info().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment