You are here

function _parser_common_syndication_RDF10_parse in Feeds 6

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

Parse RDF Site Summary (RSS) 1.0 feeds in RDF/XML format.

See also

http://web.resource.org/rss/1.0/

1 call to _parser_common_syndication_RDF10_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 250
Downloading and parsing functions for Common Syndication Parser. Pillaged from FeedAPI common syndication parser.

Code

function _parser_common_syndication_RDF10_parse($feed_XML) {

  // Declare some canonical standard prefixes for well-known namespaces:
  static $canonical_namespaces = array(
    '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#',
    'dc' => 'http://purl.org/dc/elements/1.1/',
    '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/',
  );

  // Get all namespaces declared in the feed element, with special handling
  // for PHP versions prior to 5.1.2 as they don't handle namespaces.
  $namespaces = version_compare(phpversion(), '5.1.2', '<') ? array() : $feed_XML
    ->getNamespaces(TRUE);

  // Process the <rss:channel> resource containing feed metadata:
  foreach ($feed_XML
    ->children($canonical_namespaces['rss'])->channel as $rss_channel) {
    $parsed_source = array(
      'title' => _parser_common_syndication_title((string) $rss_channel->title),
      'description' => (string) $rss_channel->description,
      'link' => (string) $rss_channel->link,
      'items' => array(),
    );
    break;
  }

  // Process each <rss:item> resource contained in the feed:
  foreach ($feed_XML
    ->children($canonical_namespaces['rss'])->item as $rss_item) {

    // Extract all available RDF statements from the feed item's RDF/XML
    // tags, allowing for both the item's attributes and child elements to
    // contain RDF properties:
    $rdf_data = array();
    foreach ($namespaces as $ns => $ns_uri) {

      // Note that we attempt to normalize the found property name
      // namespaces to well-known 'standard' prefixes where possible, as the
      // feed may in principle use any arbitrary prefixes and we should
      // still be able to correctly handle it.
      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;
      }
    }

    // Declaratively define mappings that determine how to construct the result object.
    $item = _parser_common_syndication_RDF10_item($rdf_data, array(
      'title' => array(
        'rss:title',
        'dc:title',
      ),
      'description' => array(
        'rss:description',
        'dc:description',
        'content:encoded',
      ),
      'url' => array(
        'rss:link',
        'rdf:about',
      ),
      'author_name' => array(
        'dc:creator',
        'dc:publisher',
      ),
      'guid' => 'rdf:about',
      'timestamp' => 'dc:date',
      'tags' => 'dc:subject',
    ));

    // Special handling for the title:
    $item['title'] = _parser_common_syndication_title($item['title'], $item['description']);

    // Parse any date/time values into Unix timestamps:
    $item['timestamp'] = _parser_common_syndication_parse_date($item['timestamp']);

    // If no GUID found, use the URL of the feed.
    if (empty($item['guid'])) {
      $item['guid'] = $item['url'];
    }

    // Add every found RDF property to the feed item.
    $item['rdf'] = array();
    foreach ($rdf_data as $rdf_property => $rdf_value) {

      // looks nicer in the mapper UI
      // @todo Revisit, not used with feedapi mapper anymore.
      $rdf_property = str_replace(':', '_', $rdf_property);
      $item['rdf'][$rdf_property] = $rdf_value;
    }
    $parsed_source['items'][] = $item;
  }
  return $parsed_source;
}