mirror of
https://github.com/wallabag/wallabag.git
synced 2025-08-26 18:21:02 +00:00
Hash the urls to check if they exist
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
3620dae1e6
commit
bfe02a0b48
7 changed files with 306 additions and 14 deletions
95
src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
Normal file
95
src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
Normal file
|
@ -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');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue