Add atom feed generation.

This commit is contained in:
Zdenek Borovec 2024-07-21 00:04:51 +02:00
parent f5598d3ea2
commit 4fd82cb3d9
3 changed files with 114 additions and 1 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/fcgid-bin/ /fcgid-bin/
/docs/common/config.php /docs/common/config.php
/assets/upload/** /assets/upload/**
/docs/www/blog/feed.atom

View file

@ -110,6 +110,15 @@ display_header("Blogs");
These can range from well thought out slow-burning blood-churning gems These can range from well thought out slow-burning blood-churning gems
to random brain farts in textual form, you can use tags to filter. to random brain farts in textual form, you can use tags to filter.
</p> </p>
<p>
<a href="http://www.zdenekborovec-dev.cz/blog/feed.atom">
<img src="https://assets.zdenekborovec.cz/upload/
4d8e8ce6d40218d86b2d4054cd02fe7f/22db7ac10bc3065f3dea6d797c51c627/
cd27fb6e76320b6f5ebe9ab951042ff3.png" style="height: 1em;"/></a>
Recent blogposts get posted to
<a href="http://www.zdenekborovec-dev.cz/blog/feed.atom">
my atom feed.</a>
</p>
</article> </article>
<hr> <hr>

View file

@ -13,6 +13,106 @@ if (!isset($_COOKIE["PHPSESSID"]) ||
die(); die();
} }
/**
* Generate a feed.atom file for use by atom/rss readers.
*/
function generate_atom_feed($conn) {
if (!($fp = fopen('feed.atom', 'w'))) {
return;
}
// Prepare statement for selecting all the blogposts
$stmt = $conn->prepare("SELECT blogpost_id, readable_address, title,
abstract, content, date_posted, date_edited FROM blogposts ORDER BY
date_posted DESC LIMIT 15;");
// Execute the statement
$stmt->execute();
// Prepare new statement for selecting the tags for a given blogpost
$blogposts_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
fprintf($fp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<feed xmlns=\"http://www.w3.org/2005/Atom\">
<title>Zdenek Borovec blog</title>
<link href=\"https://www.zdenekborovec.cz/blog/\"/>
<updated>%s</updated>
<author>
<name>Zdenek Borovec</name>
</author>
<id>https://www.zenekborovec.cz/blog/</id>
", date("Y-m-d\TH:i:s\Z"));
// Prepare new statement for selecting the tags for a given blogpost
$stmt = $conn->prepare("SELECT name FROM
blogpost_tags INNER JOIN blogpost_has_tag ON
blogpost_tags.tag_id = blogpost_has_tag.tag_id WHERE
blogpost_id = :blogpost_id;");
// Go through all the blogposts, fetch their tags and display them
for($i=0; $i < count($blogposts_arr); $i++) {
// Get info for the current blog
$blog = $blogposts_arr[$i];
// Bind and execute the tag select
$stmt->bindParam(":blogpost_id", $blog["blogpost_id"]);
$stmt->execute();
// Fetch the tags
$tags_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
$categoryStr = "";
for($j=0; $j < count($tags_arr); $j++) {
$tag = $tags_arr[$j];
$categoryStr = $categoryStr."<category term=\"".
$tag["name"]."\"/>";
}
if(is_null($blog["readable_address"])) {
fprintf($fp, "
<entry>
<title>%s</title>
%s
<link href=
\"https://www.zdenekborovec.cz/blog/article/?blogpost_id=%s\"/>
<id>urn:uuid:%s</id>
<published>%s</published>
<updated>%s</updated>
<summary>%s</summary>
<content type=\"html\">
%s
</content>
</entry>", $blog["title"], $categoryStr, $blog["blogpost_id"],
$blog["blogpost_id"],
date("Y-m-d\TH:i:s\Z", strtotime($blog["date_posted"])),
date("Y-m-d\TH:i:s\Z", strtotime($blog["date_edited"])),
$blog["abstract"], sanitize_input($blog["content"]));
}
else {
fprintf($fp, "
<entry>
<title>%s</title>
%s
<link href=
\"https://www.zdenekborovec.cz/blog/article/?address=%s\"/>
<id>urn:uuid:%s</id>
<published>%s</published>
<updated>%s</updated>
<summary>%s</summary>
<content type=\"html\">
%s
</content>
</entry>", $blog["title"], $categoryStr, $blog["readable_address"],
$blog["blogpost_id"],
date("Y-m-d\TH:i:s\Z", strtotime($blog["date_posted"])),
date("Y-m-d\TH:i:s\Z", strtotime($blog["date_edited"])),
$blog["abstract"], sanitize_input($blog["content"]));
}
}
fprintf($fp, "</feed>");
}
/** /**
* Explode the tag string into separate tags, if they exist, * Explode the tag string into separate tags, if they exist,
* attach them to the article. * attach them to the article.
@ -166,7 +266,10 @@ if(isset($_POST["submit"])) {
$content); $content);
} }
header("Location: "."http://www.zdenekborovec-dev.cz/blog"); generate_atom_feed($conn);
header("Location: http://www.zdenekborovec-dev.cz/blog");
die();
} }
if(isset($_GET["guid"])) { if(isset($_GET["guid"])) {