Skip to content

Commit 38bec7a

Browse files
committed
Release 1.4.2
1 parent a1eab54 commit 38bec7a

8 files changed

+150
-64
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [1.4.2] - 2019-04-04
8+
### Added
9+
- new option "--check-use-category-in-product-url"
10+
- info into log about conflicted URL Rewrites
11+
12+
### Changed
13+
- fixed logical issues in url_key regeneration
14+
- fix for category/products rewrites for multistore
15+
- fixed issue of division by zero in progress bar
16+
- updated the url_key regeneration behavior to use UrlPathGenerators
17+
- modified logic of displaying a console messages (notifications, errors, exceptions...)
18+
719
## [1.4.1] - 2019-02-20
820
### Changed
921
- fixed issue of removing previously added URL rewrites of product when same URL key exists;

Console/Command/RegenerateUrlRewrites.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ protected function configure()
101101
null,
102102
InputArgument::OPTIONAL,
103103
'Specific product ID, e.g.: 107 (Pro version only)'
104+
),
105+
new InputOption(
106+
self::INPUT_KEY_CHECK_USE_CATEGORIES_FOR_PRODUCT_URL,
107+
null,
108+
InputOption::VALUE_NONE,
109+
'Check if product use categories in URL'
104110
)
105111
]);
106112
}
@@ -123,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
123129

124130
if (count($this->_errors) > 0) {
125131
foreach ($this->_errors as $error) {
126-
$this->_displayConsoleMsg($error);
132+
$this->_addConsoleMsg($error);
127133
}
128134
return;
129135
}
@@ -175,6 +181,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
175181
$this->_output->writeln('');
176182
$this->_output->writeln('');
177183

184+
$this->_displayConsoleMsg();
185+
178186
$this->_runReindexation();
179187
$this->_runClearCache();
180188

@@ -222,6 +230,12 @@ public function getCommandOptions()
222230
$this->_commandOptions['runCacheFlush'] = false;
223231
}
224232

233+
if (
234+
isset($options[self::INPUT_KEY_CHECK_USE_CATEGORIES_FOR_PRODUCT_URL])
235+
&& $options[self::INPUT_KEY_CHECK_USE_CATEGORIES_FOR_PRODUCT_URL] === true) {
236+
$this->_commandOptions['checkUseCategoryInProductUrl'] = true;
237+
}
238+
225239
if (isset($options[self::INPUT_KEY_PRODUCTS_RANGE])) {
226240
$this->_commandOptions['productsFilter'] = $this->_generateIdsRangeArray(
227241
$options[self::INPUT_KEY_PRODUCTS_RANGE],

Console/Command/RegenerateUrlRewritesAbstract.php

+70-23
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,26 @@
3333
use Magento\UrlRewrite\Model\UrlPersistInterface as UrlPersist;
3434
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
3535
use Magento\Store\Model\StoreManagerInterface;
36+
use Magento\Framework\App\Config\ScopeConfigInterface;
3637

3738
abstract class RegenerateUrlRewritesAbstract extends Command
3839
{
39-
const INPUT_KEY_STOREID = 'store-id';
40-
const INPUT_KEY_REGENERATE_ENTITY_TYPE = 'entity-type';
41-
const INPUT_KEY_SAVE_REWRITES_HISTORY = 'save-old-urls';
42-
const INPUT_KEY_NO_REINDEX = 'no-reindex';
43-
const INPUT_KEY_NO_PROGRESS = 'no-progress';
44-
const INPUT_KEY_NO_CACHE_FLUSH = 'no-cache-flush';
45-
const INPUT_KEY_NO_CACHE_CLEAN = 'no-cache-clean';
46-
const INPUT_KEY_CATEGORIES_RANGE = 'categories-range';
47-
const INPUT_KEY_PRODUCTS_RANGE = 'products-range';
48-
const INPUT_KEY_CATEGORY_ID = 'category-id';
49-
const INPUT_KEY_PRODUCT_ID = 'product-id';
50-
51-
const CONSOLE_LOG_MAX_DOTS_IN_LINE = 70;
52-
const INPUT_KEY_REGENERATE_ENTITY_TYPE_PRODUCT = 'product';
53-
const INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY = 'category';
40+
const INPUT_KEY_STOREID = 'store-id';
41+
const INPUT_KEY_REGENERATE_ENTITY_TYPE = 'entity-type';
42+
const INPUT_KEY_SAVE_REWRITES_HISTORY = 'save-old-urls';
43+
const INPUT_KEY_NO_REINDEX = 'no-reindex';
44+
const INPUT_KEY_NO_PROGRESS = 'no-progress';
45+
const INPUT_KEY_NO_CACHE_FLUSH = 'no-cache-flush';
46+
const INPUT_KEY_NO_CACHE_CLEAN = 'no-cache-clean';
47+
const INPUT_KEY_CATEGORIES_RANGE = 'categories-range';
48+
const INPUT_KEY_PRODUCTS_RANGE = 'products-range';
49+
const INPUT_KEY_CATEGORY_ID = 'category-id';
50+
const INPUT_KEY_PRODUCT_ID = 'product-id';
51+
const INPUT_KEY_CHECK_USE_CATEGORIES_FOR_PRODUCT_URL = 'check-use-category-in-product-url';
52+
53+
const CONSOLE_LOG_MAX_DOTS_IN_LINE = 70;
54+
const INPUT_KEY_REGENERATE_ENTITY_TYPE_PRODUCT = 'product';
55+
const INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY = 'category';
5456

5557
/**
5658
* @var \Magento\Framework\App\ResourceConnection
@@ -147,6 +149,11 @@ abstract class RegenerateUrlRewritesAbstract extends Command
147149
*/
148150
protected $_productAction;
149151

152+
/**
153+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
154+
*/
155+
protected $_scopeConfig;
156+
150157
/**
151158
* @var array
152159
*/
@@ -172,6 +179,11 @@ abstract class RegenerateUrlRewritesAbstract extends Command
172179
*/
173180
protected $_errors = [];
174181

182+
/**
183+
* @var array
184+
*/
185+
protected $_consoleMsg = [];
186+
175187
/**
176188
* Constructor
177189
* @param ResourceConnection $resource
@@ -189,6 +201,7 @@ abstract class RegenerateUrlRewritesAbstract extends Command
189201
* @param DatabaseMapPool\Proxy $databaseMapPool
190202
* @param UrlPersist\Proxy $urlPersist
191203
* @param StoreManagerInterface $storeManager
204+
* @param ScopeConfigInterface $scopeConfig
192205
*/
193206
public function __construct(
194207
ResourceConnection $resource,
@@ -205,7 +218,8 @@ public function __construct(
205218
UrlRewriteHandlerFactory\Proxy $urlRewriteHandlerFactory,
206219
DatabaseMapPool\Proxy $databaseMapPool,
207220
UrlPersist\Proxy $urlPersist,
208-
StoreManagerInterface $storeManager
221+
StoreManagerInterface $storeManager,
222+
ScopeConfigInterface $scopeConfig
209223
) {
210224
$this->_resource = $resource;
211225
$this->_appState = $appState;
@@ -222,6 +236,7 @@ public function __construct(
222236
$this->_databaseMapPool = $databaseMapPool;
223237
$this->_urlPersist = $urlPersist;
224238
$this->_storeManager = $storeManager;
239+
$this->_scopeConfig = $scopeConfig;
225240

226241
$this->_dataUrlRewriteClassNames = [
227242
DataCategoryUrlRewriteDatabaseMap::class,
@@ -242,6 +257,7 @@ public function __construct(
242257
$this->_commandOptions['productsFilter'] = [];
243258
$this->_commandOptions['categoryId'] = null;
244259
$this->_commandOptions['productId'] = null;
260+
$this->_commandOptions['checkUseCategoryInProductUrl'] = false;
245261
}
246262

247263
/**
@@ -455,17 +471,34 @@ protected function _displayProgressBar($size = 70)
455471
}
456472

457473
/**
458-
* Display message in console
459-
* @param string $msg
474+
* Collect console messages
475+
* @param mixed $msg
460476
* @return void
461477
*/
462-
protected function _displayConsoleMsg($msg)
478+
protected function _addConsoleMsg($msg)
463479
{
464480
if ($msg instanceof \Magento\Framework\Phrase) {
465481
$msg = $msg->render();
466482
}
467-
$this->_output->writeln('');
468-
$this->_output->writeln($msg);
483+
484+
$this->_consoleMsg[] = (string)$msg;
485+
}
486+
487+
/**
488+
* Display all console messages
489+
* @return void
490+
*/
491+
protected function _displayConsoleMsg()
492+
{
493+
if (count($this->_consoleMsg) > 0) {
494+
$this->_output->writeln('[CONSOLE MESSAGES]');
495+
foreach ($this->_consoleMsg as $msg) {
496+
$this->_output->writeln($msg);
497+
}
498+
$this->_output->writeln('[END OF CONSOLE MESSAGES]');
499+
$this->_output->writeln('');
500+
$this->_output->writeln('');
501+
}
469502
}
470503

471504
/**
@@ -489,7 +522,7 @@ protected function _doBunchReplaceUrlRewrites($urlRewrites = array(), $type = 'C
489522
} catch (\Exception $y) {
490523
// debugging
491524
$data = $singleUrlRewrite->toArray();
492-
$this->_displayConsoleMsg($y->getMessage() .' '. $type .' ID: '. $data['entity_id'] .'. Request path: '. $data['request_path']);
525+
$this->_addConsoleMsg($y->getMessage() .' '. $type .' ID: '. $data['entity_id'] .'. Request path: '. $data['request_path']);
493526
}
494527
}
495528
}
@@ -535,6 +568,20 @@ protected function _runClearCache()
535568
*/
536569
protected function _clearRequestPath($requestPath)
537570
{
538-
return ltrim($requestPath, '/');
571+
return str_replace(['//', './'], ['/', '/'], ltrim(ltrim($requestPath, '/'), '.'));
572+
}
573+
574+
/**
575+
* Get "Use Categories Path for Product URLs" config option value
576+
* @param mixed $storeId
577+
* @return boolean
578+
*/
579+
protected function _getUseCategoriesPathForProductUrlsConfig($storeId = null)
580+
{
581+
return (bool) $this->_scopeConfig->getValue(
582+
'catalog/seo/product_use_categories',
583+
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
584+
$storeId
585+
);
539586
}
540587
}

Console/Command/RegenerateUrlRewritesCategoryAbstract.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function _categoryProcess($category, $storeId = 0)
8686
$this->_displayProgressBar();
8787
} catch (\Exception $e) {
8888
// debugging
89-
$this->_displayConsoleMsg('Exception: '. $e->getMessage() .' Category ID: '. $category->getId());
89+
$this->_addConsoleMsg('Exception: '. $e->getMessage() .' Category ID: '. $category->getId());
9090
}
9191
}
9292

@@ -104,17 +104,23 @@ protected function _regenerateCategoryUrlRewrites($category, $storeId)
104104
$categoryUrlRewriteResult = $this->_getCategoryUrlRewriteGenerator()->generate($category, true);
105105
$this->_doBunchReplaceUrlRewrites($categoryUrlRewriteResult);
106106

107-
$productUrlRewriteResult = $this->_getUrlRewriteHandler()->generateProductUrlRewrites($category);
107+
// if config option "Use Categories Path for Product URLs" is "Yes"
108+
if (
109+
($this->_commandOptions['checkUseCategoryInProductUrl'] && $this->_getUseCategoriesPathForProductUrlsConfig($storeId))
110+
|| !$this->_commandOptions['checkUseCategoryInProductUrl']
111+
) {
112+
$productUrlRewriteResult = $this->_getUrlRewriteHandler()->generateProductUrlRewrites($category);
108113

109-
// fix for double slashes issue
110-
foreach ($productUrlRewriteResult as &$urlRewrite) {
111-
$urlRewrite->setRequestPath($this->_clearRequestPath($urlRewrite->getRequestPath()));
112-
}
114+
// fix for double slashes issue and dots
115+
foreach ($productUrlRewriteResult as &$urlRewrite) {
116+
$urlRewrite->setRequestPath($this->_clearRequestPath($urlRewrite->getRequestPath()));
117+
}
113118

114-
$this->_doBunchReplaceUrlRewrites($productUrlRewriteResult, 'Product');
119+
$this->_doBunchReplaceUrlRewrites($productUrlRewriteResult, 'Product');
120+
}
115121
} catch (\Exception $e) {
116122
// debugging
117-
$this->_displayConsoleMsg('Exception: '. $e->getMessage() .' Category ID: '. $category->getId());
123+
$this->_addConsoleMsg('Exception: '. $e->getMessage() .' Category ID: '. $category->getId());
118124
}
119125
}
120126

Console/Command/RegenerateUrlRewritesProductAbstract.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected function _productProcess($product, $storeId)
8787
['url_path' => null, 'url_key' => $generatedKey],
8888
$storeId
8989
);
90-
90+
9191
$productUrlRewriteResult = $this->_getProductUrlRewriteGenerator()->generate($product);
9292

9393
$productUrlRewriteResult = $this->_sanitizeProductUrlRewrites($productUrlRewriteResult);
@@ -103,21 +103,21 @@ protected function _productProcess($product, $storeId)
103103

104104
$requestPath = implode(', ', $conflictedUrls);
105105

106-
$this->_displayConsoleMsg(
106+
$this->_addConsoleMsg(
107107
'Some URL paths already exists in url_rewrite table and not related to Product ID: '. $product->getId() .
108108
'. Please remove them and execute this command again. You can find them by following SQL:'
109109
);
110110

111-
$this->_displayConsoleMsg("SELECT * FROM url_rewrite WHERE store_id={$connection->quote($storeId, 'int')} AND request_path IN ({$requestPath});");
111+
$this->_addConsoleMsg("SELECT * FROM url_rewrite WHERE store_id={$connection->quote($storeId, 'int')} AND request_path IN ({$requestPath});");
112112
} catch (\Exception $y) {
113113
//to debugg error
114-
$this->_displayConsoleMsg($y->getMessage() .' Product ID: '. $product->getId());
114+
$this->_addConsoleMsg($y->getMessage() .' Product ID: '. $product->getId());
115115
}
116116

117117
$this->_progress++;
118118
$this->_displayProgressBar();
119119
} catch (\Exception $e) {
120-
$this->_displayConsoleMsg($e->getMessage() . ' Product ID: '. $product->getId());
120+
$this->_addConsoleMsg($e->getMessage() . ' Product ID: '. $product->getId());
121121
}
122122
}
123123

0 commit comments

Comments
 (0)