wallabag/src/Command/TagAllCommand.php

85 lines
2.3 KiB
PHP
Raw Normal View History

<?php
2024-02-19 00:30:12 +00:00
namespace Wallabag\Command;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NoResultException;
use Symfony\Component\Console\Command\Command;
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;
2024-02-19 00:30:12 +00:00
use Wallabag\Entity\User;
use Wallabag\Helper\RuleBasedTagger;
use Wallabag\Repository\UserRepository;
class TagAllCommand extends Command
{
2024-02-02 22:24:33 +00:00
protected static $defaultName = 'wallabag:tag:all';
protected static $defaultDescription = 'Tag all entries using the tagging rules.';
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();
}
protected function configure()
{
$this
->addArgument(
2024-01-01 18:11:01 +00:00
'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);
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')));
return 1;
}
2017-07-28 22:30:22 +00:00
$io->text(sprintf('Tagging entries for user <info>%s</info>...', $user->getUserName()));
$entries = $this->ruleBasedTagger->tagAllForUser($user);
$io->text('Persist ' . \count($entries) . ' entries... ');
2016-10-09 16:31:30 +00:00
foreach ($entries as $entry) {
$this->entityManager->persist($entry);
}
$this->entityManager->flush();
2017-07-28 22:30:22 +00:00
$io->success('Done.');
return 0;
}
/**
* Fetches a user from its username.
*
* @param string $username
*
2022-08-28 14:59:43 +00:00
* @return User
*/
private function getUser($username)
{
return $this->userRepository->findOneByUserName($username);
}
}