FEATURE #1550: compute changefreq for entries in sitemap
This commit is contained in:
@@ -24,7 +24,7 @@ class Sitemap extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get sitemap for all content pages
|
* Get sitemap for all content pages
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getMap()
|
public function getMap()
|
||||||
@@ -33,10 +33,9 @@ class Sitemap extends Controller
|
|||||||
$pages = $this->content->getPageQuery()
|
$pages = $this->content->getPageQuery()
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
|
|
||||||
// iterate over all pages and add tag for each of them
|
// iterate over all pages and add tag for each of them
|
||||||
foreach ($pages as $page) {
|
foreach ($pages as $page) {
|
||||||
|
|
||||||
// is this page a homepage? because of URL
|
// is this page a homepage? because of URL
|
||||||
if ($page->is_homepage) {
|
if ($page->is_homepage) {
|
||||||
$route = route('page.index');
|
$route = route('page.index');
|
||||||
@@ -46,12 +45,54 @@ class Sitemap extends Controller
|
|||||||
$priority = 0.8;
|
$priority = 0.8;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add to all pages
|
// compute update frequency
|
||||||
\Sitemap::addTag($route, $page->updated_at, 'daily', $priority);
|
$updateFrequency = $this->getUpdateFrequency($page->updated_at);
|
||||||
|
|
||||||
|
// add to all pages
|
||||||
|
\Sitemap::addTag($route, $page->updated_at, $updateFrequency, $priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
// print sitemap
|
// print sitemap
|
||||||
return \Sitemap::render();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user