2015-10-24 13:11:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Command;
|
|
|
|
|
2022-12-15 19:57:02 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2015-10-24 13:11:06 +00:00
|
|
|
use Doctrine\ORM\NoResultException;
|
2022-12-15 19:57:02 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
2015-10-24 13:11:06 +00:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2017-07-28 22:30:22 +00:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2022-04-24 15:48:59 +00:00
|
|
|
use Wallabag\CoreBundle\Helper\RuleBasedTagger;
|
2022-08-28 14:59:43 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2022-04-24 14:09:34 +00:00
|
|
|
use Wallabag\UserBundle\Repository\UserRepository;
|
2015-10-24 13:11:06 +00:00
|
|
|
|
2022-12-15 19:57:02 +00:00
|
|
|
class TagAllCommand extends Command
|
2015-10-24 13:11:06 +00:00
|
|
|
{
|
2022-12-15 19:57:02 +00:00
|
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
private RuleBasedTagger $ruleBasedTagger;
|
|
|
|
private UserRepository $userRepository;
|
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $entityManager, RuleBasedTagger $ruleBasedTagger, UserRepository $userRepository)
|
|
|
|
{
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
$this->ruleBasedTagger = $ruleBasedTagger;
|
|
|
|
$this->userRepository = $userRepository;
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2015-10-24 13:11:06 +00:00
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('wallabag:tag:all')
|
|
|
|
->setDescription('Tag all entries using the tagging rules.')
|
|
|
|
->addArgument(
|
|
|
|
'username',
|
|
|
|
InputArgument::REQUIRED,
|
|
|
|
'User to tag entries for.'
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2017-07-28 22:30:22 +00:00
|
|
|
$io = new SymfonyStyle($input, $output);
|
|
|
|
|
2015-10-24 13:11:06 +00:00
|
|
|
try {
|
|
|
|
$user = $this->getUser($input->getArgument('username'));
|
|
|
|
} catch (NoResultException $e) {
|
2017-07-28 22:30:22 +00:00
|
|
|
$io->error(sprintf('User "%s" not found.', $input->getArgument('username')));
|
2015-10-24 13:11:06 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-28 22:30:22 +00:00
|
|
|
$io->text(sprintf('Tagging entries for user <info>%s</info>...', $user->getUserName()));
|
2015-10-24 13:11:06 +00:00
|
|
|
|
2022-12-15 19:57:02 +00:00
|
|
|
$entries = $this->ruleBasedTagger->tagAllForUser($user);
|
2015-10-24 13:11:06 +00:00
|
|
|
|
2022-03-02 18:11:32 +00:00
|
|
|
$io->text('Persist ' . \count($entries) . ' entries... ');
|
2016-10-09 16:31:30 +00:00
|
|
|
|
2015-10-24 13:11:06 +00:00
|
|
|
foreach ($entries as $entry) {
|
2022-12-15 19:57:02 +00:00
|
|
|
$this->entityManager->persist($entry);
|
2015-10-24 13:11:06 +00:00
|
|
|
}
|
2022-12-15 19:57:02 +00:00
|
|
|
$this->entityManager->flush();
|
2015-10-24 13:11:06 +00:00
|
|
|
|
2017-07-28 22:30:22 +00:00
|
|
|
$io->success('Done.');
|
|
|
|
|
|
|
|
return 0;
|
2015-10-24 13:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches a user from its username.
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
*
|
2022-08-28 14:59:43 +00:00
|
|
|
* @return User
|
2015-10-24 13:11:06 +00:00
|
|
|
*/
|
|
|
|
private function getUser($username)
|
|
|
|
{
|
2022-12-15 19:57:02 +00:00
|
|
|
return $this->userRepository->findOneByUserName($username);
|
2015-10-24 13:11:06 +00:00
|
|
|
}
|
|
|
|
}
|