59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
declare(strict_types = 1);
|
|
|
|
include_once('config/default.php');
|
|
|
|
function generateFeed($unsortedContent) {
|
|
$loader = new \Twig\Loader\FilesystemLoader('templates/');
|
|
$twig = new \Twig\Environment($loader, [
|
|
'autoescape' => false,
|
|
'strict_variables' => true
|
|
]);
|
|
|
|
$fakeNow = time();
|
|
$realNow = time();
|
|
|
|
$items = [];
|
|
|
|
$content = $unsortedContent;
|
|
// Sort by date descending (newest first)
|
|
usort($content, fn($a, $b) => $a->serial < $b->serial ? 1 : -1);
|
|
|
|
foreach ($content as $page) {
|
|
$content = $twig->render('item-content.html', [
|
|
'url' => $page->href,
|
|
'imageUrl' => $page->imageUrl
|
|
]);
|
|
|
|
$item = $twig->render('item.xml', [
|
|
'title' => htmlspecialchars($page->title),
|
|
'pageNo' => $page->pageNo,
|
|
'fullTitle' => htmlspecialchars($page->pageNo . " // " . $page->title),
|
|
'date' => $page->pubDate ? date('c', $page->pubDate) : date('c', $fakeNow),
|
|
'url' => $page->href,
|
|
'imageUrl' => htmlspecialchars($page->imageUrl),
|
|
'content' => htmlspecialchars($content),
|
|
'serial' => $page->serial
|
|
]);
|
|
|
|
$items[] = $item;
|
|
|
|
// we use this to artificially enforce ordering on undated
|
|
// historical items (but note that it starts with the real
|
|
// time, so that when we see a newly published item it's
|
|
// accurately dated)
|
|
$fakeNow -= Config::feed['itemDelaySeconds'];
|
|
};
|
|
|
|
$feed = $twig->render('feed.xml', [
|
|
'title' => Config::feed['title'],
|
|
'description' => Config::feed['description'],
|
|
'baseUrl' => Config::baseUrl,
|
|
'feedUrl' => Config::feed['feedUrl'],
|
|
'date' => $realNow,
|
|
'items' => implode("\n", $items)
|
|
]);
|
|
|
|
return $feed;
|
|
};
|