diff --git a/app/Http/Controllers/Sitemap.php b/app/Http/Controllers/Sitemap.php index 21a1b603..a72e248e 100644 --- a/app/Http/Controllers/Sitemap.php +++ b/app/Http/Controllers/Sitemap.php @@ -24,7 +24,7 @@ class Sitemap extends Controller /** * Get sitemap for all content pages - * + * * @return mixed */ public function getMap() @@ -33,10 +33,9 @@ class Sitemap extends Controller $pages = $this->content->getPageQuery() ->get(); - // iterate over all pages and add tag for each of them foreach ($pages as $page) { - + // is this page a homepage? because of URL if ($page->is_homepage) { $route = route('page.index'); @@ -46,12 +45,54 @@ class Sitemap extends Controller $priority = 0.8; } - // add to all pages - \Sitemap::addTag($route, $page->updated_at, 'daily', $priority); + // compute update frequency + $updateFrequency = $this->getUpdateFrequency($page->updated_at); + // add to all pages + \Sitemap::addTag($route, $page->updated_at, $updateFrequency, $priority); } // print sitemap return \Sitemap::render(); } + + /** + * Based on updatedAt compute "changefreq" for sitemap + * + * @param $updatedAt + */ + private function getUpdateFrequency($updatedAt) + { + // shortcuts for times + $day = 24 * 60 * 60; + $week = 7 * $day; + $month = 4 * $week; + $year = 12 * $month; + + // change frequencies in format "name" => maximum age of last editation + $changeFrequencies = [ + 'always' => 300, + 'hourly' => 3600, + 'daily' => $day, + 'weekly' => $week, + 'monthly' => $month, + 'yearly' => $year, + 'never' => PHP_INT_MAX, + ]; + + // get time difference + $timeDiffInMinutes = time() - strtotime($updatedAt); + + // iterate over array with all change frequencies and break when time of edit is smaller than maximum time + do { + $changeFrequency = key($changeFrequencies); + $changeFreqMinTime = next($changeFrequencies); + } while ($timeDiffInMinutes > $changeFreqMinTime); + + if ($changeFrequency != 'never') { + $changeFrequency = key($changeFrequencies); + } + + return $changeFrequency; + } }