]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #3158 from wallabag/hash-exist-url
authorJérémy Benoist <j0k3r@users.noreply.github.com>
Thu, 25 Apr 2019 11:28:09 +0000 (13:28 +0200)
committerGitHub <noreply@github.com>
Thu, 25 Apr 2019 11:28:09 +0000 (13:28 +0200)
Hash exist url

app/DoctrineMigrations/Version20190401105353.php [new file with mode: 0644]
src/Wallabag/ApiBundle/Controller/EntryRestController.php
src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php [new file with mode: 0644]

diff --git a/app/DoctrineMigrations/Version20190401105353.php b/app/DoctrineMigrations/Version20190401105353.php
new file mode 100644 (file)
index 0000000..d27962d
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+namespace Application\Migrations;
+
+use Doctrine\DBAL\Schema\Schema;
+use Wallabag\CoreBundle\Doctrine\WallabagMigration;
+
+/**
+ * Add hashed_url in entry.
+ */
+class Version20190401105353 extends WallabagMigration
+{
+    /**
+     * @param Schema $schema
+     */
+    public function up(Schema $schema)
+    {
+        $entryTable = $schema->getTable($this->getTable('entry'));
+
+        $this->skipIf($entryTable->hasColumn('hashed_url'), 'It seems that you already played this migration.');
+
+        $entryTable->addColumn('hashed_url', 'text', [
+            'length' => 40,
+            'notnull' => false,
+        ]);
+
+        $entryTable->addIndex(['user_id', 'hashed_url'], 'hashed_url_user_id', [], ['lengths' => [null, 40]]);
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function down(Schema $schema)
+    {
+        $entryTable = $schema->getTable($this->getTable('entry'));
+
+        $this->skipIf(!$entryTable->hasColumn('hashed_url'), 'It seems that you already played this migration.');
+
+        $entryTable->dropIndex('hashed_url_user_id');
+        $entryTable->dropColumn('hashed_url');
+    }
+}
index 5c85009172ff67fd4cc9f1c567654a81e54996e9..06520af917bc0745f58efe753fb34e3639b359dd 100644 (file)
@@ -27,8 +27,10 @@ class EntryRestController extends WallabagRestController
      * @ApiDoc(
      *       parameters={
      *          {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"},
-     *          {"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"="url", "dataType"="string", "required"=true, "format"="An url", "description"="DEPRECATED, use hashed_url instead"},
+     *          {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="DEPRECATED, use hashed_urls instead"},
+     *          {"name"="hashed_url", "dataType"="string", "required"=false, "format"="A hashed url", "description"="Hashed url using SHA1 to check if it exists"},
+     *          {"name"="hashed_urls", "dataType"="string", "required"=false, "format"="An array of hashed urls (?hashed_urls[]=xxx...&hashed_urls[]=xxx...)", "description"="An array of hashed urls using SHA1 to check if they exist"}
      *       }
      * )
      *
@@ -37,17 +39,30 @@ class EntryRestController extends WallabagRestController
     public function getEntriesExistsAction(Request $request)
     {
         $this->validateAuthentication();
+        $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
 
         $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id');
+
         $urls = $request->query->get('urls', []);
+        $hashedUrls = $request->query->get('hashed_urls', []);
 
         // handle multiple urls first
+        if (!empty($hashedUrls)) {
+            $results = [];
+            foreach ($hashedUrls as $hashedUrl) {
+                $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId());
+
+                $results[$hashedUrl] = $this->returnExistInformation($res, $returnId);
+            }
+
+            return $this->sendResponse($results);
+        }
+
+        // @deprecated, to be remove in 3.0
         if (!empty($urls)) {
             $results = [];
             foreach ($urls as $url) {
-                $res = $this->getDoctrine()
-                    ->getRepository('WallabagCoreBundle:Entry')
-                    ->findByUrlAndUserId($url, $this->getUser()->getId());
+                $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId());
 
                 $results[$url] = $this->returnExistInformation($res, $returnId);
             }
@@ -57,18 +72,21 @@ class EntryRestController extends WallabagRestController
 
         // let's see if it is a simple url?
         $url = $request->query->get('url', '');
+        $hashedUrl = $request->query->get('hashed_url', '');
 
-        if (empty($url)) {
+        if (empty($url) && 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());
+        $method = 'findByUrlAndUserId';
+        if (!empty($hashedUrl)) {
+            $method = 'findByHashedUrlAndUserId';
+            $url = $hashedUrl;
+        }
 
-        $exists = $this->returnExistInformation($res, $returnId);
+        $res = $repo->$method($url, $this->getUser()->getId());
 
-        return $this->sendResponse(['exists' => $exists]);
+        return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]);
     }
 
     /**
diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
new file mode 100644 (file)
index 0000000..45bd8c5
--- /dev/null
@@ -0,0 +1,98 @@
+<?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 = (string) $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 "%d" users', \count($users)));
+
+            foreach ($users as $user) {
+                $output->writeln(sprintf('Processing user: %s', $user->getUsername()));
+                $this->generateHashedUrls($user);
+            }
+            $output->writeln('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());
+
+        $i = 1;
+        foreach ($entries as $entry) {
+            $entry->setHashedUrl(hash('sha1', $entry->getUrl()));
+            $em->persist($entry);
+
+            if (0 === ($i % 20)) {
+                $em->flush();
+            }
+            ++$i;
+        }
+
+        $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 b3cfdc4a4c26dd13f979cb0782ba8a96592d453c..c3fb87d218630875e77047002fad2bad55b091d1 100644 (file)
@@ -25,7 +25,8 @@ use Wallabag\UserBundle\Entity\User;
  *     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="hashed_url_user_id", columns={"user_id", "hashed_url"}, options={"lengths"={null, 40}})
  *     }
  * )
  * @ORM\HasLifecycleCallbacks()
@@ -75,6 +76,13 @@ class Entry
      */
     private $url;
 
+    /**
+     * @var string
+     *
+     * @ORM\Column(name="hashed_url", type="string", length=40, nullable=true)
+     */
+    private $hashedUrl;
+
     /**
      * @var bool
      *
@@ -316,6 +324,7 @@ class Entry
     public function setUrl($url)
     {
         $this->url = $url;
+        $this->hashedUrl = hash('sha1', $url);
 
         return $this;
     }
@@ -911,4 +920,24 @@ class Entry
     {
         return $this->originUrl;
     }
+
+    /**
+     * @return string
+     */
+    public function getHashedUrl()
+    {
+        return $this->hashedUrl;
+    }
+
+    /**
+     * @param mixed $hashedUrl
+     *
+     * @return Entry
+     */
+    public function setHashedUrl($hashedUrl)
+    {
+        $this->hashedUrl = $hashedUrl;
+
+        return $this;
+    }
 }
index 45366623d23204e8d8a1ffd3bfd9b1fd1c2ca832..f5089729643f94c67584445a4a0814c831949c0e 100644 (file)
@@ -346,6 +346,30 @@ class EntryRepository extends EntityRepository
         return false;
     }
 
+    /**
+     * Find an entry by its hashed url and its owner.
+     * If it exists, return the entry otherwise return false.
+     *
+     * @param string $hashedUrl Url hashed using sha1
+     * @param int    $userId
+     *
+     * @return Entry|bool
+     */
+    public function findByHashedUrlAndUserId($hashedUrl, $userId)
+    {
+        $res = $this->createQueryBuilder('e')
+            ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl)
+            ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
+            ->getQuery()
+            ->getResult();
+
+        if (\count($res)) {
+            return current($res);
+        }
+
+        return false;
+    }
+
     /**
      * Count all entries for a user.
      *
index 2151f587e7925fe3bfe340e54e7f23d11c4087cf..8cc12ed379487ab3546416026cbb601112e61fd1 100644 (file)
@@ -971,33 +971,49 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
     }
 
-    public function testGetEntriesExistsWithReturnId()
+    public function dataForEntriesExistWithUrl()
     {
-        $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1');
+        $url = hash('sha1', 'http://0.0.0.0/entry2');
 
-        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
-
-        $content = json_decode($this->client->getResponse()->getContent(), true);
-
-        // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
-        $this->assertGreaterThan(1, $content['exists']);
+        return [
+            'with_id' => [
+                'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
+                'expectedValue' => 2,
+            ],
+            'without_id' => [
+                'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
+                'expectedValue' => true,
+            ],
+            'hashed_url_with_id' => [
+                'url' => '/api/entries/exists?hashed_url=' . $url . '&return_id=1',
+                'expectedValue' => 2,
+            ],
+            'hashed_url_without_id' => [
+                'url' => '/api/entries/exists?hashed_url=' . $url . '',
+                'expectedValue' => true,
+            ],
+        ];
     }
 
-    public function testGetEntriesExistsWithoutReturnId()
+    /**
+     * @dataProvider dataForEntriesExistWithUrl
+     */
+    public function testGetEntriesExists($url, $expectedValue)
     {
-        $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
+        $this->client->request('GET', $url);
 
         $this->assertSame(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
-        $this->assertTrue($content['exists']);
+        $this->assertSame($expectedValue, $content['exists']);
     }
 
     public function testGetEntriesExistsWithManyUrls()
     {
         $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 . '&return_id=1');
 
         $this->assertSame(200, $this->client->getResponse()->getStatusCode());
@@ -1027,6 +1043,38 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertFalse($content[$url2]);
     }
 
+    public function testGetEntriesExistsWithManyUrlsHashed()
+    {
+        $url1 = 'http://0.0.0.0/entry2';
+        $url2 = 'http://0.0.0.0/entry10';
+        $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2) . '&return_id=1');
+
+        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertArrayHasKey(hash('sha1', $url1), $content);
+        $this->assertArrayHasKey(hash('sha1', $url2), $content);
+        $this->assertSame(2, $content[hash('sha1', $url1)]);
+        $this->assertNull($content[hash('sha1', $url2)]);
+    }
+
+    public function testGetEntriesExistsWithManyUrlsHashedReturnBool()
+    {
+        $url1 = 'http://0.0.0.0/entry2';
+        $url2 = 'http://0.0.0.0/entry10';
+        $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2));
+
+        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertArrayHasKey(hash('sha1', $url1), $content);
+        $this->assertArrayHasKey(hash('sha1', $url2), $content);
+        $this->assertTrue($content[hash('sha1', $url1)]);
+        $this->assertFalse($content[hash('sha1', $url2)]);
+    }
+
     public function testGetEntriesExistsWhichDoesNotExists()
     {
         $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
@@ -1038,6 +1086,17 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertFalse($content['exists']);
     }
 
+    public function testGetEntriesExistsWhichDoesNotExistsWithHashedUrl()
+    {
+        $this->client->request('GET', '/api/entries/exists?hashed_url=' . hash('sha1', 'http://google.com/entry2'));
+
+        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
+
+        $content = json_decode($this->client->getResponse()->getContent(), true);
+
+        $this->assertFalse($content['exists']);
+    }
+
     public function testGetEntriesExistsWithNoUrl()
     {
         $this->client->request('GET', '/api/entries/exists?url=');
@@ -1045,6 +1104,13 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $this->assertSame(403, $this->client->getResponse()->getStatusCode());
     }
 
+    public function testGetEntriesExistsWithNoHashedUrl()
+    {
+        $this->client->request('GET', '/api/entries/exists?hashed_url=');
+
+        $this->assertSame(403, $this->client->getResponse()->getStatusCode());
+    }
+
     public function testReloadEntryErrorWhileFetching()
     {
         $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
diff --git a/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php b/tests/Wallabag/CoreBundle/Command/GenerateUrlHashesCommandTest.php
new file mode 100644 (file)
index 0000000..17eed21
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+
+namespace Tests\Wallabag\CoreBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Console\Application;
+use Symfony\Component\Console\Tester\CommandTester;
+use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
+use Wallabag\CoreBundle\Command\GenerateUrlHashesCommand;
+use Wallabag\CoreBundle\Entity\Entry;
+
+class GenerateUrlHashesCommandTest extends WallabagCoreTestCase
+{
+    public function testRunGenerateUrlHashesCommand()
+    {
+        $application = new Application($this->getClient()->getKernel());
+        $application->add(new GenerateUrlHashesCommand());
+
+        $command = $application->find('wallabag:generate-hashed-urls');
+
+        $tester = new CommandTester($command);
+        $tester->execute([
+            'command' => $command->getName(),
+        ]);
+
+        $this->assertContains('Generating hashed urls for "3" users', $tester->getDisplay());
+        $this->assertContains('Finished generated hashed urls', $tester->getDisplay());
+    }
+
+    public function testRunGenerateUrlHashesCommandWithBadUsername()
+    {
+        $application = new Application($this->getClient()->getKernel());
+        $application->add(new GenerateUrlHashesCommand());
+
+        $command = $application->find('wallabag:generate-hashed-urls');
+
+        $tester = new CommandTester($command);
+        $tester->execute([
+            'command' => $command->getName(),
+            'username' => 'unknown',
+        ]);
+
+        $this->assertContains('User "unknown" not found', $tester->getDisplay());
+    }
+
+    public function testRunGenerateUrlHashesCommandForUser()
+    {
+        $application = new Application($this->getClient()->getKernel());
+        $application->add(new GenerateUrlHashesCommand());
+
+        $command = $application->find('wallabag:generate-hashed-urls');
+
+        $tester = new CommandTester($command);
+        $tester->execute([
+            'command' => $command->getName(),
+            'username' => 'admin',
+        ]);
+
+        $this->assertContains('Generated hashed urls for user: admin', $tester->getDisplay());
+    }
+
+    public function testGenerateUrls()
+    {
+        $url = 'http://www.lemonde.fr/sport/visuel/2017/05/05/rondelle-prison-blanchissage-comprendre-le-hockey-sur-glace_5122587_3242.html';
+        $client = $this->getClient();
+        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
+
+        $this->logInAs('admin');
+
+        $user = $em->getRepository('WallabagUserBundle:User')->findOneById($this->getLoggedInUserId());
+
+        $entry1 = new Entry($user);
+        $entry1->setUrl($url);
+
+        $em->persist($entry1);
+        $em->flush();
+
+        $application = new Application($this->getClient()->getKernel());
+        $application->add(new GenerateUrlHashesCommand());
+
+        $command = $application->find('wallabag:generate-hashed-urls');
+
+        $tester = new CommandTester($command);
+        $tester->execute([
+            'command' => $command->getName(),
+            'username' => 'admin',
+        ]);
+
+        $this->assertContains('Generated hashed urls for user: admin', $tester->getDisplay());
+
+        $entry = $em->getRepository('WallabagCoreBundle:Entry')->findOneByUrl($url);
+
+        $this->assertSame($entry->getHashedUrl(), hash('sha1', $url));
+
+        $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url');
+        $query->setParameter('url', $url);
+        $query->execute();
+    }
+}