From df509a4b856472fa22fd6d43a212c9d21bd5b40f Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Fri, 23 Jan 2015 14:32:53 +0100 Subject: fix #711 --- service/feedservice.php | 141 +++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 67 deletions(-) (limited to 'service') diff --git a/service/feedservice.php b/service/feedservice.php index 29201b1d1..ec29e66c4 100644 --- a/service/feedservice.php +++ b/service/feedservice.php @@ -182,86 +182,80 @@ class FeedService extends Service { * @return Feed the updated feed entity */ public function update($feedId, $userId){ - try { - $existingFeed = $this->feedMapper->find($feedId, $userId); + $existingFeed = $this->find($feedId, $userId); - if($existingFeed->getPreventUpdate() === true) { - return $existingFeed; - } + if($existingFeed->getPreventUpdate() === true) { + return $existingFeed; + } - // for backwards compability it can be that the location is not set - // yet, if so use the url - $location = $existingFeed->getLocation(); - if (!$location) { - $location = $existingFeed->getUrl(); + // for backwards compability it can be that the location is not set + // yet, if so use the url + $location = $existingFeed->getLocation(); + if (!$location) { + $location = $existingFeed->getUrl(); + } + + try { + list($fetchedFeed, $items) = $this->feedFetcher->fetch( + $location, + false, + $existingFeed->getLastModified(), + $existingFeed->getEtag() + ); + + // if there is no feed it means that no update took place + if (!$fetchedFeed) { + return $existingFeed; } - try { - list($fetchedFeed, $items) = $this->feedFetcher->fetch( - $location, - false, - $existingFeed->getLastModified(), - $existingFeed->getEtag() - ); + // update number of articles on every feed update + $itemCount = count($items); - // if there is no feed it means that no update took place - if (!$fetchedFeed) { - return $existingFeed; - } + // this is needed to adjust to updates that add more items + // than when the feed was created. You can't update the count + // if it's lower because it may be due to the caching headers + // that were sent as the request and it might cause unwanted + // deletion and reappearing of feeds + if ($itemCount > $existingFeed->getArticlesPerUpdate()) { + $existingFeed->setArticlesPerUpdate($itemCount); + } - // update number of articles on every feed update - $itemCount = count($items); + $existingFeed->setLastModified($fetchedFeed->getLastModified()); + $existingFeed->setEtag($fetchedFeed->getEtag()); + $existingFeed->setLocation($fetchedFeed->getLocation()); + $this->feedMapper->update($existingFeed); - // this is needed to adjust to updates that add more items - // than when the feed was created. You can't update the count - // if it's lower because it may be due to the caching headers - // that were sent as the request and it might cause unwanted - // deletion and reappearing of feeds - if ($itemCount > $existingFeed->getArticlesPerUpdate()) { - $existingFeed->setArticlesPerUpdate($itemCount); - } + // insert items in reverse order because the first one is + // usually the newest item + for($i=$itemCount-1; $i>=0; $i--){ + $item = $items[$i]; + $item->setFeedId($existingFeed->getId()); - $existingFeed->setLastModified($fetchedFeed->getLastModified()); - $existingFeed->setEtag($fetchedFeed->getEtag()); - $existingFeed->setLocation($fetchedFeed->getLocation()); - $this->feedMapper->update($existingFeed); - - // insert items in reverse order because the first one is - // usually the newest item - for($i=$itemCount-1; $i>=0; $i--){ - $item = $items[$i]; - $item->setFeedId($existingFeed->getId()); - - try { - $this->itemMapper->findByGuidHash( - $item->getGuidHash(), $feedId, $userId - ); - } catch(DoesNotExistException $ex){ - $item = $this->enhancer->enhance($item, - $existingFeed->getLink()); - $item->setBody( - $this->purifier->purify($item->getBody()) - ); - $this->itemMapper->insert($item); - } + try { + $this->itemMapper->findByGuidHash( + $item->getGuidHash(), $feedId, $userId + ); + } catch(DoesNotExistException $ex){ + $item = $this->enhancer->enhance($item, + $existingFeed->getLink()); + $item->setBody( + $this->purifier->purify($item->getBody()) + ); + $this->itemMapper->insert($item); } - - } catch(FetcherException $ex){ - // failed updating is not really a problem, so only log it - $this->logger->debug( - 'Can not update feed with url ' . $existingFeed->getUrl() . - ' and location ' . $existingFeed->getLocation() . - ': ' . $ex->getMessage(), - $this->loggerParams - ); } - return $this->feedMapper->find($feedId, $userId); - - } catch (DoesNotExistException $ex){ - throw new ServiceNotFoundException('Feed does not exist'); + } catch(FetcherException $ex){ + // failed updating is not really a problem, so only log it + $this->logger->debug( + 'Can not update feed with url ' . $existingFeed->getUrl() . + ' and location ' . $existingFeed->getLocation() . + ': ' . $ex->getMessage(), + $this->loggerParams + ); } + return $this->find($feedId, $userId); } @@ -421,4 +415,17 @@ class FeedService extends Service { } + /** + * Sets the feed ordering + * @param int $id the id of the feed + * @param int $ordering 0 for no ordering, 1 for reverse, 2 for normal + * @param string $userId the id of the user + */ + public function setOrdering ($id, $ordering, $userId) { + $feed = $this->find($id, $userId); + $feed->setOrdering($ordering); + $this->feedMapper->update($feed); + } + + } -- cgit v1.2.3