Skip to content

Commit

Permalink
Merge pull request #737 from magento-fearless-kiwis/MAGETWO-58924-jus…
Browse files Browse the repository at this point in the history
…t-UrlRewriteMap-develop-final

MAGETWO-58924 - Url rewrite map table lock
  • Loading branch information
Joan He authored Jan 14, 2017
2 parents 18a58db + e0bb933 commit 1856c28
Show file tree
Hide file tree
Showing 47 changed files with 3,284 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
use Magento\Framework\App\ObjectManager;

class ChildrenUrlRewriteGenerator
{
Expand All @@ -16,36 +19,48 @@ class ChildrenUrlRewriteGenerator
/** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory */
protected $categoryUrlRewriteGeneratorFactory;

/** @var \Magento\UrlRewrite\Model\MergeDataProvider */
private $mergeDataProviderPrototype;

/**
* @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
*/
public function __construct(
ChildrenCategoriesProvider $childrenCategoriesProvider,
CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory
CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory,
MergeDataProviderFactory $mergeDataProviderFactory = null
) {
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
$this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory;
if (!isset($mergeDataProviderFactory)) {
$mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class);
}
$this->mergeDataProviderPrototype = $mergeDataProviderFactory->create();
}

/**
* Generate list of children urls
*
* @param int $storeId
* @param \Magento\Catalog\Model\Category $category
* @param int|null $rootCategoryId
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
*/
public function generate($storeId, Category $category)
public function generate($storeId, Category $category, $rootCategoryId = null)
{
$urls = [];
foreach ($this->childrenCategoriesProvider->getChildren($category) as $childCategory) {
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
$childCategory->setStoreId($storeId);
$childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history'));
$urls = array_merge(
$urls,
$this->categoryUrlRewriteGeneratorFactory->create()->generate($childCategory)
/** @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator */
$categoryUrlRewriteGenerator = $this->categoryUrlRewriteGeneratorFactory->create();
$mergeDataProvider->merge(
$categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId)
);
}
return $urls;

return $mergeDataProvider->getData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
*/
namespace Magento\CatalogUrlRewrite\Model\Category;

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\UrlRewrite\Model\OptionProvider;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;

class CurrentUrlRewritesRegenerator
{
Expand All @@ -21,104 +16,133 @@ class CurrentUrlRewritesRegenerator
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory */
protected $urlRewriteFactory;

/** @var UrlFinderInterface */
protected $urlFinder;
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite */
private $urlRewritePrototype;

/** @var \Magento\Catalog\Model\Category */
/**
* @var \Magento\Catalog\Model\Category
* @deprecated
*/
protected $category;

/**
* @var \Magento\UrlRewrite\Model\UrlFinderInterface
* @deprecated
*/
protected $urlFinder;

/** @var \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder */
private $urlRewriteFinder;

/** @var \Magento\UrlRewrite\Model\MergeDataProvider */
private $mergeDataProviderPrototype;

/**
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory
* @param UrlFinderInterface $urlFinder
* @param \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder
* @param \Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder|null $urlRewriteFinder
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
*/
public function __construct(
CategoryUrlPathGenerator $categoryUrlPathGenerator,
UrlRewriteFactory $urlRewriteFactory,
UrlFinderInterface $urlFinder
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory $urlRewriteFactory,
\Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder,
\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder $urlRewriteFinder = null,
\Magento\UrlRewrite\Model\MergeDataProviderFactory $mergeDataProviderFactory = null
) {
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->urlRewriteFactory = $urlRewriteFactory;
$this->urlRewritePrototype = $urlRewriteFactory->create();
$this->urlFinder = $urlFinder;
$this->urlRewriteFinder = $urlRewriteFinder ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\CatalogUrlRewrite\Model\Map\UrlRewriteFinder::class);
if (!isset($mergeDataProviderFactory)) {
$mergeDataProviderFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\UrlRewrite\Model\MergeDataProviderFactory::class
);
}
$this->mergeDataProviderPrototype = $mergeDataProviderFactory->create();
}

/**
* Generate list based on current url rewrites
*
* @param int $storeId
* @param \Magento\Catalog\Model\Category $category
* @param int|null $rootCategoryId
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
*/
public function generate($storeId, Category $category)
public function generate($storeId, \Magento\Catalog\Model\Category $category, $rootCategoryId = null)
{
$this->category = $category;

$currentUrlRewrites = $this->urlFinder->findAllByData(
[
UrlRewrite::STORE_ID => $storeId,
UrlRewrite::ENTITY_ID => $category->getId(),
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
]
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
$currentUrlRewrites = $this->urlRewriteFinder->findAllByData(
$category->getEntityId(),
$storeId,
CategoryUrlRewriteGenerator::ENTITY_TYPE,
$rootCategoryId
);

$urlRewrites = [];
foreach ($currentUrlRewrites as $rewrite) {
if ($rewrite->getIsAutogenerated()) {
$urlRewrites = array_merge($urlRewrites, $this->generateForAutogenerated($rewrite, $storeId));
} else {
$urlRewrites = array_merge($urlRewrites, $this->generateForCustom($rewrite, $storeId));
}
$mergeDataProvider->merge(
$rewrite->getIsAutogenerated()
? $this->generateForAutogenerated($rewrite, $storeId, $category)
: $this->generateForCustom($rewrite, $storeId, $category)
);
}
return $urlRewrites;

return $mergeDataProvider->getData();
}

/**
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url
* @param int $storeId
* @return array
* @param \Magento\Catalog\Model\Category|null $category
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
*/
protected function generateForAutogenerated($url, $storeId)
protected function generateForAutogenerated($url, $storeId, \Magento\Catalog\Model\Category $category = null)
{
$urls = [];
if ($this->category->getData('save_rewrites_history')) {
$targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId);
if ($category->getData('save_rewrites_history')) {
$targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId);
if ($url->getRequestPath() !== $targetPath) {
$urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create()
->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)
->setEntityId($this->category->getId())
$generatedUrl = clone $this->urlRewritePrototype;
$generatedUrl->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)
->setEntityId($category->getEntityId())
->setRequestPath($url->getRequestPath())
->setTargetPath($targetPath)
->setRedirectType(OptionProvider::PERMANENT)
->setStoreId($storeId)
->setIsAutogenerated(0);
return [$generatedUrl];
}
}
return $urls;
return [];
}

/**
* @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url
* @param int $storeId
* @return array
* @param \Magento\Catalog\Model\Category|null $category
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
*/
protected function generateForCustom($url, $storeId)
protected function generateForCustom($url, $storeId, \Magento\Catalog\Model\Category $category = null)
{
$urls = [];
$targetPath = !$url->getRedirectType()
? $url->getTargetPath()
: $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId);
: $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId);
if ($url->getRequestPath() !== $targetPath) {
$urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create()
->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)
->setEntityId($this->category->getId())
$generatedUrl = clone $this->urlRewritePrototype;
$generatedUrl->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)
->setEntityId($category->getEntityId())
->setRequestPath($url->getRequestPath())
->setTargetPath($targetPath)
->setRedirectType($url->getRedirectType())
->setStoreId($storeId)
->setDescription($url->getDescription())
->setIsAutogenerated(0)
->setMetadata($url->getMetadata());
return [$generatedUrl];
}
return $urls;
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
*/
namespace Magento\CatalogUrlRewrite\Model\Category\Plugin;

use Magento\CatalogUrlRewrite\Model\Category\ProductFactory;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\UrlRewrite\Model\StorageInterface;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product;

class Storage
{
/** @var UrlFinderInterface */
protected $urlFinder;
private $urlFinder;

/** @var ProductFactory */
protected $productFactory;
/** @var Product */
private $productResource;

/**
* @param UrlFinderInterface $urlFinder
* @param ProductFactory $productFactory
* @param Product $productResource
*/
public function __construct(
UrlFinderInterface $urlFinder,
ProductFactory $productFactory
Product $productResource
) {
$this->urlFinder = $urlFinder;
$this->productFactory = $productFactory;
$this->productResource = $productResource;
}

/**
Expand All @@ -50,7 +50,7 @@ public function afterReplace(StorageInterface $object, $result, array $urls)
];
}
if ($toSave) {
$this->productFactory->create()->getResource()->saveMultiple($toSave);
$this->productResource->saveMultiple($toSave);
}
}

Expand All @@ -62,14 +62,7 @@ public function afterReplace(StorageInterface $object, $result, array $urls)
*/
public function beforeDeleteByData(StorageInterface $object, array $data)
{
$toRemove = [];
$records = $this->urlFinder->findAllByData($data);
foreach ($records as $record) {
$toRemove[] = $record->getUrlRewriteId();
}
if ($toRemove) {
$this->productFactory->create()->getResource()->removeMultiple($toRemove);
}
$this->productResource->removeMultipleByProductCategory($data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Group
/** @var ProductUrlRewriteGenerator */
protected $productUrlRewriteGenerator;

/** @var StoreManagerInterface */
/** @var StoreManagerInterface */
protected $storeManager;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator;
use Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
use Magento\Store\Model\Store;
use Magento\Catalog\Model\Product\Visibility;

Expand All @@ -28,7 +23,7 @@ class CategoryBasedProductRewriteGenerator
private $productScopeRewriteGenerator;

/**
* @param ProductScopeRewriteGenerator $productUrlRewriteGenerator
* @param ProductScopeRewriteGenerator $productScopeRewriteGenerator
*/
public function __construct(
ProductScopeRewriteGenerator $productScopeRewriteGenerator
Expand All @@ -41,9 +36,10 @@ public function __construct(
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Category $category
* @param int|null $rootCategoryId
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
*/
public function generate(Product $product, Category $category)
public function generate(Product $product, Category $category, $rootCategoryId = null)
{
if ($product->getVisibility() == Visibility::VISIBILITY_NOT_VISIBLE) {
return [];
Expand All @@ -52,10 +48,14 @@ public function generate(Product $product, Category $category)
$storeId = $product->getStoreId();

$urls = $this->productScopeRewriteGenerator->isGlobalScope($storeId)
? $this->productScopeRewriteGenerator->generateForGlobalScope([$category], $product)
: $this->productScopeRewriteGenerator->generateForSpecificStoreView($storeId, [$category], $product);
? $this->productScopeRewriteGenerator->generateForGlobalScope([$category], $product, $rootCategoryId)
: $this->productScopeRewriteGenerator->generateForSpecificStoreView(
$storeId,
[$category],
$product,
$rootCategoryId
);

$this->product = null;
return $urls;
}
}
Loading

1 comment on commit 1856c28

@hostep
Copy link
Contributor

@hostep hostep commented on 1856c28 Jan 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi

Is this going to be included in Magento 2.1.x or is it only scheduled for 2.2.x ?

I have the feeling these changes might fix issues we are experiencing in Magento 2.1.x when having different url_key's on different storeviews. Currently we have problems where Magento is taking the admin values of the url_keys for storeviews instead of the values for the specific storeviews.
I attempted to fix these problems we had in #7667. At a certain point these changes fixed our issues, but later on, we ran into the same problem again, so that PR is certainly not correct.
Maybe the changes in this commit will hopefully fix our issues.

So can you let me know if this is coming to Magento 2.1.x and if it is going to fix issues with url_key's on storeview levels?

Thanks!

Please sign in to comment.