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

Postpone sitemap deletion to shortly before generation of first chunk

parent f1998727
...@@ -66,8 +66,8 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) { ...@@ -66,8 +66,8 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
); );
// Attach some js magic to forms. // Attach some js magic to forms.
// Only attach fieldset summary js to 'additional settings' vertical tabs.
$form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form'; $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
// Only attach fieldset summary js to 'additional settings' vertical tabs.
if (isset($form['additional_settings'])) { if (isset($form['additional_settings'])) {
$form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries'; $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
} }
......
...@@ -74,17 +74,6 @@ class Batch { ...@@ -74,17 +74,6 @@ class Batch {
$operation[1][] = &$context; $operation[1][] = &$context;
call_user_func_array($operation[0], $operation[1]); call_user_func_array($operation[0], $operation[1]);
} }
$links = !empty($context['results']['generate']) ? $context['results']['generate'] : array();
if (!empty($links)) {
$chunks = array_chunk($links, $this->batchInfo['max_links']);
foreach ($chunks as $i => $chunk_links) {
SitemapGenerator::generateSitemap($chunk_links);
}
}
else {
SitemapGenerator::generateSitemap($links);
}
self::finishGeneration();
break; break;
} }
} }
...@@ -121,7 +110,7 @@ class Batch { ...@@ -121,7 +110,7 @@ class Batch {
*/ */
public static function finishBatch($success, $results, $operations) { public static function finishBatch($success, $results, $operations) {
if ($success) { if ($success) {
if (!empty($results) || 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']); SitemapGenerator::generateSitemap($results['generate']);
} }
self::finishGeneration(); self::finishGeneration();
...@@ -234,8 +223,8 @@ class Batch { ...@@ -234,8 +223,8 @@ class Batch {
} }
if (self::isBatch($batch_info)) { if (self::isBatch($batch_info)) {
self::setProgressInfo($context); self::setProgressInfo($context);
self::processSegment($context, $batch_info);
} }
self::processSegment($context, $batch_info);
} }
/** /**
...@@ -297,8 +286,8 @@ class Batch { ...@@ -297,8 +286,8 @@ class Batch {
} }
if (self::isBatch($batch_info)) { if (self::isBatch($batch_info)) {
self::setProgressInfo($context); self::setProgressInfo($context);
self::processSegment($context, $batch_info);
} }
self::processSegment($context, $batch_info);
} }
private static function pathProcessed($path, &$context) { //todo: test functionality private static function pathProcessed($path, &$context) { //todo: test functionality
...@@ -368,6 +357,7 @@ class Batch { ...@@ -368,6 +357,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;
} }
private static function setProgressInfo(&$context) { private static function setProgressInfo(&$context) {
...@@ -388,6 +378,11 @@ class Batch { ...@@ -388,6 +378,11 @@ 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) {
SitemapGenerator::removeSitemap();
}
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) {
...@@ -398,6 +393,15 @@ class Batch { ...@@ -398,6 +393,15 @@ class Batch {
} }
} }
} }
else {
$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);
}
self::finishGeneration();
}
}
/** /**
* Logs and displays an error. * Logs and displays an error.
......
...@@ -128,6 +128,5 @@ class SimplesitemapSettingsForm extends ConfigFormBase { ...@@ -128,6 +128,5 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
public function generateSitemap(array &$form, FormStateInterface $form_state) { public function generateSitemap(array &$form, FormStateInterface $form_state) {
$sitemap = \Drupal::service('simple_sitemap.generator'); $sitemap = \Drupal::service('simple_sitemap.generator');
$sitemap->generateSitemap(); $sitemap->generateSitemap();
// $sitemap->generateSitemap('nobatch');
} }
} }
...@@ -95,7 +95,6 @@ class Simplesitemap { ...@@ -95,7 +95,6 @@ class Simplesitemap {
* is to be run. * is to be run.
*/ */
public function generateSitemap($from = 'form') { public function generateSitemap($from = 'form') {
db_truncate('simple_sitemap')->execute();
$generator = new SitemapGenerator($from); $generator = new SitemapGenerator($from);
$generator->setCustomLinks($this->getConfig('custom')); $generator->setCustomLinks($this->getConfig('custom'));
$generator->setEntityTypes($this->getConfig('entity_types')); $generator->setEntityTypes($this->getConfig('entity_types'));
......
...@@ -109,6 +109,10 @@ class SitemapGenerator { ...@@ -109,6 +109,10 @@ class SitemapGenerator {
db_insert('simple_sitemap')->fields($values)->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. * 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