You are here

public function FeedsSimplePieParser::parse in Feeds 8.2

Implements FeedsParser::parse().

Overrides FeedsParser::parse

File

lib/Drupal/feeds/Plugin/feeds/parser/FeedsSimplePieParser.php, line 34
Contains FeedsSimplePieParser and related classes.

Class

FeedsSimplePieParser
Defines a CSV feed parser.

Namespace

Drupal\feeds\Plugin\feeds\parser

Code

public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
  feeds_include_simplepie();

  // Initialize SimplePie.
  $parser = new \SimplePie();
  $parser
    ->set_raw_data($fetcher_result
    ->getRaw());
  $parser
    ->set_stupidly_fast(TRUE);
  $parser
    ->encode_instead_of_strip(FALSE);

  // @todo Is caching effective when we pass in raw data?
  $parser
    ->enable_cache(TRUE);
  $parser
    ->set_cache_location($this
    ->cacheDirectory());
  $parser
    ->init();

  // Construct the standard form of the parsed feed
  $result = new FeedsParserResult();
  $result->title = html_entity_decode(($title = $parser
    ->get_title()) ? $title : $this
    ->createTitle($parser
    ->get_description()));
  $result->description = $parser
    ->get_description();
  $result->link = html_entity_decode($parser
    ->get_link());
  $items_num = $parser
    ->get_item_quantity();
  for ($i = 0; $i < $items_num; $i++) {
    $item = array();
    $simplepie_item = $parser
      ->get_item($i);
    $item['title'] = html_entity_decode(($title = $simplepie_item
      ->get_title()) ? $title : $this
      ->createTitle($simplepie_item
      ->get_content()));
    $item['description'] = $simplepie_item
      ->get_content();
    $item['url'] = html_entity_decode($simplepie_item
      ->get_link());

    // Use UNIX time. If no date is defined, fall back to REQUEST_TIME.
    $item['timestamp'] = $simplepie_item
      ->get_date("U");
    if (empty($item['timestamp'])) {
      $item['timestamp'] = REQUEST_TIME;
    }
    $item['guid'] = $simplepie_item
      ->get_id();

    // Use URL as GUID if there is no GUID.
    if (empty($item['guid'])) {
      $item['guid'] = $item['url'];
    }
    $author = $simplepie_item
      ->get_author();
    $item['author_name'] = isset($author->name) ? html_entity_decode($author->name) : '';
    $item['author_link'] = isset($author->link) ? $author->link : '';
    $item['author_email'] = isset($author->email) ? $author->email : '';

    // Enclosures
    $enclosures = $simplepie_item
      ->get_enclosures();
    if (is_array($enclosures)) {
      foreach ($enclosures as $enclosure) {
        $item['enclosures'][] = new FeedsSimplePieEnclosure($enclosure);
      }
    }

    // Location
    $latitude = $simplepie_item
      ->get_latitude();
    $longitude = $simplepie_item
      ->get_longitude();
    if (!is_null($latitude) && !is_null($longitude)) {
      $item['location_latitude'][] = $latitude;
      $item['location_longitude'][] = $longitude;
    }

    // Extract tags related to the item
    $simplepie_tags = $simplepie_item
      ->get_categories();
    $tags = array();
    $domains = array();
    if (count($simplepie_tags) > 0) {
      foreach ($simplepie_tags as $tag) {
        $tags[] = (string) $tag->term;
        $domain = (string) $tag
          ->get_scheme();
        if (!empty($domain)) {
          if (!isset($domains[$domain])) {
            $domains[$domain] = array();
          }
          $domains[$domain][] = count($tags) - 1;
        }
      }
    }
    $item['domains'] = $domains;
    $item['tags'] = $tags;

    // Allow parsing to be extended.
    $this
      ->parseExtensions($item, $simplepie_item);
    $item['raw'] = $simplepie_item->data;
    $result->items[] = $item;
  }

  // Release parser.
  unset($parser);
  return $result;
}