]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add Clean Duplicates Command
authorThomas Citharel <tcit@tcit.fr>
Fri, 24 Feb 2017 10:27:03 +0000 (11:27 +0100)
committerThomas Citharel <tcit@tcit.fr>
Mon, 1 May 2017 11:04:31 +0000 (13:04 +0200)
src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Repository/EntryRepository.php

diff --git a/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php b/src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php
new file mode 100644 (file)
index 0000000..65f35d8
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+
+namespace Wallabag\CoreBundle\Command;
+
+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;
+use Wallabag\CoreBundle\Entity\Entry;
+use Wallabag\UserBundle\Entity\User;
+
+class CleanDuplicatesCommand extends ContainerAwareCommand
+{
+    /** @var OutputInterface */
+    protected $output;
+
+    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)
+    {
+        $this->output = $output;
+
+        $username = $input->getArgument('username');
+
+        if ($username) {
+            try {
+                $user = $this->getUser($username);
+                $this->cleanDuplicates($user);
+            } catch (NoResultException $e) {
+                $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
+
+                return 1;
+            }
+        } else {
+            $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
+
+            $output->writeln(sprintf('Cleaning through %d user accounts', count($users)));
+
+            foreach ($users as $user) {
+                $output->writeln(sprintf('Processing user %s', $user->getUsername()));
+                $this->cleanDuplicates($user);
+            }
+            $output->writeln(sprintf('Finished cleaning. %d duplicates found in total', $this->duplicates));
+        }
+
+        return 0;
+    }
+
+    /**
+     * @param User $user
+     */
+    private function cleanDuplicates(User $user)
+    {
+        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
+        $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
+
+        $entries = $repo->getAllEntriesIdAndUrl($user->getId());
+
+        $duplicatesCount = 0;
+        $urls = [];
+        foreach ($entries as $entry) {
+            $url = $this->similarUrl($entry['url']);
+
+            /* @var $entry Entry */
+            if (in_array($url, $urls)) {
+                ++$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;
+
+        $this->output->writeln(sprintf('Cleaned %d duplicates for user %s', $duplicatesCount, $user->getUserName()));
+    }
+
+    private function similarUrl($url)
+    {
+        if (in_array(substr($url, -1), ['/', '#'])) { // get rid of "/" and "#" and the end of urls
+            return substr($url, 0, strlen($url));
+        }
+
+        return $url;
+    }
+
+    /**
+     * Fetches a user from its username.
+     *
+     * @param string $username
+     *
+     * @return \Wallabag\UserBundle\Entity\User
+     */
+    private function getUser($username)
+    {
+        return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
+    }
+
+    private function getDoctrine()
+    {
+        return $this->getContainer()->get('doctrine');
+    }
+}
index 1f22e901ba4d537a2e757f9901a03987bc0b5bcf..5e7b0d3a2d0884be216e03654f77893196d860f2 100644 (file)
@@ -379,4 +379,17 @@ class EntryRepository extends EntityRepository
             ->setParameter('userId', $userId)
             ->execute();
     }
+
+    /**
+     * Get id and url from all entries
+     * Used for the clean-duplicates command.
+     */
+    public function getAllEntriesIdAndUrl($userId)
+    {
+        $qb = $this->createQueryBuilder('e')
+            ->select('e.id, e.url')
+            ->where('e.user = :userid')->setParameter(':userid', $userId);
+
+        return $qb->getQuery()->getArrayResult();
+    }
 }