Commit b28b57b3 authored by gbyte.co's avatar gbyte.co

Issue #2711761 by Red2015, Q2U: Sitemap sometimes disappearing

parent a43ebe5c
......@@ -111,7 +111,8 @@ class Batch {
public static function finishBatch($success, $results, $operations) {
if ($success) {
if (!empty($results['generate']) || is_null(db_query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField())) {
SitemapGenerator::generateSitemap($results['generate']);
$remove_sitemap = empty($context['results']['chunk_count']);
SitemapGenerator::generateSitemap($results['generate'], $remove_sitemap);
}
self::finishGeneration();
}
......@@ -357,7 +358,7 @@ class Batch {
private static function SetCurrentId($id, &$context) {
$context['sandbox']['progress']++;
$context['sandbox']['current_id'] = $id;
$context['results']['progress'] = !isset($context['results']['progress']) ? 1 : $context['results']['progress'] + 1;
$context['results']['link_count'] = !isset($context['results']['link_count']) ? 1 : $context['results']['link_count'] + 1; //Not used ATM.
}
private static function setProgressInfo(&$context) {
......@@ -378,16 +379,14 @@ class Batch {
}
private static function processSegment(&$context, $batch_info) {
if (!self::isBatch($batch_info) || $context['results']['progress'] == 1) {
SitemapGenerator::removeSitemap();
}
$remove_sitemap = !self::isBatch($batch_info) || empty($context['results']['chunk_count']);
if (self::isBatch($batch_info)) {
if (!empty($batch_info['max_links']) && count($context['results']['generate']) >= $batch_info['max_links']) {
$chunks = array_chunk($context['results']['generate'], $batch_info['max_links']);
foreach ($chunks as $i => $chunk_links) {
if (count($chunk_links) == $batch_info['max_links']) {
SitemapGenerator::generateSitemap($chunk_links);
SitemapGenerator::generateSitemap($chunk_links, $remove_sitemap);
$context['results']['chunk_count'] = !isset($context['results']['chunk_count']) ? 1 : $context['results']['chunk_count'] + 1;
$context['results']['generate'] = array_slice($context['results']['generate'], count($chunk_links));
}
}
......@@ -397,7 +396,8 @@ class Batch {
$links = !empty($context['results']['generate']) ? $context['results']['generate'] : array();
$chunks = !empty($links) ? array_chunk($links, $batch_info['max_links']) : array($links);
foreach ($chunks as $i => $chunk_links) {
SitemapGenerator::generateSitemap($chunk_links);
SitemapGenerator::generateSitemap($chunk_links, $remove_sitemap);
$context['results']['chunk_count'] = !isset($context['results']['chunk_count']) ? 1 : $context['results']['chunk_count'] + 1;
}
self::finishGeneration();
}
......
......@@ -97,22 +97,19 @@ class SitemapGenerator {
* @param array $links
* All links with their multilingual versions and settings.
*/
public static function generateSitemap($links) {
public static function generateSitemap($links, $remove_sitemap = FALSE) {
// Invoke alter hook.
\Drupal::moduleHandler()->alter('simple_sitemap_links', $links);
$values = array(
'id' => db_query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
'id' => $remove_sitemap ? 1 : db_query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
'sitemap_string' => self::generateSitemapChunk($links),
'sitemap_created' => REQUEST_TIME,
);
if ($remove_sitemap)
db_truncate('simple_sitemap')->execute();
db_insert('simple_sitemap')->fields($values)->execute();
}
public static function removeSitemap() {
db_truncate('simple_sitemap')->execute();
}
/**
* Generates and returns the sitemap index for all sitemap chunks.
*
......
Markdown is supported
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