summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--businesslayer/feedbusinesslayer.php5
-rw-r--r--tests/unit/utility/FeedFetcherTest.php68
-rw-r--r--utility/feedfetcher.php36
-rw-r--r--utility/ifeedfetcher.php6
-rw-r--r--utility/twitterfetcher.php10
5 files changed, 91 insertions, 34 deletions
diff --git a/businesslayer/feedbusinesslayer.php b/businesslayer/feedbusinesslayer.php
index c0d0fdf21..2370be9c9 100644
--- a/businesslayer/feedbusinesslayer.php
+++ b/businesslayer/feedbusinesslayer.php
@@ -159,7 +159,10 @@ class FeedBusinessLayer extends BusinessLayer {
try {
list($feed, $items) = $this->feedFetcher->fetch(
- $existingFeed->getUrl());
+ $existingFeed->getUrl(), false);
+
+ // keep the current faviconLink
+ $feed->setFaviconLink($existingFeed->getFaviconLink());
// insert items in reverse order because the first one is usually
// the newest item
diff --git a/tests/unit/utility/FeedFetcherTest.php b/tests/unit/utility/FeedFetcherTest.php
index fdc817543..b783da296 100644
--- a/tests/unit/utility/FeedFetcherTest.php
+++ b/tests/unit/utility/FeedFetcherTest.php
@@ -91,13 +91,13 @@ class FeedFetcherTest extends \OCA\AppFramework\Utility\TestUtility {
->will($this->returnValue($this->time));
$this->cacheDuration = 100;
$this->cacheDirectory = 'dir/';
- $this->fetcher = new FeedFetcher($this->getAPIMock(),
- $this->coreFactory,
- $this->faviconFetcher,
- $timeFactory,
- $this->cacheDirectory,
- $this->cacheDuration,
- $this->purifier);
+ $this->fetcher = new FeedFetcher($this->getAPIMock(),
+ $this->coreFactory,
+ $this->faviconFetcher,
+ $timeFactory,
+ $this->cacheDirectory,
+ $this->cacheDuration,
+ $this->purifier);
$this->url = 'tests';
$this->permalink = 'http://permalink';
@@ -140,7 +140,7 @@ class FeedFetcherTest extends \OCA\AppFramework\Utility\TestUtility {
$this->setExpectedException('\OCA\News\Utility\FetcherException');
$this->fetcher->fetch($this->url);
}
-
+
public function testShouldCatchExceptionsAndThrowOwnException() {
$this->core->expects($this->once())
@@ -313,4 +313,56 @@ class FeedFetcherTest extends \OCA\AppFramework\Utility\TestUtility {
$this->assertEquals(array($feed, array($item)), $result);
}
+ public function testFetchMapItemsGetFavicon() {
+ $this->expectCore('get_title', $this->feedTitle);
+ $this->expectCore('get_link', $this->feedLink);
+
+ $feed = new Feed();
+ $feed->setTitle(html_entity_decode($this->feedTitle));
+ $feed->setUrl($this->url);
+ $feed->setLink($this->feedLink);
+ $feed->setUrlHash(md5($this->url));
+ $feed->setAdded($this->time);
+ $feed->setFaviconLink($this->webFavicon);
+
+ $this->core->expects($this->once())
+ ->method('init')
+ ->will($this->returnValue(true));
+
+ $this->faviconFetcher->expects($this->once())
+ ->method('fetch')
+ ->will($this->returnValue($this->webFavicon));
+
+ $item = $this->createItem(false, true);
+ $this->expectCore('get_items', array($this->item));
+ $result = $this->fetcher->fetch($this->url /*, true*/);
+
+ $this->assertEquals(array($feed, array($item)), $result);
+ }
+
+ public function testFetchMapItemsNoGetFavicon() {
+ $this->expectCore('get_title', $this->feedTitle);
+ $this->expectCore('get_link', $this->feedLink);
+
+ $feed = new Feed();
+ $feed->setTitle(html_entity_decode($this->feedTitle));
+ $feed->setUrl($this->url);
+ $feed->setLink($this->feedLink);
+ $feed->setUrlHash(md5($this->url));
+ $feed->setAdded($this->time);
+
+ $this->core->expects($this->once())
+ ->method('init')
+ ->will($this->returnValue(true));
+
+ $this->faviconFetcher->expects($this->never())
+ ->method('fetch');
+
+ $item = $this->createItem(false, true);
+ $this->expectCore('get_items', array($this->item));
+ $result = $this->fetcher->fetch($this->url, false);
+
+ $this->assertEquals(array($feed, array($item)), $result);
+ }
+
}
diff --git a/utility/feedfetcher.php b/utility/feedfetcher.php
index 63e666f65..e4a9019ac 100644
--- a/utility/feedfetcher.php
+++ b/utility/feedfetcher.php
@@ -44,11 +44,11 @@ class FeedFetcher implements IFeedFetcher {
private $time;
private $purifier;
- public function __construct(API $api,
+ public function __construct(API $api,
SimplePieAPIFactory $simplePieFactory,
FaviconFetcher $faviconFetcher,
TimeFactory $time,
- $cacheDirectory,
+ $cacheDirectory,
$cacheDuration,
$purifier){
$this->api = $api;
@@ -75,13 +75,13 @@ class FeedFetcher implements IFeedFetcher {
* @throws FetcherException if simple pie fails
* @return array an array containing the new feed and its items
*/
- public function fetch($url) {
+ public function fetch($url, $getFavicon=true) {
$simplePie = $this->simplePieFactory->getCore();
$simplePie->set_feed_url($url);
$simplePie->enable_cache(true);
$simplePie->set_cache_location($this->cacheDirectory);
$simplePie->set_cache_duration($this->cacheDuration);
-
+
if (!$simplePie->init()) {
throw new FetcherException('Could not initialize simple pie');
}
@@ -96,7 +96,7 @@ class FeedFetcher implements IFeedFetcher {
}
}
- $feed = $this->buildFeed($simplePie, $url);
+ $feed = $this->buildFeed($simplePie, $url, $getFavicon);
return array($feed, $items);
@@ -119,7 +119,7 @@ class FeedFetcher implements IFeedFetcher {
$guid = $simplePieItem->get_id();
$item->setGuid($guid);
$item->setGuidHash(md5($guid));
- $item->setBody(str_replace('<a', '<a target="_blank"',
+ $item->setBody(str_replace('<a', '<a target="_blank"',
// escape XSS
$this->purifier->purify($simplePieItem->get_content())));
$item->setPubDate($simplePieItem->get_date('U'));
@@ -127,12 +127,12 @@ class FeedFetcher implements IFeedFetcher {
$author = $simplePieItem->get_author();
if ($author !== null) {
- $name = html_entity_decode($author->get_name(),
+ $name = html_entity_decode($author->get_name(),
ENT_COMPAT, 'UTF-8' );
if ($name) {
$item->setAuthor($name);
} else {
- $item->setAuthor(html_entity_decode($author->get_email()),
+ $item->setAuthor(html_entity_decode($author->get_email()),
ENT_COMPAT, 'UTF-8' );
}
}
@@ -151,13 +151,13 @@ class FeedFetcher implements IFeedFetcher {
}
- protected function buildFeed($simplePieFeed, $url) {
+ protected function buildFeed($simplePieFeed, $url, $getFavicon) {
$feed = new Feed();
// unescape content because angularjs helps against XSS
- $title = html_entity_decode($simplePieFeed->get_title(),
+ $title = html_entity_decode($simplePieFeed->get_title(),
ENT_COMPAT, 'UTF-8' );
-
+
// if there is no title use the url
if(!$title) {
$title = $url;
@@ -169,14 +169,16 @@ class FeedFetcher implements IFeedFetcher {
$feed->setUrlHash(md5($url));
$feed->setAdded($this->time->getTime());
- // use the favicon from the page first since most feeds use a weird image
- $favicon = $this->faviconFetcher->fetch($feed->getLink());
+ if ($getFavicon) {
+ // use the favicon from the page first since most feeds use a weird image
+ $favicon = $this->faviconFetcher->fetch($feed->getLink());
+
+ if (!$favicon) {
+ $favicon = $simplePieFeed->get_image_url();
+ }
- if (!$favicon) {
- $favicon = $simplePieFeed->get_image_url();
+ $feed->setFaviconLink($favicon);
}
-
- $feed->setFaviconLink($favicon);
return $feed;
}
diff --git a/utility/ifeedfetcher.php b/utility/ifeedfetcher.php
index 2bf663c0f..c64b0a90f 100644
--- a/utility/ifeedfetcher.php
+++ b/utility/ifeedfetcher.php
@@ -26,16 +26,16 @@
namespace OCA\News\Utility;
interface IFeedFetcher {
-
+
/**
* @param string url the url that the user entered in the add feed dialog
- * box
+ * box
* @throws FetcherException if the fetcher encounters a problem
* @return array with the first element being the feed and the
* second element being an array of items. Those items will be saved into
* into the database
*/
- function fetch($url);
+ function fetch($url, $getFavicon=true);
/**
* @param string $url the url that should be fetched
diff --git a/utility/twitterfetcher.php b/utility/twitterfetcher.php
index 7e09ddcb9..aad22cf8e 100644
--- a/utility/twitterfetcher.php
+++ b/utility/twitterfetcher.php
@@ -32,10 +32,10 @@ class TwitterFetcher implements IFeedFetcher {
private $fetcher;
private $regex;
-
+
public function __construct(FeedFetcher $fetcher){
$this->fetcher = $fetcher;
-
+
// matches the following urls and extracts the username
// https://twitter.com/GeorgeTakei
// https://www.twitter.com/GeorgeTakei
@@ -53,16 +53,16 @@ class TwitterFetcher implements IFeedFetcher {
}
- public function fetch($url){
+ public function fetch($url, $getFavicon=true){
preg_match($this->regex, $url, $match);
// FIXME: implement twitter api to be future proof
// deprecated Twitter RSS API
- $rssUrl = 'https://api.twitter.com/1/statuses/user_timeline.' .
+ $rssUrl = 'https://api.twitter.com/1/statuses/user_timeline.' .
'rss?screen_name=' . $match[1];
// use transformed url to fetch the rss feeds with the default fetcher
- return $this->fetcher->fetch($rssUrl);
+ return $this->fetcher->fetch($rssUrl, $getFavicon);
}