From 6516e31c1bc78867820fead69ad65ba4c1d350ea Mon Sep 17 00:00:00 2001 From: Dag Date: Fri, 23 Aug 2024 17:34:06 +0200 Subject: [PATCH] refactor: format rendering (#4229) --- actions/DisplayAction.php | 10 ++++++++-- formats/AtomFormat.php | 10 ++-------- formats/HtmlFormat.php | 6 +----- formats/JsonFormat.php | 2 +- formats/MrssFormat.php | 7 ++----- formats/PlaintextFormat.php | 5 +---- formats/SfeedFormat.php | 9 +-------- index.php | 7 +++++-- lib/FormatAbstract.php | 13 +------------ templates/html-format.html.php | 2 +- tests/FormatTest.php | 2 +- tests/Formats/BaseFormatTest.php | 2 +- 12 files changed, 25 insertions(+), 50 deletions(-) diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php index 26f1cb40..5265abd8 100644 --- a/actions/DisplayAction.php +++ b/actions/DisplayAction.php @@ -160,9 +160,15 @@ class DisplayAction implements ActionInterface $format->setLastModified($now); $headers = [ '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 diff --git a/formats/AtomFormat.php b/formats/AtomFormat.php index 5c9f2b6a..7908eb90 100644 --- a/formats/AtomFormat.php +++ b/formats/AtomFormat.php @@ -14,9 +14,9 @@ class AtomFormat extends FormatAbstract protected const ATOM_NS = 'http://www.w3.org/2005/Atom'; 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; $feedUrl = get_current_url(); @@ -86,8 +86,6 @@ class AtomFormat extends FormatAbstract $author->appendChild($authorName); $authorName->appendChild($document->createTextNode($feedAuthor)); - - foreach ($this->getItems() as $item) { $itemArray = $item->toArray(); $entryTimestamp = $item->getTimestamp(); @@ -204,10 +202,6 @@ class AtomFormat extends FormatAbstract } $xml = $document->saveXML(); - - // Remove invalid characters - ini_set('mbstring.substitute_character', 'none'); - $xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8'); return $xml; } } diff --git a/formats/HtmlFormat.php b/formats/HtmlFormat.php index a5bcc451..04721ae1 100644 --- a/formats/HtmlFormat.php +++ b/formats/HtmlFormat.php @@ -4,7 +4,7 @@ class HtmlFormat extends FormatAbstract { const MIME_TYPE = 'text/html'; - public function stringify() + public function render(): string { // This query string is url encoded $queryString = $_SERVER['QUERY_STRING']; @@ -52,16 +52,12 @@ class HtmlFormat extends FormatAbstract $html = render_template(__DIR__ . '/../templates/html-format.html.php', [ 'bridge_name' => $bridgeName, - 'charset' => $this->getCharset(), 'title' => $feedArray['name'], 'formats' => $formats, 'uri' => $feedArray['uri'], 'items' => $items, 'donation_uri' => $donationUri, ]); - // Remove invalid characters - ini_set('mbstring.substitute_character', 'none'); - $html = mb_convert_encoding($html, $this->getCharset(), 'UTF-8'); return $html; } } diff --git a/formats/JsonFormat.php b/formats/JsonFormat.php index 586aae0a..3548ef6e 100644 --- a/formats/JsonFormat.php +++ b/formats/JsonFormat.php @@ -23,7 +23,7 @@ class JsonFormat extends FormatAbstract 'uid', ]; - public function stringify() + public function render(): string { $feedArray = $this->getFeed(); diff --git a/formats/MrssFormat.php b/formats/MrssFormat.php index aaa1d0cd..f7b11949 100644 --- a/formats/MrssFormat.php +++ b/formats/MrssFormat.php @@ -32,9 +32,9 @@ class MrssFormat extends FormatAbstract protected const ATOM_NS = 'http://www.w3.org/2005/Atom'; 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; $feed = $document->createElement('rss'); @@ -198,9 +198,6 @@ class MrssFormat extends FormatAbstract } $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; } } diff --git a/formats/PlaintextFormat.php b/formats/PlaintextFormat.php index 4e18caa6..e93c94b5 100644 --- a/formats/PlaintextFormat.php +++ b/formats/PlaintextFormat.php @@ -4,16 +4,13 @@ class PlaintextFormat extends FormatAbstract { const MIME_TYPE = 'text/plain'; - public function stringify() + public function render(): string { $feed = $this->getFeed(); foreach ($this->getItems() as $item) { $feed['items'][] = $item->toArray(); } $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; } } diff --git a/formats/SfeedFormat.php b/formats/SfeedFormat.php index 33740aaa..063e4543 100644 --- a/formats/SfeedFormat.php +++ b/formats/SfeedFormat.php @@ -4,7 +4,7 @@ class SfeedFormat extends FormatAbstract { const MIME_TYPE = 'text/plain'; - public function stringify() + public function render(): string { $text = ''; 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; } diff --git a/index.php b/index.php index c4fe104f..2a613363 100644 --- a/index.php +++ b/index.php @@ -62,8 +62,11 @@ register_shutdown_function(function () use ($logger) { $cacheFactory = new CacheFactory($logger); -// Uncomment this for debug logging -// $logger->addHandler(new StreamHandler('/tmp/rss-bridge.txt', Logger::DEBUG)); +// Uncomment this for info logging to fs +// $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()) { $logger->addHandler(new ErrorLogHandler(Logger::DEBUG)); diff --git a/lib/FormatAbstract.php b/lib/FormatAbstract.php index 9cba0d8c..17e733a7 100644 --- a/lib/FormatAbstract.php +++ b/lib/FormatAbstract.php @@ -8,11 +8,10 @@ abstract class FormatAbstract protected array $feed = []; protected array $items = []; - protected string $charset = 'UTF-8'; protected int $lastModified; - abstract public function stringify(); + abstract public function render(): string; public function setFeed(array $feed) { @@ -50,16 +49,6 @@ abstract class FormatAbstract return static::MIME_TYPE; } - public function setCharset(string $charset) - { - $this->charset = $charset; - } - - public function getCharset(): string - { - return $this->charset; - } - public function setLastModified(int $lastModified) { $this->lastModified = $lastModified; diff --git a/templates/html-format.html.php b/templates/html-format.html.php index a05acdde..8e8abfcf 100644 --- a/templates/html-format.html.php +++ b/templates/html-format.html.php @@ -1,7 +1,7 @@ - + <?= e($title) ?> diff --git a/tests/FormatTest.php b/tests/FormatTest.php index b5df395c..33e02769 100644 --- a/tests/FormatTest.php +++ b/tests/FormatTest.php @@ -58,7 +58,7 @@ class FormatTest extends TestCase class TestFormat extends \FormatAbstract { - public function stringify() + public function render(): string { } } diff --git a/tests/Formats/BaseFormatTest.php b/tests/Formats/BaseFormatTest.php index 0907b72a..4b6a549b 100644 --- a/tests/Formats/BaseFormatTest.php +++ b/tests/Formats/BaseFormatTest.php @@ -64,6 +64,6 @@ abstract class BaseFormatTest extends TestCase $format->setFeed($sample->meta); $format->setLastModified(strtotime('2000-01-01 12:00:00 UTC')); - return $format->stringify(); + return $format->render(); } }