core_url_rewrite
table on every reindex. As the result, the core_url_rewrite
table grows to huge sizes.This patch will fix it.
diff --git app/code/core/Mage/Catalog/Model/Url.php app/code/core/Mage/Catalog/Model/Url.php index fa55fc5..a755b46 100644 --- app/code/core/Mage/Catalog/Model/Url.php +++ app/code/core/Mage/Catalog/Model/Url.php @@ -609,6 +609,23 @@ class Mage_Catalog_Model_Url */ public function getUnusedPath($storeId, $requestPath, $idPath) { + $urlKey = ''; + return $this->getUnusedPathByUrlkey($storeId, $requestPath, $idPath, $urlKey); + } + + /** + * Get requestPath that was not used yet. + * + * Will try to get unique path by adding -1 -2 etc. between url_key and optional url_suffix + * + * @param int $storeId + * @param string $requestPath + * @param string $idPath + * @param string $urlKey + * @return string + */ + public function getUnusedPathByUrlkey($storeId, $requestPath, $idPath, $urlKey = '') + { if (strpos($idPath, 'product') !== false) { $suffix = $this->getProductUrlSuffix($storeId); } else { @@ -645,21 +662,22 @@ class Mage_Catalog_Model_Url } // match request_url abcdef1234(-12)(.html) pattern $match = array(); - $regularExpression = '#^([0-9a-z/-]+?)(-([0-9]+))?('.preg_quote($suffix).')?$#i'; + $regularExpression = '#(?P<prefix>(.*/)?' . preg_quote($urlKey) . ')(-(?P<increment>[0-9]+))?(?P<suffix>' + . preg_quote($suffix) . ')?$#i'; if (!preg_match($regularExpression, $requestPath, $match)) { - return $this->getUnusedPath($storeId, '-', $idPath); + return $this->getUnusedPathByUrlkey($storeId, '-', $idPath, $urlKey); } - $match[1] = $match[1] . '-'; - $match[4] = isset($match[4]) ? $match[4] : ''; + $match['prefix'] = $match['prefix'] . '-'; + $match['suffix'] = isset($match['suffix']) ? $match['suffix'] : ''; $lastRequestPath = $this->getResource() - ->getLastUsedRewriteRequestIncrement($match[1], $match[4], $storeId); + ->getLastUsedRewriteRequestIncrement($match['prefix'], $match['suffix'], $storeId); if ($lastRequestPath) { - $match[3] = $lastRequestPath; + $match['increment'] = $lastRequestPath; } - return $match[1] - . (isset($match[3]) ? ($match[3]+1) : '1') - . $match[4]; + return $match['prefix'] + . (isset($match['increment']) ? ($match['increment']+1) : '1') + . $match['suffix']; } else { return $requestPath; @@ -699,7 +717,7 @@ class Mage_Catalog_Model_Url { $storeId = $category->getStoreId(); $idPath = $this->generatePath('id', null, $category); - $suffix = $this->getCategoryUrlSuffix($storeId); + $categoryUrlSuffix = $this->getCategoryUrlSuffix($storeId); if (isset($this->_rewrites[$idPath])) { $this->_rewrite = $this->_rewrites[$idPath]; @@ -713,27 +731,27 @@ class Mage_Catalog_Model_Url $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey()); } - $categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId()); if (null === $parentPath) { $parentPath = $this->getResource()->getCategoryParentPath($category); } elseif ($parentPath == '/') { $parentPath = ''; } - $parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath, - true, $category->getStoreId()); + $parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath, true, $storeId); - $requestPath = $parentPath . $urlKey . $categoryUrlSuffix; - if (isset($existingRequestPath) && $existingRequestPath == $requestPath . $suffix) { + $requestPath = $parentPath . $urlKey; + $regexp = '/^' . preg_quote($requestPath, '/') . '(\-[0-9]+)?' . preg_quote($categoryUrlSuffix, '/') . '$/i'; + if (isset($existingRequestPath) && preg_match($regexp, $existingRequestPath)) { return $existingRequestPath; } - if ($this->_deleteOldTargetPath($requestPath, $idPath, $storeId)) { + $fullPath = $requestPath . $categoryUrlSuffix; + if ($this->_deleteOldTargetPath($fullPath, $idPath, $storeId)) { return $requestPath; } - return $this->getUnusedPath($category->getStoreId(), $requestPath, - $this->generatePath('id', null, $category) + return $this->getUnusedPathByUrlkey($storeId, $fullPath, + $this->generatePath('id', null, $category), $urlKey ); } @@ -798,7 +816,8 @@ class Mage_Catalog_Model_Url $this->_rewrite = $this->_rewrites[$idPath]; $existingRequestPath = $this->_rewrites[$idPath]->getRequestPath(); - if ($existingRequestPath == $requestPath . $suffix) { + $regexp = '/^' . preg_quote($requestPath, '/') . '(\-[0-9]+)?' . preg_quote($suffix, '/') . '$/i'; + if (preg_match($regexp, $existingRequestPath)) { return $existingRequestPath; } @@ -836,7 +855,7 @@ class Mage_Catalog_Model_Url /** * Use unique path generator */ - return $this->getUnusedPath($storeId, $requestPath.$suffix, $idPath); + return $this->getUnusedPathByUrlkey($storeId, $requestPath.$suffix, $idPath, $urlKey); } /** @@ -891,8 +910,8 @@ class Mage_Catalog_Model_Url $parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath, true, $category->getStoreId()); - return $this->getUnusedPath($category->getStoreId(), $parentPath . $urlKey . $categoryUrlSuffix, - $this->generatePath('id', null, $category) + return $this->getUnusedPathByUrlkey($category->getStoreId(), $parentPath . $urlKey . $categoryUrlSuffix, + $this->generatePath('id', null, $category), $urlKey ); } @@ -913,14 +932,14 @@ class Mage_Catalog_Model_Url $this->_addCategoryUrlPath($category); $categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(), false, $category->getStoreId()); - return $this->getUnusedPath($category->getStoreId(), $categoryUrl . '/' . $urlKey . $productUrlSuffix, - $this->generatePath('id', $product, $category) + return $this->getUnusedPathByUrlkey($category->getStoreId(), $categoryUrl . '/' . $urlKey . $productUrlSuffix, + $this->generatePath('id', $product, $category), $urlKey ); } // for product only - return $this->getUnusedPath($category->getStoreId(), $urlKey . $productUrlSuffix, - $this->generatePath('id', $product) + return $this->getUnusedPathByUrlkey($category->getStoreId(), $urlKey . $productUrlSuffix, + $this->generatePath('id', $product), $urlKey ); }