You are here

function opml_parser_parse in Feeds 8.2

Same name and namespace in other branches
  1. 6 libraries/opml_parser.inc \opml_parser_parse()
  2. 7.2 libraries/opml_parser.inc \opml_parser_parse()
  3. 7 libraries/opml_parser.inc \opml_parser_parse()

Parse OPML file.

Parameters

$raw: File contents.

Return value

An array of the parsed OPML file.

1 call to opml_parser_parse()
FeedsOPMLParser::parse in lib/Drupal/feeds/Plugin/feeds/parser/FeedsOPMLParser.php
Implements FeedsParser::parse().

File

libraries/opml_parser.inc, line 16
OPML Parser.

Code

function opml_parser_parse($raw) {
  $feeds = $items = array();
  $xml = @new SimpleXMLElement($raw);
  $feeds['title'] = (string) current($xml
    ->xpath('//head/title'));

  // @todo Make xpath case insensitive.
  $outlines = $xml
    ->xpath('//outline[@xmlUrl]');
  foreach ($outlines as $outline) {
    $item = array();
    foreach ($outline
      ->attributes() as $k => $v) {
      if (in_array(strtolower($k), array(
        'title',
        'text',
        'xmlurl',
      ))) {
        $item[strtolower($k)] = (string) $v;
      }
    }

    // If no title, forge it from text.
    if (!isset($item['title']) && isset($item['text'])) {
      if (strlen($item['text']) < 40) {
        $item['title'] = $item['text'];
      }
      else {
        $item['title'] = trim(substr($item['text'], 0, 30)) . ' ...';
      }
    }
    if (isset($item['title']) && isset($item['xmlurl'])) {
      $items[] = $item;
    }
  }
  $feeds['items'] = $items;
  return $feeds;
}