Make RSS feed
[pics.git] / rss.php
CommitLineData
ac20a7ef
JC
1<?php
2
3require_once __DIR__ . '/db.php';
4
5function post_to_item(DOMDocument $dom, Array $post) {
6 $mime_type = $post['MIME_TYPE'];
7 $post_id = $post['ID'];
8 $post_body = $post['BODY'];
9 $timestamp = $post['TIMESTAMP'];
10 $item = $dom->createElement('item');
11 $description = $dom->createElement('description', $post_body);
12 $link = $dom->createElement('link', "https://pics.jacobcasper.com/#post-{$post_id}");
13 $guid = $dom->createElement('guid', "https://pics.jacobcasper.com/#post-{$post_id}");
14 $pub_date = $dom->createElement('pubDate', date(DATE_RSS, $timestamp));
15 // TODO write the endpoint for this
16 //$enclosure = $dom->createElement('enclosure');
17 //$enclosure->setAttribute('url', "https://pics.jacobcasper.com/pic/{$post_id}");
18 //$enclosure->setAttribute('length', "9999");
19 //$enclosure->setAttribute('type', $mime_type);
20 $item->appendChild($description);
21 //$item->appendChild($enclosure);
22 $item->appendChild($link);
23 $item->appendChild($guid);
24 $item->appendChild($pub_date);
25 return $item;
26}
27
28$rss_template = file_get_contents('rss.template.xml');
29$rss_dom = new DOMDocument();
30$rss_dom->formatOutput = true;
31$rss_dom->loadXML($rss_template);
32$channel_node = $rss_dom->firstChild->childNodes[1];
33$db = get_db();
34$posts = $db->query('SELECT
35 p.ID, p.TIMESTAMP, pi.MIME_TYPE, c.BODY
36 FROM posts p
37 JOIN pics pi on p.id = pi.post_id
38 JOIN comments c on p.id = c.post_id
39 ORDER BY p.TIMESTAMP DESC');
40while ($post = $posts->fetchArray()) {
41 $item = post_to_item($rss_dom, $post);
42 $channel_node->appendChild($item);
43}
44
45file_put_contents('rss.xml', $rss_dom->saveXML());