Skip to content

Commit

Permalink
refactor: format rendering (#4229)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan authored Aug 23, 2024
1 parent c849576 commit 6516e31
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 50 deletions.
10 changes: 8 additions & 2 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,15 @@ private function createResponse(Request $request, BridgeAbstract $bridge, string
$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
Expand Down
10 changes: 2 additions & 8 deletions formats/AtomFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -86,8 +86,6 @@ public function stringify()
$author->appendChild($authorName);
$authorName->appendChild($document->createTextNode($feedAuthor));



foreach ($this->getItems() as $item) {
$itemArray = $item->toArray();
$entryTimestamp = $item->getTimestamp();
Expand Down Expand Up @@ -204,10 +202,6 @@ public function stringify()
}

$xml = $document->saveXML();

// Remove invalid characters
ini_set('mbstring.substitute_character', 'none');
$xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8');
return $xml;
}
}
6 changes: 1 addition & 5 deletions formats/HtmlFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -52,16 +52,12 @@ public function stringify()

$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;
}
}
2 changes: 1 addition & 1 deletion formats/JsonFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class JsonFormat extends FormatAbstract
'uid',
];

public function stringify()
public function render(): string
{
$feedArray = $this->getFeed();

Expand Down
7 changes: 2 additions & 5 deletions formats/MrssFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -198,9 +198,6 @@ public function stringify()
}

$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;
}
}
5 changes: 1 addition & 4 deletions formats/PlaintextFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 1 addition & 8 deletions formats/SfeedFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -26,13 +26,6 @@ public function stringify()
);
}

// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$text = mb_convert_encoding(
$text,
$this->getCharset(),
'UTF-8'
);
return $text;
}

Expand Down
7 changes: 5 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@

$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));
Expand Down
13 changes: 1 addition & 12 deletions lib/FormatAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -50,16 +49,6 @@ public function getMimeType(): string
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;
Expand Down
2 changes: 1 addition & 1 deletion templates/html-format.html.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="<?= $charset ?>">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/ >
<meta name="description" content="RSS-Bridge" />
<title><?= e($title) ?></title>
Expand Down
2 changes: 1 addition & 1 deletion tests/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testBridge()

class TestFormat extends \FormatAbstract
{
public function stringify()
public function render(): string
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Formats/BaseFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ protected function formatData(string $formatName, \stdClass $sample): string
$format->setFeed($sample->meta);
$format->setLastModified(strtotime('2000-01-01 12:00:00 UTC'));

return $format->stringify();
return $format->render();
}
}

0 comments on commit 6516e31

Please sign in to comment.