wallabag/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php

118 lines
3.7 KiB
PHP
Raw Normal View History

2017-02-24 10:27:03 +00:00
<?php
namespace Wallabag\CoreBundle\Command;
2022-08-28 00:01:46 +00:00
use Doctrine\ORM\EntityManagerInterface;
2017-02-24 10:27:03 +00:00
use Doctrine\ORM\NoResultException;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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;
2017-02-24 10:27:03 +00:00
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Repository\EntryRepository;
2017-02-24 10:27:03 +00:00
use Wallabag\UserBundle\Entity\User;
use Wallabag\UserBundle\Repository\UserRepository;
2017-02-24 10:27:03 +00:00
class CleanDuplicatesCommand extends ContainerAwareCommand
{
2017-07-28 22:30:22 +00:00
/** @var SymfonyStyle */
protected $io;
2017-02-24 10:27:03 +00:00
protected $duplicates = 0;
protected function configure()
{
$this
->setName('wallabag:clean-duplicates')
->setDescription('Cleans the database for duplicates')
->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 {
$users = $this->getContainer()->get(UserRepository::class)->findAll();
2017-02-24 10:27:03 +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-08-28 00:01:46 +00:00
$em = $this->getContainer()->get(EntityManagerInterface::class);
$repo = $this->getContainer()->get(EntryRepository::class);
2017-02-24 10:27:03 +00:00
$entries = $repo->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 */
if (\in_array($url, $urls, true)) {
2017-02-24 10:27:03 +00:00
++$duplicatesCount;
$em->remove($repo->find($entry['id']));
$em->flush(); // Flushing at the end of the loop would require the instance not being online
} 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)
{
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
*
* @return \Wallabag\UserBundle\Entity\User
*/
private function getUser($username)
{
return $this->getContainer()->get(UserRepository::class)->findOneByUserName($username);
2017-02-24 10:27:03 +00:00
}
}