function OpmlFeedAdd::parseOpml

Same name and namespace in other branches
  1. 8.9.x core/modules/aggregator/src/Form/OpmlFeedAdd.php \Drupal\aggregator\Form\OpmlFeedAdd::parseOpml()

Parses an OPML file.

Feeds are recognized as <outline> elements with the attributes "text" and "xmlurl" set.

@todo Move this to a parser in https://www.drupal.org/node/1963540.

Parameters

string $opml: The complete contents of an OPML document.

Return value

array An array of feeds, each an associative array with a "title" and a "url" element, or NULL if the OPML document failed to be parsed. An empty array will be returned if the document is valid but contains no feeds, as some OPML documents do.

1 call to OpmlFeedAdd::parseOpml()
OpmlFeedAdd::submitForm in core/modules/aggregator/src/Form/OpmlFeedAdd.php
Form submission handler.

File

core/modules/aggregator/src/Form/OpmlFeedAdd.php, line 194

Class

OpmlFeedAdd
Imports feeds from OPML.

Namespace

Drupal\aggregator\Form

Code

protected function parseOpml($opml) {
    $feeds = [];
    $xml_parser = xml_parser_create();
    xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
    if (xml_parse_into_struct($xml_parser, $opml, $values)) {
        foreach ($values as $entry) {
            if ($entry['tag'] == 'OUTLINE' && isset($entry['attributes'])) {
                $item = $entry['attributes'];
                if (!empty($item['XMLURL']) && !empty($item['TEXT'])) {
                    $feeds[] = [
                        'title' => $item['TEXT'],
                        'url' => $item['XMLURL'],
                    ];
                }
            }
        }
    }
    xml_parser_free($xml_parser);
    return $feeds;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.