mirror of
https://github.com/wallabag/wallabag.git
synced 2025-01-11 09:25:25 +00:00
Merge pull request #3326 from nclsHart/reload-entry-command
Add reload entry command
This commit is contained in:
commit
54171dd796
4 changed files with 224 additions and 2 deletions
|
@ -71,7 +71,7 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
|
||||||
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
|
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
|
||||||
$repo = $this->getContainer()->get('wallabag_core.entry_repository');
|
$repo = $this->getContainer()->get('wallabag_core.entry_repository');
|
||||||
|
|
||||||
$entries = $repo->getAllEntriesIdAndUrl($user->getId());
|
$entries = $repo->findAllEntriesIdAndUrlByUserId($user->getId());
|
||||||
|
|
||||||
$duplicatesCount = 0;
|
$duplicatesCount = 0;
|
||||||
$urls = [];
|
$urls = [];
|
||||||
|
|
90
src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php
Normal file
90
src/Wallabag/CoreBundle/Command/ReloadEntryCommand.php
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
<?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 Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
use Wallabag\CoreBundle\Event\EntrySavedEvent;
|
||||||
|
|
||||||
|
class ReloadEntryCommand extends ContainerAwareCommand
|
||||||
|
{
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->setName('wallabag:entry:reload')
|
||||||
|
->setDescription('Reload entries')
|
||||||
|
->setHelp('This command reload entries')
|
||||||
|
->addArgument('username', InputArgument::OPTIONAL, 'Reload entries only for the given user')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
{
|
||||||
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
|
||||||
|
$userId = null;
|
||||||
|
if ($username = $input->getArgument('username')) {
|
||||||
|
try {
|
||||||
|
$userId = $this->getContainer()
|
||||||
|
->get('wallabag_user.user_repository')
|
||||||
|
->findOneByUserName($username)
|
||||||
|
->getId();
|
||||||
|
} catch (NoResultException $e) {
|
||||||
|
$io->error(sprintf('User "%s" not found.', $username));
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$entryRepository = $this->getContainer()->get('wallabag_core.entry_repository');
|
||||||
|
$entryIds = $entryRepository->findAllEntriesIdByUserId($userId);
|
||||||
|
|
||||||
|
$nbEntries = count($entryIds);
|
||||||
|
if (!$nbEntries) {
|
||||||
|
$io->success('No entry to reload.');
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$io->note(
|
||||||
|
sprintf(
|
||||||
|
"You're going to reload %s entries. Depending on the number of entry to reload, this could be a very long process.",
|
||||||
|
$nbEntries
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!$io->confirm('Are you sure you want to proceed?')) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$progressBar = $io->createProgressBar($nbEntries);
|
||||||
|
|
||||||
|
$contentProxy = $this->getContainer()->get('wallabag_core.content_proxy');
|
||||||
|
$em = $this->getContainer()->get('doctrine')->getManager();
|
||||||
|
$dispatcher = $this->getContainer()->get('event_dispatcher');
|
||||||
|
|
||||||
|
$progressBar->start();
|
||||||
|
foreach ($entryIds as $entryId) {
|
||||||
|
$entry = $entryRepository->find($entryId);
|
||||||
|
|
||||||
|
$contentProxy->updateEntry($entry, $entry->getUrl());
|
||||||
|
$em->persist($entry);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
|
$dispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
|
||||||
|
$progressBar->advance();
|
||||||
|
|
||||||
|
$em->detach($entry);
|
||||||
|
}
|
||||||
|
$progressBar->finish();
|
||||||
|
|
||||||
|
$io->newLine(2);
|
||||||
|
$io->success('Done.');
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -355,7 +355,7 @@ class EntryRepository extends EntityRepository
|
||||||
* Get id and url from all entries
|
* Get id and url from all entries
|
||||||
* Used for the clean-duplicates command.
|
* Used for the clean-duplicates command.
|
||||||
*/
|
*/
|
||||||
public function getAllEntriesIdAndUrl($userId)
|
public function findAllEntriesIdAndUrlByUserId($userId)
|
||||||
{
|
{
|
||||||
$qb = $this->createQueryBuilder('e')
|
$qb = $this->createQueryBuilder('e')
|
||||||
->select('e.id, e.url')
|
->select('e.id, e.url')
|
||||||
|
@ -364,6 +364,23 @@ class EntryRepository extends EntityRepository
|
||||||
return $qb->getQuery()->getArrayResult();
|
return $qb->getQuery()->getArrayResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $userId
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function findAllEntriesIdByUserId($userId = null)
|
||||||
|
{
|
||||||
|
$qb = $this->createQueryBuilder('e')
|
||||||
|
->select('e.id');
|
||||||
|
|
||||||
|
if (null !== $userId) {
|
||||||
|
$qb->where('e.user = :userid')->setParameter(':userid', $userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $qb->getQuery()->getArrayResult();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find all entries by url and owner.
|
* Find all entries by url and owner.
|
||||||
*
|
*
|
||||||
|
|
115
tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php
Normal file
115
tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
<?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\ReloadEntryCommand;
|
||||||
|
use Wallabag\CoreBundle\Entity\Entry;
|
||||||
|
|
||||||
|
class ReloadEntryCommandTest extends WallabagCoreTestCase
|
||||||
|
{
|
||||||
|
public $url = 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var entry
|
||||||
|
*/
|
||||||
|
public $adminEntry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Entry
|
||||||
|
*/
|
||||||
|
public $bobEntry;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$userRepository = $this->getClient()->getContainer()->get('wallabag_user.user_repository');
|
||||||
|
|
||||||
|
$user = $userRepository->findOneByUserName('admin');
|
||||||
|
$this->adminEntry = new Entry($user);
|
||||||
|
$this->adminEntry->setUrl($this->url);
|
||||||
|
$this->adminEntry->setTitle('title foo');
|
||||||
|
$this->adminEntry->setContent('');
|
||||||
|
$this->getEntityManager()->persist($this->adminEntry);
|
||||||
|
|
||||||
|
$user = $userRepository->findOneByUserName('bob');
|
||||||
|
$this->bobEntry = new Entry($user);
|
||||||
|
$this->bobEntry->setUrl($this->url);
|
||||||
|
$this->bobEntry->setTitle('title foo');
|
||||||
|
$this->bobEntry->setContent('');
|
||||||
|
$this->getEntityManager()->persist($this->bobEntry);
|
||||||
|
|
||||||
|
$this->getEntityManager()->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRunReloadEntryCommand()
|
||||||
|
{
|
||||||
|
$application = new Application($this->getClient()->getKernel());
|
||||||
|
$application->add(new ReloadEntryCommand());
|
||||||
|
|
||||||
|
$command = $application->find('wallabag:entry:reload');
|
||||||
|
$tester = new CommandTester($command);
|
||||||
|
$tester->execute([
|
||||||
|
'command' => $command->getName(),
|
||||||
|
], [
|
||||||
|
'interactive' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$reloadedEntries = $this->getClient()
|
||||||
|
->getContainer()
|
||||||
|
->get('wallabag_core.entry_repository')
|
||||||
|
->findById([$this->adminEntry->getId(), $this->bobEntry->getId()]);
|
||||||
|
|
||||||
|
foreach ($reloadedEntries as $reloadedEntry) {
|
||||||
|
$this->assertNotEmpty($reloadedEntry->getContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertContains('Done', $tester->getDisplay());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRunReloadEntryWithUsernameCommand()
|
||||||
|
{
|
||||||
|
$application = new Application($this->getClient()->getKernel());
|
||||||
|
$application->add(new ReloadEntryCommand());
|
||||||
|
|
||||||
|
$command = $application->find('wallabag:entry:reload');
|
||||||
|
$tester = new CommandTester($command);
|
||||||
|
$tester->execute([
|
||||||
|
'command' => $command->getName(),
|
||||||
|
'username' => 'admin',
|
||||||
|
], [
|
||||||
|
'interactive' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$entryRepository = $this->getClient()->getContainer()->get('wallabag_core.entry_repository');
|
||||||
|
|
||||||
|
$reloadedAdminEntry = $entryRepository->find($this->adminEntry->getId());
|
||||||
|
$this->assertNotEmpty($reloadedAdminEntry->getContent());
|
||||||
|
|
||||||
|
$reloadedBobEntry = $entryRepository->find($this->bobEntry->getId());
|
||||||
|
$this->assertEmpty($reloadedBobEntry->getContent());
|
||||||
|
|
||||||
|
$this->assertContains('Done', $tester->getDisplay());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRunReloadEntryWithoutEntryCommand()
|
||||||
|
{
|
||||||
|
$application = new Application($this->getClient()->getKernel());
|
||||||
|
$application->add(new ReloadEntryCommand());
|
||||||
|
|
||||||
|
$command = $application->find('wallabag:entry:reload');
|
||||||
|
$tester = new CommandTester($command);
|
||||||
|
$tester->execute([
|
||||||
|
'command' => $command->getName(),
|
||||||
|
'username' => 'empty',
|
||||||
|
], [
|
||||||
|
'interactive' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertContains('No entry to reload', $tester->getDisplay());
|
||||||
|
$this->assertNotContains('Done', $tester->getDisplay());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue