Skip to content

Commit 19acfd3

Browse files
author
Oleksii Korshenko
authored
MAGETWO-70056: Get sitemap product images from image cache, if available #9082
2 parents 82416d7 + 442e54b commit 19acfd3

File tree

6 files changed

+74
-27
lines changed

6 files changed

+74
-27
lines changed

app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
99
use Magento\Store\Model\Store;
10+
use Magento\Framework\App\ObjectManager;
1011

1112
/**
1213
* Sitemap resource product collection model
1314
*
14-
* @author Magento Core Team <core@magentocommerce.com>
1515
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1616
*/
1717
class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
@@ -71,9 +71,20 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
7171

7272
/**
7373
* @var \Magento\Catalog\Model\Product\Media\Config
74+
* @deprecated unused
7475
*/
7576
protected $_mediaConfig;
7677

78+
/**
79+
* @var \Magento\Catalog\Model\Product
80+
*/
81+
private $productModel;
82+
83+
/**
84+
* @var \Magento\Catalog\Helper\Image
85+
*/
86+
private $catalogImageHelper;
87+
7788
/**
7889
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
7990
* @param \Magento\Sitemap\Helper\Data $sitemapData
@@ -85,6 +96,8 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
8596
* @param \Magento\Catalog\Model\Product\Gallery\ReadHandler $mediaGalleryReadHandler
8697
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
8798
* @param string $connectionName
99+
* @param \Magento\Catalog\Model\Product $productModel
100+
* @param \Magento\Catalog\Helper\Image $catalogImageHelper
88101
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
89102
*/
90103
public function __construct(
@@ -97,7 +110,9 @@ public function __construct(
97110
\Magento\Catalog\Model\ResourceModel\Product\Gallery $mediaGalleryResourceModel,
98111
\Magento\Catalog\Model\Product\Gallery\ReadHandler $mediaGalleryReadHandler,
99112
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
100-
$connectionName = null
113+
$connectionName = null,
114+
\Magento\Catalog\Model\Product $productModel = null,
115+
\Magento\Catalog\Helper\Image $catalogImageHelper = null
101116
) {
102117
$this->_productResource = $productResource;
103118
$this->_storeManager = $storeManager;
@@ -107,6 +122,9 @@ public function __construct(
107122
$this->mediaGalleryReadHandler = $mediaGalleryReadHandler;
108123
$this->_mediaConfig = $mediaConfig;
109124
$this->_sitemapData = $sitemapData;
125+
$this->productModel = $productModel ?: ObjectManager::getInstance()->get(\Magento\Catalog\Model\Product::class);
126+
$this->catalogImageHelper = $catalogImageHelper ?: ObjectManager::getInstance()
127+
->get(\Magento\Catalog\Helper\Image::class);
110128
parent::__construct($context, $connectionName);
111129
}
112130

@@ -339,7 +357,7 @@ protected function _loadProductImages($product, $storeId)
339357
) {
340358
$imagesCollection = [
341359
new \Magento\Framework\DataObject(
342-
['url' => $this->_getMediaConfig()->getBaseMediaUrlAddition() . $product->getImage()]
360+
['url' => $this->getProductImageUrl($product->getImage())]
343361
),
344362
];
345363
}
@@ -348,7 +366,7 @@ protected function _loadProductImages($product, $storeId)
348366
// Determine thumbnail path
349367
$thumbnail = $product->getThumbnail();
350368
if ($thumbnail && $product->getThumbnail() != self::NOT_SELECTED_IMAGE) {
351-
$thumbnail = $this->_getMediaConfig()->getBaseMediaUrlAddition() . $thumbnail;
369+
$thumbnail = $this->getProductImageUrl($thumbnail);
352370
} else {
353371
$thumbnail = $imagesCollection[0]->getUrl();
354372
}
@@ -378,11 +396,10 @@ protected function _getAllProductImages($product, $storeId)
378396

379397
$imagesCollection = [];
380398
if ($gallery) {
381-
$productMediaPath = $this->_getMediaConfig()->getBaseMediaUrlAddition();
382399
foreach ($gallery as $image) {
383400
$imagesCollection[] = new \Magento\Framework\DataObject(
384401
[
385-
'url' => $productMediaPath . $image['file'],
402+
'url' => $this->getProductImageUrl($image['file']),
386403
'caption' => $image['label'] ? $image['label'] : $image['label_default'],
387404
]
388405
);
@@ -396,9 +413,28 @@ protected function _getAllProductImages($product, $storeId)
396413
* Get media config
397414
*
398415
* @return \Magento\Catalog\Model\Product\Media\Config
416+
* @deprecated No longer used, as we're getting full image URL from getProductImageUrl method
417+
* @see getProductImageUrl()
399418
*/
400419
protected function _getMediaConfig()
401420
{
402421
return $this->_mediaConfig;
403422
}
423+
424+
/**
425+
* Get product image URL from image filename and path
426+
*
427+
* @param string $image
428+
* @return string
429+
*/
430+
private function getProductImageUrl($image)
431+
{
432+
$productObject = $this->productModel;
433+
$imgUrl = $this->catalogImageHelper
434+
->init($productObject, 'product_page_image_large')
435+
->setImageFile($image)
436+
->getUrl();
437+
438+
return $imgUrl;
439+
}
404440
}

app/code/Magento/Sitemap/Model/Sitemap.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ protected function _isSplitRequired($row)
448448
* @param null|string $lastmod
449449
* @param null|string $changefreq
450450
* @param null|string $priority
451-
* @param null|array $images
451+
* @param null|array|\Magento\Framework\DataObject $images
452452
* @return string
453453
* Sitemap images
454454
* @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=178636
@@ -473,7 +473,7 @@ protected function _getSitemapRow($url, $lastmod = null, $changefreq = null, $pr
473473
// Add Images to sitemap
474474
foreach ($images->getCollection() as $image) {
475475
$row .= '<image:image>';
476-
$row .= '<image:loc>' . htmlspecialchars($this->_getMediaUrl($image->getUrl())) . '</image:loc>';
476+
$row .= '<image:loc>' . htmlspecialchars($image->getUrl()) . '</image:loc>';
477477
$row .= '<image:title>' . htmlspecialchars($images->getTitle()) . '</image:title>';
478478
if ($image->getCaption()) {
479479
$row .= '<image:caption>' . htmlspecialchars($image->getCaption()) . '</image:caption>';
@@ -483,9 +483,7 @@ protected function _getSitemapRow($url, $lastmod = null, $changefreq = null, $pr
483483
// Add PageMap image for Google web search
484484
$row .= '<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0"><DataObject type="thumbnail">';
485485
$row .= '<Attribute name="name" value="' . htmlspecialchars($images->getTitle()) . '"/>';
486-
$row .= '<Attribute name="src" value="' . htmlspecialchars(
487-
$this->_getMediaUrl($images->getThumbnail())
488-
) . '"/>';
486+
$row .= '<Attribute name="src" value="' . htmlspecialchars($images->getThumbnail()) . '"/>';
489487
$row .= '</DataObject></PageMap>';
490488
}
491489

@@ -591,6 +589,7 @@ protected function _getBaseDir()
591589
*/
592590
protected function _getStoreBaseUrl($type = \Magento\Framework\UrlInterface::URL_TYPE_LINK)
593591
{
592+
/** @var \Magento\Store\Model\Store $store */
594593
$store = $this->_storeManager->getStore($this->getStoreId());
595594
$isSecure = $store->isUrlSecure();
596595
return rtrim($store->getBaseUrl($type, $isSecure), '/') . '/';
@@ -613,6 +612,8 @@ protected function _getUrl($url, $type = \Magento\Framework\UrlInterface::URL_TY
613612
*
614613
* @param string $url
615614
* @return string
615+
* @deprecated No longer used, as we're generating product image URLs inside collection instead
616+
* @see \Magento\Sitemap\Model\ResourceModel\Catalog\Product::_loadProductImages()
616617
*/
617618
protected function _getMediaUrl($url)
618619
{

app/code/Magento/Sitemap/Test/Unit/Model/SitemapTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ protected function _getModelMock($mockBeforeSave = false)
535535
]
536536
)
537537
);
538+
539+
$storeBaseMediaUrl = 'http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/';
538540
$this->_sitemapProductMock->expects(
539541
$this->any()
540542
)->method(
@@ -553,13 +555,16 @@ protected function _getModelMock($mockBeforeSave = false)
553555
[
554556
'collection' => [
555557
new \Magento\Framework\DataObject(
556-
['url' => 'image1.png', 'caption' => 'caption & > title < "']
558+
[
559+
'url' => $storeBaseMediaUrl.'i/m/image1.png',
560+
'caption' => 'caption & > title < "'
561+
]
557562
),
558563
new \Magento\Framework\DataObject(
559-
['url' => 'image_no_caption.png', 'caption' => null]
564+
['url' => $storeBaseMediaUrl.'i/m/image_no_caption.png', 'caption' => null]
560565
),
561566
],
562-
'thumbnail' => 'thumbnail.jpg',
567+
'thumbnail' => $storeBaseMediaUrl.'t/h/thumbnail.jpg',
563568
'title' => 'Product & > title < "',
564569
]
565570
),

app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-1-4.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
<changefreq>monthly</changefreq>
1515
<priority>0.5</priority>
1616
<image:image>
17-
<image:loc>http://store.com/image1.png</image:loc>
17+
<image:loc>http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image1.png</image:loc>
1818
<image:title>Product &amp; &gt; title &lt; &quot;</image:title>
1919
<image:caption>caption &amp; &gt; title &lt; &quot;</image:caption>
2020
</image:image>
2121
<image:image>
22-
<image:loc>http://store.com/image_no_caption.png</image:loc>
22+
<image:loc>http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image_no_caption.png</image:loc>
2323
<image:title>Product &amp; &gt; title &lt; &quot;</image:title>
2424
</image:image>
2525
<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0">
2626
<DataObject type="thumbnail">
2727
<Attribute name="name" value="Product &amp; &gt; title &lt; &quot;"/>
28-
<Attribute name="src" value="http://store.com/thumbnail.jpg"/>
28+
<Attribute name="src" value="http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/t/h/thumbnail.jpg"/>
2929
</DataObject>
3030
</PageMap>
3131
</url>

app/code/Magento/Sitemap/Test/Unit/Model/_files/sitemap-single.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@
3232
<changefreq>monthly</changefreq>
3333
<priority>0.5</priority>
3434
<image:image>
35-
<image:loc>http://store.com/image1.png</image:loc>
35+
<image:loc>http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image1.png</image:loc>
3636
<image:title>Product &amp; &gt; title &lt; &quot;</image:title>
3737
<image:caption>caption &amp; &gt; title &lt; &quot;</image:caption>
3838
</image:image>
3939
<image:image>
40-
<image:loc>http://store.com/image_no_caption.png</image:loc>
40+
<image:loc>http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/i/m/image_no_caption.png</image:loc>
4141
<image:title>Product &amp; &gt; title &lt; &quot;</image:title>
4242
</image:image>
4343
<PageMap xmlns="http://www.google.com/schemas/sitemap-pagemap/1.0">
4444
<DataObject type="thumbnail">
4545
<Attribute name="name" value="Product &amp; &gt; title &lt; &quot;"/>
46-
<Attribute name="src" value="http://store.com/thumbnail.jpg"/>
46+
<Attribute name="src" value="http://store.com/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff/t/h/thumbnail.jpg"/>
4747
</DataObject>
4848
</PageMap>
4949
</url>

dev/tests/integration/testsuite/Magento/Sitemap/Model/ResourceModel/Catalog/ProductTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
*/
1515
class ProductTest extends \PHPUnit_Framework_TestCase
1616
{
17+
/**
18+
* Base product image path
19+
*/
20+
const BASE_IMAGE_PATH = 'http://localhost/pub/media/catalog/product/cache/c9e0b0ef589f3508e5ba515cde53c5ff';
21+
1722
/**
1823
* Test getCollection None images
1924
* 1) Check that image attributes were not loaded
@@ -72,20 +77,20 @@ public function testGetCollectionAll()
7277
$this->assertNotEmpty($products[4]->getImages(), 'Images were not loaded');
7378
$this->assertEquals('Simple Images', $products[4]->getImages()->getTitle(), 'Incorrect title');
7479
$this->assertEquals(
75-
'catalog/product/m/a/magento_image_sitemap.png',
80+
self::BASE_IMAGE_PATH.'/m/a/magento_image_sitemap.png',
7681
$products[4]->getImages()->getThumbnail(),
7782
'Incorrect thumbnail'
7883
);
7984
$this->assertCount(2, $products[4]->getImages()->getCollection(), 'Not all images were loaded');
8085

8186
$imagesCollection = $products[4]->getImages()->getCollection();
8287
$this->assertEquals(
83-
'catalog/product/m/a/magento_image_sitemap.png',
88+
self::BASE_IMAGE_PATH.'/m/a/magento_image_sitemap.png',
8489
$imagesCollection[0]->getUrl(),
8590
'Incorrect image url'
8691
);
8792
$this->assertEquals(
88-
'catalog/product/s/e/second_image.png',
93+
self::BASE_IMAGE_PATH.'/s/e/second_image.png',
8994
$imagesCollection[1]->getUrl(),
9095
'Incorrect image url'
9196
);
@@ -97,12 +102,12 @@ public function testGetCollectionAll()
97102
$imagesCollection = $products[5]->getImages()->getCollection();
98103
$this->assertCount(1, $imagesCollection);
99104
$this->assertEquals(
100-
'catalog/product/s/e/second_image_1.png',
105+
self::BASE_IMAGE_PATH.'/s/e/second_image_1.png',
101106
$imagesCollection[0]->getUrl(),
102107
'Image url is incorrect'
103108
);
104109
$this->assertEquals(
105-
'catalog/product/s/e/second_image_1.png',
110+
self::BASE_IMAGE_PATH.'/s/e/second_image_1.png',
106111
$products[5]->getImages()->getThumbnail(),
107112
'Product thumbnail is incorrect'
108113
);
@@ -140,15 +145,15 @@ public function testGetCollectionBase()
140145
$this->assertNotEmpty($products[4]->getImages(), 'Images were not loaded');
141146
$this->assertEquals('Simple Images', $products[4]->getImages()->getTitle(), 'Incorrect title');
142147
$this->assertEquals(
143-
'catalog/product/s/e/second_image.png',
148+
self::BASE_IMAGE_PATH.'/s/e/second_image.png',
144149
$products[4]->getImages()->getThumbnail(),
145150
'Incorrect thumbnail'
146151
);
147152
$this->assertCount(1, $products[4]->getImages()->getCollection(), 'Number of loaded images is incorrect');
148153

149154
$imagesCollection = $products[4]->getImages()->getCollection();
150155
$this->assertEquals(
151-
'catalog/product/s/e/second_image.png',
156+
self::BASE_IMAGE_PATH.'/s/e/second_image.png',
152157
$imagesCollection[0]->getUrl(),
153158
'Incorrect image url'
154159
);

0 commit comments

Comments
 (0)