Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: passing invalid ID to Mage_Catalog_Model_Url::refreshRewrites() #4259

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions app/code/core/Mage/Catalog/Model/Resource/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public function getLastUsedRewriteRequestIncrement($prefix, $suffix, $storeId)
$select = $adapter->select()
->from($this->getMainTable(), new Zend_Db_Expr('MAX(' . $urlIncrementPartExpression . ')'))
->where('store_id = :store_id')
// phpcs:ignore Ecg.Sql.SlowQuery.SlowRawSql
->where('request_path LIKE :request_path')
->where($adapter->prepareSqlCondition('request_path', [
'regexp' => '^' . preg_quote($prefix) . '[0-9]*' . preg_quote($suffix) . '$'
Expand Down Expand Up @@ -264,6 +265,7 @@ public function prepareRewrites($storeId, $categoryIds = null, $productIds = nul
$select->where('product_id IN(?)', $productIds);
}

// phpcs:ignore Ecg.Performance.FetchAll.Found
$rowSet = $adapter->fetchAll($select, $bind);

foreach ($rowSet as $row) {
Expand Down Expand Up @@ -467,6 +469,7 @@ protected function _getCategoryAttribute($attributeCode, $categoryIds, $storeId)
$bind['store_id'] = $storeId;
}

// phpcs:ignore Ecg.Performance.FetchAll.Found
$rowSet = $adapter->fetchAll($select, $bind);

$attributes = [];
Expand Down Expand Up @@ -608,6 +611,7 @@ public function _getProductAttribute($attributeCode, $productIds, $storeId)
$bind['store_id'] = $storeId;
}

// phpcs:ignore Ecg.Performance.FetchAll.Found
$rowSet = $adapter->fetchAll($select, $bind);

$attributes = [];
Expand Down Expand Up @@ -672,7 +676,7 @@ protected function _prepareStoreRootCategories($stores)
* Retrieve categories objects
* Either $categoryIds or $path (with ending slash) must be specified
*
* @param int|array $categoryIds
* @param int|array|null $categoryIds
* @param int $storeId
* @param string $path
* @return array
Expand Down Expand Up @@ -706,6 +710,7 @@ protected function _getCategories($categoryIds, $storeId = null, $path = null)
}

$select
// phpcs:ignore Ecg.Sql.SlowQuery.SlowRawSql
->where('main_table.path LIKE ?', $path . '%')
->order('main_table.path');
}
Expand All @@ -730,6 +735,7 @@ protected function _getCategories($categoryIds, $storeId = null, $path = null)
'store_id' => (int)$storeId
];

// phpcs:ignore Ecg.Performance.FetchAll.Found
$rowSet = $adapter->fetchAll($select, $bind);
foreach ($rowSet as $row) {
if ($storeId !== null) {
Expand Down Expand Up @@ -856,9 +862,11 @@ public function getRootChildrenIds($categoryId, $categoryPath, $includeStart = t
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from([$this->getTable('catalog/category')], ['entity_id'])
// phpcs:ignore Ecg.Sql.SlowQuery.SlowRawSql
->where('path LIKE ?', $categoryPath . '/%');

$categoryIds = [];
// phpcs:ignore Ecg.Performance.FetchAll.Found
$rowSet = $adapter->fetchAll($select);
foreach ($rowSet as $row) {
$categoryIds[$row['entity_id']] = $row['entity_id'];
Expand Down Expand Up @@ -918,7 +926,7 @@ public function getProductIdsByCategory($category)
/**
* Retrieve Product data objects
*
* @param int|array $productIds
* @param int|array|null $productIds
* @param int $storeId
* @param int $entityId
* @param int $lastEntityId
Expand Down Expand Up @@ -953,6 +961,7 @@ protected function _getProducts($productIds, $storeId, $entityId, &$lastEntityId
$select->where('e.entity_id IN(?)', $productIds);
}

// phpcs:ignore Ecg.Performance.FetchAll.Found
$rowSet = $adapter->fetchAll($select, $bind);
foreach ($rowSet as $row) {
$product = new Varien_Object($row);
Expand All @@ -972,6 +981,7 @@ protected function _getProducts($productIds, $storeId, $entityId, &$lastEntityId
['product_id', 'category_id']
)
->where('product_id IN(?)', array_keys($products));
// phpcs:ignore Ecg.Performance.FetchAll.Found
$categories = $adapter->fetchAll($select);
foreach ($categories as $category) {
$productId = $category['product_id'];
Expand Down Expand Up @@ -1103,13 +1113,17 @@ public function clearStoreCategoriesInvalidRewrites($storeId)
{
// Form a list of all current store categories ids
$store = $this->getStores($storeId);
if (!$store instanceof Mage_Core_Model_Store) {
return $this;
}

$rootCategoryId = $store->getRootCategoryId();
if (!$rootCategoryId) {
return $this;
}
$categoryIds = $this->getRootChildrenIds($rootCategoryId, $store->getRootCategoryPath());

// Remove all store catalog rewrites that are for some category or cartegory/product not within store categories
// Remove all store catalog rewrites that are for some category or category/product not within store categories
$where = [
'store_id = ?' => $storeId,
'category_id IS NOT NULL', // For sure check that it's a catalog rewrite
Expand All @@ -1136,6 +1150,10 @@ public function clearStoreCategoriesInvalidRewrites($storeId)
public function clearStoreProductsInvalidRewrites($storeId, $productId = null)
{
$store = $this->getStores($storeId);
if (!$store instanceof Mage_Core_Model_Store) {
return $this;
}

$adapter = $this->_getReadAdapter();
$bind = [
'website_id' => (int)$store->getWebsiteId(),
Expand Down Expand Up @@ -1269,6 +1287,7 @@ public function getRewriteByProductStore(array $products)
$select->orWhere($cond);
}

// phpcs:ignore Ecg.Performance.FetchAll.Found
$rowSet = $adapter->fetchAll($select, $bind);
foreach ($rowSet as $row) {
$result[$row['product_id']] = [
Expand Down
14 changes: 11 additions & 3 deletions app/code/core/Mage/Catalog/Model/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ public function refreshRewrites($storeId = null)
}

$this->clearStoreInvalidRewrites($storeId);
$this->refreshCategoryRewrite($this->getStores($storeId)->getRootCategoryId(), $storeId, false);
$store = $this->getStores($storeId);
if ($store instanceof Mage_Core_Model_Store) {
$this->refreshCategoryRewrite($store->getRootCategoryId(), $storeId, false);
}
$this->refreshProductRewrites($storeId);
$this->getResource()->clearCategoryProduct($storeId);

Expand Down Expand Up @@ -504,9 +507,14 @@ public function refreshProductRewrite($productId, $storeId = null)
*/
public function refreshProductRewrites($storeId)
{
$store = $this->getStores($storeId);
if (!$store instanceof Mage_Core_Model_Store) {
return $this;
}

$this->_categories = [];
$storeRootCategoryId = $this->getStores($storeId)->getRootCategoryId();
$storeRootCategoryPath = $this->getStores($storeId)->getRootCategoryPath();
$storeRootCategoryId = $store->getRootCategoryId();
$storeRootCategoryPath = $store->getRootCategoryPath();
$this->_categories[$storeRootCategoryId] = $this->getResource()->getCategory($storeRootCategoryId, $storeId);

$lastEntityId = 0;
Expand Down