|
31 | 31 | $baseUrl = rtrim($baseUrl, '/'); |
32 | 32 |
|
33 | 33 | // Additional endpoints not fetched via API entities |
| 34 | +$nowDate = date_format(new DateTime(), 'Y-m-d'); |
34 | 35 | $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], |
39 | 40 | ]; |
40 | 41 |
|
41 | 42 | // Get all shelf URLs |
42 | 43 | $shelves = getAllOfAtListEndpoint("api/shelves", []); |
43 | 44 | $shelfEndpoints = array_map(function ($shelf) { |
44 | | - return '/shelves/' . $shelf['slug']; |
| 45 | + return ['endpoint' => '/shelves/' . $shelf['slug'], 'updated' => $shelf['updated_at']]; |
45 | 46 | }, $shelves); |
46 | 47 |
|
47 | 48 | // Get all book URLs and map for chapters & pages |
48 | 49 | $books = getAllOfAtListEndpoint("api/books", []); |
49 | 50 | $bookSlugsById = []; |
50 | 51 | $bookEndpoints = array_map(function ($book) use (&$bookSlugsById) { |
51 | 52 | $bookSlugsById[$book['id']] = $book['slug']; |
52 | | - return '/books/' . $book['slug']; |
| 53 | + return ['endpoint' => '/books/' . $book['slug'], 'updated' => $book['updated_at']]; |
53 | 54 | }, $books); |
54 | 55 |
|
55 | 56 | // Get all chapter URLs and map for pages |
56 | 57 | $chapters = getAllOfAtListEndpoint("api/chapters", []); |
57 | 58 | $chapterEndpoints = array_map(function ($chapter) use ($bookSlugsById) { |
58 | 59 | $bookSlug = $bookSlugsById[$chapter['book_id']]; |
59 | | - return '/books/' . $bookSlug . '/chapter/' . $chapter['slug']; |
| 60 | + return ['endpoint' => '/books/' . $bookSlug . '/chapter/' . $chapter['slug'], 'updated' => $chapter['updated_at']]; |
60 | 61 | }, $chapters); |
61 | 62 |
|
62 | 63 | // Get all page URLs |
63 | 64 | $pages = getAllOfAtListEndpoint("api/pages", []); |
64 | 65 | $pageEndpoints = array_map(function ($page) use ($bookSlugsById) { |
65 | 66 | $bookSlug = $bookSlugsById[$page['book_id']]; |
66 | | - return '/books/' . $bookSlug . '/page/' . $page['slug']; |
| 67 | + return ['endpoint' => '/books/' . $bookSlug . '/page/' . $page['slug'], 'updated' => $page['updated_at']]; |
67 | 68 | }, $pages); |
68 | 69 |
|
69 | 70 | // Gather all our endpoints |
|
85 | 86 | function generateSitemapXml(array $endpoints): string |
86 | 87 | { |
87 | 88 | global $baseUrl; |
88 | | - $nowDate = date_format(new DateTime(), 'Y-m-d'); |
89 | 89 | $doc = new DOMDocument("1.0", "UTF-8"); |
90 | 90 | $urlset = $doc->createElement('urlset'); |
91 | 91 | $urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); |
92 | 92 |
|
93 | 93 | $doc->appendChild($urlset); |
94 | | - foreach ($endpoints as $endpoint) { |
| 94 | + foreach ($endpoints as $endpointInfo) { |
| 95 | + $date = (new DateTime($endpointInfo['updated']))->format('Y-m-d'); |
95 | 96 | $url = $doc->createElement('url'); |
96 | 97 | $loc = $url->appendChild($doc->createElement('loc')); |
97 | | - $urlText = $doc->createTextNode($baseUrl . $endpoint); |
| 98 | + $urlText = $doc->createTextNode($baseUrl . $endpointInfo['endpoint']); |
98 | 99 | $loc->appendChild($urlText); |
99 | | - $url->appendChild($doc->createElement('lastmod', $nowDate)); |
| 100 | + $url->appendChild($doc->createElement('lastmod', $date)); |
100 | 101 | $url->appendChild($doc->createElement('changefreq', 'monthly')); |
101 | 102 | $url->appendChild($doc->createElement('priority', '0.8')); |
102 | 103 | $urlset->appendChild($url); |
|
0 commit comments