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']],
['mood', 'MoodController'],
['mood', 'MoodController@handleMood', ['POST']],
['feed/rss', 'FeedController@rss'],
['feed/atom', 'FeedController@atom'],
];
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
#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);
$siteUrl = htmlspecialchars($config->baseUrl);
$basePath = $siteUrl . htmlspecialchars($config->basePath);
$updated = date(DATE_ATOM, strtotime($ticks[0]['timestamp'] ?? 'now'));
header('Content-Type: application/atom+xml; charset=utf-8');
echo <<<XML
<?xml version="1.0" encoding="utf-8"?>
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{$siteTitle}</title>
<link href="{$siteUrl}atom" rel="self"/>
<link href="{$siteUrl}"/>
<updated>{$updated}</updated>
<id>{$siteUrl}</id>
<title><?= $siteTitle ?></title>
<link ref="<?= $siteUrl ?>feed/atom" rel="self"/>
<link href="<?= $siteUrl ?>"/>
<updated><?= $updated ?></updated>
<id><?= $siteUrl ?></id>
<author>
<name>{$siteTitle}</name>
<name><?= $siteTitle ?></name>
</author>
XML;
foreach ($ticks as $tick) {
<?php foreach ($ticks as $tick):
[$date, $time] = explode(' ', $tick['timestamp']);
$dateParts = explode('-', $date);
$timeParts = explode(':', $time);
@ -39,19 +29,14 @@ foreach ($ticks as $tick) {
$tickPath = "$year/$month/$day/$hour/$minute/$second";
$tickUrl = htmlspecialchars($basePath . "tick.php?path=" . $tickPath);
$tickTime = date(DATE_ATOM, strtotime($tick['timestamp']));
$tickText = htmlspecialchars($tick['tick']);
echo <<<ENTRY
$tickText = htmlspecialchars($tick['tick']);
?>
<entry>
<title>{$tickText}</title>
<link href="{$tickUrl}"/>
<id>{$tickUrl}</id>
<updated>{$tickTime}</updated>
<content type="html">{$tickText}</content>
<title><?= $tickText ?></title>
<link href="<?= $tickUrl ?>"/>
<id><?= $tickUrl ?></id>
<updated><?= $tickTime ?></updated>
<content type="html"><?= $tickText ?></content>
</entry>
ENTRY;
}
echo "</feed>";
<?php endforeach; ?>
</feed>

View File

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