refactor: format rendering (#4229)

This commit is contained in:
Dag 2024-08-23 17:34:06 +02:00 committed by GitHub
parent c849576c93
commit 6516e31c1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 25 additions and 50 deletions

View file

@ -160,9 +160,15 @@ class DisplayAction implements ActionInterface
$format->setLastModified($now); $format->setLastModified($now);
$headers = [ $headers = [
'last-modified' => gmdate('D, d M Y H:i:s ', $now) . 'GMT', 'last-modified' => gmdate('D, d M Y H:i:s ', $now) . 'GMT',
'content-type' => $format->getMimeType() . '; charset=' . $format->getCharset(), 'content-type' => $format->getMimeType() . '; charset=UTF-8',
]; ];
return new Response($format->stringify(), 200, $headers); $body = $format->render();
// This is supposed to remove non-utf8 byte sequences, but I'm unsure if it works
ini_set('mbstring.substitute_character', 'none');
$body = mb_convert_encoding($body, 'UTF-8', 'UTF-8');
return new Response($body, 200, $headers);
} }
private function createFeedItemFromException($e, BridgeAbstract $bridge): array private function createFeedItemFromException($e, BridgeAbstract $bridge): array

View file

@ -14,9 +14,9 @@ class AtomFormat extends FormatAbstract
protected const ATOM_NS = 'http://www.w3.org/2005/Atom'; protected const ATOM_NS = 'http://www.w3.org/2005/Atom';
protected const MRSS_NS = 'http://search.yahoo.com/mrss/'; protected const MRSS_NS = 'http://search.yahoo.com/mrss/';
public function stringify() public function render(): string
{ {
$document = new \DomDocument('1.0', $this->getCharset()); $document = new \DomDocument('1.0', 'UTF-8');
$document->formatOutput = true; $document->formatOutput = true;
$feedUrl = get_current_url(); $feedUrl = get_current_url();
@ -86,8 +86,6 @@ class AtomFormat extends FormatAbstract
$author->appendChild($authorName); $author->appendChild($authorName);
$authorName->appendChild($document->createTextNode($feedAuthor)); $authorName->appendChild($document->createTextNode($feedAuthor));
foreach ($this->getItems() as $item) { foreach ($this->getItems() as $item) {
$itemArray = $item->toArray(); $itemArray = $item->toArray();
$entryTimestamp = $item->getTimestamp(); $entryTimestamp = $item->getTimestamp();
@ -204,10 +202,6 @@ class AtomFormat extends FormatAbstract
} }
$xml = $document->saveXML(); $xml = $document->saveXML();
// Remove invalid characters
ini_set('mbstring.substitute_character', 'none');
$xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8');
return $xml; return $xml;
} }
} }

View file

@ -4,7 +4,7 @@ class HtmlFormat extends FormatAbstract
{ {
const MIME_TYPE = 'text/html'; const MIME_TYPE = 'text/html';
public function stringify() public function render(): string
{ {
// This query string is url encoded // This query string is url encoded
$queryString = $_SERVER['QUERY_STRING']; $queryString = $_SERVER['QUERY_STRING'];
@ -52,16 +52,12 @@ class HtmlFormat extends FormatAbstract
$html = render_template(__DIR__ . '/../templates/html-format.html.php', [ $html = render_template(__DIR__ . '/../templates/html-format.html.php', [
'bridge_name' => $bridgeName, 'bridge_name' => $bridgeName,
'charset' => $this->getCharset(),
'title' => $feedArray['name'], 'title' => $feedArray['name'],
'formats' => $formats, 'formats' => $formats,
'uri' => $feedArray['uri'], 'uri' => $feedArray['uri'],
'items' => $items, 'items' => $items,
'donation_uri' => $donationUri, 'donation_uri' => $donationUri,
]); ]);
// Remove invalid characters
ini_set('mbstring.substitute_character', 'none');
$html = mb_convert_encoding($html, $this->getCharset(), 'UTF-8');
return $html; return $html;
} }
} }

View file

@ -23,7 +23,7 @@ class JsonFormat extends FormatAbstract
'uid', 'uid',
]; ];
public function stringify() public function render(): string
{ {
$feedArray = $this->getFeed(); $feedArray = $this->getFeed();

View file

@ -32,9 +32,9 @@ class MrssFormat extends FormatAbstract
protected const ATOM_NS = 'http://www.w3.org/2005/Atom'; protected const ATOM_NS = 'http://www.w3.org/2005/Atom';
protected const MRSS_NS = 'http://search.yahoo.com/mrss/'; protected const MRSS_NS = 'http://search.yahoo.com/mrss/';
public function stringify() public function render(): string
{ {
$document = new \DomDocument('1.0', $this->getCharset()); $document = new \DomDocument('1.0', 'UTF-8');
$document->formatOutput = true; $document->formatOutput = true;
$feed = $document->createElement('rss'); $feed = $document->createElement('rss');
@ -198,9 +198,6 @@ class MrssFormat extends FormatAbstract
} }
$xml = $document->saveXML(); $xml = $document->saveXML();
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8');
return $xml; return $xml;
} }
} }

View file

@ -4,16 +4,13 @@ class PlaintextFormat extends FormatAbstract
{ {
const MIME_TYPE = 'text/plain'; const MIME_TYPE = 'text/plain';
public function stringify() public function render(): string
{ {
$feed = $this->getFeed(); $feed = $this->getFeed();
foreach ($this->getItems() as $item) { foreach ($this->getItems() as $item) {
$feed['items'][] = $item->toArray(); $feed['items'][] = $item->toArray();
} }
$text = print_r($feed, 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');
return $text; return $text;
} }
} }

View file

@ -4,7 +4,7 @@ class SfeedFormat extends FormatAbstract
{ {
const MIME_TYPE = 'text/plain'; const MIME_TYPE = 'text/plain';
public function stringify() public function render(): string
{ {
$text = ''; $text = '';
foreach ($this->getItems() as $item) { foreach ($this->getItems() as $item) {
@ -26,13 +26,6 @@ class SfeedFormat extends FormatAbstract
); );
} }
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$text = mb_convert_encoding(
$text,
$this->getCharset(),
'UTF-8'
);
return $text; return $text;
} }

View file

@ -62,8 +62,11 @@ register_shutdown_function(function () use ($logger) {
$cacheFactory = new CacheFactory($logger); $cacheFactory = new CacheFactory($logger);
// Uncomment this for debug logging // Uncomment this for info logging to fs
// $logger->addHandler(new StreamHandler('/tmp/rss-bridge.txt', Logger::DEBUG)); // $logger->addHandler(new StreamHandler('/tmp/rss-bridge.txt', Logger::INFO));
// Uncomment this for debug logging to fs
// $logger->addHandler(new StreamHandler('/tmp/rss-bridge-debug.txt', Logger::DEBUG));
if (Debug::isEnabled()) { if (Debug::isEnabled()) {
$logger->addHandler(new ErrorLogHandler(Logger::DEBUG)); $logger->addHandler(new ErrorLogHandler(Logger::DEBUG));

View file

@ -8,11 +8,10 @@ abstract class FormatAbstract
protected array $feed = []; protected array $feed = [];
protected array $items = []; protected array $items = [];
protected string $charset = 'UTF-8';
protected int $lastModified; protected int $lastModified;
abstract public function stringify(); abstract public function render(): string;
public function setFeed(array $feed) public function setFeed(array $feed)
{ {
@ -50,16 +49,6 @@ abstract class FormatAbstract
return static::MIME_TYPE; return static::MIME_TYPE;
} }
public function setCharset(string $charset)
{
$this->charset = $charset;
}
public function getCharset(): string
{
return $this->charset;
}
public function setLastModified(int $lastModified) public function setLastModified(int $lastModified)
{ {
$this->lastModified = $lastModified; $this->lastModified = $lastModified;

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="<?= $charset ?>"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/ > <meta name="viewport" content="width=device-width, initial-scale=1.0"/ >
<meta name="description" content="RSS-Bridge" /> <meta name="description" content="RSS-Bridge" />
<title><?= e($title) ?></title> <title><?= e($title) ?></title>

View file

@ -58,7 +58,7 @@ class FormatTest extends TestCase
class TestFormat extends \FormatAbstract class TestFormat extends \FormatAbstract
{ {
public function stringify() public function render(): string
{ {
} }
} }

View file

@ -64,6 +64,6 @@ abstract class BaseFormatTest extends TestCase
$format->setFeed($sample->meta); $format->setFeed($sample->meta);
$format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); $format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));
return $format->stringify(); return $format->render();
} }
} }