You are here

function _parser_common_syndication_atom10_parse in Feeds 7.2

Same name and namespace in other branches
  1. 8.2 libraries/common_syndication_parser.inc \_parser_common_syndication_atom10_parse()
  2. 6 libraries/common_syndication_parser.inc \_parser_common_syndication_atom10_parse()
  3. 7 libraries/common_syndication_parser.inc \_parser_common_syndication_atom10_parse()

Parse atom feeds.

1 call to _parser_common_syndication_atom10_parse()
common_syndication_parser_parse in libraries/common_syndication_parser.inc
Parse the feed into a data structure.

File

libraries/common_syndication_parser.inc, line 82
Downloading and parsing functions for Common Syndication Parser. Pillaged from FeedAPI common syndication parser.

Code

function _parser_common_syndication_atom10_parse($feed_XML) {
  $parsed_source = array();
  $ns = array(
    "georss" => "http://www.georss.org/georss",
  );
  $base = _parser_common_syndication_atom10_parse_base_url($feed_XML);

  // Detect the title.
  $parsed_source['title'] = isset($feed_XML->title) ? _parser_common_syndication_title("{$feed_XML->title}") : "";

  // Detect the description.
  $parsed_source['description'] = isset($feed_XML->subtitle) ? "{$feed_XML->subtitle}" : "";
  $parsed_source['link'] = _parser_common_syndication_link($feed_XML->link);
  if ($base && !valid_url($parsed_source['link'], TRUE) && valid_url($parsed_source['link'])) {
    $parsed_source['link'] = $base . $parsed_source['link'];
  }
  $parsed_source['items'] = array();
  foreach ($feed_XML->entry as $news) {
    $georss = (array) $news
      ->children($ns["georss"]);
    $geoname = '';
    if (isset($georss['featureName'])) {
      $geoname = "{$georss['featureName']}";
    }
    $latlon = $lat = $lon = NULL;
    if (isset($georss['point'])) {
      $latlon = explode(' ', $georss['point']);
      $lat = "{$latlon[0]}";
      $lon = "{$latlon[1]}";
      if (!$geoname) {
        $geoname = "{$lat} {$lon}";
      }
    }
    $additional_taxonomies = array();
    if (isset($news->category)) {
      $additional_taxonomies['ATOM Categories'] = array();
      $additional_taxonomies['ATOM Domains'] = array();
      foreach ($news->category as $category) {
        if (isset($category['scheme'])) {
          $domain = "{$category['scheme']}";
          if (!empty($domain)) {
            if (!isset($additional_taxonomies['ATOM Domains'][$domain])) {
              $additional_taxonomies['ATOM Domains'][$domain] = array();
            }
            $additional_taxonomies['ATOM Domains'][$domain][] = count($additional_taxonomies['ATOM Categories']) - 1;
          }
        }
        $additional_taxonomies['ATOM Categories'][] = "{$category['term']}";
      }
    }
    $title = "{$news->title}";
    $body = '';
    if (!empty($news->content)) {
      foreach ($news->content
        ->children() as $child) {
        $body .= $child
          ->asXML();
      }
      $body .= "{$news->content}";
    }
    elseif (!empty($news->summary)) {
      foreach ($news->summary
        ->children() as $child) {
        $body .= $child
          ->asXML();
      }
      $body .= "{$news->summary}";
    }
    $original_author = '';
    if (!empty($news->source->author->name)) {
      $original_author = "{$news->source->author->name}";
    }
    elseif (!empty($news->author->name)) {
      $original_author = "{$news->author->name}";
    }
    elseif (!empty($feed_XML->author->name)) {
      $original_author = "{$feed_XML->author->name}";
    }
    $item = array();
    $item['title'] = _parser_common_syndication_title($title, $body);
    $item['description'] = $body;
    $item['author_name'] = $original_author;

    // Fall back to updated for timestamp if both published and issued are
    // empty.
    if (isset($news->published)) {
      $item['timestamp'] = _parser_common_syndication_parse_date("{$news->published}");
    }
    elseif (isset($news->issued)) {
      $item['timestamp'] = _parser_common_syndication_parse_date("{$news->issued}");
    }
    elseif (isset($news->updated)) {
      $item['timestamp'] = _parser_common_syndication_parse_date("{$news->updated}");
    }
    $item['guid'] = (string) $news->id;
    $item['url'] = _parser_common_syndication_link($news->link);
    if (!$item['url'] && !empty($news->content['src']) && valid_url($news->content['src'], TRUE)) {
      $item['url'] = (string) $news->content['src'];
    }
    if (!strlen($item['url']) && $item['guid'] && valid_url($item['guid'], TRUE)) {
      $item['url'] = $item['guid'];
    }
    if (!valid_url($item['url'], TRUE) && valid_url($item['url'])) {
      if ($item_base = _parser_common_syndication_atom10_parse_base_url($news)) {
        $item['url'] = $item_base . $item['url'];
      }
      elseif ($base) {
        $item['url'] = $base . $item['url'];
      }
    }

    // Fall back on URL if GUID is empty.
    if (!strlen($item['guid'])) {
      $item['guid'] = $item['url'];
    }
    $item['geolocations'] = array();
    if ($lat && $lon) {
      $item['geolocations'] = array(
        array(
          'name' => $geoname,
          'lat' => $lat,
          'lon' => $lon,
        ),
      );
    }
    $item['tags'] = isset($additional_taxonomies['ATOM Categories']) ? $additional_taxonomies['ATOM Categories'] : array();
    $item['domains'] = isset($additional_taxonomies['ATOM Domains']) ? $additional_taxonomies['ATOM Domains'] : array();
    $parsed_source['items'][] = $item;
  }
  return $parsed_source;
}