]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php
Merge pull request #4151 from ldidry/fix-4060
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / CleanDuplicatesCommand.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Command;
4
5 use Doctrine\ORM\NoResultException;
6 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7 use Symfony\Component\Console\Input\InputArgument;
8 use Symfony\Component\Console\Input\InputInterface;
9 use Symfony\Component\Console\Output\OutputInterface;
10 use Symfony\Component\Console\Style\SymfonyStyle;
11 use Wallabag\CoreBundle\Entity\Entry;
12 use Wallabag\UserBundle\Entity\User;
13
14 class CleanDuplicatesCommand extends ContainerAwareCommand
15 {
16 /** @var SymfonyStyle */
17 protected $io;
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 {
36 $this->io = new SymfonyStyle($input, $output);
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) {
45 $this->io->error(sprintf('User "%s" not found.', $username));
46
47 return 1;
48 }
49
50 $this->io->success('Finished cleaning.');
51 } else {
52 $users = $this->getContainer()->get('wallabag_user.user_repository')->findAll();
53
54 $this->io->text(sprintf('Cleaning through <info>%d</info> user accounts', \count($users)));
55
56 foreach ($users as $user) {
57 $this->io->text(sprintf('Processing user <info>%s</info>', $user->getUsername()));
58 $this->cleanDuplicates($user);
59 }
60 $this->io->success(sprintf('Finished cleaning. %d duplicates found in total', $this->duplicates));
61 }
62
63 return 0;
64 }
65
66 private function cleanDuplicates(User $user)
67 {
68 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
69 $repo = $this->getContainer()->get('wallabag_core.entry_repository');
70
71 $entries = $repo->findAllEntriesIdAndUrlByUserId($user->getId());
72
73 $duplicatesCount = 0;
74 $urls = [];
75 foreach ($entries as $entry) {
76 $url = $this->similarUrl($entry['url']);
77
78 /* @var $entry Entry */
79 if (\in_array($url, $urls, true)) {
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
91 $this->io->text(sprintf('Cleaned <info>%d</info> duplicates for user <info>%s</info>', $duplicatesCount, $user->getUserName()));
92 }
93
94 private function similarUrl($url)
95 {
96 if (\in_array(substr($url, -1), ['/', '#'], true)) { // get rid of "/" and "#" and the end of urls
97 return substr($url, 0, \strlen($url));
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 {
112 return $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($username);
113 }
114 }