1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-05 18:41:02 +00:00
wallabag/src/Command/TagAllCommand.php

80 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2024-02-19 01:30:12 +01: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-29 00:30:22 +02:00
use Symfony\Component\Console\Style\SymfonyStyle;
2024-02-19 01:30:12 +01:00
use Wallabag\Entity\User;
use Wallabag\Helper\RuleBasedTagger;
use Wallabag\Repository\UserRepository;
class TagAllCommand extends Command
{
2024-02-02 23:24:33 +01:00
protected static $defaultName = 'wallabag:tag:all';
protected static $defaultDescription = 'Tag all entries using the tagging rules.';
public function __construct(
2025-04-05 13:59:36 +02:00
private readonly EntityManagerInterface $entityManager,
private readonly RuleBasedTagger $ruleBasedTagger,
private readonly UserRepository $userRepository,
) {
parent::__construct();
}
protected function configure()
{
$this
->addArgument(
2024-01-01 19:11:01 +01:00
'username',
InputArgument::REQUIRED,
'User to tag entries for.'
)
;
}
2025-01-18 23:56:06 +01:00
protected function execute(InputInterface $input, OutputInterface $output): int
{
2017-07-29 00:30:22 +02:00
$io = new SymfonyStyle($input, $output);
try {
$user = $this->getUser($input->getArgument('username'));
2025-04-05 13:56:56 +02:00
} catch (NoResultException) {
2024-08-14 16:39:36 +02:00
$io->error(\sprintf('User "%s" not found.', $input->getArgument('username')));
return 1;
}
2024-08-14 16:39:36 +02: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 18:31:30 +02:00
foreach ($entries as $entry) {
$this->entityManager->persist($entry);
}
$this->entityManager->flush();
2017-07-29 00:30:22 +02:00
$io->success('Done.');
return 0;
}
/**
* Fetches a user from its username.
*
* @param string $username
*
2022-08-28 16:59:43 +02:00
* @return User
*/
private function getUser($username)
{
return $this->userRepository->findOneByUserName($username);
}
}