]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php
Merge pull request #4151 from ldidry/fix-4060
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / CleanDuplicatesCommand.php
CommitLineData
e2f3800c
TC
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Doctrine\ORM\NoResultException;
6use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
e1b33efb 10use Symfony\Component\Console\Style\SymfonyStyle;
e2f3800c
TC
11use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\UserBundle\Entity\User;
13
14class CleanDuplicatesCommand extends ContainerAwareCommand
15{
e1b33efb
NH
16 /** @var SymfonyStyle */
17 protected $io;
e2f3800c
TC
18
19 protected $duplicates = 0;
20
21 protected function configure()
22 {
23 $this
24 ->setName('wallabag:clean-duplicates')
25 ->setDescription('Cleans the database for duplicates')
26 ->setHelp('This command helps you to clean your articles list in case of duplicates')
27 ->addArgument(
28 'username',
29 InputArgument::OPTIONAL,
30 'User to clean'
31 );
32 }
33
34 protected function execute(InputInterface $input, OutputInterface $output)
35 {
e1b33efb 36 $this->io = new SymfonyStyle($input, $output);
e2f3800c
TC
37
38 $username = $input->getArgument('username');
39
40 if ($username) {
41 try {
42 $user = $this->getUser($username);
43 $this->cleanDuplicates($user);
44 } catch (NoResultException $e) {
e1b33efb 45 $this->io->error(sprintf('User "%s" not found.', $username));
e2f3800c
TC
46
47 return 1;
48 }
e1b33efb
NH
49
50 $this->io->success('Finished cleaning.');
e2f3800c 51 } else {
03ce43d4 52 $users = $this->getContainer()->get('wallabag_user.user_repository')->findAll();
e2f3800c 53
2a1ceb67 54 $this->io->text(sprintf('Cleaning through <info>%d</info> user accounts', \count($users)));
e2f3800c
TC
55
56 foreach ($users as $user) {
e1b33efb 57 $this->io->text(sprintf('Processing user <info>%s</info>', $user->getUsername()));
e2f3800c
TC
58 $this->cleanDuplicates($user);
59 }
e1b33efb 60 $this->io->success(sprintf('Finished cleaning. %d duplicates found in total', $this->duplicates));
e2f3800c
TC
61 }
62
63 return 0;
64 }
65
e2f3800c
TC
66 private function cleanDuplicates(User $user)
67 {
68 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
03ce43d4 69 $repo = $this->getContainer()->get('wallabag_core.entry_repository');
e2f3800c 70
dbf1188c 71 $entries = $repo->findAllEntriesIdAndUrlByUserId($user->getId());
e2f3800c
TC
72
73 $duplicatesCount = 0;
74 $urls = [];
75 foreach ($entries as $entry) {
76 $url = $this->similarUrl($entry['url']);
77
78 /* @var $entry Entry */
2a1ceb67 79 if (\in_array($url, $urls, true)) {
e2f3800c
TC
80 ++$duplicatesCount;
81
82 $em->remove($repo->find($entry['id']));
83 $em->flush(); // Flushing at the end of the loop would require the instance not being online
84 } else {
85 $urls[] = $entry['url'];
86 }
87 }
88
89 $this->duplicates += $duplicatesCount;
90
e1b33efb 91 $this->io->text(sprintf('Cleaned <info>%d</info> duplicates for user <info>%s</info>', $duplicatesCount, $user->getUserName()));
e2f3800c
TC
92 }
93
94 private function similarUrl($url)
95 {
2a1ceb67
KD
96 if (\in_array(substr($url, -1), ['/', '#'], true)) { // get rid of "/" and "#" and the end of urls
97 return substr($url, 0, \strlen($url));
e2f3800c
TC
98 }
99
100 return $url;
101 }
102
103 /**
104 * Fetches a user from its username.
105 *
106 * @param string $username
107 *
108 * @return \Wallabag\UserBundle\Entity\User
109 */
110 private function getUser($username)
111 {
03ce43d4 112 return $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($username);
e2f3800c 113 }
e2f3800c 114}