Add latest posts atom feed
authorChristian Weiske <cweiske@cweiske.de>
Mon, 14 Mar 2022 17:34:57 +0000 (18:34 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 14 Mar 2022 17:34:57 +0000 (18:34 +0100)
data/templates/latest-atom.htm [new file with mode: 0644]
data/templates/latest.htm
www/latest-atom.php [new file with mode: 0644]
www/www-header.php

diff --git a/data/templates/latest-atom.htm b/data/templates/latest-atom.htm
new file mode 100644 (file)
index 0000000..e3c40ff
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+ <title>Latest comments</title>
+ <link href="{{baseUrl}}/latest-atom.php"/>
+ <updated>{{lastComment.comment_published|date("c")}}</updated>
+ <id>{{baseUrl}}/latest-atom.php</id>
+
+ {% for comment in comments %}
+ <entry>
+  <title>{{comment.comment_type}} #{{comment.comment_id}} to {{comment.domain}}</title>
+  <link href="{{baseUrl}}{{comment.url}}"/>
+  <id>{{baseUrl}}{{comment.url}}</id>
+  <updated>{{comment.comment_published|date("c")}}</updated>
+  <author>
+   <name>{{comment.user.user_name}}</name>
+  </author>
+  <summary>
+   {{comment.comment_type}} to
+   {{comment.comment_of_url}}
+  </summary>
+ </entry>
+ {% endfor %}
+</feed>
index 62868196515e68ed1587f4a946ffbad0817f2648..5d9d38022c67707ec775be9f5cdc03ef49ed9d77 100644 (file)
@@ -4,6 +4,7 @@
   <meta charset="utf-8"/>
   <title>Latest comments</title>
   <link rel="stylesheet" type="text/css" href="css/latest.css"/>
+  <link rel="alternate" type="application/atom+xml" href="latest-atom.php"/>
   <meta name="generator" content="anoweco"/>
  </head>
  <body class="h-entry">
diff --git a/www/latest-atom.php b/www/latest-atom.php
new file mode 100644 (file)
index 0000000..6232b57
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+namespace anoweco;
+require 'www-header.php';
+
+$storage = new Storage();
+$comments = $storage->listLatest();
+
+foreach ($comments as $comment) {
+    $comment->url = Urls::comment($comment->comment_id);
+    $comment->domain = parse_url($comment->comment_of_url, PHP_URL_HOST);
+}
+
+$vars = [
+    'baseUrl'     => getBaseUrl(),
+    'comments'    => $comments,
+    'lastComment' => $comments[0],
+];
+header('Content-Type: application/atom+xml');
+render('latest-atom', $vars);
+?>
index 37dd4b11e943eaf368b2209e2394790b480fc3d7..ec7e8b3dafc3d1e59963de74466507d402d6e3ee 100644 (file)
@@ -73,4 +73,13 @@ function render($tplname, $vars = array(), $return = false)
         echo $template->render($vars);
     }
 }
+
+/**
+ * No trailing slash
+ */
+function getBaseUrl()
+{
+    return $_SERVER['REQUEST_SCHEME']
+        . '://' . $_SERVER['HTTP_HOST'];
+}
 ?>