zend-blog-3-backend
94 строки · 2.3 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 22.04.18
6* Time: 15:38
7*/
8
9namespace App\Command;10
11use App\Entity\Comment;12use App\Utils\ExternalLinkProcessor;13use Doctrine\ORM\EntityManager;14use Doctrine\ORM\EntityManagerInterface;15use Symfony\Component\Console\Command\Command;16use Symfony\Component\Console\Input\InputInterface;17use Symfony\Component\Console\Output\OutputInterface;18
19class ExternalLinksCommand extends Command20{
21/**22* @var EntityManager
23*/
24private $em;25
26/**27* @param EntityManagerInterface $em
28*/
29public function __construct(EntityManagerInterface $em)30{31$this->em = $em;32$em->getConfiguration()->setSQLLogger(null);33
34parent::__construct();35}36
37protected function configure(): void38{39$this40->setName('mtt:comments:no-follow')41->setDescription('Batch update all posts')42;43}44
45protected function execute(InputInterface $input, OutputInterface $output): int46{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;54do {55$updated = false;56
57$qb = $repo->createQueryBuilder('c');58$qb59->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));67foreach ($comments as $entity) {68$newContent = $linkProcessor->upgradeLinks($entity->getText());69if ($newContent) {70$cnt++;71$entity->setText($newContent);72$this->em->flush();73}74}75
76if (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(89sprintf('<info>Total time: <comment>%s</comment> sec</info>', round($endTime - $startTime, 3))90);91
92return 0;93}94}
95