Пример #1
0
 public function rssProxyAction()
 {
     $overrideFeedUrl = $this->getEvent()->getRequest()->getQuery()->get('urlOverride');
     $limit = $this->getEvent()->getRequest()->getQuery()->get('limit');
     $instanceId = $this->getEvent()->getRequest()->getQuery()->get('instanceId');
     /** @var \Rcm\Service\PluginManager $pluginManager */
     $pluginManager = $this->serviceLocator->get('\\Rcm\\Service\\PluginManager');
     if ($instanceId > 0) {
         $instanceConfig = $pluginManager->getInstanceConfig($instanceId);
     } else {
         $instanceConfig = $pluginManager->getDefaultInstanceConfig('RcmRssFeed');
     }
     $feedUrl = $instanceConfig['rssFeedUrl'];
     $cacheKey = 'rcmrssfeed-' . md5($feedUrl);
     if ($this->cacheMgr->hasItem($cacheKey)) {
         $viewRssData = json_decode($this->cacheMgr->getItem($cacheKey));
         $this->sendJson($viewRssData);
     }
     if (!empty($overrideFeedUrl) && $overrideFeedUrl != 'null') {
         //$permissions = $this->userMgr->getLoggedInAdminPermissions();
         $permissions = null;
         /**
          * Only admins can override the url. This prevents people from using
          * our proxy to DDOS other sites.
          */
         $allowed = $this->rcmIsAllowed('sites.' . $this->siteId, 'admin');
         if ($allowed) {
             $feedUrl = $overrideFeedUrl;
         }
     }
     if (empty($limit)) {
         $limit = $instanceConfig['rssFeedLimit'];
     }
     $rssReader = new Reader();
     //Tried to add a timeout like this but it didnt work
     $httpClient = new Client($feedUrl, ['timeout' => 5]);
     $rssReader->setHttpClient($httpClient);
     try {
         $feedData = $rssReader->import($feedUrl);
     } catch (\Exception $e) {
         $feedData = [];
     }
     $feedCount = 0;
     $viewRssData = [];
     foreach ($feedData as $entry) {
         if ($feedCount == $limit) {
             break;
         }
         $viewRssData[] = ['feedtitle' => $entry->getTitle(), 'description' => $entry->getDescription(), 'dateModified' => $entry->getDateModified(), 'authors' => $entry->getAuthors(), 'feedlink' => $entry->getLink()];
         $feedCount++;
     }
     $this->cacheMgr->addItem($cacheKey, json_encode($viewRssData));
     $this->sendJson($viewRssData);
 }
Пример #2
0
 /**
  * Get an array with feed authors
  *
  * @return array
  */
 public function getAuthors()
 {
     if (array_key_exists('authors', $this->data)) {
         return $this->data['authors'];
     }
     $authors = array();
     $list = $this->getXpath()->query('//dc11:creator');
     if (!$list->length) {
         $list = $this->getXpath()->query('//dc10:creator');
     }
     if (!$list->length) {
         $list = $this->getXpath()->query('//dc11:publisher');
         if (!$list->length) {
             $list = $this->getXpath()->query('//dc10:publisher');
         }
     }
     if ($list->length) {
         foreach ($list as $author) {
             $authors[] = array('name' => $author->nodeValue);
         }
         $authors = new Collection\Author(Reader\Reader::arrayUnique($authors));
     } else {
         $authors = null;
     }
     $this->data['authors'] = $authors;
     return $this->data['authors'];
 }
Пример #3
0
 public function testCanDetectHubs()
 {
     $feed = \Zend\Feed\Reader\Reader::importFile(__DIR__ . '/_files/rss20.xml');
     $this->assertEquals(array(
         'http://www.example.com/hub', 'http://www.example.com/hub2'
     ), PubSubHubbub\PubSubHubbub::detectHubs($feed));
 }
Пример #4
0
 /**
  * Get an array with feed authors
  *
  * @return array
  */
 public function getAuthors()
 {
     if (array_key_exists('authors', $this->_data)) {
         return $this->_data['authors'];
     }
     $authors = array();
     $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:author');
     if (!$list->length) {
         /**
          * TODO: Limit query to feed level els only!
          */
         $list = $this->getXpath()->query('//atom:author');
     }
     if ($list->length) {
         foreach ($list as $author) {
             $author = $this->_getAuthor($author);
             if (!empty($author)) {
                 $authors[] = $author;
             }
         }
     }
     if (count($authors) == 0) {
         $authors = null;
     } else {
         $authors = new Collection\Author(Reader\Reader::arrayUnique($authors));
     }
     $this->_data['authors'] = $authors;
     return $this->_data['authors'];
 }
Пример #5
0
    /**
     * Get an array with feed authors
     *
     * @return array
     */
    public function getAuthors()
    {
        if (array_key_exists('authors', $this->_data)) {
            return $this->_data['authors'];
        }

        $list = $this->_xpath->query('//atom:author');

        $authors = array();

        if ($list->length) {
            foreach ($list as $author) {
                $author = $this->_getAuthor($author);
                if (!empty($author)) {
                    $authors[] = $author;
                }
            }
        }

        if (count($authors) == 0) {
            $authors = null;
        } else {
            $authors = new Collection\Author(
                Reader\Reader::arrayUnique($authors)
            );
        }

        $this->_data['authors'] = $authors;

        return $this->_data['authors'];
    }
Пример #6
0
 /**
  * Process RSS feed and return results.
  *
  * @param $feed_url
  * @param null $cache_name
  * @param int $cache_expires
  * @return array|mixed
  */
 public static function getNewsFeed($feed_url, $cache_name = NULL, $cache_expires = 900)
 {
     if (!is_null($cache_name)) {
         $feed_cache = Cache::get('feed_' . $cache_name);
     } else {
         $feed_cache = null;
     }
     if ($feed_cache) {
         return $feed_cache;
     }
     // Catch the occasional error when the RSS feed is malformed or the HTTP request times out.
     try {
         $news_feed = Reader::import($feed_url);
     } catch (\Exception $e) {
         $news_feed = NULL;
     }
     if (!is_null($news_feed)) {
         $latest_news = array();
         $article_num = 0;
         foreach ($news_feed as $item) {
             $article_num++;
             $news_item = array('num' => $article_num, 'title' => $item->getTitle(), 'timestamp' => $item->getDateModified()->getTimestamp(), 'description' => trim($item->getDescription()), 'link' => $item->getLink(), 'categories' => $item->getCategories()->getValues());
             $latest_news[] = $news_item;
         }
         $latest_news = array_slice($latest_news, 0, 10);
         if (!is_null($cache_name)) {
             Cache::set($latest_news, 'feed_' . $cache_name, array('feeds', $cache_name), $cache_expires);
         }
         return $latest_news;
     }
 }
Пример #7
0
 /**
  * Loads a newsfeed object.
  *
  * @param string $feedurl
  * @param bool   $cache
  * @return Reader
  */
 public function load($url, $cache = true)
 {
     if ($cache) {
         Reader::setCache(new ZendCacheDriver('cache/expensive'));
     }
     $feed = Reader::import($url);
     return $feed;
 }
Пример #8
0
 public function read() : array
 {
     $url = sprintf(self::ATOM_FORMAT, $this->user);
     $feed = FeedReader::import($url);
     $entries = Collection::create($feed)->filterChain($this->filters)->slice($this->limit)->map(function ($entry) {
         return ['title' => $entry->getTitle(), 'link' => $entry->getLink()];
     });
     return ['last_modified' => $feed->getDateModified(), 'link' => $feed->getLink(), 'links' => $entries->toArray()];
 }
Пример #9
0
 /**
  * Loads feed from an url or file path
  *
  * @param string $file
  *
  * @return Reader
  */
 public function load($file)
 {
     if (file_exists($file)) {
         $this->feed = ZendReader::importFile($file);
     } else {
         $this->feed = ZendReader::import($file);
     }
     return $this;
 }
Пример #10
0
 /**
  * Returns if the provided $content_type is a feed.
  *
  * @param string $document
  *   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;
 }
Пример #11
0
 /**
  * Loads a newsfeed object.
  *
  * @param string $feedurl
  * @param int    $cache - number of seconds to cache the RSS feed data for
  * @return Reader
  */
 public function load($url, $cache = 3600)
 {
     if ($cache !== false) {
         Reader::setCache(new ZendCacheDriver('cache/expensive', $cache));
     }
     // Load the RSS feed, either from remote URL or from cache
     // (if specified above and still fresh)
     $feed = Reader::import($url);
     return $feed;
 }
Пример #12
0
 /**
  * Simple utility function which imports any feed URL and
  * determines the existence of Hub Server endpoints. This works
  * best if directly given an instance of Zend_Feed_Reader_Atom|Rss
  * to leverage off.
  *
  * @param  \Zend\Feed\Reader\Feed\AbstractFeed|string $source
  * @return array
  * @throws Exception\InvalidArgumentException
  */
 public static function detectHubs($source)
 {
     if (is_string($source)) {
         $feed = Reader\Reader::import($source);
     } elseif ($source instanceof Reader\Feed\AbstractFeed) {
         $feed = $source;
     } else {
         throw new Exception\InvalidArgumentException('The source parameter was' . ' invalid, i.e. not a URL string or an instance of type' . ' Zend\\Feed\\Reader\\Feed\\AbstractFeed');
     }
     return $feed->getHubs();
 }
Пример #13
0
 public function run()
 {
     $feed = Reader::import($this->feedUrl);
     $data = array('title' => $feed->getTitle(), 'link' => $feed->getLink(), 'dateModified' => $feed->getDateModified(), 'description' => $feed->getDescription(), 'language' => $feed->getLanguage(), 'entries' => array());
     foreach ($feed as $entry) {
         $edata = array('title' => $entry->getTitle(), 'description' => $entry->getDescription(), 'dateModified' => $entry->getDateModified(), 'authors' => $entry->getAuthors(), 'link' => $entry->getLink(), 'content' => $entry->getContent());
         $data['entries'][] = $edata;
     }
     echo $this->render('default', ['data' => $data]);
     // $this->registerClientScript();
 }
Пример #14
0
 /**
  * Constructor
  *
  * @param  DOMDocument $dom
  * @param  string $type
  */
 public function __construct(DOMDocument $dom, $type = null)
 {
     parent::__construct($dom, $type);
     $atomClass = Reader\Reader::getPluginLoader()->getClassName('Atom\\Feed');
     $this->_extensions['Atom\\Feed'] = new $atomClass($dom, $this->_data['type'], $this->_xpath);
     $atomClass = Reader\Reader::getPluginLoader()->getClassName('DublinCore\\Feed');
     $this->_extensions['DublinCore\\Feed'] = new $atomClass($dom, $this->_data['type'], $this->_xpath);
     foreach ($this->_extensions as $extension) {
         $extension->setXpathPrefix('/atom:feed');
     }
 }
Пример #15
0
 public function __invoke($services)
 {
     $http = $services->get('http');
     FeedReader::setHttpClient($http);
     FeedReader::setExtensionManager($this->createExtensionManager());
     $config = $services->get('Config');
     $config = $config['github'];
     $reader = new AtomReader($config['user']);
     $reader->setLimit($config['limit']);
     return $reader;
 }
Пример #16
0
 /**
  * Constructor
  *
  * @param  DOMElement $entry
  * @param  int $entryKey
  * @param  string $type
  * @return void
  */
 public function __construct(\DOMElement $entry, $entryKey, $type = null)
 {
     parent::__construct($entry, $entryKey, $type);
     // Everyone by now should know XPath indices start from 1 not 0
     $this->_xpathQuery = '//atom:entry[' . ($this->_entryKey + 1) . ']';
     $atomClass = Reader\Reader::getPluginLoader()->getClassName('Atom\\Entry');
     $this->_extensions['Atom\\Entry'] = new $atomClass($entry, $entryKey, $type);
     $threadClass = Reader\Reader::getPluginLoader()->getClassName('Thread\\Entry');
     $this->_extensions['Thread\\Entry'] = new $threadClass($entry, $entryKey, $type);
     $threadClass = Reader\Reader::getPluginLoader()->getClassName('DublinCore\\Entry');
     $this->_extensions['DublinCore\\Entry'] = new $threadClass($entry, $entryKey, $type);
 }
Пример #17
0
 function setUp()
 {
     parent::setUp();
     // Do not delete old aggregator items during these tests, since our sample
     // feeds have hardcoded dates in them (which may be expired when this test
     // is run).
     $this->container->get('config.factory')->get('aggregator.settings')->set('items.expire', AGGREGATOR_CLEAR_NEVER)->save();
     // Reset any reader cache between tests.
     Reader::reset();
     // Set our bridge extension manager to Zend Feed.
     $bridge = $this->container->get('feed.bridge.reader');
     Reader::setExtensionManager($bridge);
 }
Пример #18
0
 public function import()
 {
     $this->logger('Import ' . $this->uri, 'info', $this->logger_level);
     try {
         $this->feed = Reader::import($this->uri);
         if (!isset($this->feed)) {
             throw new \Exception('Unreadble');
         }
     } catch (\Exception $e) {
         $this->logger('Feed empty ', 'err', $this->logger_level);
         return false;
     }
 }
Пример #19
0
 /**
  * @return array
  */
 public function getEntries()
 {
     if (empty($this->entries)) {
         $feeds = $this->getOutlet()->getFeeds();
         foreach ($feeds as $feed) {
             $reader = Reader::import($feed->getUrl());
             foreach ($reader as $entry) {
                 $this->entries[] = $entry;
             }
         }
     }
     return $this->entries;
 }
Пример #20
0
 /**
  * Simple utility function which imports any feed URL and
  * determines the existence of Hub Server endpoints. This works
  * best if directly given an instance of Zend_Feed_Reader_Atom|Rss
  * to leverage off.
  *
  * @param  Zend_Feed_Reader_FeedAbstract|\Zend\Feed\AbstractFeed|string $source
  * @return array
  */
 public static function detectHubs($source)
 {
     if (is_string($source)) {
         $feed = Reader\Reader::import($source);
     } elseif (is_object($source) && $source instanceof Reader\FeedAbstract) {
         $feed = $source;
     } elseif (is_object($source) && $source instanceof \Zend\Feed\AbstractFeed) {
         $feed = Reader\Reader::importFeed($source);
     } else {
         require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
         throw new Exception('The source parameter was' . ' invalid, i.e. not a URL string or an instance of type' . ' Zend_Feed_Reader_FeedAbstract or Zend_Feed_Abstract');
     }
     return $feed->getHubs();
 }
Пример #21
0
 /**
  * Constructor: Create a Source object which is largely just a normal
  * Zend\Feed\Reader\AbstractFeed object only designed to retrieve feed level
  * metadata from an Atom entry's source element.
  *
  * @param DOMElement $source
  * @param string $xpathPrefix Passed from parent Entry object
  * @param string $type Nearly always Atom 1.0
  */
 public function __construct(\DOMElement $source, $xpathPrefix, $type = Reader\Reader::TYPE_ATOM_10)
 {
     $this->_domDocument = $source->ownerDocument;
     $this->_xpath = new \DOMXPath($this->_domDocument);
     $this->_data['type'] = $type;
     $this->_registerNamespaces();
     $this->_loadExtensions();
     $atomClass = Reader\Reader::getPluginLoader()->getClassName('Atom\\Feed');
     $this->_extensions['Atom\\Feed'] = new $atomClass($this->_domDocument, $this->_data['type'], $this->_xpath);
     $atomClass = Reader\Reader::getPluginLoader()->getClassName('DublinCore\\Feed');
     $this->_extensions['DublinCore\\Feed'] = new $atomClass($this->_domDocument, $this->_data['type'], $this->_xpath);
     foreach ($this->_extensions as $extension) {
         $extension->setXpathPrefix(rtrim($xpathPrefix, '/') . '/atom:source');
     }
 }
Пример #22
0
 /**
  * Constructor
  *
  * @param  DOMElement $entry
  * @param  int $entryKey
  * @param  string $type
  */
 public function __construct(DOMElement $entry, $entryKey, $type = null)
 {
     parent::__construct($entry, $entryKey, $type);
     // Everyone by now should know XPath indices start from 1 not 0
     $this->xpathQuery = '//atom:entry[' . ($this->entryKey + 1) . ']';
     $manager = Reader\Reader::getExtensionManager();
     $extensions = array('Atom\\Entry', 'Thread\\Entry', 'DublinCore\\Entry');
     foreach ($extensions as $name) {
         $extension = $manager->get($name);
         $extension->setEntryElement($entry);
         $extension->setEntryKey($entryKey);
         $extension->setType($type);
         $this->extensions[$name] = $extension;
     }
 }
Пример #23
0
 /**
  * @inheritDoc
  *
  */
 public function import()
 {
     if (empty($this->feedUri) || !is_string($this->feedUri)) {
         throw new Exception\UnexpectedValueException(sprintf('Feed uri not valid.'));
     }
     $feed = Reader::import($this->feedUri);
     foreach ($feed as $entry) {
         if ($this->entryIsNew($entry)) {
             $postId = $this->createPostFromEntry($entry);
             if (!empty($this->entryParams['taxonomy'])) {
                 $this->createTermsFromEntry($postId, $entry);
             }
         }
     }
 }
Пример #24
0
 /**
  * Constructor
  *
  * @param  Zend_Feed_Abstract $feed The source Zend_Feed object
  * @param  string $type Feed type
  * @return void
  */
 public function __construct(\DomDocument $dom, $type = null, \DOMXPath $xpath = null)
 {
     $this->_domDocument = $dom;
     if ($type !== null) {
         $this->_data['type'] = $type;
     } else {
         $this->_data['type'] = Reader\Reader::detectType($dom);
     }
     if ($xpath !== null) {
         $this->_xpath = $xpath;
     } else {
         $this->_xpath = new \DOMXPath($this->_domDocument);
     }
     $this->_registerNamespaces();
 }
Пример #25
0
 public function __invoke(ContainerInterface $container) : AtomReader
 {
     $http = $container->get(FeedReaderHttpClientInterface::class);
     FeedReader::setHttpClient($http);
     FeedReader::setExtensionManager(new StandaloneExtensionManager());
     $config = $container->get('config');
     $config = $config['github'];
     $reader = new AtomReader($config['user']);
     $reader->setLimit($config['limit']);
     $reader->addFilter(function ($entry) {
         if (false !== strpos($entry->getLink(), 'weierophinney/mwop.net')) {
             return false;
         }
         return true;
     });
     return $reader;
 }
Пример #26
0
 /**
  * Constructor
  *
  * @param  DOMDocument $dom
  * @param  string $type
  */
 public function __construct(DOMDocument $dom, $type = null)
 {
     parent::__construct($dom, $type);
     $manager = Reader\Reader::getExtensionManager();
     $atomFeed = $manager->get('Atom\\Feed');
     $atomFeed->setDomDocument($dom);
     $atomFeed->setType($this->data['type']);
     $atomFeed->setXpath($this->xpath);
     $this->extensions['Atom\\Feed'] = $atomFeed;
     $atomFeed = $manager->get('DublinCore\\Feed');
     $atomFeed->setDomDocument($dom);
     $atomFeed->setType($this->data['type']);
     $atomFeed->setXpath($this->xpath);
     $this->extensions['DublinCore\\Feed'] = $atomFeed;
     foreach ($this->extensions as $extension) {
         $extension->setXpathPrefix('/atom:feed');
     }
 }
Пример #27
0
 public function getData()
 {
     // Fetch the latest Slashdot headlines
     try {
         $slashdotRss = \Zend\Feed\Reader\Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
     } catch (\Zend\Feed\Exception\Reader\RuntimeException $e) {
         // feed import failed
         echo "Exception caught importing feed: {$e->getMessage()}\n";
         exit;
     }
     // Initialize the channel/feed data array
     $channel = array('title' => $slashdotRss->getTitle(), 'link' => $slashdotRss->getLink(), 'description' => $slashdotRss->getDescription(), 'items' => array());
     // Loop over each channel item/entry and store relevant data for each
     foreach ($slashdotRss as $item) {
         $channel['items'][] = array('title' => $item->getTitle(), 'link' => $item->getLink(), 'description' => $item->getDescription());
     }
     return $channel;
 }
Пример #28
0
 /**
  * Constructor
  *
  * @param  Zend_Feed_Entry_Abstract $entry
  * @param  int $entryKey
  * @param  string $type
  * @return void
  */
 public function __construct(\DOMElement $entry, $entryKey, $type = null)
 {
     $this->_entry = $entry;
     $this->_entryKey = $entryKey;
     $this->_domDocument = $entry->ownerDocument;
     if ($type !== null) {
         $this->_data['type'] = $type;
     } else {
         $this->_data['type'] = Reader\Reader::detectType($entry->ownerDocument, true);
     }
     // set the XPath query prefix for the entry being queried
     if ($this->getType() == Reader\Reader::TYPE_RSS_10 || $this->getType() == Reader\Reader::TYPE_RSS_090) {
         $this->setXpathPrefix('//rss:item[' . ($this->_entryKey + 1) . ']');
     } elseif ($this->getType() == Reader\Reader::TYPE_ATOM_10 || $this->getType() == Reader\Reader::TYPE_ATOM_03) {
         $this->setXpathPrefix('//atom:entry[' . ($this->_entryKey + 1) . ']');
     } else {
         $this->setXpathPrefix('//item[' . ($this->_entryKey + 1) . ']');
     }
 }
Пример #29
0
 /**
  * Constructor: Create a Source object which is largely just a normal
  * Zend\Feed\Reader\AbstractFeed object only designed to retrieve feed level
  * metadata from an Atom entry's source element.
  *
  * @param DOMElement $source
  * @param string $xpathPrefix Passed from parent Entry object
  * @param string $type Nearly always Atom 1.0
  */
 public function __construct(DOMElement $source, $xpathPrefix, $type = Reader\Reader::TYPE_ATOM_10)
 {
     $this->domDocument = $source->ownerDocument;
     $this->xpath = new DOMXPath($this->domDocument);
     $this->data['type'] = $type;
     $this->registerNamespaces();
     $this->loadExtensions();
     $manager = Reader\Reader::getExtensionManager();
     $extensions = array('Atom\\Feed', 'DublinCore\\Feed');
     foreach ($extensions as $name) {
         $extension = $manager->get($name);
         $extension->setDomDocument($this->domDocument);
         $extension->setType($this->data['type']);
         $extension->setXpath($this->xpath);
         $this->extensions[$name] = $extension;
     }
     foreach ($this->extensions as $extension) {
         $extension->setXpathPrefix(rtrim($xpathPrefix, '/') . '/atom:source');
     }
 }
Пример #30
0
 public static function fetch($feed_url, $params = array())
 {
     try {
         $news_feed = Reader::import($feed_url);
     } catch (\Exception $e) {
         return array();
     }
     if (is_null($news_feed)) {
         return array();
     }
     $latest_news = array();
     $article_num = 0;
     foreach ($news_feed as $item) {
         $article_num++;
         $guid = $item->getId();
         $title = $item->getTitle();
         // Process categories.
         $categories_raw = $item->getCategories()->getValues();
         // Process main description.
         $description = trim($item->getDescription());
         // Remove extraneous tags.
         $description = str_replace(array("\r", "\n"), array('', ' '), $description);
         // Strip new lines.
         $description = preg_replace('/<a[^(>)]+>read more<\\/a>/iu', '', $description);
         // Remove "read more" link.
         $web_url = $item->getLink();
         if (is_array($web_url)) {
             $web_url = $web_url[0];
         }
         if (!$web_url && substr($guid, 0, 4) == 'http') {
             $web_url = $guid;
         }
         $author = $item->getAuthor();
         if (is_array($author)) {
             $author = $author[0]->nodeValue;
         }
         $news_item = array('guid' => 'rss_' . md5($guid), 'timestamp' => $item->getDateModified()->getTimestamp(), 'media_format' => 'mixed', 'title' => $title, 'body' => $description, 'web_url' => $web_url, 'author' => $author);
         $latest_news[] = $news_item;
     }
     return $latest_news;
 }