From f597f0e338b76cfe9a53af7733bf6d9cfddeaca1 Mon Sep 17 00:00:00 2001 From: Dag Date: Thu, 19 Oct 2023 23:11:45 +0200 Subject: [PATCH] yup --- formats/MrssFormat.php | 115 ++++++++++-------- lib/BridgeAbstract.php | 2 +- .../expectedMrssFormat/feed.common.xml | 6 +- .../samples/expectedMrssFormat/feed.empty.xml | 2 +- .../expectedMrssFormat/feed.emptyItems.xml | 2 +- .../expectedMrssFormat/feed.microblog.xml | 6 +- 6 files changed, 75 insertions(+), 58 deletions(-) diff --git a/formats/MrssFormat.php b/formats/MrssFormat.php index 4745b413495..8e30a830f93 100644 --- a/formats/MrssFormat.php +++ b/formats/MrssFormat.php @@ -35,13 +35,8 @@ class MrssFormat extends FormatAbstract public function stringify() { $document = new \DomDocument('1.0', $this->getCharset()); - - $feedUrl = get_current_url(); - $feedArray = $this->getFeed(); - - $uri = $feedArray['uri']; - $document->formatOutput = true; + $feed = $document->createElement('rss'); $document->appendChild($feed); $feed->setAttribute('version', '2.0'); @@ -51,51 +46,73 @@ public function stringify() $channel = $document->createElement('channel'); $feed->appendChild($channel); + $feedArray = $this->getFeed(); + $uri = $feedArray['uri']; $title = $feedArray['name']; - $channelTitle = $document->createElement('title'); - $channel->appendChild($channelTitle); - $channelTitle->appendChild($document->createTextNode($title)); - - $link = $document->createElement('link'); - $channel->appendChild($link); - $link->appendChild($document->createTextNode($uri)); - - $description = $document->createElement('description'); - $channel->appendChild($description); - $description->appendChild($document->createTextNode($title)); - - $allowedIconExtensions = [ - '.gif', - '.jpg', - '.png', - '.ico', - ]; - $icon = $feedArray['icon']; - if ($icon && in_array(substr($icon, -4), $allowedIconExtensions)) { - $feedImage = $document->createElement('image'); - $channel->appendChild($feedImage); - $iconUrl = $document->createElement('url'); - $iconUrl->appendChild($document->createTextNode($icon)); - $feedImage->appendChild($iconUrl); - $iconTitle = $document->createElement('title'); - $iconTitle->appendChild($document->createTextNode($title)); - $feedImage->appendChild($iconTitle); - $iconLink = $document->createElement('link'); - $iconLink->appendChild($document->createTextNode($uri)); - $feedImage->appendChild($iconLink); - } - $linkAlternate = $document->createElementNS(self::ATOM_NS, 'link'); - $channel->appendChild($linkAlternate); - $linkAlternate->setAttribute('rel', 'alternate'); - $linkAlternate->setAttribute('type', 'text/html'); - $linkAlternate->setAttribute('href', $uri); - - $linkSelf = $document->createElementNS(self::ATOM_NS, 'link'); - $channel->appendChild($linkSelf); - $linkSelf->setAttribute('rel', 'self'); - $linkSelf->setAttribute('type', 'application/atom+xml'); - $linkSelf->setAttribute('href', $feedUrl); + foreach ($feedArray as $feedKey => $feedValue) { + if (in_array($feedKey, ['atom', 'donationUri'])) { + continue; + } + if ($feedKey === 'name') { + $channelTitle = $document->createElement('title'); + $channel->appendChild($channelTitle); + $channelTitle->appendChild($document->createTextNode($title)); + + $description = $document->createElement('description'); + $channel->appendChild($description); + $description->appendChild($document->createTextNode($title)); + } elseif ($feedKey === 'uri') { + $link = $document->createElement('link'); + $channel->appendChild($link); + $link->appendChild($document->createTextNode($uri)); + + $linkAlternate = $document->createElementNS(self::ATOM_NS, 'link'); + $channel->appendChild($linkAlternate); + $linkAlternate->setAttribute('rel', 'alternate'); + $linkAlternate->setAttribute('type', 'text/html'); + $linkAlternate->setAttribute('href', $uri); + + $linkSelf = $document->createElementNS(self::ATOM_NS, 'link'); + $channel->appendChild($linkSelf); + $linkSelf->setAttribute('rel', 'self'); + $linkSelf->setAttribute('type', 'application/atom+xml'); + $feedUrl = get_current_url(); + $linkSelf->setAttribute('href', $feedUrl); + } elseif ($feedKey === 'icon') { + $allowedIconExtensions = [ + '.gif', + '.jpg', + '.png', + '.ico', + ]; + $icon = $feedValue; + if ($icon && in_array(substr($icon, -4), $allowedIconExtensions)) { + $feedImage = $document->createElement('image'); + $channel->appendChild($feedImage); + $iconUrl = $document->createElement('url'); + $iconUrl->appendChild($document->createTextNode($icon)); + $feedImage->appendChild($iconUrl); + $iconTitle = $document->createElement('title'); + $iconTitle->appendChild($document->createTextNode($title)); + $feedImage->appendChild($iconTitle); + $iconLink = $document->createElement('link'); + $iconLink->appendChild($document->createTextNode($uri)); + $feedImage->appendChild($iconLink); + } + } elseif ($feedKey === 'itunes') { + $feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:itunes', self::ITUNES_NS); + foreach ($feedValue as $itunesKey => $itunesValue) { + $itunesProperty = $document->createElementNS(self::ITUNES_NS, $itunesKey); + $channel->appendChild($itunesProperty); + $itunesProperty->appendChild($document->createTextNode($itunesValue)); + } + } else { + $channelTitle = $document->createElement($feedKey); + $channel->appendChild($channelTitle); + $channelTitle->appendChild($document->createTextNode($feedValue)); + } + } foreach ($this->getItems() as $item) { $itemArray = $item->toArray(); diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index 38ee10d3813..ea6c131a181 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -3,7 +3,7 @@ abstract class BridgeAbstract { const NAME = 'Unnamed bridge'; - const URI = null; + const URI = ''; const DONATION_URI = ''; const DESCRIPTION = 'No description provided'; const MAINTAINER = 'No maintainer'; diff --git a/tests/Formats/samples/expectedMrssFormat/feed.common.xml b/tests/Formats/samples/expectedMrssFormat/feed.common.xml index 38a16f88afc..92838ae883e 100644 --- a/tests/Formats/samples/expectedMrssFormat/feed.common.xml +++ b/tests/Formats/samples/expectedMrssFormat/feed.common.xml @@ -2,15 +2,15 @@ Sample feed with common data - https://example.com/blog/ Sample feed with common data + https://example.com/blog/ + + https://example.com/logo.png Sample feed with common data https://example.com/blog/ - - Test Entry diff --git a/tests/Formats/samples/expectedMrssFormat/feed.empty.xml b/tests/Formats/samples/expectedMrssFormat/feed.empty.xml index 888c42b6cf0..40eecfc6ff9 100644 --- a/tests/Formats/samples/expectedMrssFormat/feed.empty.xml +++ b/tests/Formats/samples/expectedMrssFormat/feed.empty.xml @@ -2,8 +2,8 @@ Sample feed with minimum data - https://github.com/RSS-Bridge/rss-bridge/ Sample feed with minimum data + https://github.com/RSS-Bridge/rss-bridge/ diff --git a/tests/Formats/samples/expectedMrssFormat/feed.emptyItems.xml b/tests/Formats/samples/expectedMrssFormat/feed.emptyItems.xml index 9e712ddd998..8839f5a5918 100644 --- a/tests/Formats/samples/expectedMrssFormat/feed.emptyItems.xml +++ b/tests/Formats/samples/expectedMrssFormat/feed.emptyItems.xml @@ -2,8 +2,8 @@ Sample feed with minimum data - https://github.com/RSS-Bridge/rss-bridge/ Sample feed with minimum data + https://github.com/RSS-Bridge/rss-bridge/ diff --git a/tests/Formats/samples/expectedMrssFormat/feed.microblog.xml b/tests/Formats/samples/expectedMrssFormat/feed.microblog.xml index 81dac87a793..63c04c0f420 100644 --- a/tests/Formats/samples/expectedMrssFormat/feed.microblog.xml +++ b/tests/Formats/samples/expectedMrssFormat/feed.microblog.xml @@ -2,15 +2,15 @@ Sample microblog feed - https://example.com/blog/ Sample microblog feed + https://example.com/blog/ + + https://example.com/logo.png Sample microblog feed https://example.com/blog/ - - 1918f084648b82057c1dd3faa3d091da82a6fac2