97 private $_crlf = [
"win" =>
"\r\n",
"unix" =>
"\n",
"mac" =>
"\r"];
169 private $itemProvider;
176 private $configReader;
183 private $sitemapItemFactory;
190 private $lastModMinTsVal;
219 \
Magento\Framework\Escaper $escaper,
225 \
Magento\Framework\Stdlib\DateTime\DateTime $modelDate,
230 \
Magento\Framework\Data\Collection\AbstractDb $resourceCollection =
null,
235 \
Magento\
Sitemap\Model\SitemapItemInterfaceFactory $sitemapItemFactory =
null 237 $this->_escaper = $escaper;
238 $this->_sitemapData = $sitemapData;
240 $this->_directory =
$filesystem->getDirectoryWrite($documentRoot->getPath());
241 $this->_categoryFactory = $categoryFactory;
243 $this->_cmsFactory = $cmsFactory;
244 $this->_dateModel = $modelDate;
263 $this->
_init(SitemapResource::class);
274 if ($this->_stream) {
291 $this->_sitemapItems[] = $sitemapItem;
303 public function collectSitemapItems()
313 'collection' => $this->_categoryFactory->create()->getCollection(
$storeId),
321 'collection' => $this->_productFactory->create()->getCollection(
$storeId),
329 'collection' => $this->_cmsFactory->create()->getCollection(
$storeId),
341 $sitemapItems = $this->itemProvider->getItems($this->getStoreId());
342 $mappedItems = $this->mapToSitemapItem();
343 $this->_sitemapItems = array_merge($sitemapItems, $mappedItems);
346 self::TYPE_INDEX => [
347 self::OPEN_TAG_KEY =>
'<?xml version="1.0" encoding="UTF-8"?>' .
349 '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' .
351 self::CLOSE_TAG_KEY =>
'</sitemapindex>',
354 self::OPEN_TAG_KEY =>
'<?xml version="1.0" encoding="UTF-8"?>' .
356 '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' .
357 ' xmlns:content="http://www.google.com/schemas/sitemap-content/1.0"' .
358 ' xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">' .
360 self::CLOSE_TAG_KEY =>
'</urlset>',
373 $path = $this->getSitemapPath();
378 if (
$path && preg_match(
'#\.\.[\\\/]#',
$path)) {
384 if (!$this->_directory->isExist(
$path)) {
387 'Please create the specified folder "%1" before saving the sitemap.',
388 $this->_escaper->escapeHtml($this->getSitemapPath())
393 if (!$this->_directory->isWritable(
$path)) {
395 __(
'Please make sure that "%1" is writable by the web-server.', $this->getSitemapPath())
401 if (!preg_match(
'#^[a-zA-Z0-9_\.]+$#', $this->getSitemapFilename())) {
404 'Please use only letters (a-z or A-Z), numbers (0-9) or underscores (_) in the filename.' 405 .
' No spaces or other characters are allowed.' 409 if (!preg_match(
'#\.xml$#', $this->getSitemapFilename())) {
410 $this->setSitemapFilename($this->getSitemapFilename() .
'.xml');
413 $this->setSitemapPath(rtrim(str_replace(str_replace(
'\\',
'/', $this->
_getBaseDir()),
'',
$path),
'/') .
'/');
415 return parent::beforeSave();
425 public function generateXml()
430 foreach ($this->_sitemapItems as
$item) {
433 $item->getUpdatedAt(),
434 $item->getChangeFrequency(),
435 $item->getPriority(),
443 if (!$this->_fileSize) {
450 $this->_fileSize += strlen($xml);
455 if ($this->_sitemapIncrement == 1) {
458 $this->getSitemapPath(),
461 $this->_sitemapIncrement
463 $destination = rtrim($this->getSitemapPath(),
'/') .
'/' . $this->getSitemapFilename();
465 $this->_directory->renameFile(
$path, $destination);
471 $this->setSitemapTime($this->_dateModel->gmtDate(
'Y-m-d H:i:s'));
484 $this->
_createSitemap($this->getSitemapFilename(), self::TYPE_INDEX);
499 return (
new \DateTime())->format(\
Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
511 if ($this->_lineCount + 1 > $this->configReader->getMaximumLinesNumber(
$storeId)) {
515 if ($this->_fileSize + strlen(
$row) > $this->configReader->getMaximumFileSize(
$storeId)) {
537 protected function _getSitemapRow(
$url, $lastmod =
null, $changefreq =
null, $priority =
null, $images =
null)
540 $row =
'<loc>' . htmlspecialchars(
$url) .
'</loc>';
545 $row .=
'<changefreq>' . $changefreq .
'</changefreq>';
548 $row .= sprintf(
'<priority>%.1f</priority>', $priority);
552 foreach ($images->getCollection() as
$image) {
553 $row .=
'<image:image>';
554 $row .=
'<image:loc>' . htmlspecialchars(
$image->getUrl()) .
'</image:loc>';
555 $row .=
'<image:title>' . htmlspecialchars($images->getTitle()) .
'</image:title>';
556 if (
$image->getCaption()) {
557 $row .=
'<image:caption>' . htmlspecialchars(
$image->getCaption()) .
'</image:caption>';
559 $row .=
'</image:image>';
562 $row .=
'<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0"><DataObject type="thumbnail">';
563 $row .=
'<Attribute name="name" value="' . htmlspecialchars($images->getTitle()) .
'"/>';
564 $row .=
'<Attribute name="src" value="' . htmlspecialchars($images->getThumbnail()) .
'"/>';
565 $row .=
'</DataObject></PageMap>';
568 return '<url>' .
$row .
'</url>';
581 $row =
'<loc>' . htmlspecialchars(
$url) .
'</loc>';
586 return '<sitemap>' .
$row .
'</sitemap>';
600 $this->_sitemapIncrement++;
605 $this->_stream = $this->_directory->openFile(
$path);
607 $fileHeader = sprintf($this->_tags[
$type][self::OPEN_TAG_KEY],
$type);
608 $this->_stream->write($fileHeader);
609 $this->_fileSize = strlen($fileHeader . sprintf($this->_tags[
$type][self::CLOSE_TAG_KEY],
$type));
631 if ($this->_stream) {
632 $this->_stream->write(sprintf($this->_tags[
$type][self::CLOSE_TAG_KEY],
$type));
633 $this->_stream->close();
637 $this->_lineCount = 0;
638 $this->_fileSize = 0;
649 return str_replace(
'.xml',
'', $this->getSitemapFilename()) .
'-' . $this->getStoreId() .
'-' .
$index .
'.xml';
659 return $this->_directory->getAbsolutePath();
671 $store = $this->_storeManager->getStore($this->getStoreId());
672 $isSecure =
$store->isUrlSecure();
673 return rtrim(
$store->getBaseUrl(
$type, $isSecure),
'/') .
'/';
685 return $this->_getStoreBaseUrl(
$type) . ltrim(
$url,
'/');
709 if ($this->lastModMinTsVal ===
null) {
710 $this->lastModMinTsVal = strtotime(self::LAST_MOD_MIN_VAL);
712 $timestamp = max(strtotime($date), $this->lastModMinTsVal);
713 return date(
'c', $timestamp);
723 return realpath($this->_request->getServer(
'DOCUMENT_ROOT'));
733 $storeParsedUrl = parse_url($this->_getStoreBaseUrl());
734 $url = $storeParsedUrl[
'scheme'] .
'://' . $storeParsedUrl[
'host'];
736 $documentRoot = trim(str_replace(
'\\',
'/', $this->
_getDocumentRoot()),
'/');
739 if (strpos(
$baseDir, $documentRoot) === 0) {
741 $installationFolder = trim(str_replace($documentRoot,
'',
$baseDir),
'/');
742 $storeDomain = rtrim(
$url .
'/' . $installationFolder,
'/');
746 $storeDomain = rtrim(
$url,
'/');
761 return $this->
_getStoreBaseDomain() . str_replace(
'//',
'/', $sitemapPath .
'/' . $sitemapFileName);
774 return (
bool)$this->configReader->getEnableSubmissionRobots(
$storeId);
787 $robotsSitemapLine =
'Sitemap: ' . $this->
getSitemapUrl($this->getSitemapPath(), $sitemapFileName);
789 $filename =
'robots.txt';
791 if ($this->_directory->isExist($filename)) {
792 $content = $this->_directory->readFile($filename);
795 if (strpos(
$content, $robotsSitemapLine) ===
false) {
802 $this->_directory->writeFile($filename,
$content);
811 private function _findNewLinesDelimiter(
$text)
813 foreach ($this->_crlf as $delimiter) {
814 if (strpos(
$text, $delimiter) !==
false) {
827 private function mapToSitemapItem()
831 foreach ($this->_sitemapItems as
$data) {
833 $items[] = $this->sitemapItemFactory->create([
834 'url' =>
$item->getUrl(),
835 'updatedAt' =>
$item->getUpdatedAt(),
836 'images' =>
$item->getImages(),
837 'priority' =>
$data->getPriority(),
838 'changeFrequency' =>
$data->getChangeFrequency(),
_getCurrentSitemapFilename($index)
_createSitemap($fileName=null, $type=self::TYPE_URL)
_getUrl($url, $type=UrlInterface::URL_TYPE_LINK)
_getFormattedLastmodDate($date)
_getSitemapIndexRow($sitemapFilename, $lastmod=null)
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Escaper $escaper, \Magento\Sitemap\Helper\Data $sitemapData, \Magento\Framework\Filesystem $filesystem, \Magento\Sitemap\Model\ResourceModel\Catalog\CategoryFactory $categoryFactory, \Magento\Sitemap\Model\ResourceModel\Catalog\ProductFactory $productFactory, \Magento\Sitemap\Model\ResourceModel\Cms\PageFactory $cmsFactory, \Magento\Framework\Stdlib\DateTime\DateTime $modelDate, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\RequestInterface $request, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], \Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot=null, ItemProviderInterface $itemProvider=null, SitemapConfigReaderInterface $configReader=null, \Magento\Sitemap\Model\SitemapItemInterfaceFactory $sitemapItemFactory=null)
addSitemapItem(DataObject $sitemapItem)
_addSitemapToRobotsTxt($sitemapFileName)
getSitemapUrl($sitemapPath, $sitemapFileName)
_finalizeSitemap($type=self::TYPE_URL)
_isEnabledSubmissionRobots()
_getSitemapRow($url, $lastmod=null, $changefreq=null, $priority=null, $images=null)