]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Hash the urls to check if they exist
authorThomas Citharel <tcit@tcit.fr>
Sun, 28 May 2017 12:53:04 +0000 (14:53 +0200)
committerThomas Citharel <tcit@tcit.fr>
Sun, 28 May 2017 12:53:04 +0000 (14:53 +0200)
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
src/Wallabag/ApiBundle/Controller/EntryRestController.php
src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php [new file with mode: 0644]
src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/CoreBundle/Helper/ContentProxy.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php

index 4801811d63354a659c814f1b41299a9e8821184c..2b07cb592a353400e06f44fb20b0f2b0c073c7b4 100644 (file)
@@ -21,8 +21,8 @@ class EntryRestController extends WallabagRestController
      *
      * @ApiDoc(
      *       parameters={
-     *          {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
-     *          {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"}
+     *          {"name"="hashedurl", "dataType"="string", "required"=true, "format"="An url", "description"="SHA512 Url to check if it exists"},
+     *          {"name"="hashedurls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="SHA512 Urls (as an array) to check if it exists"}
      *       }
      * )
      *
@@ -32,33 +32,38 @@ class EntryRestController extends WallabagRestController
     {
         $this->validateAuthentication();
 
-        $urls = $request->query->get('urls', []);
+        $hashedUrls = $request->query->get('hashedurls', []);
 
         // handle multiple urls first
-        if (!empty($urls)) {
+        if (!empty($hashedUrls)) {
             $results = [];
-            foreach ($urls as $url) {
+            foreach ($hashedUrls as $hashedUrl) {
                 $res = $this->getDoctrine()
                     ->getRepository('WallabagCoreBundle:Entry')
-                    ->findByUrlAndUserId($url, $this->getUser()->getId());
+                    ->findOneBy([
+                        'hashedUrl' => $hashedUrl,
+                        'user' => $this->getUser()->getId(),
+                    ]);
 
-                $results[$url] = $res instanceof Entry ? $res->getId() : false;
+                $results[$hashedUrl] = $res instanceof Entry ? $res->getId() : false;
             }
 
             return $this->sendResponse($results);
         }
 
         // let's see if it is a simple url?
-        $url = $request->query->get('url', '');
+        $hashedUrl = $request->query->get('hashedurl', '');
 
-        if (empty($url)) {
+        if (empty($hashedUrl)) {
             throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
         }
 
         $res = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($url, $this->getUser()->getId());
-
+            ->findOneBy([
+                'hashedUrl' => $hashedUrl,
+                'user' => $this->getUser()->getId(),
+            ]);
         $exists = $res instanceof Entry ? $res->getId() : false;
 
         return $this->sendResponse(['exists' => $exists]);
diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
new file mode 100644 (file)
index 0000000..fe2644f
--- /dev/null
@@ -0,0 +1,95 @@
+<?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\UserBundle\Entity\User;
+
+class GenerateUrlHashesCommand extends ContainerAwareCommand
+{
+    /** @var OutputInterface */
+    protected $output;
+
+    protected function configure()
+    {
+        $this
+            ->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'
+            );
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $this->output = $output;
+
+        $username = $input->getArgument('username');
+
+        if ($username) {
+            try {
+                $user = $this->getUser($username);
+                $this->generateHashedUrls($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('Generating hashed urls for the %d user account entries', count($users)));
+
+            foreach ($users as $user) {
+                $output->writeln(sprintf('Processing user %s', $user->getUsername()));
+                $this->generateHashedUrls($user);
+            }
+            $output->writeln(sprintf('Finished generated hashed urls'));
+        }
+
+        return 0;
+    }
+
+    /**
+     * @param User $user
+     */
+    private function generateHashedUrls(User $user)
+    {
+        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
+        $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
+
+        $entries = $repo->findByUser($user->getId());
+
+        foreach ($entries as $entry) {
+            $entry->setHashedUrl(hash('sha512', $entry->getUrl()));
+            $em->persist($entry);
+            $em->flush();
+        }
+
+        $this->output->writeln(sprintf('Generated hashed urls for user %s', $user->getUserName()));
+    }
+
+    /**
+     * 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 fedad00993f6b3b5ff354f72c74bcea76e3e9c46..2288261214fb9c39ac34d70daeefe54e164686ac 100644 (file)
@@ -32,6 +32,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
 
         $entry2 = new Entry($this->getReference('admin-user'));
         $entry2->setUrl('http://0.0.0.0/entry2');
+        $entry2->setHashedUrl(hash('sha512', 'http://0.0.0.0/entry2'));
         $entry2->setReadingTime(1);
         $entry2->setDomainName('domain.io');
         $entry2->setMimetype('text/html');
index 08a67c34b1f00275b1f61b3cf5bed3b734800560..49affe2a3c6660aef3dcdb0ea98df80849512ce9 100644 (file)
@@ -24,7 +24,8 @@ use Wallabag\AnnotationBundle\Entity\Annotation;
  *     options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
  *     indexes={
  *         @ORM\Index(name="created_at", columns={"created_at"}),
- *         @ORM\Index(name="uid", columns={"uid"})
+ *         @ORM\Index(name="uid", columns={"uid"}),
+ *         @ORM\Index(name="hashedurl", columns={"hashedurl"})
  *     }
  * )
  * @ORM\HasLifecycleCallbacks()
@@ -72,6 +73,13 @@ class Entry
      */
     private $url;
 
+    /**
+     * @var string
+     *
+     * @ORM\Column(name="hashedurl", type="text", nullable=true)
+     */
+    private $hashedUrl;
+
     /**
      * @var bool
      *
@@ -763,4 +771,24 @@ class Entry
 
         return $this;
     }
+
+    /**
+     * @return string
+     */
+    public function getHashedUrl()
+    {
+        return $this->hashedUrl;
+    }
+
+    /**
+     * @param mixed $hashedUrl
+     *
+     * @return Entry
+     */
+    public function setHashedUrl($hashedUrl)
+    {
+        $this->hashedUrl = $hashedUrl;
+
+        return $this;
+    }
 }
index 9a08db3d8e81738fdef20fdcd1871d5f4a81d9de..83ecaa660ce9aa8f283694f50f97a35f350a59b9 100644 (file)
@@ -75,6 +75,7 @@ class ContentProxy
         }
 
         $entry->setUrl($content['url'] ?: $url);
+        $entry->setHashedUrl(hash('sha512', $entry->getUrl()));
         $entry->setTitle($title);
         $entry->setContent($html);
         $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
index bf7d373aa94d57f8716ed59f5f6d02f37ccd3b6f..e3a44390aeb7e20c4b14ccf9a2ef74aaa479dbdd 100644 (file)
@@ -686,7 +686,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
 
     public function testGetEntriesExists()
     {
-        $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
+        $this->client->request('GET', '/api/entries/exists?hashedurl=' . hash('sha512', 'http://0.0.0.0/entry2'));
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
 
@@ -699,21 +699,21 @@ class EntryRestControllerTest extends WallabagApiTestCase
     {
         $url1 = 'http://0.0.0.0/entry2';
         $url2 = 'http://0.0.0.0/entry10';
-        $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2);
+        $this->client->request('GET', '/api/entries/exists?hashedurls[]='.hash('sha512',$url1).'&hashedurls[]='.hash('sha512',$url2));
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
-        $this->assertArrayHasKey($url1, $content);
-        $this->assertArrayHasKey($url2, $content);
-        $this->assertEquals(2, $content[$url1]);
-        $this->assertEquals(false, $content[$url2]);
+        $this->assertArrayHasKey(hash('sha512', $url1), $content);
+        $this->assertArrayHasKey(hash('sha512', $url2), $content);
+        $this->assertEquals(2, $content[hash('sha512', $url1)]);
+        $this->assertEquals(false, $content[hash('sha512', $url2)]);
     }
 
     public function testGetEntriesExistsWhichDoesNotExists()
     {
-        $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
+        $this->client->request('GET', '/api/entries/exists?hashedurl='.hash('sha512','http://google.com/entry2'));
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
 
@@ -724,7 +724,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
 
     public function testGetEntriesExistsWithNoUrl()
     {
-        $this->client->request('GET', '/api/entries/exists?url=');
+        $this->client->request('GET', '/api/entries/exists?hashedurl=');
 
         $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
     }