From 95530f62513a82c385d9378b4a59da57d74092d9 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 5 Nov 2014 11:30:27 +0100 Subject: update picofeed, add max size setting, fix #642 --- fetcher/feedfetcher.php | 81 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 28 deletions(-) (limited to 'fetcher') diff --git a/fetcher/feedfetcher.php b/fetcher/feedfetcher.php index 733506187..d59761b26 100644 --- a/fetcher/feedfetcher.php +++ b/fetcher/feedfetcher.php @@ -13,6 +13,18 @@ namespace OCA\News\Fetcher; +use \PicoFeed\Parser\MalFormedXmlException; +use \PicoFeed\Reader\Reader; +use \PicoFeed\Reader\SubscriptionNotFoundException; +use \PicoFeed\Reader\UnsupportedFeedFormatException; +use \PicoFeed\Client\InvalidCertificateException; +use \PicoFeed\Client\InvalidUrlException; +use \PicoFeed\Client\MaxRedirectException; +use \PicoFeed\Client\MaxSizeException; +use \PicoFeed\Client\TimeoutException; + +use \OCP\IL10N; + use \OCA\News\Db\Item; use \OCA\News\Db\Feed; use \OCA\News\Utility\PicoFeedFaviconFactory; @@ -21,15 +33,18 @@ use \OCA\News\Utility\PicoFeedReaderFactory; class FeedFetcher implements IFeedFetcher { private $faviconFactory; - private $readerFactory; + private $reader; + private $l10n; private $time; - public function __construct(PicoFeedReaderFactory $readerFactory, + public function __construct(Reader $reader, PicoFeedFaviconFactory $faviconFactory, + IL10N $l10n, $time){ $this->faviconFactory = $faviconFactory; - $this->readerFactory = $readerFactory; + $this->reader = $reader; $this->time = $time; + $this->l10n = $l10n; } @@ -58,46 +73,56 @@ class FeedFetcher implements IFeedFetcher { */ public function fetch($url, $getFavicon=true, $lastModified=null, $etag=null) { - $reader = $this->readerFactory->build(); - $resource = $reader->download($url, $lastModified, $etag); + try { + $resource = $this->reader->discover($url, $lastModified, $etag); - $modified = $resource->getLastModified(); - $etag = $resource->getEtag(); - $location = $resource->getUrl(); + if (!$resource->isModified()) { + return [null, null]; + } - if (!$resource->isModified()) { - return [null, null]; - } + $location = $resource->getUrl(); + $etag = $resource->getEtag(); + $content = $resource->getContent(); + $encoding = $resource->getEncoding(); + $lastModified = $resource->getLastModified(); - try { - $parser = $reader->getParser(); + $parser = $this->reader->getParser($location, $content, $encoding); - if (!$parser) { - throw new \Exception( - 'Could not find a valid feed at url ' . $url - ); - } $parsedFeed = $parser->execute(); - if (!$parsedFeed) { - throw new \Exception( - 'Could not parse feed ' . $url - ); - } + $feed = $this->buildFeed( + $parsedFeed, $url, $getFavicon, $lastModified, $etag, $location + ); $items = []; foreach($parsedFeed->getItems() as $item) { $items[] = $this->buildItem($item); } - $feed = $this->buildFeed( - $parsedFeed, $url, $getFavicon, $modified, $etag, $location - ); - return [$feed, $items]; } catch(\Exception $ex){ - throw new FetcherException($ex->getMessage()); + $msg = $ex->getMessage(); + + if ($ex instanceof MalFormedXmlException) { + $msg = $this->l10n->t('Feed contains invalid XML'); + } else if ($ex instanceof SubscriptionNotFoundException) { + $msg = $this->l10n->t('Could not find a feed'); + } else if ($ex instanceof UnsupportedFeedFormatException) { + $msg = $this->l10n->t('Detected feed format is not supported'); + } else if ($ex instanceof InvalidCertificateException) { + $msg = $this->l10n->t('SSL Certificate is invalid'); + } else if ($ex instanceof InvalidUrlException) { + $msg = $this->l10n->t('Website not found'); + } else if ($ex instanceof MaxRedirectException) { + $msg = $this->l10n->t('More redirects than allowed, aborting'); + } else if ($ex instanceof MaxSizeException) { + $msg = $this->l10n->t('Bigger than maximum allowed size'); + } else if ($ex instanceof TimeoutException) { + $msg = $this->l10n->t('Request timed out'); + } + + throw new FetcherException($msg); } } -- cgit v1.2.3