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

Use transliterator_transliterate to generate "url_key" #4315

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f626477
added symfony/string
sreichel Oct 27, 2024
5bc7c82
added default slugger config
sreichel Oct 27, 2024
56cb4be
added AsciiSlugger to Mage_Catalog_Model_Url
sreichel Oct 27, 2024
c73695b
added locale getter/setter to Model_Product & Model_Category
sreichel Oct 27, 2024
023525f
sync lMage_Catalog_Model_Product_Url & Mage_Catalog_Model_Category_Url
sreichel Oct 27, 2024
3751066
Mage_Catalog_Model_Product_Url & Mage_Catalog_Model_Category_Url exte…
sreichel Oct 27, 2024
afe32b3
added tests
sreichel Oct 27, 2024
150fe53
updated tests
sreichel Oct 27, 2024
0bad5c8
refactor
sreichel Oct 27, 2024
39bd714
phpstan
sreichel Oct 27, 2024
fab39dd
phpstan
sreichel Oct 27, 2024
97969a6
rector
sreichel Oct 27, 2024
2c71174
updated tests
sreichel Oct 27, 2024
1fe7d66
use getConvertTable() as before
sreichel Oct 27, 2024
bef5201
refactor
sreichel Oct 27, 2024
7a2ae70
phpstan typo
sreichel Oct 27, 2024
45100b8
cleanup
sreichel Oct 27, 2024
4060b06
Merge branch 'main' into slugger
sreichel Oct 29, 2024
8dd27d3
cs
sreichel Oct 29, 2024
004af24
updated tests
sreichel Oct 29, 2024
ad1cd6c
ignore convert table, but load config xml changed strings
sreichel Oct 29, 2024
2a09b03
added method for tests
sreichel Oct 29, 2024
3f06d84
added method for tests
sreichel Oct 29, 2024
7ca6622
Merge remote-tracking branch 'origin/slugger' into slugger
sreichel Oct 29, 2024
2739b70
fix
sreichel Oct 29, 2024
6dd2462
Merge branch 'main' into slugger
sreichel Oct 29, 2024
c01d683
Merge branch 'main' into slugger
sreichel Nov 22, 2024
b45b21c
Merge branch 'main' into slugger
sreichel Nov 22, 2024
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
5 changes: 0 additions & 5 deletions .phpstan.dist.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1760,11 +1760,6 @@ parameters:
count: 1
path: app/code/core/Mage/Catalog/Model/Category/Attribute/Api.php

-
message: "#^Property Mage_Catalog_Model_Category_Url\\:\\:\\$_url \\(Mage_Core_Model_Url\\) does not accept bool\\|Mage_Core_Model_Abstract\\.$#"
count: 1
path: app/code/core/Mage/Catalog/Model/Category/Url.php

-
message: "#^Call to an undefined method Mage_Catalog_Model_Config\\:\\:_init\\(\\)\\.$#"
count: 1
Expand Down
22 changes: 20 additions & 2 deletions app/code/core/Mage/Catalog/Helper/Product/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class Mage_Catalog_Helper_Product_Url extends Mage_Core_Helper_Url
'צ' => 'c', 'ק' => 'q', 'ר' => 'r', 'ש' => 'w', 'ת' => 't', '™' => 'tm',
];

protected array $_convertTableShort = ['@' => 'at', '©' => 'c', '®' => 'r', '™' => 'tm'];

protected array $_convertTableCustom = [];

/**
* Check additional instruction for conversion table in configuration
*/
Expand All @@ -92,7 +96,9 @@ public function __construct()
$convertNode = Mage::getConfig()->getNode('default/url/convert');
if ($convertNode) {
foreach ($convertNode->children() as $node) {
$this->_convertTable[(string) $node->from] = (string) $node->to;
if (property_exists($node, 'from') && property_exists($node, 'to')) {
$this->_convertTableCustom[(string) $node->from] = (string) $node->to;
}
}
}
}
Expand All @@ -104,14 +110,26 @@ public function __construct()
*/
public function getConvertTable()
{
return $this->_convertTable;
return $this->_convertTable + $this->_convertTableShort + $this->_convertTableCustom;
}

public function getConvertTableCustom(): array
{
return $this->_convertTableCustom;
}

public function getConvertTableShort(): array
{
return $this->_convertTableShort + $this->_convertTableCustom;
}

/**
* Process string based on conversion table
*
* @param string $string
* @return string
* @deprecated
* @see Mage_Catalog_Model_Url::formatUrlKey()
*/
public function format($string)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function beforeSave($object)
$urlKey = $object->getName();
}

if (method_exists($object, 'setLocale')) {
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $object->getStoreId());
$object->setLocale($locale);
}

$object->setData($attributeName, $object->formatUrlKey($urlKey));

return $this;
Expand Down
25 changes: 18 additions & 7 deletions app/code/core/Mage/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ class Mage_Catalog_Model_Category extends Mage_Catalog_Model_Abstract
*/
protected $_urlModel;


protected ?string $locale = null;

/**
* Initialize resource mode
*/
Expand Down Expand Up @@ -500,9 +503,10 @@ public function getUrlModel()
public function getCategoryIdUrl()
{
Varien_Profiler::start('REGULAR: ' . __METHOD__);
$urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->formatUrlKey($this->getName());
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $this->getStoreId());
$urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->setLocale($locale)->formatUrlKey($this->getName());
$url = $this->getUrlInstance()->getUrl('catalog/category/view', [
's' => $urlKey,
's' => $urlKey,
'id' => $this->getId(),
]);
Varien_Profiler::stop('REGULAR: ' . __METHOD__);
Expand All @@ -517,11 +521,18 @@ public function getCategoryIdUrl()
*/
public function formatUrlKey($str)
{
$str = Mage::helper('catalog/product_url')->format($str);
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', $str);
$urlKey = strtolower($urlKey);
$urlKey = trim($urlKey, '-');
return $urlKey;
return $this->getUrlModel()->setLocale($this->getLocale())->formatUrlKey($str);
}

public function getLocale(): ?string
{
return $this->locale;
}

public function setLocale(?string $locale)
{
$this->locale = $locale;
return $this;
}

/**
Expand Down
51 changes: 2 additions & 49 deletions app/code/core/Mage/Catalog/Model/Category/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,13 @@
*/

/**
* Catalog category url
* Catalog Url model
*
* @category Mage
* @package Mage_Catalog
*/
class Mage_Catalog_Model_Category_Url
class Mage_Catalog_Model_Category_Url extends Mage_Catalog_Model_Url
{
/**
* Url instance
*
* @var Mage_Core_Model_Url
*/
protected $_url;

/**
* Factory instance
*
* @var Mage_Catalog_Model_Factory
*/
protected $_factory;

/**
* Url rewrite instance
*
* @var Mage_Core_Model_Url_Rewrite
*/
protected $_urlRewrite;

/**
* Initialize Url model
*/
Expand Down Expand Up @@ -112,30 +91,4 @@ protected function _getRequestPath(Mage_Catalog_Model_Category $category)
}
return false;
}

/**
* Retrieve Url instance
*
* @return Mage_Core_Model_Url
*/
public function getUrlInstance()
{
if ($this->_url === null) {
$this->_url = $this->_factory->getModel('core/url');
}
return $this->_url;
}

/**
* Retrieve Url rewrite instance
*
* @return Mage_Core_Model_Url_Rewrite
*/
public function getUrlRewrite()
{
if ($this->_urlRewrite === null) {
$this->_urlRewrite = $this->_factory->getUrlRewriteInstance();
}
return $this->_urlRewrite;
}
}
15 changes: 14 additions & 1 deletion app/code/core/Mage/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ class Mage_Catalog_Model_Product extends Mage_Catalog_Model_Abstract
*/
protected $_reviewSummary = [];

protected ?string $locale = null;

/**
* Initialize resources
*/
Expand Down Expand Up @@ -1680,7 +1682,18 @@ public function getUrlInStore($params = [])
*/
public function formatUrlKey($str)
{
return $this->getUrlModel()->formatUrlKey($str);
return $this->getUrlModel()->setLocale($this->getLocale())->formatUrlKey($str);
}

public function getLocale(): ?string
{
return $this->locale;
}

public function setLocale(?string $locale)
{
$this->locale = $locale;
return $this;
}

/**
Expand Down
65 changes: 1 addition & 64 deletions app/code/core/Mage/Catalog/Model/Product/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,10 @@
* @category Mage
* @package Mage_Catalog
*/
class Mage_Catalog_Model_Product_Url extends Varien_Object
class Mage_Catalog_Model_Product_Url extends Mage_Catalog_Model_Url
{
public const CACHE_TAG = 'url_rewrite';

/**
* URL instance
*
* @var Mage_Core_Model_Url
*/
protected $_url;

/**
* URL Rewrite Instance
*
* @var Mage_Core_Model_Url_Rewrite
*/
protected $_urlRewrite;

/**
* Factory instance
*
* @var Mage_Catalog_Model_Factory
*/
protected $_factory;

/**
* @var Mage_Core_Model_Store
*/
Expand All @@ -58,32 +37,6 @@ public function __construct(array $args = [])
$this->_store = !empty($args['store']) ? $args['store'] : Mage::app()->getStore();
}

/**
* Retrieve URL Instance
*
* @return Mage_Core_Model_Url
*/
public function getUrlInstance()
{
if ($this->_url === null) {
$this->_url = Mage::getModel('core/url');
}
return $this->_url;
}

/**
* Retrieve URL Rewrite Instance
*
* @return Mage_Core_Model_Url_Rewrite
*/
public function getUrlRewrite()
{
if ($this->_urlRewrite === null) {
$this->_urlRewrite = $this->_factory->getUrlRewriteInstance();
}
return $this->_urlRewrite;
}

/**
* 'no_selection' shouldn't be a valid image attribute value
*
Expand Down Expand Up @@ -131,21 +84,6 @@ public function getProductUrl($product, $useSid = null)
return $this->getUrl($product, $params);
}

/**
* Format Key for URL
*
* @param string $str
* @return string
*/
public function formatUrlKey($str)
{
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str));
$urlKey = strtolower($urlKey);
$urlKey = trim($urlKey, '-');

return $urlKey;
}

/**
* Retrieve Product Url path (with category if exists)
*
Expand Down Expand Up @@ -280,7 +218,6 @@ protected function _getRequestPath($product, $categoryId)
if ($rewrite->getId()) {
return $rewrite->getRequestPath();
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function beforeSave($object)
$urlKey = $object->getName();
}

if (method_exists($object, 'setLocale')) {
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $object->getStoreId());
$object->setLocale($locale);
}
$object->setData($attributeName, $object->formatUrlKey($urlKey));

return $this;
Expand Down
Loading