Keep url in exists endpoint

- Add migration
- Use md5 instead of sha512 (we don't need security here, just a hash)
- Update tests
This commit is contained in:
Jeremy Benoist
2019-04-01 11:50:33 +02:00
parent bfe02a0b48
commit 9c2b2aae70
8 changed files with 155 additions and 78 deletions

View File

@ -45,13 +45,13 @@ 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;
@ -67,13 +67,20 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
$entries = $repo->findByUser($user->getId());
$i = 1;
foreach ($entries as $entry) {
$entry->setHashedUrl(hash('sha512', $entry->getUrl()));
$entry->setHashedUrl(hash('md5', $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()));
}
/**

View File

@ -30,7 +30,6 @@ class EntryFixtures extends Fixture implements DependentFixtureInterface
'entry2' => [
'user' => 'admin-user',
'url' => 'http://0.0.0.0/entry2',
'hashed_url' => hash('md5', 'http://0.0.0.0/entry2'),
'reading_time' => 1,
'domain' => 'domain.io',
'mime' => 'text/html',
@ -90,6 +89,7 @@ class EntryFixtures extends Fixture implements DependentFixtureInterface
foreach ($entries as $reference => $item) {
$entry = new Entry($this->getReference($item['user']));
$entry->setUrl($item['url']);
$entry->setHashedUrl(hash('md5', $item['url']));
$entry->setReadingTime($item['reading_time']);
$entry->setDomainName($item['domain']);
$entry->setMimetype($item['mime']);

View File

@ -26,7 +26,7 @@ use Wallabag\UserBundle\Entity\User;
* indexes={
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="uid", columns={"uid"}),
* @ORM\Index(name="hashedurl", columns={"hashedurl"})
* @ORM\Index(name="hashed_url", columns={"hashed_url"})
* }
* )
* @ORM\HasLifecycleCallbacks()
@ -79,7 +79,7 @@ class Entry
/**
* @var string
*
* @ORM\Column(name="hashedurl", type="text", nullable=true)
* @ORM\Column(name="hashed_url", type="string", length=32, nullable=true)
*/
private $hashedUrl;

View 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 $hashedUrl
* @param $userId
*
* @return Entry|bool
*/
public function findByHashedUrlAndUserId($hashedUrl, $userId)
{
$res = $this->createQueryBuilder('e')
->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', urldecode($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.
*