Skip to content

Commit

Permalink
feat: support itunes namespace in top channel feed (#3776)
Browse files Browse the repository at this point in the history
Also preserves other properties.
  • Loading branch information
dvikan authored Jan 9, 2024
1 parent ea58c8d commit 3ce9440
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 198 deletions.
11 changes: 3 additions & 8 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function execute(array $request)
private function createResponse(array $request, BridgeAbstract $bridge, FormatAbstract $format)
{
$items = [];
$infos = [];
$feed = [];

try {
$bridge->loadConfiguration();
Expand All @@ -116,12 +116,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
Expand Down Expand Up @@ -155,7 +150,7 @@ private function createResponse(array $request, BridgeAbstract $bridge, FormatAb
}

$format->setItems($items);
$format->setExtraInfos($infos);
$format->setFeed($feed);
$now = time();
$format->setLastModified($now);
$headers = [
Expand Down
6 changes: 3 additions & 3 deletions bridges/ItakuBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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) {
Expand Down Expand Up @@ -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";

Expand Down
81 changes: 44 additions & 37 deletions formats/AtomFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,61 @@ class AtomFormat extends FormatAbstract
public function stringify()
{
$document = new \DomDocument('1.0', $this->getCharset());
$document->formatOutput = true;

$feedUrl = get_current_url();

$extraInfos = $this->getExtraInfos();
if (empty($extraInfos['uri'])) {
$uri = REPOSITORY;
} else {
$uri = $extraInfos['uri'];
}

$document->formatOutput = true;
$feed = $document->createElementNS(self::ATOM_NS, 'feed');
$document->appendChild($feed);
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:media', self::MRSS_NS);

$title = $document->createElement('title');
$feed->appendChild($title);
$title->setAttribute('type', 'text');
$title->appendChild($document->createTextNode($extraInfos['name']));
$feedArray = $this->getFeed();
foreach ($feedArray as $feedKey => $feedValue) {
if (in_array($feedKey, ['donationUri'])) {
continue;
}
if ($feedKey === 'name') {
$title = $document->createElement('title');
$feed->appendChild($title);
$title->setAttribute('type', 'text');
$title->appendChild($document->createTextNode($feedValue));
} elseif ($feedKey === 'icon') {
if ($feedValue) {
$icon = $document->createElement('icon');
$feed->appendChild($icon);
$icon->appendChild($document->createTextNode($feedValue));

$logo = $document->createElement('logo');
$feed->appendChild($logo);
$logo->appendChild($document->createTextNode($feedValue));
}
} elseif ($feedKey === 'uri') {
if ($feedValue) {
$linkAlternate = $document->createElement('link');
$feed->appendChild($linkAlternate);
$linkAlternate->setAttribute('rel', 'alternate');
$linkAlternate->setAttribute('type', 'text/html');
$linkAlternate->setAttribute('href', $feedValue);

$linkSelf = $document->createElement('link');
$feed->appendChild($linkSelf);
$linkSelf->setAttribute('rel', 'self');
$linkSelf->setAttribute('type', 'application/atom+xml');
$linkSelf->setAttribute('href', $feedUrl);
}
} elseif ($feedKey === 'itunes') {
// todo: skip?
} else {
$element = $document->createElement($feedKey);
$feed->appendChild($element);
$element->appendChild($document->createTextNode($feedValue));
}
}

$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));

$logo = $document->createElement('logo');
$feed->appendChild($logo);
$logo->appendChild($document->createTextNode($iconUrl));

$feedTimestamp = gmdate(DATE_ATOM, $this->lastModified);
$updated = $document->createElement('updated');
$feed->appendChild($updated);
Expand All @@ -69,17 +86,7 @@ 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);

$linkSelf = $document->createElement('link');
$feed->appendChild($linkSelf);
$linkSelf->setAttribute('rel', 'self');
$linkSelf->setAttribute('type', 'application/atom+xml');
$linkSelf->setAttribute('href', $feedUrl);


foreach ($this->getItems() as $item) {
$itemArray = $item->toArray();
Expand Down
12 changes: 6 additions & 6 deletions formats/HtmlFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function stringify()
{
$queryString = $_SERVER['QUERY_STRING'];

$extraInfos = $this->getExtraInfos();
$feedArray = $this->getFeed();
$formatFactory = new FormatFactory();
$buttons = [];
$linkTags = [];
Expand All @@ -29,17 +29,17 @@ 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',
];
}

$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(),
Expand All @@ -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,
]);
Expand Down
18 changes: 9 additions & 9 deletions formats/JsonFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
124 changes: 71 additions & 53 deletions formats/MrssFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,8 @@ class MrssFormat extends FormatAbstract
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'];
}

$document->formatOutput = true;

$feed = $document->createElement('rss');
$document->appendChild($feed);
$feed->setAttribute('version', '2.0');
Expand All @@ -54,50 +46,73 @@ public function stringify()
$channel = $document->createElement('channel');
$feed->appendChild($channel);

$title = $extraInfos['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($extraInfos['name']));

$allowedIconExtensions = [
'.gif',
'.jpg',
'.png',
];
$icon = $extraInfos['icon'];
if (!empty($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);
}
$feedArray = $this->getFeed();
$uri = $feedArray['uri'];
$title = $feedArray['name'];

$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 {
$element = $document->createElement($feedKey);
$channel->appendChild($element);
$element->appendChild($document->createTextNode($feedValue));
}
}

foreach ($this->getItems() as $item) {
$itemArray = $item->toArray();
Expand Down Expand Up @@ -135,14 +150,17 @@ public function stringify()
$entry->appendChild($itunesProperty);
$itunesProperty->appendChild($document->createTextNode($itunesValue));
}

if (isset($itemArray['enclosure'])) {
$itunesEnclosure = $document->createElement('enclosure');
$entry->appendChild($itunesEnclosure);
$itunesEnclosure->setAttribute('url', $itemArray['enclosure']['url']);
$itunesEnclosure->setAttribute('length', $itemArray['enclosure']['length']);
$itunesEnclosure->setAttribute('type', $itemArray['enclosure']['type']);
}
} if (!empty($itemUri)) {
}

if (!empty($itemUri)) {
$entryLink = $document->createElement('link');
$entry->appendChild($entryLink);
$entryLink->appendChild($document->createTextNode($itemUri));
Expand Down
Loading

0 comments on commit 3ce9440

Please sign in to comment.