FeedsSitemapParser.inc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsSitemapParser and related classes.
  5. */
  6. /**
  7. * A parser for the Sitemap specification http://www.sitemaps.org/protocol.php
  8. */
  9. class FeedsSitemapParser extends FeedsParser {
  10. /**
  11. * Implements FeedsParser::parse().
  12. */
  13. public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
  14. // Set time zone to GMT for parsing dates with strtotime().
  15. $tz = date_default_timezone_get();
  16. date_default_timezone_set('GMT');
  17. // Yes, using a DOM parser is a bit inefficient, but will do for now
  18. $xml = new SimpleXMLElement($fetcher_result->getRaw());
  19. $result = new FeedsParserResult();
  20. foreach ($xml->url as $url) {
  21. $item = array('url' => (string) $url->loc);
  22. if ($url->lastmod) {
  23. $item['lastmod'] = strtotime($url->lastmod);
  24. }
  25. if ($url->changefreq) {
  26. $item['changefreq'] = (string) $url->changefreq;
  27. }
  28. if ($url->priority) {
  29. $item['priority'] = (string) $url->priority;
  30. }
  31. $result->items[] = $item;
  32. }
  33. date_default_timezone_set($tz);
  34. return $result;
  35. }
  36. /**
  37. * Implements FeedsParser::getMappingSources().
  38. */
  39. public function getMappingSources() {
  40. return array(
  41. 'url' => array(
  42. 'name' => t('Item URL (link)'),
  43. 'description' => t('URL of the feed item.'),
  44. ),
  45. 'lastmod' => array(
  46. 'name' => t('Last modification date'),
  47. 'description' => t('Last modified date as UNIX time GMT of the feed item.'),
  48. ),
  49. 'changefreq' => array(
  50. 'name' => t('Change frequency'),
  51. 'description' => t('How frequently the page is likely to change.'),
  52. ),
  53. 'priority' => array(
  54. 'name' => t('Priority'),
  55. 'description' => t('The priority of this URL relative to other URLs on the site.'),
  56. ),
  57. ) + parent::getMappingSources();
  58. }
  59. }