Skip to content

Sitemap.xml: lastmod timestamp can contain invalid dates #9151

Closed
@hostep

Description

@hostep

Preconditions

  1. Magento CE 2.1.5

Steps to reproduce

  1. Have a category in the database where the created_at timestamp is a valid date, and the updated_at timestamp is 0000-00-00 00:00:00
    I don't think you can manage this by manually creating a category in the backend of Magento, but we managed to have some categories like this by importing them using a custom PHP script.
  2. Generate a sitemap.xml file
  3. Look at the generated sitemap.xml file for that category.

Expected result

  1. The lastmod value in the xml should contain the created_at timestamp

Actual result

  1. The lastmod value in the xml looks like this:
<lastmod>-001-11-30T00:00:00+00:00</lastmod>

The negative value is most likely caused by our timezone setting: Europe/Brussels.

Possible solution

diff --git a/app/code/Magento/Sitemap/Model/Sitemap.php b/app/code/Magento/Sitemap/Model/Sitemap.php
index 3b6f646f9a5..434393ae63a 100644
--- a/app/code/Magento/Sitemap/Model/Sitemap.php
+++ b/app/code/Magento/Sitemap/Model/Sitemap.php
@@ -341,9 +341,10 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel
             $changefreq = $sitemapItem->getChangefreq();
             $priority = $sitemapItem->getPriority();
             foreach ($sitemapItem->getCollection() as $item) {
+                $lastmod = $item->getUpdatedAt() === '0000-00-00 00:00:00' ? $item->getCreatedAt() : $item->getUpdatedAt();
                 $xml = $this->_getSitemapRow(
                     $item->getUrl(),
-                    $item->getUpdatedAt(),
+                    $lastmod,
                     $changefreq,
                     $priority,
                     $item->getImages()

Discussion

I'm not sure if this is an actual bug in Magento, due to the fact that we inserted some data in the database in a possibly incorrect way.

Also: what should happen when the created_at timestamp contains '0000-00-00 00:00:00'? In that case the lastmod field shouldn't appear I assume, since it is optional: https://www.sitemaps.org/protocol.html#xmlTagDefinitions

If somebody could let me know what the best solution would be in this case, I can create a pull request for this.
If it is decided that this isn't an actual bug, then we should fix our custom import code I guess.

Please let me know, thanks! :)

Metadata

Metadata

Assignees

Labels

Fixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releasebug report

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions