Skip to content

Commit ad9aa6e

Browse files
committed
Merge remote-tracking branch 'mainline/2.3-develop' into 2.3-develop-pr38
2 parents 3f3a788 + e3e85a1 commit ad9aa6e

File tree

82 files changed

+3767
-121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+3767
-121
lines changed

app/code/Magento/Catalog/Model/Indexer/Category/Product.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ protected function executeAction($ids)
137137

138138
/** @var Product\Action\Rows $action */
139139
$action = $this->rowsActionFactory->create();
140-
if ($indexer->isWorking()) {
140+
if ($indexer->isScheduled()) {
141141
$action->execute($ids, true);
142+
} else {
143+
$action->execute($ids);
142144
}
143-
$action->execute($ids);
144145

145146
return $this;
146147
}

app/code/Magento/Catalog/Model/Indexer/Category/Product/Action/Rows.php

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Category\Product\Action;
77

8+
use Magento\Framework\DB\Adapter\AdapterInterface;
9+
use Magento\Framework\Indexer\CacheContext;
10+
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Framework\DB\Query\Generator as QueryGenerator;
15+
use Magento\Framework\EntityManager\MetadataPool;
16+
use Magento\Catalog\Model\Config;
17+
use Magento\Catalog\Model\Category;
18+
use Magento\Framework\Indexer\IndexerRegistry;
19+
use Magento\Catalog\Model\Indexer\Product\Category as ProductCategoryIndexer;
20+
821
/**
922
* Reindex multiple rows action.
1023
*
1124
* @package Magento\Catalog\Model\Indexer\Category\Product\Action
25+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1226
*/
1327
class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractAction
1428
{
@@ -19,12 +33,54 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
1933
*/
2034
protected $limitationByCategories;
2135

36+
/**
37+
* @var CacheContext
38+
*/
39+
private $cacheContext;
40+
41+
/**
42+
* @var EventManagerInterface|null
43+
*/
44+
private $eventManager;
45+
46+
/**
47+
* @var IndexerRegistry
48+
*/
49+
private $indexerRegistry;
50+
51+
/**
52+
* @param ResourceConnection $resource
53+
* @param StoreManagerInterface $storeManager
54+
* @param Config $config
55+
* @param QueryGenerator|null $queryGenerator
56+
* @param MetadataPool|null $metadataPool
57+
* @param CacheContext|null $cacheContext
58+
* @param EventManagerInterface|null $eventManager
59+
* @param IndexerRegistry|null $indexerRegistry
60+
*/
61+
public function __construct(
62+
ResourceConnection $resource,
63+
StoreManagerInterface $storeManager,
64+
Config $config,
65+
QueryGenerator $queryGenerator = null,
66+
MetadataPool $metadataPool = null,
67+
CacheContext $cacheContext = null,
68+
EventManagerInterface $eventManager = null,
69+
IndexerRegistry $indexerRegistry = null
70+
) {
71+
parent::__construct($resource, $storeManager, $config, $queryGenerator, $metadataPool);
72+
$this->cacheContext = $cacheContext ?: ObjectManager::getInstance()->get(CacheContext::class);
73+
$this->eventManager = $eventManager ?: ObjectManager::getInstance()->get(EventManagerInterface::class);
74+
$this->indexerRegistry = $indexerRegistry ?: ObjectManager::getInstance()->get(IndexerRegistry::class);
75+
}
76+
2277
/**
2378
* Refresh entities index
2479
*
2580
* @param int[] $entityIds
2681
* @param bool $useTempTable
2782
* @return $this
83+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2884
*/
2985
public function execute(array $entityIds = [], $useTempTable = false)
3086
{
@@ -40,12 +96,58 @@ public function execute(array $entityIds = [], $useTempTable = false)
4096
}
4197
$this->limitationByCategories = array_unique($this->limitationByCategories);
4298
$this->useTempTable = $useTempTable;
43-
$this->removeEntries();
99+
$indexer = $this->indexerRegistry->get(ProductCategoryIndexer::INDEXER_ID);
100+
$workingState = $indexer->isWorking();
101+
102+
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
103+
foreach ($this->storeManager->getStores() as $store) {
104+
$this->connection->truncateTable($this->getIndexTable($store->getId()));
105+
}
106+
} else {
107+
$this->removeEntries();
108+
}
109+
44110
$this->reindex();
45111

112+
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
113+
foreach ($this->storeManager->getStores() as $store) {
114+
$removalCategoryIds = array_diff($this->limitationByCategories, [$this->getRootCategoryId($store)]);
115+
$this->connection->delete(
116+
$this->tableMaintainer->getMainTable($store->getId()),
117+
['category_id IN (?)' => $removalCategoryIds]
118+
);
119+
$select = $this->connection->select()
120+
->from($this->tableMaintainer->getMainReplicaTable($store->getId()));
121+
$this->connection->query(
122+
$this->connection->insertFromSelect(
123+
$select,
124+
$this->tableMaintainer->getMainTable($store->getId()),
125+
[],
126+
AdapterInterface::INSERT_ON_DUPLICATE
127+
)
128+
);
129+
}
130+
}
131+
132+
$this->registerCategories($entityIds);
133+
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
134+
46135
return $this;
47136
}
48137

138+
/**
139+
* Register categories assigned to products
140+
*
141+
* @param array $categoryIds
142+
* @return void
143+
*/
144+
private function registerCategories(array $categoryIds)
145+
{
146+
if ($categoryIds) {
147+
$this->cacheContext->registerEntities(Category::CACHE_TAG, $categoryIds);
148+
}
149+
}
150+
49151
/**
50152
* Return array of all category root IDs + tree root ID
51153
*

app/code/Magento/Catalog/Model/Indexer/Product/Category/Action/Rows.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
1616
use Magento\Framework\Indexer\CacheContext;
1717
use Magento\Store\Model\StoreManagerInterface;
18+
use Magento\Framework\DB\Adapter\AdapterInterface;
19+
use Magento\Framework\Indexer\IndexerRegistry;
20+
use Magento\Catalog\Model\Indexer\Category\Product as CategoryProductIndexer;
1821

1922
/**
2023
* Category rows indexer.
@@ -40,6 +43,11 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
4043
*/
4144
private $eventManager;
4245

46+
/**
47+
* @var IndexerRegistry
48+
*/
49+
private $indexerRegistry;
50+
4351
/**
4452
* @param ResourceConnection $resource
4553
* @param StoreManagerInterface $storeManager
@@ -48,6 +56,7 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
4856
* @param MetadataPool|null $metadataPool
4957
* @param CacheContext|null $cacheContext
5058
* @param EventManagerInterface|null $eventManager
59+
* @param IndexerRegistry|null $indexerRegistry
5160
*/
5261
public function __construct(
5362
ResourceConnection $resource,
@@ -56,11 +65,13 @@ public function __construct(
5665
QueryGenerator $queryGenerator = null,
5766
MetadataPool $metadataPool = null,
5867
CacheContext $cacheContext = null,
59-
EventManagerInterface $eventManager = null
68+
EventManagerInterface $eventManager = null,
69+
IndexerRegistry $indexerRegistry = null
6070
) {
6171
parent::__construct($resource, $storeManager, $config, $queryGenerator, $metadataPool);
6272
$this->cacheContext = $cacheContext ?: ObjectManager::getInstance()->get(CacheContext::class);
6373
$this->eventManager = $eventManager ?: ObjectManager::getInstance()->get(EventManagerInterface::class);
74+
$this->indexerRegistry = $indexerRegistry ?: ObjectManager::getInstance()->get(IndexerRegistry::class);
6475
}
6576

6677
/**
@@ -78,12 +89,37 @@ public function execute(array $entityIds = [], $useTempTable = false)
7889

7990
$this->limitationByProducts = $idsToBeReIndexed;
8091
$this->useTempTable = $useTempTable;
92+
$indexer = $this->indexerRegistry->get(CategoryProductIndexer::INDEXER_ID);
93+
$workingState = $indexer->isWorking();
8194

8295
$affectedCategories = $this->getCategoryIdsFromIndex($idsToBeReIndexed);
8396

84-
$this->removeEntries();
85-
97+
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
98+
foreach ($this->storeManager->getStores() as $store) {
99+
$this->connection->truncateTable($this->getIndexTable($store->getId()));
100+
}
101+
} else {
102+
$this->removeEntries();
103+
}
86104
$this->reindex();
105+
if ($useTempTable && !$workingState && $indexer->isScheduled()) {
106+
foreach ($this->storeManager->getStores() as $store) {
107+
$this->connection->delete(
108+
$this->tableMaintainer->getMainTable($store->getId()),
109+
['product_id IN (?)' => $this->limitationByProducts]
110+
);
111+
$select = $this->connection->select()
112+
->from($this->tableMaintainer->getMainReplicaTable($store->getId()));
113+
$this->connection->query(
114+
$this->connection->insertFromSelect(
115+
$select,
116+
$this->tableMaintainer->getMainTable($store->getId()),
117+
[],
118+
AdapterInterface::INSERT_ON_DUPLICATE
119+
)
120+
);
121+
}
122+
}
87123

88124
$affectedCategories = array_merge($affectedCategories, $this->getCategoryIdsFromIndex($idsToBeReIndexed));
89125

app/code/Magento/Catalog/Test/Unit/Model/Indexer/Category/ProductTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,13 @@ public function testExecuteWithIndexerWorking()
8484
{
8585
$ids = [1, 2, 3];
8686

87-
$this->indexerMock->expects($this->once())->method('isWorking')->will($this->returnValue(true));
8887
$this->prepareIndexer();
8988

9089
$rowMock = $this->createPartialMock(
9190
\Magento\Catalog\Model\Indexer\Category\Product\Action\Rows::class,
9291
['execute']
9392
);
94-
$rowMock->expects($this->at(0))->method('execute')->with($ids, true)->will($this->returnSelf());
95-
$rowMock->expects($this->at(1))->method('execute')->with($ids, false)->will($this->returnSelf());
93+
$rowMock->expects($this->at(0))->method('execute')->with($ids)->will($this->returnSelf());
9694

9795
$this->rowsMock->expects($this->once())->method('create')->will($this->returnValue($rowMock));
9896

@@ -103,14 +101,13 @@ public function testExecuteWithIndexerNotWorking()
103101
{
104102
$ids = [1, 2, 3];
105103

106-
$this->indexerMock->expects($this->once())->method('isWorking')->will($this->returnValue(false));
107104
$this->prepareIndexer();
108105

109106
$rowMock = $this->createPartialMock(
110107
\Magento\Catalog\Model\Indexer\Category\Product\Action\Rows::class,
111108
['execute']
112109
);
113-
$rowMock->expects($this->once())->method('execute')->with($ids, false)->will($this->returnSelf());
110+
$rowMock->expects($this->once())->method('execute')->with($ids)->will($this->returnSelf());
114111

115112
$this->rowsMock->expects($this->once())->method('create')->will($this->returnValue($rowMock));
116113

app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/CategoryTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,13 @@ public function testExecuteWithIndexerWorking()
8484
{
8585
$ids = [1, 2, 3];
8686

87-
$this->indexerMock->expects($this->once())->method('isWorking')->will($this->returnValue(true));
8887
$this->prepareIndexer();
8988

9089
$rowMock = $this->createPartialMock(
9190
\Magento\Catalog\Model\Indexer\Product\Category\Action\Rows::class,
9291
['execute']
9392
);
94-
$rowMock->expects($this->at(0))->method('execute')->with($ids, true)->will($this->returnSelf());
95-
$rowMock->expects($this->at(1))->method('execute')->with($ids, false)->will($this->returnSelf());
93+
$rowMock->expects($this->at(0))->method('execute')->with($ids)->will($this->returnSelf());
9694

9795
$this->rowsMock->expects($this->once())->method('create')->will($this->returnValue($rowMock));
9896

@@ -103,14 +101,13 @@ public function testExecuteWithIndexerNotWorking()
103101
{
104102
$ids = [1, 2, 3];
105103

106-
$this->indexerMock->expects($this->once())->method('isWorking')->will($this->returnValue(false));
107104
$this->prepareIndexer();
108105

109106
$rowMock = $this->createPartialMock(
110107
\Magento\Catalog\Model\Indexer\Product\Category\Action\Rows::class,
111108
['execute']
112109
);
113-
$rowMock->expects($this->once())->method('execute')->with($ids, false)->will($this->returnSelf());
110+
$rowMock->expects($this->once())->method('execute')->with($ids)->will($this->returnSelf());
114111

115112
$this->rowsMock->expects($this->once())->method('create')->will($this->returnValue($rowMock));
116113

@@ -123,7 +120,7 @@ public function testExecuteWithIndexerNotWorking()
123120

124121
protected function prepareIndexer()
125122
{
126-
$this->indexerRegistryMock->expects($this->once())
123+
$this->indexerRegistryMock->expects($this->any())
127124
->method('get')
128125
->with(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID)
129126
->will($this->returnValue($this->indexerMock));

0 commit comments

Comments
 (0)