]> BookStack Code Mirror - api-scripts/commitdiff
Updated sitemap script to use entity updated_dates
authorDan Brown <redacted>
Fri, 5 Mar 2021 21:36:42 +0000 (21:36 +0000)
committerDan Brown <redacted>
Fri, 5 Mar 2021 21:36:42 +0000 (21:36 +0000)
php-generate-sitemap/generate-sitemap.php

index 55cc7402afd22ec9c72d2cdc5cec8407d09d51b3..5105fdfa49aeba9f00ac9a9910b4517c4add77a4 100755 (executable)
@@ -31,17 +31,18 @@ if (!is_dir($outDir)) {
 $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
@@ -49,21 +50,21 @@ $books = getAllOfAtListEndpoint("api/books", []);
 $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
@@ -85,18 +86,18 @@ file_put_contents($outputFile, $xmlSitemap);
 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);