Commit b28b57b3 authored by Pawel G's avatar Pawel G
Browse files

Issue #2711761 by Red2015, Q2U: Sitemap sometimes disappearing

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