$baseUrl = rtrim($baseUrl, '/');
// Additional endpoints not fetched via API entities
+$nowDate = date_format(new DateTime(), 'Y-m-d');
$additionalEndpoints = [
- '/',
- '/books',
- '/search',
- '/login',
+ ['endpoint' => '/', 'updated' => $nowDate],
+ ['endpoint' => '/books', 'updated' => $nowDate],
+ ['endpoint' => '/search', 'updated' => $nowDate],
+ ['endpoint' => '/login', 'updated' => $nowDate],
];
// Get all shelf URLs
$shelves = getAllOfAtListEndpoint("api/shelves", []);
$shelfEndpoints = array_map(function ($shelf) {
- return '/shelves/' . $shelf['slug'];
+ return ['endpoint' => '/shelves/' . $shelf['slug'], 'updated' => $shelf['updated_at']];
}, $shelves);
// Get all book URLs and map for chapters & pages
$bookSlugsById = [];
$bookEndpoints = array_map(function ($book) use (&$bookSlugsById) {
$bookSlugsById[$book['id']] = $book['slug'];
- return '/books/' . $book['slug'];
+ return ['endpoint' => '/books/' . $book['slug'], 'updated' => $book['updated_at']];
}, $books);
// Get all chapter URLs and map for pages
$chapters = getAllOfAtListEndpoint("api/chapters", []);
$chapterEndpoints = array_map(function ($chapter) use ($bookSlugsById) {
$bookSlug = $bookSlugsById[$chapter['book_id']];
- return '/books/' . $bookSlug . '/chapter/' . $chapter['slug'];
+ return ['endpoint' => '/books/' . $bookSlug . '/chapter/' . $chapter['slug'], 'updated' => $chapter['updated_at']];
}, $chapters);
// Get all page URLs
$pages = getAllOfAtListEndpoint("api/pages", []);
$pageEndpoints = array_map(function ($page) use ($bookSlugsById) {
$bookSlug = $bookSlugsById[$page['book_id']];
- return '/books/' . $bookSlug . '/page/' . $page['slug'];
+ return ['endpoint' => '/books/' . $bookSlug . '/page/' . $page['slug'], 'updated' => $page['updated_at']];
}, $pages);
// Gather all our endpoints
function generateSitemapXml(array $endpoints): string
{
global $baseUrl;
- $nowDate = date_format(new DateTime(), 'Y-m-d');
$doc = new DOMDocument("1.0", "UTF-8");
$urlset = $doc->createElement('urlset');
$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$doc->appendChild($urlset);
- foreach ($endpoints as $endpoint) {
+ foreach ($endpoints as $endpointInfo) {
+ $date = (new DateTime($endpointInfo['updated']))->format('Y-m-d');
$url = $doc->createElement('url');
$loc = $url->appendChild($doc->createElement('loc'));
- $urlText = $doc->createTextNode($baseUrl . $endpoint);
+ $urlText = $doc->createTextNode($baseUrl . $endpointInfo['endpoint']);
$loc->appendChild($urlText);
- $url->appendChild($doc->createElement('lastmod', $nowDate));
+ $url->appendChild($doc->createElement('lastmod', $date));
$url->appendChild($doc->createElement('changefreq', 'monthly'));
$url->appendChild($doc->createElement('priority', '0.8'));
$urlset->appendChild($url);