Spamworldpro Mini Shell
Spamworldpro


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/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Catalog/Console/RemoveLinksFromCategories.php
<?php
/**
 * Copyright (c) 2021 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 RemoveLinksFromCategories 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:category:remove-links');
        $this->setDescription('Remove <a> from category description field.');

        $this->addOption(
            'remove_completly',
            null,
            InputOption::VALUE_NONE,
            'Remove completly instead of leaving text'
        );
        $this->addOption(
            'category_id',
            'i',
            InputOption::VALUE_REQUIRED,
            'Category 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 Categories Description command</info>');

        $removeCompletly = $input->getOption('remove_completly');
        $category_id = $input->getOption('category_id');
        $attribute = $this->eavConfig->getAttribute('catalog_category', 'description');

        $data = $this->getData($attribute, $category_id);
        $output->writeln('<info>Found ' . count($data) . ' items to correct</info>');
        $toUpdate = [];
        foreach ($data as $key => $categoryData) {
            $toUpdate []= [
                'value_id' => $categoryData['value_id'],
                'value' => $this->correctText($categoryData['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 $categoryId
     * @return array
     */
    protected function getData(AbstractAttribute $attribute, $categoryId = null): array
    {
        $connection = $this->resourceConnection->getConnection();
        $sql = "SELECT value_id, value FROM {$attribute->getBackendTable()} WHERE value LIKE '%<a%'";
        if ($categoryId) {
            $sql .= " AND entity_id = {$categoryId}";
        }
        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('');
    }
}

Spamworldpro Mini