Make RSS feed
authorJacob Casper <dev@jacobcasper.com>
Sat, 25 Jun 2022 22:26:26 +0000 (17:26 -0500)
committerJacob Casper <dev@jacobcasper.com>
Sat, 25 Jun 2022 22:26:26 +0000 (17:26 -0500)
RSS feed template, generator, and wiring. Needs to be displayed
prominently on the index and automated.

README.md
index.php
rss.php [new file with mode: 0644]
rss.template.xml [new file with mode: 0644]
rss_test.php [new file with mode: 0644]

index ab4860e..20c3320 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
 Requirements:
 - php7.3+
 - php-imagick
+- php-xml
 - sqlite3
index 88310ff..c8f23f5 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,6 +1,7 @@
 <html>
 <head>
 <link rel="stylesheet" href="style.css">
+<link rel="alternate" title="Pics dot JC" type="application/rss+xml" href="/rss.xml">
 </head>
 
 <body>
diff --git a/rss.php b/rss.php
new file mode 100644 (file)
index 0000000..0312255
--- /dev/null
+++ b/rss.php
@@ -0,0 +1,45 @@
+<?php
+
+require_once __DIR__ . '/db.php';
+
+function post_to_item(DOMDocument $dom, Array $post) {
+      $mime_type = $post['MIME_TYPE'];
+      $post_id = $post['ID'];
+      $post_body = $post['BODY'];
+      $timestamp = $post['TIMESTAMP'];
+      $item = $dom->createElement('item');
+      $description = $dom->createElement('description', $post_body);
+      $link = $dom->createElement('link', "https://pics.jacobcasper.com/#post-{$post_id}");
+      $guid = $dom->createElement('guid', "https://pics.jacobcasper.com/#post-{$post_id}");
+      $pub_date = $dom->createElement('pubDate', date(DATE_RSS, $timestamp));
+      // TODO write the endpoint for this
+      //$enclosure = $dom->createElement('enclosure');
+      //$enclosure->setAttribute('url', "https://pics.jacobcasper.com/pic/{$post_id}");
+      //$enclosure->setAttribute('length', "9999");
+      //$enclosure->setAttribute('type', $mime_type);
+      $item->appendChild($description);
+      //$item->appendChild($enclosure);
+      $item->appendChild($link);
+      $item->appendChild($guid);
+      $item->appendChild($pub_date);
+      return $item;
+}
+
+$rss_template = file_get_contents('rss.template.xml');
+$rss_dom = new DOMDocument();
+$rss_dom->formatOutput = true;
+$rss_dom->loadXML($rss_template);
+$channel_node = $rss_dom->firstChild->childNodes[1];
+$db = get_db();
+$posts = $db->query('SELECT
+    p.ID, p.TIMESTAMP, pi.MIME_TYPE, c.BODY
+    FROM posts p
+    JOIN pics pi on p.id = pi.post_id
+    JOIN comments c on p.id = c.post_id
+    ORDER BY p.TIMESTAMP DESC');
+while ($post = $posts->fetchArray()) {
+    $item = post_to_item($rss_dom, $post);
+    $channel_node->appendChild($item);
+}
+
+file_put_contents('rss.xml', $rss_dom->saveXML());
diff --git a/rss.template.xml b/rss.template.xml
new file mode 100644 (file)
index 0000000..7975fe5
--- /dev/null
@@ -0,0 +1,11 @@
+<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
+       <channel>
+               <atom:link href="https://pics.jacobcasper.com/rss.xml" rel="self" type="application/rss+xml"/>
+               <title>Jacob's Not-gram</title>
+               <link>https://pics.jacobcasper.com</link>
+               <description>picture of JC</description>
+               <language>en-US</language>
+               <webMaster>pics-wm@jacobcasper.com (Jacob Casper)</webMaster>
+        <ttl>1440</ttl> <!-- 24 hour cache (in minutes) -->
+       </channel>
+</rss>
diff --git a/rss_test.php b/rss_test.php
new file mode 100644 (file)
index 0000000..4d86918
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+require_once __DIR__ . '/rss.php';
+assert_options(ASSERT_ACTIVE, 1);
+
+function test_post_to_item_success() {
+    $post = [
+        'MIME_TYPE' => 'image/jpeg',
+        'ID' => 'uuid-v4',
+        'BODY' => 'lorem ipsum dolor',
+        'TIMESTAMP' => '1400000000',
+    ];
+    $dom = new DOMDocument();
+    $dom->formatOutput = true;
+    $item_node = post_to_item($dom, $post);
+    var_dump($item_node);
+    $dom->appendChild($item_node);
+    var_dump($dom->saveXML($dom));
+}
+
+test_post_to_item_success();