You are here

function atomrdf_parse in Feeds Atom 6

Same name and namespace in other branches
  1. 7 libraries/atomrdf_parser.inc \atomrdf_parse()

Parse AtomRDF format - for example:

<entry> <title>Week in DC Tech: April 12th Edition</title> <id>19</id> <updated>2010-04-29T12:02:55-07:00</updated> <published>2010-04-12T12:16:31-07:00</published> <author><name></name></author> <content type="application/rdf+xml" xml:lang="en"> <RDF> <entity> <title>Week in DC Tech: April 12th Edition </title> <properties> <created>2010-04-12T12:16:31-07:00</created> <changed>2010-04-29T12:02:55-07:00</changed> <uid>0</uid> <status>1</status> <promote>1</promote> </properties> </entity> <field type="number_float" name="field_superlative"> <field-instance> <column name="value">1234.57</column> </field-instance> </field> </RDF> </content> </entry>

1 call to atomrdf_parse()
atomrdf_parser_parse in libraries/atomrdf_parser.inc
Parse the feed into a data structure.

File

libraries/atomrdf_parser.inc, line 106
Contains the atomrd parsing functions.

Code

function atomrdf_parse($feed_XML, $feed_raw) {

  // declare normalized view of feed that will be returned when this method is done
  $parsed_source = array();

  // valid base?
  $base = (string) array_shift($feed_XML
    ->xpath("@base"));
  if (!valid_url($base, TRUE)) {
    $base = FALSE;
  }

  /*
  // Unused right now:
  // Declare some canonical standard prefixes for well-known namespaces:
  static $canonical_namespaces = array(
    "content"  => "http://purl.org/rss/1.0/modules/content/",
    "dc"       => "http://purl.org/dc/elements/1.1/",
    'rdf'      => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'rdfs'     => 'http://www.w3.org/2000/01/rdf-schema#',
    'xsi'      => 'http://www.w3.org/2001/XMLSchema-instance#',
    'xsd'      => 'http://www.w3.org/2001/XMLSchema#',
    'owl'      => 'http://www.w3.org/2002/07/owl#',
    'dcterms'  => 'http://purl.org/dc/terms/',
    'dcmitype' => 'http://purl.org/dc/dcmitype/',
    'foaf'     => 'http://xmlns.com/foaf/0.1/',
    'rss'      => 'http://purl.org/rss/1.0/',
  );

  // Also get any other namespaces declared in the feed
  $namespaces = version_compare(phpversion(), '5.1.2', '<') ? array() : $feed_XML->getNamespaces(TRUE);
  */

  // feed metadata
  if (TRUE) {

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

    // feed description
    $parsed_source['description'] = isset($feed_XML->subtitle) ? "{$feed_XML->subtitle}" : "";

    // feed link
    $parsed_source['link'] = _parser_common_syndication_link($feed_XML->link);
    if (valid_url($parsed_source['link']) && !valid_url($parsed_source['link'], TRUE) && !empty($base)) {
      $parsed_source['link'] = $base . $parsed_source['link'];
    }
  }
  $parsed_source['items'] = array();

  // Parse out each entry in the feed.
  foreach ($feed_XML->entry as $news) {

    // Sometimes the id is the URL
    $original_url = NULL;
    $guid = !empty($news->id) ? "{$news->id}" : NULL;
    if (valid_url($guid, TRUE)) {
      $original_url = $guid;
    }

    /*
    // Unused right now:
    // Pull out RDF statements and normalize the prefixes
    // Actually there's no point in doing this since AtomRDF keeps RDF in a separate bucket.
    $rdf_data = array();
    foreach ($namespaces as $ns => $ns_uri) {
      foreach ($rss_item->attributes($ns_uri) as $attr_name => $attr_value) {
        $ns_prefix = ($ns_prefix = array_search($ns_uri, $canonical_namespaces)) ? $ns_prefix : $ns;
        $rdf_data[$ns_prefix .':'. $attr_name][] = (string)$attr_value;
      }
      foreach ($rss_item->children($ns_uri) as $rss_property) {
        $ns_prefix = ($ns_prefix = array_search($ns_uri, $canonical_namespaces)) ? $ns_prefix : $ns;
        $rdf_data[$ns_prefix .':'. $rss_property->getName()][] = (string)$rss_property;
      }
    }
    */

    // taxonomies
    $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
    $title = "{$news->title}";

    // body
    $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}";
    }

    // author
    $author_found = FALSE;
    if (!empty($news->source->author->name)) {
      $original_author = "{$news->source->author->name}";
      if (!empty($news->source->author->email)) {
        $original_author_email = "{$news->source->author->email}";
      }
      if (!empty($news->source->author->uri)) {
        $original_author_uri = "{$news->source->author->uri}";
      }
      $author_found = TRUE;
    }
    elseif (!empty($news->author->name)) {
      $original_author = "{$news->author->name}";
      if (!empty($news->author->email)) {
        $original_author_email = "{$news->author->email}";
      }
      if (!empty($news->author->uri)) {
        $original_author_uri = "{$news->author->uri}";
      }
      $author_found = TRUE;
    }
    if (!empty($feed_XML->author->name) && !$author_found) {
      $original_author = "{$feed_XML->author->name}";
      if (!empty($feed_XML->author->email)) {
        $original_author_email = "{$feed_XML->author->email}";
      }
      if (!empty($feed_XML->author->uri)) {
        $original_author_uri = "{$feed_XML->author->uri}";
      }
    }

    // some src elements in some valid atom feeds contained no urls at all
    $original_url = _parser_common_syndication_link($news->link);
    if (!$original_url && !empty($news->content['src'])) {
      if (valid_url("{$news->content['src']}", TRUE)) {
        $original_url = "{$news->content['src']}";
      }
    }

    // Parse out atom values and store them
    $item = array();
    $item['guid'] = !empty($guid) ? $guid : $item['url'];
    $item['url'] = trim($original_url);
    if (valid_url($item['url']) && !valid_url($item['url'], TRUE) && !empty($base)) {
      $item['url'] = $base . $item['url'];
    }
    $item['title'] = _parser_common_syndication_title($title, $body);
    $item['description'] = $body;
    $item['author_name'] = $original_author;
    $item['author_email'] = $original_author_email;
    $item['author_uri'] = $original_author_uri;
    $item['timestamp'] = _parser_common_syndication_parse_date(isset($news->published) ? "{$news->published}" : "{$news->issued}");
    $item['tags'] = isset($additional_taxonomies['ATOM Categories']) ? $additional_taxonomies['ATOM Categories'] : array();
    $item['domains'] = isset($additional_taxonomies['ATOM Domains']) ? $additional_taxonomies['ATOM Domains'] : array();

    // Parse out RDF values
    $rdf = $news->content ? $news->content : $news;
    $rdfns1 = $rdf
      ->children("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    if ($rdfns1) {
      $rdfns2 = $rdfns1
        ->children("http://drupal.org/");
      if ($rdfns2) {
        $item['rdf'] = atomrdf_custom_copy($rdfns2);
      }
    }

    // Store this item
    $parsed_source['items'][] = $item;
  }

  // Add any deleted items to the parsed data.  These items follow the proposed
  // Atom Tombstone specification: http://tools.ietf.org/html/draft-snell-atompub-tombstones-06
  // @todo Ignore deleted-entry items that have a newer entry item.

  //$parsed_source['deleted'] = array();
  if (in_array(FEEDS_ATOM_TOMBSTONE_NAMESPACE, $feed_XML
    ->getDocNamespaces())) {
    foreach ($feed_XML
      ->xpath('at:deleted-entry') as $deleted) {
      $item = array(
        'guid' => "{$deleted['ref']}",
        'deleted' => TRUE,
        'deleted_when' => "{$deleted['when']}",
        'deleted_by' => array(
          'name' => '',
          'email' => '',
        ),
      );

      // We really should also be parsing out the by, name, and email information, too.
      // However, because the XML namespace changes for those sub elements it is
      // ridiculously hard to parse out in PHP, due to PHP sucking.  Since they're
      // not required parts of the tombstone definition, and not needed for just
      // delete-and-forget processing, we're going to ignore them.
      // @todo When we figure out how to parse that XML in the insanity that is
      // PHP, add that in here as well.
      $parsed_source['items'][] = $item;

      //$parsed_source['deleted']["{$deleted['ref']}"] = $record;
    }
  }

  // Return list of items
  return $parsed_source;
}