From: Dan Brown Date: Fri, 5 Mar 2021 21:36:42 +0000 (+0000) Subject: Updated sitemap script to use entity updated_dates X-Git-Url: http://source.bookstackapp.com/api-scripts/commitdiff_plain/2339cf1bc02732951d9c862b54b281547a25ef18 Updated sitemap script to use entity updated_dates --- diff --git a/php-generate-sitemap/generate-sitemap.php b/php-generate-sitemap/generate-sitemap.php index 55cc740..5105fdf 100755 --- a/php-generate-sitemap/generate-sitemap.php +++ b/php-generate-sitemap/generate-sitemap.php @@ -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);