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] =>
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | domain_simple_sitemap-duplicate-insertion-fix-2901449-1.patch | 1.77 KB | nghai |
| #7 | domain_simple_sitemap-duplicate-insertion-fix-2901449.patch | 1.77 KB | nghai |
Comments
Comment #2
nghai commentedComment #3
keopxHi @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.
Comment #4
keopxComment #5
nghai commentedComment #6
keopx@nghai are you working on it to solve?
Comment #7
nghai commentedHi @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
Comment #8
keopx@nghai
I can not apply patch.
I will try to apply manually this patch.
Comment #9
nghai commentedAah, I corrected the name but seemed like upload the wrong file.
Please find new patch with corrected name. See if it works.
Comment #10
keopx@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.)
Comment #11
nghai commented@keopx,
Understood! does that splitting of sitemap was working before with domain access behavior ?
Comment #13
keopx@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
Comment #14
nghai commented@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 ;)
Comment #15
keopxLOL