summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-10-22 11:35:12 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-10-22 11:35:12 +0200
commitc102123dc902b855842d3e9942b4629abba83ebb (patch)
tree8d3058583cc0727ef79daa6d5ab34b2dd669fb0e
parentdc8b8301d387d48e38624423cba9cf5323f26291 (diff)
add caching
-rw-r--r--CHANGELOG.md1
-rw-r--r--fetcher/feedfetcher.php6
-rw-r--r--service/feedservice.php17
-rw-r--r--tests/unit/fetcher/FeedFetcherTest.php15
-rw-r--r--tests/unit/service/FeedServiceTest.php41
5 files changed, 69 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e6648ef5..a53b17cbc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@ owncloud-news (4.001)
* **Enhancement**: Get rid of cacheCuration setting and add maxRedirects setting in config.ini
* **Enhancement**: Get rid SimplePie feed parser library and switch to PicoFeed because SimplePie is unmaintained and full of bugs
* **Enhancement**: Use ownCloud internal proxy settings
+* **Enhancement**: Also provide feed last modified and etag headers over API
owncloud-news (3.406)
* **Enhancement**: Make config.ini editable in the admin interface
diff --git a/fetcher/feedfetcher.php b/fetcher/feedfetcher.php
index a3a2ab8bb..aa6a465b3 100644
--- a/fetcher/feedfetcher.php
+++ b/fetcher/feedfetcher.php
@@ -62,10 +62,13 @@ class FeedFetcher implements IFeedFetcher {
$modified = $resource->getLastModified();
$etag = $resource->getEtag();
+ if (!$resource->isModified()) {
+ return [null, null];
+ }
+
try {
$parser = $this->reader->getParser();
- // TBD: if ($resource->isModified()) { }
if (!$parser) {
throw new \Exception(
'Could not find a valid feed at url ' . $url
@@ -83,7 +86,6 @@ class FeedFetcher implements IFeedFetcher {
$link = $parsedFeed->getUrl();
foreach($parsedFeed->getItems() as $item) {
- //throw new \Exception($resource->getEncoding() . '' . $item->getContent());
$items[] = $this->buildItem($item);
}
diff --git a/service/feedservice.php b/service/feedservice.php
index 872f05ef8..6ecf543b1 100644
--- a/service/feedservice.php
+++ b/service/feedservice.php
@@ -192,14 +192,25 @@ class FeedService extends Service {
}
try {
- list(, $items) = $this->feedFetcher->fetch(
- $existingFeed->getUrl(), false);
+ list($fetchedFeed, $items) = $this->feedFetcher->fetch(
+ $existingFeed->getUrl(),
+ false,
+ $existingFeed->getLastModified(),
+ $existingFeed->getEtag()
+ );
+
+ // if there is no feed it means that no update took place
+ if (!$fetchedFeed) {
+ return $existingFeed;
+ }
// update number of articles on every feed update
if($existingFeed->getArticlesPerUpdate() !== count($items)) {
$existingFeed->setArticlesPerUpdate(count($items));
- $this->feedMapper->update($existingFeed);
}
+ $existingFeed->setLastModified($fetchedFeed->getLastModified());
+ $existingFeed->setEtag($fetchedFeed->getEtag());
+ $this->feedMapper->update($existingFeed);
// insert items in reverse order because the first one is
// usually the newest item
diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php
index db954d952..a0bd0b866 100644
--- a/tests/unit/fetcher/FeedFetcherTest.php
+++ b/tests/unit/fetcher/FeedFetcherTest.php
@@ -121,7 +121,10 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase {
->method('getEtag')
->will($this->returnValue($this->etag));
- if ($noParser) {
+ if (!$modified) {
+ $this->reader->expects($this->never())
+ ->method('getParser');
+ } else if ($noParser) {
$this->reader->expects($this->once())
->method('getParser')
->will($this->returnValue(false));
@@ -141,10 +144,9 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase {
}
}
- // uncomment if testing caching
- /*$this->client->expects($this->once())
+ $this->client->expects($this->once())
->method('isModified')
- ->will($this->returnValue($modified));*/
+ ->will($this->returnValue($modified));
}
@@ -234,6 +236,11 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase {
$this->fetcher->fetch($this->url);
}
+ public function testNoFetchIfNotModified(){
+ $this->setUpReader($this->url, false);;
+ $result = $this->fetcher->fetch($this->url);
+ }
+
public function testFetch(){
$this->setUpReader($this->url);
$item = $this->createItem();
diff --git a/tests/unit/service/FeedServiceTest.php b/tests/unit/service/FeedServiceTest.php
index 104413788..2b7205dba 100644
--- a/tests/unit/service/FeedServiceTest.php
+++ b/tests/unit/service/FeedServiceTest.php
@@ -262,7 +262,11 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase {
$feed = new Feed();
$feed->setId(3);
$feed->setArticlesPerUpdate(1);
+ $feed->setLink('http://test');
+ $feed->setUrl('http://test');
$feed->setUrlHash('yo');
+ $feed->setLastModified(3);
+ $feed->setEtag(4);
$item = new Item();
$item->setGuidHash(md5('hi'));
@@ -280,7 +284,16 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase {
->will($this->returnValue($feed));
$this->fetcher->expects($this->once())
->method('fetch')
+ ->with(
+ $this->equalTo('http://test'),
+ $this->equalTo(false),
+ $this->equalTo(3),
+ $this->equalTo(4)
+ )
->will($this->returnValue($fetchReturn));
+ $this->feedMapper->expects($this->at(1))
+ ->method('update')
+ ->with($this->equalTo($feed));
$this->itemMapper->expects($this->once())
->method('findByGuidHash')
->with($this->equalTo($items[0]->getGuidHash()),
@@ -300,11 +313,12 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase {
->method('insert')
->with($this->equalTo($items[0]));
- $this->feedMapper->expects($this->at(1))
+ $this->feedMapper->expects($this->at(2))
->method('find')
->with($feed->getId(), $this->user)
->will($this->returnValue($feed));
+
$return = $this->feedService->update($feed->getId(), $this->user);
$this->assertEquals($return, $feed);
@@ -408,6 +422,9 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase {
->with($this->equalTo($feed->getId()),
$this->equalTo($this->user))
->will($this->returnValue($feed));
+ $this->feedMapper->expects($this->at(1))
+ ->method('update')
+ ->with($this->equalTo($feed));
$this->fetcher->expects($this->once())
->method('fetch')
->will($this->returnValue($fetchReturn));
@@ -418,7 +435,7 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase {
$this->equalTo($this->user))
->will($this->returnValue($item2));;
- $this->feedMapper->expects($this->at(1))
+ $this->feedMapper->expects($this->at(2))
->method('find')
->with($this->equalTo($feed->getId()),
$this->equalTo($this->user))
@@ -450,6 +467,26 @@ class FeedServiceTest extends \PHPUnit_Framework_TestCase {
}
+ public function testUpdateDoesntUpdateIfNoFeed() {
+ $feedId = 3;
+ $feed = new Feed();
+ $feed->setFolderId(16);
+ $feed->setId($feedId);
+
+ $this->feedMapper->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo($feedId),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($feed));
+ $this->fetcher->expects($this->once())
+ ->method('fetch')
+ ->will($this->returnValue([null, null]));
+
+ $return = $this->feedService->update($feedId, $this->user);
+ $this->assertEquals($feed, $return);
+ }
+
+
public function testMove(){
$feedId = 3;
$folderId = 4;