You are here

public function FeedsSitemapParser::parse in Feeds 8.2

Implements FeedsParser::parse().

Overrides FeedsParser::parse

File

lib/Drupal/feeds/Plugin/feeds/parser/FeedsSitemapParser.php, line 31
Contains FeedsSitemapParser and related classes.

Class

FeedsSitemapParser
Defines a SitemapXML feed parser.

Namespace

Drupal\feeds\Plugin\feeds\parser

Code

public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {

  // Set time zone to GMT for parsing dates with strtotime().
  $tz = date_default_timezone_get();
  date_default_timezone_set('GMT');

  // Yes, using a DOM parser is a bit inefficient, but will do for now
  $xml = new SimpleXMLElement($fetcher_result
    ->getRaw());
  $result = new FeedsParserResult();
  foreach ($xml->url as $url) {
    $item = array(
      'url' => (string) $url->loc,
    );
    if ($url->lastmod) {
      $item['lastmod'] = strtotime($url->lastmod);
    }
    if ($url->changefreq) {
      $item['changefreq'] = (string) $url->changefreq;
    }
    if ($url->priority) {
      $item['priority'] = (string) $url->priority;
    }
    $result->items[] = $item;
  }
  date_default_timezone_set($tz);
  return $result;
}