From 482beb9fb18e27c41c4fdb8ec953b544fa6ea216 Mon Sep 17 00:00:00 2001 From: Greg Sarjeant <1686767+gsarjeant@users.noreply.github.com> Date: Tue, 3 Jun 2025 09:29:04 -0400 Subject: [PATCH] Convert feeds to MVC pattern. --- public/index.php | 3 +- src/Controller/Feed/Feed.php | 23 +++++++++ templates/{atom/index.php => feed/atom.php} | 55 ++++++++------------- templates/{rss/index.php => feed/rss.php} | 22 +++------ 4 files changed, 52 insertions(+), 51 deletions(-) create mode 100644 src/Controller/Feed/Feed.php rename templates/{atom/index.php => feed/atom.php} (50%) rename templates/{rss/index.php => feed/rss.php} (79%) diff --git a/public/index.php b/public/index.php index 11b2a89..86194e2 100644 --- a/public/index.php +++ b/public/index.php @@ -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) { diff --git a/src/Controller/Feed/Feed.php b/src/Controller/Feed/Feed.php new file mode 100644 index 0000000..3e362bd --- /dev/null +++ b/src/Controller/Feed/Feed.php @@ -0,0 +1,23 @@ +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); + } +} diff --git a/templates/atom/index.php b/templates/feed/atom.php similarity index 50% rename from templates/atom/index.php rename to templates/feed/atom.php index e33d7de..dabb21e 100644 --- a/templates/atom/index.php +++ b/templates/feed/atom.php @@ -1,34 +1,24 @@ + + 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 << +echo '' . "\n"; +?> - {$siteTitle} - - - {$updated} - {$siteUrl} + <?= $siteTitle ?> + + + + - {$siteTitle} + -XML; - -foreach ($ticks as $tick) { + - {$tickText} - - {$tickUrl} - {$tickTime} - {$tickText} + <?= $tickText ?> + + + + - -ENTRY; -} - -echo ""; + + diff --git a/templates/rss/index.php b/templates/feed/rss.php similarity index 79% rename from templates/rss/index.php rename to templates/feed/rss.php index f036e3a..ee5f5f9 100644 --- a/templates/rss/index.php +++ b/templates/feed/rss.php @@ -1,16 +1,10 @@ + + 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 '' . "\n"; ?> @@ -20,8 +14,8 @@ echo '' . "\n"; My tkr en-us - ' . "\n"; $tickPath = "$year/$month/$day/$hour/$minute/$second"; ?> - <?php echo htmlspecialchars($tick['tick']); ?> basePath . "tick.php?path=$tickPath"); ?> @@ -39,7 +32,6 @@ echo '' . "\n"; - - +