From bc01761221384c0bbac0297d38e85bcaa6286a9a Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 28 Sep 2020 21:07:24 +0200 Subject: Fix repair step and test it Signed-off-by: Sean Molenaar --- lib/Fetcher/Client/LegacyGuzzleClient.php | 66 ---------------------- lib/Fetcher/Client/LegacyGuzzleResponse.php | 86 ----------------------------- lib/Fetcher/FeedFetcher.php | 22 +++++--- lib/Fetcher/Fetcher.php | 24 ++++---- lib/Fetcher/IFeedFetcher.php | 10 +++- lib/Fetcher/YoutubeFetcher.php | 19 ++++++- 6 files changed, 51 insertions(+), 176 deletions(-) delete mode 100644 lib/Fetcher/Client/LegacyGuzzleClient.php delete mode 100644 lib/Fetcher/Client/LegacyGuzzleResponse.php (limited to 'lib/Fetcher') diff --git a/lib/Fetcher/Client/LegacyGuzzleClient.php b/lib/Fetcher/Client/LegacyGuzzleClient.php deleted file mode 100644 index 59bbc9dc4..000000000 --- a/lib/Fetcher/Client/LegacyGuzzleClient.php +++ /dev/null @@ -1,66 +0,0 @@ - - * @copyright 2018 Sean Molenaar - */ - -namespace OCA\News\Fetcher\Client; - -use FeedIo\Adapter\ClientInterface as FeedIoClientInterface; -use FeedIo\Adapter\ResponseInterface; -use FeedIo\Adapter\NotFoundException; -use FeedIo\Adapter\ServerErrorException; -use Guzzle\Service\ClientInterface; -use GuzzleHttp\Exception\BadResponseException; - -/** - * Guzzle dependent HTTP client - */ -class LegacyGuzzleClient implements FeedIoClientInterface -{ - /** - * @var ClientInterface - */ - protected $guzzleClient; - - /** - * @param ClientInterface $guzzleClient - */ - public function __construct(ClientInterface $guzzleClient) - { - $this->guzzleClient = $guzzleClient; - } - - /** - * @param string $url - * @param \DateTime $modifiedSince - * @throws \FeedIo\Adapter\NotFoundException - * @throws \FeedIo\Adapter\ServerErrorException - * @return \FeedIo\Adapter\ResponseInterface - */ - public function getResponse(string $url, \DateTime $modifiedSince) : ResponseInterface - { - $modifiedSince->setTimezone(new \DateTimeZone('GMT')); - try { - $options = [ - 'headers' => [ - 'If-Modified-Since' => $modifiedSince->format('D, d M Y H:i:s e') - ] - ]; - - return new LegacyGuzzleResponse($this->guzzleClient->get($url, $options)); - } catch (BadResponseException $e) { - switch ((int) $e->getResponse()->getStatusCode()) { - case 404: - throw new NotFoundException($e->getMessage()); - default: - throw new ServerErrorException($e->getMessage()); - } - } - } -} diff --git a/lib/Fetcher/Client/LegacyGuzzleResponse.php b/lib/Fetcher/Client/LegacyGuzzleResponse.php deleted file mode 100644 index f8d6d7601..000000000 --- a/lib/Fetcher/Client/LegacyGuzzleResponse.php +++ /dev/null @@ -1,86 +0,0 @@ - - * @copyright 2018 Sean Molenaar - */ - -namespace OCA\News\Fetcher\Client; - -use FeedIo\Adapter\ResponseInterface; -use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface; - -/** - * Guzzle dependent HTTP Response - */ -class LegacyGuzzleResponse implements ResponseInterface -{ - const HTTP_LAST_MODIFIED = 'Last-Modified'; - - /** - * @var \GuzzleHttp\Message\ResponseInterface - */ - protected $response; - - /** - * @param \GuzzleHttp\Message\ResponseInterface - */ - public function __construct(GuzzleResponseInterface $psrResponse) - { - $this->response = $psrResponse; - } - - /** - * @return boolean - */ - public function isModified() : bool - { - return $this->response->getStatusCode() !== 304 && $this->response->getBody()->getSize() > 0; - } - - /** - * @return \Psr\Http\Message\StreamInterface - */ - public function getBody() : ? string - { - return $this->response->getBody(); - } - - /** - * @return \DateTime|null - */ - public function getLastModified() : ?\DateTime - { - if ($this->response->hasHeader(static::HTTP_LAST_MODIFIED)) { - $lastModified = \DateTime::createFromFormat( - 'D, d M Y H:i:s e', - $this->getHeader(static::HTTP_LAST_MODIFIED) - ); - - return false === $lastModified ? null : $lastModified; - } - - return null; - } - - /** - * @return array - */ - public function getHeaders() : iterable - { - return $this->response->getHeaders(); - } - - /** - * @param string $name - * @return string[] - */ - public function getHeader(string $name) : iterable - { - return current($this->response->getHeader($name)); - } -} diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 4e93424ba..5a84e2170 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -75,8 +75,14 @@ class FeedFetcher implements IFeedFetcher * * @inheritdoc */ - public function fetch(string $url, bool $favicon, $lastModified, bool $fullTextEnabled, $user, $password): array - { + public function fetch( + string $url, + bool $favicon, + ?string $lastModified, + bool $fullTextEnabled, + ?string $user, + ?string $password + ): array { $url2 = new Net_URL2($url); if (!empty($user) && !empty(trim($user))) { $url2->setUserinfo(urlencode($user), urlencode($password)); @@ -148,7 +154,7 @@ class FeedFetcher implements IFeedFetcher * * @return string */ - private function decodeTwice($string): string + private function decodeTwice(string $string): string { return html_entity_decode( html_entity_decode( @@ -195,12 +201,12 @@ class FeedFetcher implements IFeedFetcher * Build an item based on a feed. * * @param ItemInterface $parsedItem The item to use - * @param string $body Text of the item, if not provided use description from $parsedItem + * @param string|null $body Text of the item, if not provided use description from $parsedItem * @param bool $RTL True if the feed is RTL (Right-to-left) * * @return Item */ - protected function buildItem(ItemInterface $parsedItem, string $body = null, bool $RTL = false): Item + protected function buildItem(ItemInterface $parsedItem, ?string $body = null, bool $RTL = false): Item { $item = new Item(); $item->setUnread(true); @@ -208,18 +214,18 @@ class FeedFetcher implements IFeedFetcher $item->setGuid($parsedItem->getPublicId()); $item->setGuidHash(md5($item->getGuid())); - $lastmodified = $parsedItem->getLastModified() ?? new DateTime(); + $lastModified = $parsedItem->getLastModified() ?? new DateTime(); if ($parsedItem->getValue('pubDate') !== null) { $pubDT = new DateTime($parsedItem->getValue('pubDate')); } elseif ($parsedItem->getValue('published') !== null) { $pubDT = new DateTime($parsedItem->getValue('published')); } else { - $pubDT = $lastmodified; + $pubDT = $lastModified; } $item->setPubDate($pubDT->getTimestamp()); - $item->setLastModified($lastmodified->getTimestamp()); + $item->setLastModified($lastModified->getTimestamp()); $item->setRtl($RTL); // unescape content because angularjs helps against XSS diff --git a/lib/Fetcher/Fetcher.php b/lib/Fetcher/Fetcher.php index f52141dda..955875084 100644 --- a/lib/Fetcher/Fetcher.php +++ b/lib/Fetcher/Fetcher.php @@ -43,25 +43,25 @@ class Fetcher /** * Fetch a feed from remote * - * @param string $url remote url of the feed - * @param boolean $getFavicon if the favicon should also be fetched, defaults to true - * @param string $lastModified a last modified value from an http header defaults to false. + * @param string $url remote url of the feed + * @param boolean $getFavicon if the favicon should also be fetched, defaults to true + * @param string|null $lastModified a last modified value from an http header defaults to false. * If lastModified matches the http header from the feed no results are fetched - * @param bool $fullTextEnabled If true use a scraper to download the full article - * @param string $user if given, basic auth is set for this feed - * @param string $password if given, basic auth is set for this feed. Ignored if user is empty + * @param bool $fullTextEnabled If true use a scraper to download the full article + * @param string|null $user if given, basic auth is set for this feed + * @param string|null $password if given, basic auth is set for this feed. Ignored if user is empty * * @throws ReadErrorException if FeedIO fails * @return array an array containing the new feed and its items, first * element being the Feed and second element being an array of Items */ public function fetch( - $url, - $getFavicon = true, - $lastModified = null, - $fullTextEnabled = false, - $user = null, - $password = null + string $url, + bool $getFavicon = true, + ?string $lastModified = null, + bool $fullTextEnabled = false, + ?string $user = null, + ?string $password = null ) { foreach ($this->fetchers as $fetcher) { if (!$fetcher->canHandle($url)) { diff --git a/lib/Fetcher/IFeedFetcher.php b/lib/Fetcher/IFeedFetcher.php index ce44ec104..aaba9de67 100644 --- a/lib/Fetcher/IFeedFetcher.php +++ b/lib/Fetcher/IFeedFetcher.php @@ -31,9 +31,17 @@ interface IFeedFetcher * * @return array an array containing the new feed and its items, first * element being the Feed and second element being an array of Items + * * @throws ReadErrorException if the Feed-IO fetcher encounters a problem */ - public function fetch(string $url, bool $favicon, $lastModified, bool $fullTextEnabled, $user, $password): array; + public function fetch( + string $url, + bool $favicon, + ?string $lastModified, + bool $fullTextEnabled, + ?string $user, + ?string $password + ): array; /** * Can a fetcher handle a feed. diff --git a/lib/Fetcher/YoutubeFetcher.php b/lib/Fetcher/YoutubeFetcher.php index 85aad5d59..67a4d5afa 100644 --- a/lib/Fetcher/YoutubeFetcher.php +++ b/lib/Fetcher/YoutubeFetcher.php @@ -22,7 +22,14 @@ class YoutubeFetcher implements IFeedFetcher } - private function buildUrl($url) + /** + * Build YouTube URL + * + * @param string $url + * + * @return string + */ + private function buildUrl(string $url) { $baseRegex = '%(?:https?://|//)?(?:www.)?youtube.com'; $playRegex = $baseRegex . '.*?list=([^&]*)%'; @@ -54,8 +61,14 @@ class YoutubeFetcher implements IFeedFetcher * * @inheritdoc */ - public function fetch(string $url, bool $favicon, $lastModified, bool $fullTextEnabled, $user, $password): array - { + public function fetch( + string $url, + bool $favicon, + ?string $lastModified, + bool $fullTextEnabled, + ?string $user, + ?string $password + ): array { $transformedUrl = $this->buildUrl($url); $result = $this->feedFetcher->fetch( -- cgit v1.2.3