Problem/Motivations

Using Drupal 8.3.6 with simple_sitemap v8.x-2.9 and domain_simple_sitemap v8.x-1.0-beta7, when I enable the module I get this error and also while "Regenerating the sitemap" using drush or configuration page:

Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '33' for key 'PRIMARY': INSERT INTO {simple_sitemap} (id, domain_id, sitemap_string, sitemap_created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array ( [:db_insert_placeholder_0] => 33 [:db_insert_placeholder_1] =>

Comments

nghai created an issue. See original summary.

nghai’s picture

Issue summary: View changes
keopx’s picture

Version: 8.x-1.0-beta7 » 8.x-1.0-beta8

Hi @nghai

Can you reproduce this error with the lastest release 8.x-1.0-beta8?

I removed execution when install module #2897372: DomainSimpleSitemapGenerator::generateSitemap() must be of the type array, null given

Thank you.

keopx’s picture

Status: Active » Needs work
nghai’s picture

Issue summary: View changes
keopx’s picture

@nghai are you working on it to solve?

nghai’s picture

Hi @keopx

Firstly, thank you very much for the great module.

1) Regarding the above error, it comes whenever you generate the sitemap. On installation and subsequent sitemap regeneration both.

I believed it came from the below function in the file: DomainSimpleSitemapGenerator.php


public function generateSitemap(array $links, $remove_sitemap = FALSE) {
$new_links_array = [];
foreach ($links as $link) {
$chunk_id = $this->db->query('SELECT id FROM {simple_sitemap} WHERE domain_id = :domain_id', [':domain_id' => $link['domain_id']])->fetchField();
if (!$chunk_id) {
$chunk_id = $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1;
$values = [
'id' => $chunk_id,
'domain_id' => $link['domain_id'],
'sitemap_string' => $this->generateSitemapChunk([]),
'sitemap_created' => REQUEST_TIME,
];

$this->db->insert('simple_sitemap')->fields($values)->execute();
}
$new_links_array[$chunk_id][$link['domain_id']][] = $link;
}
// Invoke alter hook.
$this->moduleHandler->alter('simple_sitemap_links', $links);
if ($remove_sitemap) {
$this->db->truncate('simple_sitemap')->execute();
}
foreach ($new_links_array as $id => $new_links) {
$values = [
'id' => $id,
'domain_id' => key($new_links),
'sitemap_string' => $this->generateSitemapChunk(array_shift($new_links)),
'sitemap_created' => REQUEST_TIME,
];

$this->db->insert('simple_sitemap')->fields($values)->execute();
}
}

I found two issues:
a) If $remove_sitemap is set TRUE then the sitemap table should truncate at the very first not after checking and creating fresh chunk for the domain,
b) - It is firstly checking whether sitemap chunk is already created for the domain if not then a new one with domain_id is inserted => CORRECT
- Then it should update the table entry in the end not insert with the line "$this->db->insert('simple_sitemap')->fields($values)->execute();" => WRONG
- I implemented a update command instead of "$this->db->insert('simple_sitemap')->fields($values)->execute();"

I tried to fix the errors with provided patch. Not sure whether it follows your concept. I would appreciate if you review it once.

2) Regarding 8.x-1.0-beta8 release the other issue error is gone since now sitemap is not generating on installation. Although the truncate action is a better replacement :)
But this duplication error was still there.

Best Regards,
Neha

keopx’s picture

@nghai

--- DomainSimpleSitemapGenerator.php	2017-08-11 13:22:50
+++ DomainSimpleSitemapGenerator-copy.php	2017-08-11 13:56

I can not apply patch.

I will try to apply manually this patch.

nghai’s picture

Aah, I corrected the name but seemed like upload the wrong file.

Please find new patch with corrected name. See if it works.

keopx’s picture

Priority: Normal » Major

@nghai

This patch solve some things but the code rewrite all time existing simple_sitemap.id values.

I do not know best way to create chunk xml files (generateSitemapChunk).

The thing is when you have a lot of links system split into different other files (xml). In that case not works correctly :-/

Note: admin/config/search/simplesitemap -> Maximum links in a sitemap (The maximum number of links one sitemap can hold. If more links are generated than set here, a sitemap index will be created and the links split into several sub-sitemaps.
50 000 links is the maximum Google will parse per sitemap, however it is advisable to set this to a lower number. If left blank, all links will be shown on a single sitemap.)

nghai’s picture

@keopx,

Understood! does that splitting of sitemap was working before with domain access behavior ?

  • keopx committed 11a28c9 on 8.x-1.x authored by nghai
    Issue #2901449 by nghai, keopx: SQLSTATE[23000]: Integrity constraint...
keopx’s picture

Status: Needs work » Fixed

@nghai yes, but I think that is not a problem for the moment. Your patch works fine.

FYI: I corrected some coding standards mistakes.

Thank you :D

nghai’s picture

@keopx,

I will also take a look if can provide a solution.

@coding_standards : I know there are many .. even deprecated functions too. thought like fixing the error first ;)

keopx’s picture

LOL

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.