You are here

class Feed in Feeds 8.3

Same name in this branch
  1. 8.3 src/Utility/Feed.php \Drupal\feeds\Utility\Feed
  2. 8.3 src/Entity/Feed.php \Drupal\feeds\Entity\Feed

Helper functions for dealing with feeds.

Hierarchy

  • class \Drupal\feeds\Utility\Feed

Expanded class hierarchy of Feed

2 files declare their use of Feed
HttpFetcher.php in src/Feeds/Fetcher/HttpFetcher.php
HttpFetcherFeedForm.php in src/Feeds/Fetcher/Form/HttpFetcherFeedForm.php

File

src/Utility/Feed.php, line 12

Namespace

Drupal\feeds\Utility
View source
class Feed {

  /**
   * Discovers RSS or Atom feeds from a document.
   *
   * If the document is an HTML document, this attempts to discover RSS or Atom
   * feeds referenced from the page.
   *
   * @param string $url
   *   The URL of the document.
   * @param string $document
   *   The document to find feeds in. Either an HTML or XML document.
   *
   * @return string|false
   *   The discovered feed, or false if a feed could not be found.
   */
  public static function getCommonSyndication($url, $document) {

    // If this happens to be a feed then just return the url.
    if (static::isFeed($document)) {
      return $url;
    }
    return static::findFeed($url, $document);
  }

  /**
   * Returns if the provided $content_type is a feed.
   *
   * @param string $data
   *   The actual HTML or XML document from the HTTP request.
   *
   * @return bool
   *   Returns true if this is a parsable feed, false if not.
   */
  public static function isFeed($data) {
    Reader::setExtensionManager(\Drupal::service('feed.bridge.reader'));
    try {
      $feed_type = Reader::detectType($data);
    } catch (\Exception $e) {
      return FALSE;
    }
    return $feed_type != Reader::TYPE_ANY;
  }

  /**
   * Finds potential feed tags in an HTML document.
   *
   * @param string $url
   *   The URL of the document, to use as a base URL.
   * @param string $html
   *   The HTML document to search.
   *
   * @return string|false
   *   The URL of the first feed link found, or false if unable to find a link.
   */
  public static function findFeed($url, $html) {
    $use_error = libxml_use_internal_errors(TRUE);
    $entity_loader = libxml_disable_entity_loader(TRUE);
    $dom = new \DOMDocument();
    $status = $dom
      ->loadHTML(trim($html));
    libxml_disable_entity_loader($entity_loader);
    libxml_use_internal_errors($use_error);
    if (!$status) {
      return FALSE;
    }
    $feed_set = new FeedSet();
    $feed_set
      ->addLinks($dom
      ->getElementsByTagName('link'), $url);

    // Load the first feed type found.
    foreach ([
      'atom',
      'rss',
      'rdf',
    ] as $feed_type) {
      if (isset($feed_set->{$feed_type})) {
        return $feed_set->{$feed_type};
      }
    }
    return FALSE;
  }

  /**
   * Translates the scheme of feed-type URLs into HTTP.
   *
   * @param string $url
   *   The URL to translate.
   *
   * @return string
   *   The URL with the scheme translated.
   *
   * @throws \InvalidArgumentException
   *   Thrown then the URL contains an invalid scheme.
   */
  public static function translateSchemes($url) {
    $uri = new Uri($url);
    switch ($uri
      ->getScheme()) {
      case 'http':
      case 'feed':
      case 'webcal':
        return (string) $uri
          ->withScheme('http');
      case 'https':
      case 'feeds':
      case 'webcals':
        return (string) $uri
          ->withScheme('https');
    }
    throw new \InvalidArgumentException();
  }

  /**
   * Returns which url schemes are supported by Feeds.
   *
   * @return array
   *   The support schemes.
   */
  public static function getSupportedSchemes() {
    return [
      'http',
      'feed',
      'webcal',
      'https',
      'feeds',
      'webcals',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Feed::findFeed public static function Finds potential feed tags in an HTML document.
Feed::getCommonSyndication public static function Discovers RSS or Atom feeds from a document.
Feed::getSupportedSchemes public static function Returns which url schemes are supported by Feeds.
Feed::isFeed public static function Returns if the provided $content_type is a feed.
Feed::translateSchemes public static function Translates the scheme of feed-type URLs into HTTP.