]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
Update deps
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / GenerateUrlHashesCommand.php
index fe2644f2829e4246f5f25caa6435eac2edb9753e..a0e9221e33d5ae673ac54618ad6c5d151f7902b7 100644 (file)
@@ -7,6 +7,7 @@ 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\Helper\UrlHasher;
 use Wallabag\UserBundle\Entity\User;
 
 class GenerateUrlHashesCommand extends ContainerAwareCommand
@@ -20,18 +21,14 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
             ->setName('wallabag:generate-hashed-urls')
             ->setDescription('Generates hashed urls for each entry')
             ->setHelp('This command helps you to generates hashes of the url of each entry, to check through API if an URL is already saved')
-            ->addArgument(
-                'username',
-                InputArgument::OPTIONAL,
-                'User to process entries'
-            );
+            ->addArgument('username', InputArgument::OPTIONAL, 'User to process entries');
     }
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $this->output = $output;
 
-        $username = $input->getArgument('username');
+        $username = (string) $input->getArgument('username');
 
         if ($username) {
             try {
@@ -45,21 +42,18 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
         } else {
             $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
 
-            $output->writeln(sprintf('Generating hashed urls for the %d user account entries', count($users)));
+            $output->writeln(sprintf('Generating hashed urls for "%d" users', \count($users)));
 
             foreach ($users as $user) {
-                $output->writeln(sprintf('Processing user %s', $user->getUsername()));
+                $output->writeln(sprintf('Processing user: %s', $user->getUsername()));
                 $this->generateHashedUrls($user);
             }
-            $output->writeln(sprintf('Finished generated hashed urls'));
+            $output->writeln('Finished generated hashed urls');
         }
 
         return 0;
     }
 
-    /**
-     * @param User $user
-     */
     private function generateHashedUrls(User $user)
     {
         $em = $this->getContainer()->get('doctrine.orm.entity_manager');
@@ -67,13 +61,20 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
 
         $entries = $repo->findByUser($user->getId());
 
+        $i = 1;
         foreach ($entries as $entry) {
-            $entry->setHashedUrl(hash('sha512', $entry->getUrl()));
+            $entry->setHashedUrl(UrlHasher::hashUrl($entry->getUrl()));
             $em->persist($entry);
-            $em->flush();
+
+            if (0 === ($i % 20)) {
+                $em->flush();
+            }
+            ++$i;
         }
 
-        $this->output->writeln(sprintf('Generated hashed urls for user %s', $user->getUserName()));
+        $em->flush();
+
+        $this->output->writeln(sprintf('Generated hashed urls for user: %s', $user->getUserName()));
     }
 
     /**
@@ -81,7 +82,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
      *
      * @param string $username
      *
-     * @return \Wallabag\UserBundle\Entity\User
+     * @return User
      */
     private function getUser($username)
     {