Commit 00740557 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Remove daily sitemap before writing if it already exists



If for some reason the sitemap generator is called twice in a day, it
fails the second time while trying to overwrite the previous version.
This can be fixed by removing all sitemaps for the current day before
attempting to regenerate them.

Change-Id: I48c3ff5c99948e4c5ab93007f068f0319dfac2f8
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 3ed83078
...@@ -81,18 +81,24 @@ class Sitemap { ...@@ -81,18 +81,24 @@ class Sitemap {
// on the first of the month, or if forced, generate the full sitemap // on the first of the month, or if forced, generate the full sitemap
if (date("d") == 1 || $forcefull === true) { if (date("d") == 1 || $forcefull === true) {
$this->date_to_check = null; $this->date_to_check = null;
$remove = 'sitemap_*.xml';
}
else { // otherwise limit to 'yesterday'
$this->date_to_check = date("Y-m-d", strtotime('yesterday'));
$remove = 'sitemap_' . date('Ymd') . '_*.xml';
}
// remove all content from the sitemaps directory so that // remove any sitemaps we're about to replace
// only newly created sitemaps are added to the index if (!$oldsitemaps = glob($this->directory . $remove)) {
$sitemaps = glob(get_config('dataroot') . '/sitemaps/sitemap_*.xml*'); $oldsitemaps = array();
foreach ($sitemaps as $sitemap) { }
if (!unlink($sitemap)) { if ($oldcompressed = glob($this->directory . $remove . '.gz')) {
log_warn(sprintf("Failed to remove sitemap: %s, please check directory and file permissions.", basename($sitemap))); $oldsitemaps = array_merge($oldsitemaps, $oldcompressed);
} }
foreach ($oldsitemaps as $sitemap) {
if (!unlink($sitemap)) {
log_warn(sprintf("Failed to remove sitemap: %s, please check directory and file permissions.", basename($sitemap)));
} }
} // otherwise limit to 'yesterday'
else {
$this->date_to_check = date("Y-m-d", strtotime('yesterday'));
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment