You are here

class XMLSitemapWriter in XML sitemap 7.2

Same name and namespace in other branches
  1. 6.2 xmlsitemap.xmlsitemap.inc \XMLSitemapWriter

Extended class for writing XML sitemap files.

Hierarchy

Expanded class hierarchy of XMLSitemapWriter

File

./xmlsitemap.xmlsitemap.inc, line 25
XML sitemap integration functions for xmlsitemap.module.

View source
class XMLSitemapWriter extends XMLWriter {
  protected $uri = NULL;
  protected $sitemapElementCount = 0;
  protected $linkCountFlush = 500;
  protected $sitemap = NULL;

  /**
   * Sitemap Page.
   *
   * @var string
   *
   * @codingStandardsIgnoreStart
   */
  protected $sitemap_page = NULL;

  /**
   * Root Element.
   *
   * @var string.
   *
   * @codingStandardsIgnoreEnd
   */
  protected $rootElement = 'urlset';

  /**
   * Constructor.
   *
   * @param array $sitemap
   *   The sitemap array.
   * @param string $page
   *   The current page of the sitemap being generated.
   *
   * @codingStandardsIgnoreStart
   */
  public function __construct(stdClass $sitemap, $page) {

    // @codingStandardsIgnoreEnd
    $this->sitemap = $sitemap;
    $this->sitemap_page = $page;
    $this->uri = xmlsitemap_sitemap_get_file($sitemap, $page);
    $this
      ->openUri($this->uri);
  }

  /**
   * Open 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;
  }

  /**
   * Start Document.
   */
  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;
  }

  /**
   * Get Sitemap URL.
   */
  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);
  }

  /**
   * Add the XML stylesheet to the XML page.
   *
   * @codingStandardsIgnoreStart
   */
  public function writeXSL() {

    // @codingStandardsIgnoreEnd
    $this
      ->writePi('xml-stylesheet', 'type="text/xsl" href="' . $this
      ->getSitemapUrl('sitemap.xsl', array(
      'absolute' => FALSE,
    )) . '"');
    $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';
    }
    drupal_alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
    return $attributes;
  }

  /**
   * Generate XML.
   *
   * @codingStandardsIgnoreStart
   */
  public function generateXML() {

    // @codingStandardsIgnoreEnd
    return xmlsitemap_generate_chunk($this->sitemap, $this, $this->sitemap_page);
  }

  /**
   * Start Element.
   */
  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 string $name
   *   The element name.
   * @param array $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 string $name
   *   The element name.
   * @param string|array $content
   *   The element contents or an array of the elements' sub-elements.
   *
   * @todo Missing a return value since XMLWriter::writeElement() has one.
   */
  public function writeElement($name, $content = NULL) {
    if (is_array($content)) {
      $this
        ->startElement($name);
      $xml_content = format_xml_elements($content);

      // Remove additional spaces from the output.
      $xml_content = str_replace(array(
        " <",
        ">\n",
      ), array(
        "<",
        ">",
      ), $xml_content);
      $this
        ->writeRaw($xml_content);
      $this
        ->endElement();
    }
    else {
      parent::writeElement($name, check_plain((string) $content));
    }
  }

  /**
   * Get URI.
   *
   * @codingStandardsIgnoreStart
   */
  public function getURI() {

    // @codingStandardsIgnoreEnd
    return $this->uri;
  }

  /**
   * Get Count Sitemap Element.
   */
  public function getSitemapElementCount() {
    return $this->sitemapElementCount;
  }

  /**
   * Document.
   */
  public function endDocument() {
    $return = parent::endDocument();
    if (!$return) {
      throw new XMLSitemapGenerationException(t('Unknown error occurred while writing to file @file.', array(
        '@file' => $this->uri,
      )));
    }

    // @code
    // If (xmlsitemap_var('gz')) {
    //  $file_gz = $file . '.gz';
    //  file_put_contents($file_gz, gzencode(file_get_contents($file), 9));
    // }
    // @endcode
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
XMLSitemapWriter::$linkCountFlush protected property
XMLSitemapWriter::$rootElement protected property Root Element. 1
XMLSitemapWriter::$sitemap protected property
XMLSitemapWriter::$sitemapElementCount protected property
XMLSitemapWriter::$sitemap_page protected property Sitemap Page.
XMLSitemapWriter::$uri protected property
XMLSitemapWriter::endDocument public function Document.
XMLSitemapWriter::generateXML public function Generate XML. 1
XMLSitemapWriter::getRootAttributes public function Return an array of attributes for the root element of the XML. 1
XMLSitemapWriter::getSitemapElementCount public function Get Count Sitemap Element.
XMLSitemapWriter::getSitemapUrl public function Get Sitemap URL.
XMLSitemapWriter::getURI public function Get URI.
XMLSitemapWriter::openUri public function Open URI.
XMLSitemapWriter::startDocument public function Start Document.
XMLSitemapWriter::startElement public function Start Element.
XMLSitemapWriter::writeElement public function Write full element tag including support for nested elements.
XMLSitemapWriter::writeSitemapElement public function Write an full XML sitemap element tag.
XMLSitemapWriter::writeXSL public function Add the XML stylesheet to the XML page.
XMLSitemapWriter::__construct public function Constructor. 1