Skip to content

Commit 5311709

Browse files
Merge pull request #5972 from magento-borg/MC-32796
[CIA] Bugfixes
2 parents f20fb6d + d0e6327 commit 5311709

File tree

3 files changed

+464
-0
lines changed

3 files changed

+464
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Magento\Quote\Setup\Patch\Data;
8+
9+
use Magento\Framework\DB\Query\Generator;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Serialize\Serializer\Json;
12+
use Magento\Framework\Setup\Patch\DataPatchInterface;
13+
use Magento\Quote\Setup\QuoteSetupFactory;
14+
use Psr\Log\LoggerInterface;
15+
16+
/**
17+
* Class Clean Up Data Removes unused data
18+
*/
19+
class WishlistDataCleanUp implements DataPatchInterface
20+
{
21+
/**
22+
* Batch size for query
23+
*/
24+
private const BATCH_SIZE = 1000;
25+
26+
/**
27+
* @var QuoteSetupFactory
28+
*/
29+
private $quoteSetupFactory;
30+
31+
/**
32+
* @var Generator
33+
*/
34+
private $queryGenerator;
35+
36+
/**
37+
* @var Json
38+
*/
39+
private $json;
40+
41+
/**
42+
* @var LoggerInterface
43+
*/
44+
private $logger;
45+
46+
/**
47+
* RemoveData constructor.
48+
* @param Json $json
49+
* @param Generator $queryGenerator
50+
* @param QuoteSetupFactory $quoteSetupFactory
51+
* @param LoggerInterface $logger
52+
*/
53+
public function __construct(
54+
Json $json,
55+
Generator $queryGenerator,
56+
QuoteSetupFactory $quoteSetupFactory,
57+
LoggerInterface $logger
58+
) {
59+
$this->json = $json;
60+
$this->queryGenerator = $queryGenerator;
61+
$this->quoteSetupFactory = $quoteSetupFactory;
62+
$this->logger = $logger;
63+
}
64+
65+
/**
66+
* @inheritdoc
67+
*/
68+
public function apply()
69+
{
70+
try {
71+
$this->cleanQuoteItemOptionTable();
72+
} catch (\Throwable $e) {
73+
$this->logger->warning(
74+
'Quote module WishlistDataCleanUp patch experienced an error and could not be completed.'
75+
. ' Please submit a support ticket or email us at security@magento.com.'
76+
);
77+
78+
return $this;
79+
}
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* Remove login data from quote_item_option table.
86+
*
87+
* @throws LocalizedException
88+
*/
89+
private function cleanQuoteItemOptionTable()
90+
{
91+
$quoteSetup = $this->quoteSetupFactory->create();
92+
$tableName = $quoteSetup->getTable('quote_item_option');
93+
$select = $quoteSetup
94+
->getConnection()
95+
->select()
96+
->from(
97+
$tableName,
98+
['option_id', 'value']
99+
)
100+
->where(
101+
'value LIKE ?',
102+
'%login%'
103+
);
104+
$iterator = $this->queryGenerator->generate('option_id', $select, self::BATCH_SIZE);
105+
$rowErrorFlag = false;
106+
foreach ($iterator as $selectByRange) {
107+
$optionRows = $quoteSetup->getConnection()->fetchAll($selectByRange);
108+
foreach ($optionRows as $optionRow) {
109+
try {
110+
$rowValue = $this->json->unserialize($optionRow['value']);
111+
if (is_array($rowValue)
112+
&& array_key_exists('login', $rowValue)
113+
) {
114+
unset($rowValue['login']);
115+
}
116+
$rowValue = $this->json->serialize($rowValue);
117+
$quoteSetup->getConnection()->update(
118+
$tableName,
119+
['value' => $rowValue],
120+
['option_id = ?' => $optionRow['option_id']]
121+
);
122+
} catch (\Throwable $e) {
123+
$rowErrorFlag = true;
124+
continue;
125+
}
126+
}
127+
}
128+
if ($rowErrorFlag) {
129+
$this->logger->warning(
130+
'Data clean up could not be completed due to unexpected data format in the table "'
131+
. $tableName
132+
. '". Please submit a support ticket or email us at security@magento.com.'
133+
);
134+
}
135+
}
136+
137+
/**
138+
* @inheritdoc
139+
*/
140+
public static function getDependencies()
141+
{
142+
return [
143+
ConvertSerializedDataToJson::class
144+
];
145+
}
146+
147+
/**
148+
* @inheritdoc
149+
*/
150+
public function getAliases()
151+
{
152+
return [];
153+
}
154+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Magento\Sales\Setup\Patch\Data;
8+
9+
use Magento\Framework\DB\Query\Generator;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Serialize\Serializer\Json;
12+
use Magento\Framework\Setup\Patch\DataPatchInterface;
13+
use Magento\Sales\Setup\SalesSetupFactory;
14+
use Psr\Log\LoggerInterface;
15+
16+
/**
17+
* Class Clean Up Data Removes unused data
18+
*/
19+
class WishlistDataCleanUp implements DataPatchInterface
20+
{
21+
/**
22+
* Batch size for query
23+
*/
24+
private const BATCH_SIZE = 1000;
25+
26+
/**
27+
* @var SalesSetupFactory
28+
*/
29+
private $salesSetupFactory;
30+
31+
/**
32+
* @var Generator
33+
*/
34+
private $queryGenerator;
35+
36+
/**
37+
* @var Json
38+
*/
39+
private $json;
40+
41+
/**
42+
* @var LoggerInterface
43+
*/
44+
private $logger;
45+
46+
/**
47+
* RemoveData constructor.
48+
* @param Json $json
49+
* @param Generator $queryGenerator
50+
* @param SalesSetupFactory $salesSetupFactory
51+
* @param LoggerInterface $logger
52+
*/
53+
public function __construct(
54+
Json $json,
55+
Generator $queryGenerator,
56+
SalesSetupFactory $salesSetupFactory,
57+
LoggerInterface $logger
58+
) {
59+
$this->json = $json;
60+
$this->queryGenerator = $queryGenerator;
61+
$this->salesSetupFactory = $salesSetupFactory;
62+
$this->logger = $logger;
63+
}
64+
65+
/**
66+
* @inheritdoc
67+
*
68+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
69+
*/
70+
public function apply()
71+
{
72+
try {
73+
$this->cleanSalesOrderItemTable();
74+
} catch (\Throwable $e) {
75+
$this->logger->warning(
76+
'Sales module WishlistDataCleanUp patch experienced an error and could not be completed.'
77+
. ' Please submit a support ticket or email us at security@magento.com.'
78+
);
79+
80+
return $this;
81+
}
82+
83+
return $this;
84+
}
85+
86+
/**
87+
* Remove login data from sales_order_item table.
88+
*
89+
* @throws LocalizedException
90+
*/
91+
private function cleanSalesOrderItemTable()
92+
{
93+
$salesSetup = $this->salesSetupFactory->create();
94+
$tableName = $salesSetup->getTable('sales_order_item');
95+
$select = $salesSetup
96+
->getConnection()
97+
->select()
98+
->from(
99+
$tableName,
100+
['item_id', 'product_options']
101+
)
102+
->where(
103+
'product_options LIKE ?',
104+
'%login%'
105+
);
106+
$iterator = $this->queryGenerator->generate('item_id', $select, self::BATCH_SIZE);
107+
$rowErrorFlag = false;
108+
foreach ($iterator as $selectByRange) {
109+
$itemRows = $salesSetup->getConnection()->fetchAll($selectByRange);
110+
foreach ($itemRows as $itemRow) {
111+
try {
112+
$rowValue = $this->json->unserialize($itemRow['product_options']);
113+
if (is_array($rowValue)
114+
&& array_key_exists('info_buyRequest', $rowValue)
115+
&& array_key_exists('login', $rowValue['info_buyRequest'])
116+
) {
117+
unset($rowValue['info_buyRequest']['login']);
118+
}
119+
$rowValue = $this->json->serialize($rowValue);
120+
$salesSetup->getConnection()->update(
121+
$tableName,
122+
['product_options' => $rowValue],
123+
['item_id = ?' => $itemRow['item_id']]
124+
);
125+
} catch (\Throwable $e) {
126+
$rowErrorFlag = true;
127+
continue;
128+
}
129+
}
130+
}
131+
if ($rowErrorFlag) {
132+
$this->logger->warning(
133+
'Data clean up could not be completed due to unexpected data format in the table "'
134+
. $tableName
135+
. '". Please submit a support ticket or email us at security@magento.com.'
136+
);
137+
}
138+
}
139+
140+
/**
141+
* @inheritdoc
142+
*/
143+
public static function getDependencies()
144+
{
145+
return [
146+
ConvertSerializedDataToJson::class
147+
];
148+
}
149+
150+
/**
151+
* @inheritdoc
152+
*/
153+
public function getAliases()
154+
{
155+
return [];
156+
}
157+
}

0 commit comments

Comments
 (0)