]> BookStack Code Mirror - api-scripts/blobdiff - php-generate-sitemap/generate-sitemap.php
Update README.md
[api-scripts] / php-generate-sitemap / generate-sitemap.php
index 55cc7402afd22ec9c72d2cdc5cec8407d09d51b3..6dd2617eb0a3a8ce5ac5d109728323bf5c186713 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,29 +50,31 @@ $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
-$allEndpoints = $additionalEndpoints
-    + $pageEndpoints
-    + $chapterEndpoints
-    + $bookEndpoints
-    + $shelfEndpoints;
+$allEndpoints = array_merge(
+    $additionalEndpoints,
+    $pageEndpoints,
+    $chapterEndpoints,
+    $bookEndpoints,
+    $shelfEndpoints
+);
 
 // Fetch our sitemap XML
 $xmlSitemap = generateSitemapXml($allEndpoints);
@@ -85,18 +88,19 @@ 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");
+    $doc->formatOutput = true;
     $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);