Convert feeds to MVC pattern.

This commit is contained in:
Greg Sarjeant 2025-06-03 09:29:04 -04:00
parent 79a161b0e6
commit 482beb9fb1
4 changed files with 52 additions and 51 deletions

View File

@ -121,7 +121,8 @@ $routes = [
['logout', 'AuthController@handleLogout', ['GET', 'POST']], ['logout', 'AuthController@handleLogout', ['GET', 'POST']],
['mood', 'MoodController'], ['mood', 'MoodController'],
['mood', 'MoodController@handleMood', ['POST']], ['mood', 'MoodController@handleMood', ['POST']],
['feed/rss', 'FeedController@rss'],
['feed/atom', 'FeedController@atom'],
]; ];
foreach ($routes as $routeConfig) { foreach ($routes as $routeConfig) {

View File

@ -0,0 +1,23 @@
<?php
class FeedController {
private Config $config;
private array $ticks;
private array $vars;
public function __construct(){
$this->config = Config::load();
$this->ticks = iterator_to_array(Tick::streamTicks($this->config->itemsPerPage));
$this->vars = [
'config' => $this->config,
'ticks' => $this->ticks,
];
}
public function rss(){
echo render_template(TEMPLATES_DIR . "/feed/rss.php", $this->vars);
}
public function atom(){
echo render_template(TEMPLATES_DIR . "/feed/atom.php", $this->vars);
}
}

View File

@ -1,34 +1,24 @@
<?php /** @var Config $config */ ?>
<?php /** @var array $ticks */ ?>
<?php <?php
#require_once __DIR__ . '/../../bootstrap.php';
#confirm_setup();
#require_once CLASSES_DIR . '/Config.php';
#require_once LIB_DIR . '/ticks.php';
$config = Config::load();
$ticks = iterator_to_array(stream_ticks($config->itemsPerPage));
$siteTitle = htmlspecialchars($config->siteTitle); $siteTitle = htmlspecialchars($config->siteTitle);
$siteUrl = htmlspecialchars($config->baseUrl); $siteUrl = htmlspecialchars($config->baseUrl);
$basePath = $siteUrl . htmlspecialchars($config->basePath); $basePath = $siteUrl . htmlspecialchars($config->basePath);
$updated = date(DATE_ATOM, strtotime($ticks[0]['timestamp'] ?? 'now')); $updated = date(DATE_ATOM, strtotime($ticks[0]['timestamp'] ?? 'now'));
header('Content-Type: application/atom+xml; charset=utf-8'); header('Content-Type: application/atom+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
echo <<<XML ?>
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"> <feed xmlns="http://www.w3.org/2005/Atom">
<title>{$siteTitle}</title> <title><?= $siteTitle ?></title>
<link href="{$siteUrl}atom" rel="self"/> <link ref="<?= $siteUrl ?>feed/atom" rel="self"/>
<link href="{$siteUrl}"/> <link href="<?= $siteUrl ?>"/>
<updated>{$updated}</updated> <updated><?= $updated ?></updated>
<id>{$siteUrl}</id> <id><?= $siteUrl ?></id>
<author> <author>
<name>{$siteTitle}</name> <name><?= $siteTitle ?></name>
</author> </author>
XML; <?php foreach ($ticks as $tick):
foreach ($ticks as $tick) {
[$date, $time] = explode(' ', $tick['timestamp']); [$date, $time] = explode(' ', $tick['timestamp']);
$dateParts = explode('-', $date); $dateParts = explode('-', $date);
$timeParts = explode(':', $time); $timeParts = explode(':', $time);
@ -40,18 +30,13 @@ foreach ($ticks as $tick) {
$tickUrl = htmlspecialchars($basePath . "tick.php?path=" . $tickPath); $tickUrl = htmlspecialchars($basePath . "tick.php?path=" . $tickPath);
$tickTime = date(DATE_ATOM, strtotime($tick['timestamp'])); $tickTime = date(DATE_ATOM, strtotime($tick['timestamp']));
$tickText = htmlspecialchars($tick['tick']); $tickText = htmlspecialchars($tick['tick']);
?>
echo <<<ENTRY
<entry> <entry>
<title>{$tickText}</title> <title><?= $tickText ?></title>
<link href="{$tickUrl}"/> <link href="<?= $tickUrl ?>"/>
<id>{$tickUrl}</id> <id><?= $tickUrl ?></id>
<updated>{$tickTime}</updated> <updated><?= $tickTime ?></updated>
<content type="html">{$tickText}</content> <content type="html"><?= $tickText ?></content>
</entry> </entry>
<?php endforeach; ?>
ENTRY; </feed>
}
echo "</feed>";

View File

@ -1,16 +1,10 @@
<?php /** @var Config $config */ ?>
<?php /** @var array $ticks */ ?>
<?php <?php
#require_once __DIR__ . '/../../bootstrap.php'; // Need to have a little php here because the starting xml tag
// will mess up the PHP parser.
#confirm_setup(); // TODO - I think short php tags can be disabled to prevent that.
#require_once CLASSES_DIR . '/Config.php';
#require_once LIB_DIR . '/ticks.php';
$config = Config::load();
$ticks = iterator_to_array(stream_ticks($config->itemsPerPage));
header('Content-Type: application/rss+xml; charset=utf-8'); header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?> ?>
<rss version="2.0"> <rss version="2.0">
@ -20,8 +14,8 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
<description>My tkr</description> <description>My tkr</description>
<language>en-us</language> <language>en-us</language>
<lastBuildDate><?php echo date(DATE_RSS); ?></lastBuildDate> <lastBuildDate><?php echo date(DATE_RSS); ?></lastBuildDate>
<?php foreach ($ticks as $tick): <?php foreach ($ticks as $tick):
// TODO: Make this a util function.
[$date, $time] = explode(' ', $tick['timestamp']); [$date, $time] = explode(' ', $tick['timestamp']);
$dateParts = explode('-', $date); $dateParts = explode('-', $date);
$timeParts = explode(':', $time); $timeParts = explode(':', $time);
@ -31,7 +25,6 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$tickPath = "$year/$month/$day/$hour/$minute/$second"; $tickPath = "$year/$month/$day/$hour/$minute/$second";
?> ?>
<item> <item>
<title><?php echo htmlspecialchars($tick['tick']); ?></title> <title><?php echo htmlspecialchars($tick['tick']); ?></title>
<link><?php echo htmlspecialchars($config->basePath . "tick.php?path=$tickPath"); ?></link> <link><?php echo htmlspecialchars($config->basePath . "tick.php?path=$tickPath"); ?></link>
@ -40,6 +33,5 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
<guid><?php echo htmlspecialchars($tickPath); ?></guid> <guid><?php echo htmlspecialchars($tickPath); ?></guid>
</item> </item>
<?php endforeach; ?> <?php endforeach; ?>
</channel> </channel>
</rss> </rss>