![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Console/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Krzysztof Majkowski <[email protected]> <[email protected]> */ declare(strict_types=1); namespace Cnc\Catalog\Console; use Magento\Eav\Model\Config as EavConfig; use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; use Magento\Framework\App\ResourceConnection; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class RemoveLinksFromProducts extends Command { /** * @var EavConfig */ private $eavConfig; /** * @var ResourceConnection */ private $resourceConnection; public function __construct( EavConfig $eavConfig, ResourceConnection $resourceConnection, string $name = null ) { parent::__construct($name); $this->eavConfig = $eavConfig; $this->resourceConnection = $resourceConnection; } /** * @inheritDoc */ protected function configure() { $this->setName('catalog:products:remove-links'); $this->setDescription('Remove <a> from product description field.'); $this->addOption( 'remove_completly', null, InputOption::VALUE_NONE, 'Remove completly instead of leaving text' ); $this->addOption( 'product_id', 'i', InputOption::VALUE_REQUIRED, 'Product Id' ); parent::configure(); } /** * CLI command description * * @param InputInterface $input * @param OutputInterface $output * * @return void */ protected function execute(InputInterface $input, OutputInterface $output): void { $output->writeln('<info>Running Remove Links From Products Description command</info>'); $removeCompletly = $input->getOption('remove_completly'); $productId = $input->getOption('product_id'); $attribute = $this->eavConfig->getAttribute('catalog_product', 'description'); $data = $this->getData($attribute, $productId); $output->writeln('<info>Found ' . count($data) . ' items to correct</info>'); $toUpdate = []; foreach ($data as $key => $productData) { $toUpdate []= [ 'value_id' => $productData['value_id'], 'value' => $this->correctText($productData['value'], $removeCompletly) ]; } $output->writeln('<info>Publishing ' . count($toUpdate) . ' items</info>'); $this->publishUpdated($toUpdate, $attribute->getBackendTable(), $output); $output->writeln('<info>Correct & published ' . count($toUpdate) . ' items</info>'); } /** * @param AbstractAttribute $attribute * @param null $productId * @return array */ protected function getData(AbstractAttribute $attribute, $productId = null): array { $connection = $this->resourceConnection->getConnection(); $sql = "SELECT value_id, value FROM {$attribute->getBackendTable()} WHERE value LIKE '%<a%'"; if ($productId) { $sql .= " AND entity_id = {$productId}"; } return $connection->fetchAll($sql); } /** * @param $text * @param bool $removeCompletly * @return array|string|string[]|null */ protected function correctText($text, bool $removeCompletly) { return $removeCompletly ? preg_replace('#<a.*?>.*?</a>#i', '', $text) : preg_replace('#<a.*?>(.*?)</a>#i', '\1', $text); } /** * @param array $toUpdate * @param string $tableName * @param OutputInterface $output */ protected function publishUpdated(array $toUpdate, string $tableName, OutputInterface $output) { $i = 1; $connection = $this->resourceConnection->getConnection(); foreach ($toUpdate as $update) { if ($i % 100 == 0) { $output->write('.'); } if ($i % 1000 == 0) { $output->write($i); } $i++; $connection->insertOnDuplicate($tableName, $update); } $output->writeln(''); } }