zend-blog-3-backend

Форк
0
/
ExternalLinksCommand.php 
94 строки · 2.3 Кб
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: morontt
5
 * Date: 22.04.18
6
 * Time: 15:38
7
 */
8

9
namespace App\Command;
10

11
use App\Entity\Comment;
12
use App\Utils\ExternalLinkProcessor;
13
use Doctrine\ORM\EntityManager;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18

19
class ExternalLinksCommand extends Command
20
{
21
    /**
22
     * @var EntityManager
23
     */
24
    private $em;
25

26
    /**
27
     * @param EntityManagerInterface $em
28
     */
29
    public function __construct(EntityManagerInterface $em)
30
    {
31
        $this->em = $em;
32
        $em->getConfiguration()->setSQLLogger(null);
33

34
        parent::__construct();
35
    }
36

37
    protected function configure(): void
38
    {
39
        $this
40
            ->setName('mtt:comments:no-follow')
41
            ->setDescription('Batch update all posts')
42
        ;
43
    }
44

45
    protected function execute(InputInterface $input, OutputInterface $output): int
46
    {
47
        $startTime = microtime(true);
48
        $cnt = 0;
49

50
        $repo = $this->em->getRepository(Comment::class);
51
        $linkProcessor = new ExternalLinkProcessor(['xelbot.com']);
52

53
        $i = 0;
54
        do {
55
            $updated = false;
56

57
            $qb = $repo->createQueryBuilder('c');
58
            $qb
59
                ->orderBy('c.id')
60
                ->setFirstResult($i * 20)
61
                ->setMaxResults(20)
62
            ;
63

64
            /* @var Comment[] $comments */
65
            $comments = $qb->getQuery()->getResult();
66
            $output->writeln(count($comments));
67
            foreach ($comments as $entity) {
68
                $newContent = $linkProcessor->upgradeLinks($entity->getText());
69
                if ($newContent) {
70
                    $cnt++;
71
                    $entity->setText($newContent);
72
                    $this->em->flush();
73
                }
74
            }
75

76
            if (count($comments)) {
77
                $updated = true;
78
            }
79

80
            $i++;
81
        } while ($updated);
82

83
        $output->writeln('');
84
        $output->writeln(sprintf('<info>Update <comment>%d</comment> comments</info>', $cnt));
85

86
        $endTime = microtime(true);
87

88
        $output->writeln(
89
            sprintf('<info>Total time: <comment>%s</comment> sec</info>', round($endTime - $startTime, 3))
90
        );
91

92
        return 0;
93
    }
94
}
95

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.