*
* @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"}
* }
* )
*
{
$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]);
--- /dev/null
+<?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');
+ }
+}
$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');
* 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()
*/
private $url;
+ /**
+ * @var string
+ *
+ * @ORM\Column(name="hashedurl", type="text", nullable=true)
+ */
+ private $hashedUrl;
+
/**
* @var bool
*
return $this;
}
+
+ /**
+ * @return string
+ */
+ public function getHashedUrl()
+ {
+ return $this->hashedUrl;
+ }
+
+ /**
+ * @param mixed $hashedUrl
+ *
+ * @return Entry
+ */
+ public function setHashedUrl($hashedUrl)
+ {
+ $this->hashedUrl = $hashedUrl;
+
+ return $this;
+ }
}
}
$entry->setUrl($content['url'] ?: $url);
+ $entry->setHashedUrl(hash('sha512', $entry->getUrl()));
$entry->setTitle($title);
$entry->setContent($html);
$entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
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());
{
$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());
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());
}