diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php index e3b25fef8a2..6f73ccc6b74 100644 --- a/actions/DisplayAction.php +++ b/actions/DisplayAction.php @@ -96,7 +96,7 @@ public function execute(array $request) private function createResponse(array $request, BridgeAbstract $bridge, FormatAbstract $format) { $items = []; - $infos = []; + $feed = []; try { $bridge->loadConfiguration(); @@ -112,12 +112,7 @@ private function createResponse(array $request, BridgeAbstract $bridge, FormatAb } $items = $feedItems; } - $infos = [ - 'name' => $bridge->getName(), - 'uri' => $bridge->getURI(), - 'donationUri' => $bridge->getDonationURI(), - 'icon' => $bridge->getIcon() - ]; + $feed = $bridge->getFeed(); } catch (\Exception $e) { if ($e instanceof HttpException) { // Reproduce (and log) these responses regardless of error output and report limit @@ -151,7 +146,7 @@ private function createResponse(array $request, BridgeAbstract $bridge, FormatAb } $format->setItems($items); - $format->setExtraInfos($infos); + $format->setFeed($feed); $now = time(); $format->setLastModified($now); $headers = [ diff --git a/bridges/ItakuBridge.php b/bridges/ItakuBridge.php index 62a130ff8b6..f49865eca14 100644 --- a/bridges/ItakuBridge.php +++ b/bridges/ItakuBridge.php @@ -280,7 +280,7 @@ public function collectData() $opt['range'] = ''; $user_id = $this->getInput('user_id') ?? $this->getOwnerID($this->getInput('user')); - $data = $this->getFeed( + $data = $this->getFeedData( $opt, $user_id ); @@ -289,7 +289,7 @@ public function collectData() if ($this->queriedContext === 'Home feed') { $opt['order'] = $this->getInput('order'); $opt['range'] = $this->getInput('range'); - $data = $this->getFeed($opt); + $data = $this->getFeedData($opt); } foreach ($data['results'] as $record) { @@ -409,7 +409,7 @@ private function getPostsSearch(array $opt) return $this->getData($url, false, true); } - private function getFeed(array $opt, $ownerID = null) + private function getFeedData(array $opt, $ownerID = null) { $url = self::URI . "/api/feed/?date_range={$opt['range']}&ordering={$opt['order']}&page=1&page_size=30&format=json"; diff --git a/formats/AtomFormat.php b/formats/AtomFormat.php index d59e42fea05..a34378f7540 100644 --- a/formats/AtomFormat.php +++ b/formats/AtomFormat.php @@ -20,12 +20,9 @@ public function stringify() $feedUrl = get_current_url(); - $extraInfos = $this->getExtraInfos(); - if (empty($extraInfos['uri'])) { - $uri = REPOSITORY; - } else { - $uri = $extraInfos['uri']; - } + $feedArray = $this->getFeed(); + + $uri = $feedArray['uri']; $document->formatOutput = true; $feed = $document->createElementNS(self::ATOM_NS, 'feed'); @@ -35,25 +32,22 @@ public function stringify() $title = $document->createElement('title'); $feed->appendChild($title); $title->setAttribute('type', 'text'); - $title->appendChild($document->createTextNode($extraInfos['name'])); + $title->appendChild($document->createTextNode($feedArray['name'])); $id = $document->createElement('id'); $feed->appendChild($id); $id->appendChild($document->createTextNode($feedUrl)); - $uriparts = parse_url($uri); - if (empty($extraInfos['icon'])) { - $iconUrl = $uriparts['scheme'] . '://' . $uriparts['host'] . '/favicon.ico'; - } else { - $iconUrl = $extraInfos['icon']; - } - $icon = $document->createElement('icon'); - $feed->appendChild($icon); - $icon->appendChild($document->createTextNode($iconUrl)); + $iconUrl = $feedArray['icon']; + if ($iconUrl) { + $icon = $document->createElement('icon'); + $feed->appendChild($icon); + $icon->appendChild($document->createTextNode($iconUrl)); - $logo = $document->createElement('logo'); - $feed->appendChild($logo); - $logo->appendChild($document->createTextNode($iconUrl)); + $logo = $document->createElement('logo'); + $feed->appendChild($logo); + $logo->appendChild($document->createTextNode($iconUrl)); + } $feedTimestamp = gmdate(DATE_ATOM, $this->lastModified); $updated = $document->createElement('updated'); @@ -69,11 +63,13 @@ public function stringify() $author->appendChild($authorName); $authorName->appendChild($document->createTextNode($feedAuthor)); - $linkAlternate = $document->createElement('link'); - $feed->appendChild($linkAlternate); - $linkAlternate->setAttribute('rel', 'alternate'); - $linkAlternate->setAttribute('type', 'text/html'); - $linkAlternate->setAttribute('href', $uri); + if ($uri) { + $linkAlternate = $document->createElement('link'); + $feed->appendChild($linkAlternate); + $linkAlternate->setAttribute('rel', 'alternate'); + $linkAlternate->setAttribute('type', 'text/html'); + $linkAlternate->setAttribute('href', $uri); + } $linkSelf = $document->createElement('link'); $feed->appendChild($linkSelf); diff --git a/formats/HtmlFormat.php b/formats/HtmlFormat.php index 4933af8d720..ef66f493375 100644 --- a/formats/HtmlFormat.php +++ b/formats/HtmlFormat.php @@ -8,7 +8,7 @@ public function stringify() { $queryString = $_SERVER['QUERY_STRING']; - $extraInfos = $this->getExtraInfos(); + $feedArray = $this->getFeed(); $formatFactory = new FormatFactory(); $buttons = []; $linkTags = []; @@ -29,9 +29,9 @@ public function stringify() ]; } - if (Configuration::getConfig('admin', 'donations') && $extraInfos['donationUri'] !== '') { + if (Configuration::getConfig('admin', 'donations') && $feedArray['donationUri']) { $buttons[] = [ - 'href' => e($extraInfos['donationUri']), + 'href' => e($feedArray['donationUri']), 'value' => 'Donate to maintainer', ]; } @@ -39,7 +39,7 @@ public function stringify() $items = []; foreach ($this->getItems() as $item) { $items[] = [ - 'url' => $item->getURI() ?: $extraInfos['uri'], + 'url' => $item->getURI() ?: $feedArray['uri'], 'title' => $item->getTitle() ?? '(no title)', 'timestamp' => $item->getTimestamp(), 'author' => $item->getAuthor(), @@ -51,9 +51,9 @@ public function stringify() $html = render_template(__DIR__ . '/../templates/html-format.html.php', [ 'charset' => $this->getCharset(), - 'title' => $extraInfos['name'], + 'title' => $feedArray['name'], 'linkTags' => $linkTags, - 'uri' => $extraInfos['uri'], + 'uri' => $feedArray['uri'], 'buttons' => $buttons, 'items' => $items, ]); diff --git a/formats/JsonFormat.php b/formats/JsonFormat.php index dd61da41d8c..016e75e1177 100644 --- a/formats/JsonFormat.php +++ b/formats/JsonFormat.php @@ -25,18 +25,18 @@ class JsonFormat extends FormatAbstract public function stringify() { - $host = $_SERVER['HTTP_HOST'] ?? ''; - $extraInfos = $this->getExtraInfos(); + $feedArray = $this->getFeed(); + $data = [ - 'version' => 'https://jsonfeed.org/version/1', - 'title' => empty($extraInfos['name']) ? $host : $extraInfos['name'], - 'home_page_url' => empty($extraInfos['uri']) ? REPOSITORY : $extraInfos['uri'], - 'feed_url' => get_current_url(), + 'version' => 'https://jsonfeed.org/version/1', + 'title' => $feedArray['name'], + 'home_page_url' => $feedArray['uri'], + 'feed_url' => get_current_url(), ]; - if (!empty($extraInfos['icon'])) { - $data['icon'] = $extraInfos['icon']; - $data['favicon'] = $extraInfos['icon']; + if ($feedArray['icon']) { + $data['icon'] = $feedArray['icon']; + $data['favicon'] = $feedArray['icon']; } $items = []; diff --git a/formats/MrssFormat.php b/formats/MrssFormat.php index 4fd06439775..4745b413495 100644 --- a/formats/MrssFormat.php +++ b/formats/MrssFormat.php @@ -37,12 +37,9 @@ public function stringify() $document = new \DomDocument('1.0', $this->getCharset()); $feedUrl = get_current_url(); - $extraInfos = $this->getExtraInfos(); - if (empty($extraInfos['uri'])) { - $uri = REPOSITORY; - } else { - $uri = $extraInfos['uri']; - } + $feedArray = $this->getFeed(); + + $uri = $feedArray['uri']; $document->formatOutput = true; $feed = $document->createElement('rss'); @@ -54,7 +51,7 @@ public function stringify() $channel = $document->createElement('channel'); $feed->appendChild($channel); - $title = $extraInfos['name']; + $title = $feedArray['name']; $channelTitle = $document->createElement('title'); $channel->appendChild($channelTitle); $channelTitle->appendChild($document->createTextNode($title)); @@ -65,15 +62,16 @@ public function stringify() $description = $document->createElement('description'); $channel->appendChild($description); - $description->appendChild($document->createTextNode($extraInfos['name'])); + $description->appendChild($document->createTextNode($title)); $allowedIconExtensions = [ '.gif', '.jpg', '.png', + '.ico', ]; - $icon = $extraInfos['icon']; - if (!empty($icon) && in_array(substr($icon, -4), $allowedIconExtensions)) { + $icon = $feedArray['icon']; + if ($icon && in_array(substr($icon, -4), $allowedIconExtensions)) { $feedImage = $document->createElement('image'); $channel->appendChild($feedImage); $iconUrl = $document->createElement('url'); diff --git a/formats/PlaintextFormat.php b/formats/PlaintextFormat.php index 0a9237d04a9..4e18caa6058 100644 --- a/formats/PlaintextFormat.php +++ b/formats/PlaintextFormat.php @@ -6,11 +6,11 @@ class PlaintextFormat extends FormatAbstract public function stringify() { - $data = []; + $feed = $this->getFeed(); foreach ($this->getItems() as $item) { - $data[] = $item->toArray(); + $feed['items'][] = $item->toArray(); } - $text = print_r($data, true); + $text = print_r($feed, true); // Remove invalid non-UTF8 characters ini_set('mbstring.substitute_character', 'none'); $text = mb_convert_encoding($text, $this->getCharset(), 'UTF-8'); diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index a7b811a84da..38ee10d3813 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -3,7 +3,7 @@ abstract class BridgeAbstract { const NAME = 'Unnamed bridge'; - const URI = ''; + const URI = null; const DONATION_URI = ''; const DESCRIPTION = 'No description provided'; const MAINTAINER = 'No maintainer'; @@ -40,49 +40,66 @@ public function __construct( abstract public function collectData(); - public function getItems() + public function getFeed(): array { - return $this->items; + return [ + 'name' => $this->getName(), + 'uri' => $this->getURI(), + 'donationUri' => $this->getDonationURI(), + 'icon' => $this->getIcon(), + ]; } - public function getOption(string $name) + public function getName() { - return $this->configuration[$name] ?? null; + return static::NAME; } - public function getDescription() + public function getURI() { - return static::DESCRIPTION; + return static::URI ?? 'https://github.com/RSS-Bridge/rss-bridge/'; } - public function getMaintainer(): string + public function getDonationURI(): string { - return static::MAINTAINER; + return static::DONATION_URI; } - public function getName() + public function getIcon() { - return static::NAME; + if (static::URI) { + // This favicon may or may not exist + return rtrim(static::URI, '/') . '/favicon.ico'; + } + return ''; } - public function getIcon() + public function getOption(string $name) { - return static::URI . '/favicon.ico'; + return $this->configuration[$name] ?? null; } - public function getParameters(): array + /** + * The description is currently not used in feed production + */ + public function getDescription() { - return static::PARAMETERS; + return static::DESCRIPTION; } - public function getURI() + public function getMaintainer(): string { - return static::URI; + return static::MAINTAINER; } - public function getDonationURI(): string + public function getParameters(): array { - return static::DONATION_URI; + return static::PARAMETERS; + } + + public function getItems() + { + return $this->items; } public function getCacheTimeout() diff --git a/lib/FormatAbstract.php b/lib/FormatAbstract.php index c76d1e42166..28eb4bbfa7a 100644 --- a/lib/FormatAbstract.php +++ b/lib/FormatAbstract.php @@ -9,28 +9,25 @@ abstract class FormatAbstract protected string $charset = 'UTF-8'; protected array $items = []; protected int $lastModified; - protected array $extraInfos = []; - abstract public function stringify(); - - public function getMimeType(): string - { - return static::MIME_TYPE; - } + protected array $feed = []; - public function setCharset(string $charset) - { - $this->charset = $charset; - } + abstract public function stringify(); - public function getCharset(): string + public function setFeed(array $feed) { - return $this->charset; + $default = [ + 'name' => '', + 'uri' => '', + 'icon' => '', + 'donationUri' => '', + ]; + $this->feed = array_merge($default, $feed); } - public function setLastModified(int $lastModified) + public function getFeed(): array { - $this->lastModified = $lastModified; + return $this->feed; } /** @@ -49,27 +46,23 @@ public function getItems(): array return $this->items; } - public function setExtraInfos(array $infos = []) + public function getMimeType(): string { - $extras = [ - 'name', - 'uri', - 'icon', - 'donationUri', - ]; - foreach ($extras as $extra) { - if (!isset($infos[$extra])) { - $infos[$extra] = ''; - } - } - $this->extraInfos = $infos; + return static::MIME_TYPE; } - public function getExtraInfos(): array + public function setCharset(string $charset) { - if (!$this->extraInfos) { - $this->setExtraInfos(); - } - return $this->extraInfos; + $this->charset = $charset; + } + + public function getCharset(): string + { + return $this->charset; + } + + public function setLastModified(int $lastModified) + { + $this->lastModified = $lastModified; } } diff --git a/lib/bootstrap.php b/lib/bootstrap.php index a95de9dd0ef..85d823e92c1 100644 --- a/lib/bootstrap.php +++ b/lib/bootstrap.php @@ -9,9 +9,6 @@ /** Path to the cache folder */ const PATH_CACHE = __DIR__ . '/../cache/'; -/** URL to the RSS-Bridge repository */ -const REPOSITORY = 'https://github.com/RSS-Bridge/rss-bridge/'; - // Allow larger files for simple_html_dom // todo: extract to config (if possible) const MAX_FILE_SIZE = 10000000; diff --git a/tests/Formats/BaseFormatTest.php b/tests/Formats/BaseFormatTest.php index 71e196f0260..8999e7722af 100644 --- a/tests/Formats/BaseFormatTest.php +++ b/tests/Formats/BaseFormatTest.php @@ -61,7 +61,7 @@ protected function formatData(string $formatName, \stdClass $sample): string $formatFactory = new FormatFactory(); $format = $formatFactory->create($formatName); $format->setItems($sample->items); - $format->setExtraInfos($sample->meta); + $format->setFeed($sample->meta); $format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); return $format->stringify(); diff --git a/tests/Formats/samples/expectedAtomFormat/feed.empty.xml b/tests/Formats/samples/expectedAtomFormat/feed.empty.xml index fc04304da51..34f053bec53 100644 --- a/tests/Formats/samples/expectedAtomFormat/feed.empty.xml +++ b/tests/Formats/samples/expectedAtomFormat/feed.empty.xml @@ -3,8 +3,6 @@