Skip to content

Commit 89c7670

Browse files
authored
Use transliterator_transliterate to generate "url_key" (#4315)
* added symfony/string * added default slugger config * added AsciiSlugger to Mage_Catalog_Model_Url * added locale getter/setter to Model_Product & Model_Category * sync lMage_Catalog_Model_Product_Url & Mage_Catalog_Model_Category_Url - cosmetic change - added method Mage_Catalog_Model_Category_Url * Mage_Catalog_Model_Product_Url & Mage_Catalog_Model_Category_Url extend Mage_Catalog_Model_Url - moved duplicate code tp parent * added tests * updated tests * refactor * phpstan * phpstan * rector * updated tests * use getConvertTable() as before * refactor * phpstan typo * cleanup * cs * updated tests * ignore convert table, but load config xml changed strings * added method for tests * added method for tests * fix * rector
1 parent 66a09e2 commit 89c7670

File tree

16 files changed

+554
-172
lines changed

16 files changed

+554
-172
lines changed

app/code/core/Mage/Catalog/Helper/Product/Url.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class Mage_Catalog_Helper_Product_Url extends Mage_Core_Helper_Url
8585
'צ' => 'c', 'ק' => 'q', 'ר' => 'r', 'ש' => 'w', 'ת' => 't', '' => 'tm',
8686
];
8787

88+
protected array $_convertTableShort = ['@' => 'at', '©' => 'c', '®' => 'r', '' => 'tm'];
89+
90+
protected array $_convertTableCustom = [];
91+
8892
/**
8993
* Check additional instruction for conversion table in configuration
9094
*/
@@ -93,7 +97,9 @@ public function __construct()
9397
$convertNode = Mage::getConfig()->getNode('default/url/convert');
9498
if ($convertNode) {
9599
foreach ($convertNode->children() as $node) {
96-
$this->_convertTable[(string) $node->from] = (string) $node->to;
100+
if (property_exists($node, 'from') && property_exists($node, 'to')) {
101+
$this->_convertTableCustom[(string) $node->from] = (string) $node->to;
102+
}
97103
}
98104
}
99105
}
@@ -105,14 +111,26 @@ public function __construct()
105111
*/
106112
public function getConvertTable()
107113
{
108-
return $this->_convertTable;
114+
return $this->_convertTable + $this->_convertTableShort + $this->_convertTableCustom;
115+
}
116+
117+
public function getConvertTableCustom(): array
118+
{
119+
return $this->_convertTableCustom;
120+
}
121+
122+
public function getConvertTableShort(): array
123+
{
124+
return $this->_convertTableShort + $this->_convertTableCustom;
109125
}
110126

111127
/**
112128
* Process string based on conversion table
113129
*
114130
* @param string $string
115131
* @return string
132+
* @deprecated
133+
* @see Mage_Catalog_Model_Url::formatUrlKey()
116134
*/
117135
public function format($string)
118136
{

app/code/core/Mage/Catalog/Model/Attribute/Backend/Urlkey/Abstract.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function beforeSave($object)
4040
$urlKey = $object->getName();
4141
}
4242

43+
if (method_exists($object, 'setLocale')) {
44+
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $object->getStoreId());
45+
$object->setLocale($locale);
46+
}
47+
4348
$object->setData($attributeName, $object->formatUrlKey($urlKey));
4449

4550
return $this;

app/code/core/Mage/Catalog/Model/Category.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ class Mage_Catalog_Model_Category extends Mage_Catalog_Model_Abstract
163163
*/
164164
protected $_urlModel;
165165

166+
167+
protected ?string $locale = null;
168+
166169
/**
167170
* Initialize resource mode
168171
*/
@@ -501,9 +504,10 @@ public function getUrlModel()
501504
public function getCategoryIdUrl()
502505
{
503506
Varien_Profiler::start('REGULAR: ' . __METHOD__);
504-
$urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->formatUrlKey($this->getName());
507+
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $this->getStoreId());
508+
$urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->setLocale($locale)->formatUrlKey($this->getName());
505509
$url = $this->getUrlInstance()->getUrl('catalog/category/view', [
506-
's' => $urlKey,
510+
's' => $urlKey,
507511
'id' => $this->getId(),
508512
]);
509513
Varien_Profiler::stop('REGULAR: ' . __METHOD__);
@@ -518,11 +522,18 @@ public function getCategoryIdUrl()
518522
*/
519523
public function formatUrlKey($str)
520524
{
521-
$str = Mage::helper('catalog/product_url')->format($str);
522-
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', $str);
523-
$urlKey = strtolower($urlKey);
524-
$urlKey = trim($urlKey, '-');
525-
return $urlKey;
525+
return $this->getUrlModel()->setLocale($this->getLocale())->formatUrlKey($str);
526+
}
527+
528+
public function getLocale(): ?string
529+
{
530+
return $this->locale;
531+
}
532+
533+
public function setLocale(?string $locale)
534+
{
535+
$this->locale = $locale;
536+
return $this;
526537
}
527538

528539
/**

app/code/core/Mage/Catalog/Model/Category/Url.php

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,13 @@
1515
*/
1616

1717
/**
18-
* Catalog category url
18+
* Catalog Url model
1919
*
2020
* @category Mage
2121
* @package Mage_Catalog
2222
*/
23-
class Mage_Catalog_Model_Category_Url
23+
class Mage_Catalog_Model_Category_Url extends Mage_Catalog_Model_Url
2424
{
25-
/**
26-
* Url instance
27-
*
28-
* @var Mage_Core_Model_Url
29-
*/
30-
protected $_url;
31-
32-
/**
33-
* Factory instance
34-
*
35-
* @var Mage_Catalog_Model_Factory
36-
*/
37-
protected $_factory;
38-
39-
/**
40-
* Url rewrite instance
41-
*
42-
* @var Mage_Core_Model_Url_Rewrite
43-
*/
44-
protected $_urlRewrite;
45-
4625
/**
4726
* Initialize Url model
4827
*/
@@ -113,32 +92,4 @@ protected function _getRequestPath(Mage_Catalog_Model_Category $category)
11392
}
11493
return false;
11594
}
116-
117-
/**
118-
* Retrieve Url instance
119-
*
120-
* @return Mage_Core_Model_Url
121-
*/
122-
public function getUrlInstance()
123-
{
124-
if ($this->_url === null) {
125-
/** @var Mage_Core_Model_Url $model */
126-
$model = $this->_factory->getModel('core/url');
127-
$this->_url = $model;
128-
}
129-
return $this->_url;
130-
}
131-
132-
/**
133-
* Retrieve Url rewrite instance
134-
*
135-
* @return Mage_Core_Model_Url_Rewrite
136-
*/
137-
public function getUrlRewrite()
138-
{
139-
if ($this->_urlRewrite === null) {
140-
$this->_urlRewrite = $this->_factory->getUrlRewriteInstance();
141-
}
142-
return $this->_urlRewrite;
143-
}
14495
}

app/code/core/Mage/Catalog/Model/Product.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ class Mage_Catalog_Model_Product extends Mage_Catalog_Model_Abstract
339339
*/
340340
protected $_reviewSummary = [];
341341

342+
protected ?string $locale = null;
343+
342344
/**
343345
* Initialize resources
344346
*/
@@ -1681,7 +1683,18 @@ public function getUrlInStore($params = [])
16811683
*/
16821684
public function formatUrlKey($str)
16831685
{
1684-
return $this->getUrlModel()->formatUrlKey($str);
1686+
return $this->getUrlModel()->setLocale($this->getLocale())->formatUrlKey($str);
1687+
}
1688+
1689+
public function getLocale(): ?string
1690+
{
1691+
return $this->locale;
1692+
}
1693+
1694+
public function setLocale(?string $locale)
1695+
{
1696+
$this->locale = $locale;
1697+
return $this;
16851698
}
16861699

16871700
/**

app/code/core/Mage/Catalog/Model/Product/Url.php

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,10 @@
2020
* @category Mage
2121
* @package Mage_Catalog
2222
*/
23-
class Mage_Catalog_Model_Product_Url extends Varien_Object
23+
class Mage_Catalog_Model_Product_Url extends Mage_Catalog_Model_Url
2424
{
2525
public const CACHE_TAG = 'url_rewrite';
2626

27-
/**
28-
* URL instance
29-
*
30-
* @var Mage_Core_Model_Url
31-
*/
32-
protected $_url;
33-
34-
/**
35-
* URL Rewrite Instance
36-
*
37-
* @var Mage_Core_Model_Url_Rewrite
38-
*/
39-
protected $_urlRewrite;
40-
41-
/**
42-
* Factory instance
43-
*
44-
* @var Mage_Catalog_Model_Factory
45-
*/
46-
protected $_factory;
47-
4827
/**
4928
* @var Mage_Core_Model_Store
5029
*/
@@ -59,32 +38,6 @@ public function __construct(array $args = [])
5938
$this->_store = !empty($args['store']) ? $args['store'] : Mage::app()->getStore();
6039
}
6140

62-
/**
63-
* Retrieve URL Instance
64-
*
65-
* @return Mage_Core_Model_Url
66-
*/
67-
public function getUrlInstance()
68-
{
69-
if ($this->_url === null) {
70-
$this->_url = Mage::getModel('core/url');
71-
}
72-
return $this->_url;
73-
}
74-
75-
/**
76-
* Retrieve URL Rewrite Instance
77-
*
78-
* @return Mage_Core_Model_Url_Rewrite
79-
*/
80-
public function getUrlRewrite()
81-
{
82-
if ($this->_urlRewrite === null) {
83-
$this->_urlRewrite = $this->_factory->getUrlRewriteInstance();
84-
}
85-
return $this->_urlRewrite;
86-
}
87-
8841
/**
8942
* 'no_selection' shouldn't be a valid image attribute value
9043
*
@@ -132,21 +85,6 @@ public function getProductUrl($product, $useSid = null)
13285
return $this->getUrl($product, $params);
13386
}
13487

135-
/**
136-
* Format Key for URL
137-
*
138-
* @param string $str
139-
* @return string
140-
*/
141-
public function formatUrlKey($str)
142-
{
143-
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str));
144-
$urlKey = strtolower($urlKey);
145-
$urlKey = trim($urlKey, '-');
146-
147-
return $urlKey;
148-
}
149-
15088
/**
15189
* Retrieve Product Url path (with category if exists)
15290
*
@@ -281,7 +219,6 @@ protected function _getRequestPath($product, $categoryId)
281219
if ($rewrite->getId()) {
282220
return $rewrite->getRequestPath();
283221
}
284-
285222
return false;
286223
}
287224
}

app/code/core/Mage/Catalog/Model/Resource/Product/Attribute/Backend/Urlkey.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function beforeSave($object)
3737
$urlKey = $object->getName();
3838
}
3939

40+
if (method_exists($object, 'setLocale')) {
41+
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $object->getStoreId());
42+
$object->setLocale($locale);
43+
}
4044
$object->setData($attributeName, $object->formatUrlKey($urlKey));
4145

4246
return $this;

0 commit comments

Comments
 (0)