2017-02-24 10:27:03 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Wallabag\Command;
|
2017-02-24 10:27:03 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2017-02-24 10:27:03 +00:00
|
|
|
use Doctrine\ORM\NoResultException;
|
2022-12-15 19:57:02 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
2017-02-24 10:27:03 +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;
|
2024-02-19 00:30:12 +00:00
|
|
|
use Wallabag\Entity\Entry;
|
|
|
|
use Wallabag\Entity\User;
|
|
|
|
use Wallabag\Repository\EntryRepository;
|
|
|
|
use Wallabag\Repository\UserRepository;
|
2017-02-24 10:27:03 +00:00
|
|
|
|
2022-12-15 19:57:02 +00:00
|
|
|
class CleanDuplicatesCommand extends Command
|
2017-02-24 10:27:03 +00:00
|
|
|
{
|
2024-02-02 22:24:33 +00:00
|
|
|
protected static $defaultName = 'wallabag:clean-duplicates';
|
|
|
|
protected static $defaultDescription = 'Cleans the database for duplicates';
|
|
|
|
|
2022-12-15 19:57:02 +00:00
|
|
|
protected SymfonyStyle $io;
|
|
|
|
protected int $duplicates = 0;
|
|
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
private EntryRepository $entryRepository;
|
|
|
|
private UserRepository $userRepository;
|
2017-02-24 10:27:03 +00:00
|
|
|
|
2022-12-15 19:57:02 +00:00
|
|
|
public function __construct(EntityManagerInterface $entityManager, EntryRepository $entryRepository, UserRepository $userRepository)
|
|
|
|
{
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
$this->entryRepository = $entryRepository;
|
|
|
|
$this->userRepository = $userRepository;
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2017-02-24 10:27:03 +00:00
|
|
|
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setHelp('This command helps you to clean your articles list in case of duplicates')
|
|
|
|
->addArgument(
|
|
|
|
'username',
|
|
|
|
InputArgument::OPTIONAL,
|
|
|
|
'User to clean'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2017-07-28 22:30:22 +00:00
|
|
|
$this->io = new SymfonyStyle($input, $output);
|
2017-02-24 10:27:03 +00:00
|
|
|
|
|
|
|
$username = $input->getArgument('username');
|
|
|
|
|
|
|
|
if ($username) {
|
|
|
|
try {
|
|
|
|
$user = $this->getUser($username);
|
|
|
|
$this->cleanDuplicates($user);
|
|
|
|
} catch (NoResultException $e) {
|
2017-07-28 22:30:22 +00:00
|
|
|
$this->io->error(sprintf('User "%s" not found.', $username));
|
2017-02-24 10:27:03 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2017-07-28 22:30:22 +00:00
|
|
|
|
|
|
|
$this->io->success('Finished cleaning.');
|
2017-02-24 10:27:03 +00:00
|
|
|
} else {
|
2022-12-15 19:57:02 +00:00
|
|
|
$users = $this->userRepository->findAll();
|
2017-02-24 10:27:03 +00:00
|
|
|
|
2018-09-05 12:25:32 +00:00
|
|
|
$this->io->text(sprintf('Cleaning through <info>%d</info> user accounts', \count($users)));
|
2017-02-24 10:27:03 +00:00
|
|
|
|
|
|
|
foreach ($users as $user) {
|
2017-07-28 22:30:22 +00:00
|
|
|
$this->io->text(sprintf('Processing user <info>%s</info>', $user->getUsername()));
|
2017-02-24 10:27:03 +00:00
|
|
|
$this->cleanDuplicates($user);
|
|
|
|
}
|
2017-07-28 22:30:22 +00:00
|
|
|
$this->io->success(sprintf('Finished cleaning. %d duplicates found in total', $this->duplicates));
|
2017-02-24 10:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cleanDuplicates(User $user)
|
|
|
|
{
|
2022-12-15 19:57:02 +00:00
|
|
|
$entries = $this->entryRepository->findAllEntriesIdAndUrlByUserId($user->getId());
|
2017-02-24 10:27:03 +00:00
|
|
|
|
|
|
|
$duplicatesCount = 0;
|
|
|
|
$urls = [];
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$url = $this->similarUrl($entry['url']);
|
|
|
|
|
|
|
|
/* @var $entry Entry */
|
2018-09-05 12:25:32 +00:00
|
|
|
if (\in_array($url, $urls, true)) {
|
2017-02-24 10:27:03 +00:00
|
|
|
++$duplicatesCount;
|
|
|
|
|
2022-12-15 19:57:02 +00:00
|
|
|
$this->entityManager->remove($this->entryRepository->find($entry['id']));
|
|
|
|
$this->entityManager->flush(); // Flushing at the end of the loop would require the instance not being online
|
2017-02-24 10:27:03 +00:00
|
|
|
} else {
|
|
|
|
$urls[] = $entry['url'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->duplicates += $duplicatesCount;
|
|
|
|
|
2017-07-28 22:30:22 +00:00
|
|
|
$this->io->text(sprintf('Cleaned <info>%d</info> duplicates for user <info>%s</info>', $duplicatesCount, $user->getUserName()));
|
2017-02-24 10:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function similarUrl($url)
|
|
|
|
{
|
2018-09-05 12:25:32 +00:00
|
|
|
if (\in_array(substr($url, -1), ['/', '#'], true)) { // get rid of "/" and "#" and the end of urls
|
|
|
|
return substr($url, 0, \strlen($url));
|
2017-02-24 10:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches a user from its username.
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
*
|
2022-08-28 14:59:43 +00:00
|
|
|
* @return User
|
2017-02-24 10:27:03 +00:00
|
|
|
*/
|
|
|
|
private function getUser($username)
|
|
|
|
{
|
2022-12-15 19:57:02 +00:00
|
|
|
return $this->userRepository->findOneByUserName($username);
|
2017-02-24 10:27:03 +00:00
|
|
|
}
|
|
|
|
}
|