Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Oct 19, 2023
1 parent 563c2a3 commit 689cfa8
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 125 deletions.
11 changes: 3 additions & 8 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function execute(array $request)
private function createResponse(array $request, BridgeAbstract $bridge, FormatAbstract $format)
{
$items = [];
$infos = [];
$feed = [];

try {
$bridge->loadConfiguration();
Expand All @@ -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
Expand Down Expand Up @@ -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 = [
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
44 changes: 20 additions & 24 deletions formats/AtomFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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);
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
18 changes: 8 additions & 10 deletions formats/MrssFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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));
Expand All @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions formats/PlaintextFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
55 changes: 36 additions & 19 deletions lib/BridgeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit 689cfa8

Please sign in to comment.