[HtmlFormat] Dynamically build buttons for other feed formats

Adding or removing feed formats from the "formats/" directory
currently has no effect on the buttons shown in the HTML format.
This can cause errors if users press one of the buttons for a
format that is no longer available on the server.

This commit changes the behavior to dynamically add buttons based
on the available formats. Syndication feeds, however, are no longer
supported as they require knowledge about the content type, which
is not known without further changes to the formats API (may be
added later if there is a demand).

Closes #942
This commit is contained in:
logmanoriginal 2019-06-19 23:09:08 +02:00
parent 1989252608
commit 7ff97c0c7b
1 changed files with 22 additions and 6 deletions

View File

@ -4,8 +4,21 @@ class HtmlFormat extends FormatAbstract {
$extraInfos = $this->getExtraInfos();
$title = htmlspecialchars($extraInfos['name']);
$uri = htmlspecialchars($extraInfos['uri']);
$atomquery = str_replace('format=Html', 'format=Atom', htmlentities($_SERVER['QUERY_STRING']));
$mrssquery = str_replace('format=Html', 'format=Mrss', htmlentities($_SERVER['QUERY_STRING']));
// Dynamically build buttons for all formats (except HTML)
$formatFac = new FormatFactory();
$formatFac->setWorkingDir(PATH_LIB_FORMATS);
$buttons = '';
foreach($formatFac->getFormatNames() as $format) {
if(strcasecmp($format, 'HTML') === 0) {
continue;
}
$query = str_replace('format=Html', 'format=' . $format, htmlentities($_SERVER['QUERY_STRING']));
$buttons .= $this->buildButton($format, $query) . PHP_EOL;
}
$entries = '';
foreach($this->getItems() as $item) {
@ -84,16 +97,13 @@ EOD;
<meta charset="{$charset}">
<title>{$title}</title>
<link href="static/HtmlFormat.css" rel="stylesheet">
<link rel="alternate" type="application/atom+xml" title="Atom" href="./?{$atomquery}" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="/?{$mrssquery}" />
<meta name="robots" content="noindex, follow">
</head>
<body>
<h1 class="pagetitle"><a href="{$uri}" target="_blank">{$title}</a></h1>
<div class="buttons">
<a href="./#bridge-{$_GET['bridge']}"><button class="backbutton"> back to rss-bridge</button></a>
<a href="./?{$atomquery}"><button class="rss-feed">RSS feed (ATOM)</button></a>
<a href="./?{$mrssquery}"><button class="rss-feed">RSS feed (MRSS)</button></a>
{$buttons}
</div>
{$entries}
</body>
@ -113,4 +123,10 @@ EOD;
return parent::display();
}
private function buildButton($format, $query) {
return <<<EOD
<a href="./?{$query}"><button class="rss-feed">{$format}</button></a>
EOD;
}
}