Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit 2339cf1

Browse files
committed
Updated sitemap script to use entity updated_dates
1 parent 2b6ce7b commit 2339cf1

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

php-generate-sitemap/generate-sitemap.php

+13-12
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,40 @@
3131
$baseUrl = rtrim($baseUrl, '/');
3232

3333
// Additional endpoints not fetched via API entities
34+
$nowDate = date_format(new DateTime(), 'Y-m-d');
3435
$additionalEndpoints = [
35-
'/',
36-
'/books',
37-
'/search',
38-
'/login',
36+
['endpoint' => '/', 'updated' => $nowDate],
37+
['endpoint' => '/books', 'updated' => $nowDate],
38+
['endpoint' => '/search', 'updated' => $nowDate],
39+
['endpoint' => '/login', 'updated' => $nowDate],
3940
];
4041

4142
// Get all shelf URLs
4243
$shelves = getAllOfAtListEndpoint("api/shelves", []);
4344
$shelfEndpoints = array_map(function ($shelf) {
44-
return '/shelves/' . $shelf['slug'];
45+
return ['endpoint' => '/shelves/' . $shelf['slug'], 'updated' => $shelf['updated_at']];
4546
}, $shelves);
4647

4748
// Get all book URLs and map for chapters & pages
4849
$books = getAllOfAtListEndpoint("api/books", []);
4950
$bookSlugsById = [];
5051
$bookEndpoints = array_map(function ($book) use (&$bookSlugsById) {
5152
$bookSlugsById[$book['id']] = $book['slug'];
52-
return '/books/' . $book['slug'];
53+
return ['endpoint' => '/books/' . $book['slug'], 'updated' => $book['updated_at']];
5354
}, $books);
5455

5556
// Get all chapter URLs and map for pages
5657
$chapters = getAllOfAtListEndpoint("api/chapters", []);
5758
$chapterEndpoints = array_map(function ($chapter) use ($bookSlugsById) {
5859
$bookSlug = $bookSlugsById[$chapter['book_id']];
59-
return '/books/' . $bookSlug . '/chapter/' . $chapter['slug'];
60+
return ['endpoint' => '/books/' . $bookSlug . '/chapter/' . $chapter['slug'], 'updated' => $chapter['updated_at']];
6061
}, $chapters);
6162

6263
// Get all page URLs
6364
$pages = getAllOfAtListEndpoint("api/pages", []);
6465
$pageEndpoints = array_map(function ($page) use ($bookSlugsById) {
6566
$bookSlug = $bookSlugsById[$page['book_id']];
66-
return '/books/' . $bookSlug . '/page/' . $page['slug'];
67+
return ['endpoint' => '/books/' . $bookSlug . '/page/' . $page['slug'], 'updated' => $page['updated_at']];
6768
}, $pages);
6869

6970
// Gather all our endpoints
@@ -85,18 +86,18 @@
8586
function generateSitemapXml(array $endpoints): string
8687
{
8788
global $baseUrl;
88-
$nowDate = date_format(new DateTime(), 'Y-m-d');
8989
$doc = new DOMDocument("1.0", "UTF-8");
9090
$urlset = $doc->createElement('urlset');
9191
$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
9292

9393
$doc->appendChild($urlset);
94-
foreach ($endpoints as $endpoint) {
94+
foreach ($endpoints as $endpointInfo) {
95+
$date = (new DateTime($endpointInfo['updated']))->format('Y-m-d');
9596
$url = $doc->createElement('url');
9697
$loc = $url->appendChild($doc->createElement('loc'));
97-
$urlText = $doc->createTextNode($baseUrl . $endpoint);
98+
$urlText = $doc->createTextNode($baseUrl . $endpointInfo['endpoint']);
9899
$loc->appendChild($urlText);
99-
$url->appendChild($doc->createElement('lastmod', $nowDate));
100+
$url->appendChild($doc->createElement('lastmod', $date));
100101
$url->appendChild($doc->createElement('changefreq', 'monthly'));
101102
$url->appendChild($doc->createElement('priority', '0.8'));
102103
$urlset->appendChild($url);

0 commit comments

Comments
 (0)