This commit is contained in:
Bernhard Posselt 2014-10-30 11:30:12 +01:00
Родитель b1e7d7b084
Коммит 91f302245e
3 изменённых файлов: 23 добавлений и 55 удалений

Просмотреть файл

@ -63,6 +63,7 @@ class FeedFetcher implements IFeedFetcher {
$modified = $resource->getLastModified(); $modified = $resource->getLastModified();
$etag = $resource->getEtag(); $etag = $resource->getEtag();
$location = $resource->getUrl();
if (!$resource->isModified()) { if (!$resource->isModified()) {
return [null, null]; return [null, null];
@ -85,14 +86,12 @@ class FeedFetcher implements IFeedFetcher {
} }
$items = []; $items = [];
$link = $parsedFeed->getUrl();
foreach($parsedFeed->getItems() as $item) { foreach($parsedFeed->getItems() as $item) {
$items[] = $this->buildItem($item); $items[] = $this->buildItem($item);
} }
$feed = $this->buildFeed( $feed = $this->buildFeed(
$parsedFeed, $url, $getFavicon, $modified, $etag $parsedFeed, $url, $getFavicon, $modified, $etag, $location
); );
return [$feed, $items]; return [$feed, $items];
@ -120,14 +119,15 @@ class FeedFetcher implements IFeedFetcher {
protected function buildItem($parsedItem) { protected function buildItem($parsedItem) {
$item = new Item(); $item = new Item();
$item->setStatus(0);
$item->setUnread(); $item->setUnread();
$item->setUrl($parsedItem->getUrl()); $item->setUrl($parsedItem->getUrl());
$item->setGuid($parsedItem->getId());
$item->setPubDate($parsedItem->getDate());
$item->setLastModified($this->time->getTime());
// unescape content because angularjs helps against XSS // unescape content because angularjs helps against XSS
$item->setTitle($this->decodeTwice($parsedItem->getTitle())); $item->setTitle($this->decodeTwice($parsedItem->getTitle()));
$guid = $parsedItem->getId(); $item->setAuthor($this->decodeTwice($parsedItem->getAuthor()));
$item->setGuid($guid);
// purification is done in the service layer // purification is done in the service layer
$body = $parsedItem->getContent(); $body = $parsedItem->getContent();
@ -135,17 +135,6 @@ class FeedFetcher implements IFeedFetcher {
mb_detect_encoding($body)); mb_detect_encoding($body));
$item->setBody($body); $item->setBody($body);
// pubdate is not required. if not given use the current date
$date = $parsedItem->getDate();
$item->setPubDate($date);
$item->setLastModified($this->time->getTime());
$author = $parsedItem->getAuthor();
$item->setAuthor($this->decodeTwice($author));
// TODO: make it work for video files also
$enclosureUrl = $parsedItem->getEnclosureUrl(); $enclosureUrl = $parsedItem->getEnclosureUrl();
if($enclosureUrl) { if($enclosureUrl) {
$enclosureType = $parsedItem->getEnclosureType(); $enclosureType = $parsedItem->getEnclosureType();
@ -161,28 +150,23 @@ class FeedFetcher implements IFeedFetcher {
protected function buildFeed($parsedFeed, $url, $getFavicon, $modified, protected function buildFeed($parsedFeed, $url, $getFavicon, $modified,
$etag) { $etag, $location) {
$feed = new Feed(); $feed = new Feed();
$link = $parsedFeed->getUrl();
if (!$link) {
$link = $location;
}
// unescape content because angularjs helps against XSS // unescape content because angularjs helps against XSS
$title = strip_tags($this->decodeTwice($parsedFeed->getTitle())); $title = strip_tags($this->decodeTwice($parsedFeed->getTitle()));
// if there is no title use the url
if(!$title) {
$title = $url;
}
$feed->setTitle($title); $feed->setTitle($title);
$feed->setUrl($url); $feed->setUrl($url); // the url used to add the feed
$feed->setLocation($location); // the url where the feed was found
$feed->setLink($link); // <link> attribute in the feed
$feed->setLastModified($modified); $feed->setLastModified($modified);
$feed->setEtag($etag); $feed->setEtag($etag);
$link = $parsedFeed->getUrl();
if (!$link) {
$link = $url;
}
$feed->setLink($link);
$feed->setAdded($this->time->getTime()); $feed->setAdded($this->time->getTime());
if ($getFavicon) { if ($getFavicon) {

Просмотреть файл

@ -219,6 +219,7 @@ class FeedService extends Service {
$existingFeed->setLastModified($fetchedFeed->getLastModified()); $existingFeed->setLastModified($fetchedFeed->getLastModified());
$existingFeed->setEtag($fetchedFeed->getEtag()); $existingFeed->setEtag($fetchedFeed->getEtag());
$existingFeed->setLocation($fetchedFeed->getLocation());
$this->feedMapper->update($existingFeed); $this->feedMapper->update($existingFeed);
// insert items in reverse order because the first one is // insert items in reverse order because the first one is

Просмотреть файл

@ -47,6 +47,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase {
private $webFavicon; private $webFavicon;
private $modified; private $modified;
private $etag; private $etag;
private $location;
protected function setUp(){ protected function setUp(){
$this->reader = $this->getMockBuilder( $this->reader = $this->getMockBuilder(
@ -133,6 +134,9 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase {
$this->client->expects($this->once()) $this->client->expects($this->once())
->method('getEtag') ->method('getEtag')
->will($this->returnValue($this->etag)); ->will($this->returnValue($this->etag));
$this->client->expects($this->once())
->method('getUrl')
->will($this->returnValue($this->location));
if (!$modified) { if (!$modified) {
$this->reader->expects($this->never()) $this->reader->expects($this->never())
@ -211,7 +215,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase {
private function createFeed($hasFavicon=false) { private function createFeed($hasFavicon=false) {
$this->expectFeed('getTitle', $this->feedTitle); $this->expectFeed('getTitle', $this->feedTitle);
$this->expectFeed('getUrl', $this->feedLink, 2); $this->expectFeed('getUrl', $this->feedLink);
$feed = new Feed(); $feed = new Feed();
$feed->setTitle('&its a title'); $feed->setTitle('&its a title');
@ -220,6 +224,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase {
$feed->setAdded($this->time); $feed->setAdded($this->time);
$feed->setLastModified($this->modified); $feed->setLastModified($this->modified);
$feed->setEtag($this->etag); $feed->setEtag($this->etag);
$feed->setLocation($this->location);
if($hasFavicon) { if($hasFavicon) {
$this->faviconFactory->expects($this->once()) $this->faviconFactory->expects($this->once())
@ -267,28 +272,6 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase {
} }
public function testNoTitleUsesUrl(){
$this->setUpReader($this->url);
$this->expectFeed('getTitle', '');
$this->expectFeed('getUrl', $this->feedLink, 2);
$feed = new Feed();
$feed->setTitle($this->url);
$feed->setUrl($this->url);
$feed->setLink($this->feedLink);
$feed->setAdded($this->time);
$feed->setFaviconLink(null);
$feed->setLastModified($this->modified);
$feed->setEtag($this->etag);
$item = $this->createItem();
$this->expectFeed('getItems', [$this->item]);
$result = $this->fetcher->fetch($this->url, false);
$this->assertEquals([$feed, [$item]], $result);
}
public function testAudioEnclosure(){ public function testAudioEnclosure(){
$this->setUpReader($this->url); $this->setUpReader($this->url);
$item = $this->createItem('audio/ogg'); $item = $this->createItem('audio/ogg');