Skip to content

Commit

Permalink
Merge pull request #4173 from magento-performance/MC-4244-PR
Browse files Browse the repository at this point in the history
[performance] MC-4244: Skip URL rewrites multiplication
  • Loading branch information
duhon authored May 15, 2019
2 parents faf4db0 + f9329d8 commit 211dd25
Show file tree
Hide file tree
Showing 41 changed files with 1,624 additions and 215 deletions.
50 changes: 30 additions & 20 deletions app/code/Magento/Catalog/Model/ResourceModel/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
use Magento\Eav\Model\Entity\Attribute\UniqueValidationInterface;
use Magento\Framework\EntityManager\EntityManager;
use Magento\Framework\Model\AbstractModel;

/**
* Product entity resource model
Expand Down Expand Up @@ -44,7 +46,7 @@ class Product extends AbstractResource
/**
* Category collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
* @var Category\CollectionFactory
*/
protected $_categoryCollectionFactory;

Expand All @@ -64,7 +66,7 @@ class Product extends AbstractResource
protected $typeFactory;

/**
* @var \Magento\Framework\EntityManager\EntityManager
* @var EntityManager
* @since 101.0.0
*/
protected $entityManager;
Expand All @@ -81,7 +83,7 @@ class Product extends AbstractResource
protected $availableCategoryIdsCache = [];

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CategoryLink
* @var Product\CategoryLink
*/
private $productCategoryLink;

Expand Down Expand Up @@ -110,7 +112,7 @@ public function __construct(
\Magento\Eav\Model\Entity\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\Factory $modelFactory,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
Category\CollectionFactory $categoryCollectionFactory,
Category $catalogCategory,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Eav\Model\Entity\Attribute\SetFactory $setFactory,
Expand Down Expand Up @@ -236,7 +238,7 @@ public function getWebsiteIdsByProductIds($productIds)
/**
* Retrieve product category identifiers
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Product $product
* @return array
*/
public function getCategoryIds($product)
Expand All @@ -248,7 +250,7 @@ public function getCategoryIds($product)
/**
* Get product identifier by sku
*
* @param string $sku
* @param string $sku
* @return int|false
*/
public function getIdBySku($sku)
Expand Down Expand Up @@ -348,11 +350,11 @@ protected function _saveCategories(\Magento\Framework\DataObject $object)
* Get collection of product categories
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
* @return Category\Collection
*/
public function getCategoryCollection($product)
{
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
/** @var Category\Collection $collection */
$collection = $this->_categoryCollectionFactory->create();
$collection->joinField(
'product_id',
Expand Down Expand Up @@ -428,18 +430,26 @@ public function getDefaultAttributeSourceModel()
/**
* Check availability display product in category
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Product|int $product
* @param int $categoryId
* @return string
*/
public function canBeShowInCategory($product, $categoryId)
{
if ($product instanceof \Magento\Catalog\Model\Product) {
$productId = $product->getEntityId();
$storeId = $product->getStoreId();
} else {
$productId = $product;
$storeId = $this->_storeManager->getStore()->getId();
}

$select = $this->getConnection()->select()->from(
$this->tableMaintainer->getMainTable($product->getStoreId()),
$this->tableMaintainer->getMainTable($storeId),
'product_id'
)->where(
'product_id = ?',
(int)$product->getEntityId()
(int)$productId
)->where(
'category_id = ?',
(int)$categoryId
Expand Down Expand Up @@ -614,7 +624,7 @@ public function validate($object)
/**
* Reset firstly loaded attributes
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param AbstractModel $object
* @param integer $entityId
* @param array|null $attributes
* @return $this
Expand Down Expand Up @@ -667,12 +677,12 @@ protected function evaluateDelete($object, $id, $connection)
/**
* Save entity's attributes into the object's resource
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param AbstractModel $object
* @return $this
* @throws \Exception
* @since 101.0.0
*/
public function save(\Magento\Framework\Model\AbstractModel $object)
public function save(AbstractModel $object)
{
$this->getEntityManager()->save($object);
return $this;
Expand All @@ -681,13 +691,13 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
/**
* Retrieve entity manager object
*
* @return \Magento\Framework\EntityManager\EntityManager
* @return EntityManager
*/
private function getEntityManager()
{
if (null === $this->entityManager) {
$this->entityManager = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\EntityManager::class);
$this->entityManager = ObjectManager::getInstance()
->get(EntityManager::class);
}
return $this->entityManager;
}
Expand All @@ -707,13 +717,13 @@ private function getProductWebsiteLink()
* Retrieve CategoryLink object
*
* @deprecated 101.1.0
* @return \Magento\Catalog\Model\ResourceModel\Product\CategoryLink
* @return Product\CategoryLink
*/
private function getProductCategoryLink()
{
if (null === $this->productCategoryLink) {
$this->productCategoryLink = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\ResourceModel\Product\CategoryLink::class);
$this->productCategoryLink = ObjectManager::getInstance()
->get(Product\CategoryLink::class);
}
return $this->productCategoryLink;
}
Expand Down
70 changes: 32 additions & 38 deletions app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler;
use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\Storage\DbStorage;
use Magento\Customer\Api\GroupManagementInterface;
use Magento\Customer\Model\Indexer\CustomerGroupDimensionProvider;
use Magento\Framework\App\ObjectManager;
Expand Down Expand Up @@ -296,6 +297,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
*/
private $emptyItem;

/**
* @var DbStorage
*/
private $urlFinder;

/**
* Collection constructor
*
Expand Down Expand Up @@ -388,6 +394,19 @@ public function __construct(
?: ObjectManager::getInstance()->get(DimensionFactory::class);
}

/**
* Retrieve urlFinder
*
* @return GalleryReadHandler
*/
private function getUrlFinder()
{
if ($this->urlFinder === null) {
$this->urlFinder = ObjectManager::getInstance()->get(DbStorage::class);
}
return $this->urlFinder;
}

/**
* Get cloned Select after dispatching 'catalog_prepare_price_select' event
*
Expand Down Expand Up @@ -1417,44 +1436,21 @@ protected function _addUrlRewrite()
foreach ($this->getItems() as $item) {
$productIds[] = $item->getEntityId();
}
if (!$productIds) {
return;
}

$select = $this->getConnection()
->select()
->from(['u' => $this->getTable('url_rewrite')], ['u.entity_id', 'u.request_path'])
->where('u.store_id = ?', $this->_storeManager->getStore($this->getStoreId())->getId())
->where('u.is_autogenerated = 1')
->where('u.entity_type = ?', ProductUrlRewriteGenerator::ENTITY_TYPE)
->where('u.entity_id IN(?)', $productIds);

$filter = [
'entity_type' => 'product',
'entity_id' => $productIds,
'store_id' => $this->getStoreId(),
'is_autogenerated' => 1
];
if ($this->_urlRewriteCategory) {
$select->joinInner(
['cu' => $this->getTable('catalog_url_rewrite_product_category')],
'u.url_rewrite_id=cu.url_rewrite_id'
)->where('cu.category_id IN (?)', $this->_urlRewriteCategory);
} else {
$select->joinLeft(
['cu' => $this->getTable('catalog_url_rewrite_product_category')],
'u.url_rewrite_id=cu.url_rewrite_id'
)->where('cu.url_rewrite_id IS NULL');
}

// more priority is data with category id
$urlRewrites = [];

foreach ($this->getConnection()->fetchAll($select) as $row) {
if (!isset($urlRewrites[$row['entity_id']])) {
$urlRewrites[$row['entity_id']] = $row['request_path'];
}
$filter['metadata']['category_id'] = $this->_urlRewriteCategory;
}

foreach ($this->getItems() as $item) {
if (isset($urlRewrites[$item->getEntityId()])) {
$item->setData('request_path', $urlRewrites[$item->getEntityId()]);
} else {
$item->setData('request_path', false);
$rewrites = $this->getUrlFinder()->findAllByData($filter);
foreach ($rewrites as $rewrite) {
if ($item = $this->getItemById($rewrite->getEntityId())) {
$item->setData('request_path', $rewrite->getRequestPath());
}
}
}
Expand Down Expand Up @@ -1976,8 +1972,7 @@ protected function _productLimitationPrice($joinLeft = false)
}
// Set additional field filters
foreach ($this->_priceDataFieldFilters as $filterData) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$select->where(call_user_func_array('sprintf', $filterData));
$select->where(sprintf(...$filterData));
}
} else {
$fromPart['price_index']['joinCondition'] = $joinCond;
Expand Down Expand Up @@ -2282,8 +2277,7 @@ private function getBackend()
public function addPriceDataFieldFilter($comparisonFormat, $fields)
{
if (!preg_match('/^%s( (<|>|=|<=|>=|<>) %s)*$/', $comparisonFormat)) {
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception('Invalid comparison format.');
throw new \InvalidArgumentException('Invalid comparison format.');
}

if (!is_array($fields)) {
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/Catalog/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@
</group>
<group id="seo" translate="label" type="text" sortOrder="500" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Search Engine Optimization</label>
<field id="title_separator" translate="label" type="text" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<field id="title_separator" translate="label" type="text" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Page Title Separator</label>
</field>
<field id="category_canonical_tag" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<field id="category_canonical_tag" translate="label" type="select" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Use Canonical Link Meta Tag For Categories</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="product_canonical_tag" translate="label" type="select" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<field id="product_canonical_tag" translate="label" type="select" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Use Canonical Link Meta Tag For Products</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product;

/**
* Storage Plugin
*/
class Storage
{
/**
Expand All @@ -36,6 +39,8 @@ public function __construct(
}

/**
* Save product/category urlRewrite association
*
* @param \Magento\UrlRewrite\Model\StorageInterface $object
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $result
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urls
Expand All @@ -53,13 +58,15 @@ public function afterReplace(StorageInterface $object, array $result, array $url
'product_id' => $record->getEntityId(),
];
}
if ($toSave) {
if (count($toSave) > 0) {
$this->productResource->saveMultiple($toSave);
}
return $result;
}

/**
* Remove product/category urlRewrite association
*
* @param \Magento\UrlRewrite\Model\StorageInterface $object
* @param array $data
* @return void
Expand All @@ -71,6 +78,8 @@ public function beforeDeleteByData(StorageInterface $object, array $data)
}

/**
* Filter urls
*
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urls
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
*/
Expand All @@ -96,6 +105,8 @@ protected function filterUrls(array $urls)
}

/**
* Check if url is correct
*
* @param UrlRewrite $url
* @return bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;

/**
* Generate url rewrites for anchor categories
*/
class AnchorUrlRewriteGenerator
{
/**
Expand Down Expand Up @@ -52,7 +57,7 @@ public function __construct(
* @param int $storeId
* @param Product $product
* @param ObjectRegistry $productCategories
* @return UrlRewrite[]
* @return UrlRewrite[]|array
*/
public function generate($storeId, Product $product, ObjectRegistry $productCategories)
{
Expand Down
Loading

0 comments on commit 211dd25

Please sign in to comment.