3 namespace Wallabag\CoreBundle\Command
;
5 use Doctrine\ORM\NoResultException
;
6 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
7 use Symfony\Component\Console\Input\InputArgument
;
8 use Symfony\Component\Console\Input\InputInterface
;
9 use Symfony\Component\Console\Output\OutputInterface
;
10 use Symfony\Component\Console\Output\StreamOutput
;
12 class ExportCommand
extends ContainerAwareCommand
14 protected function configure()
17 ->setName('wallabag:export')
18 ->setDescription('Export all entries for an user')
19 ->setHelp('This command helps you to export all entries for an user')
22 InputArgument
::REQUIRED
,
23 'User from which to export entries'
27 InputArgument
::OPTIONAL
,
28 'Path of the exported file'
33 protected function execute(InputInterface
$input, OutputInterface
$output)
36 $user = $this->getUser($input->getArgument('username'));
37 } catch (NoResultException
$e) {
38 $output->writeln(sprintf('<error>User "%s" not found.</error>', $input->getArgument('username')));
41 $entries = $this->getDoctrine()
42 ->getRepository('WallabagCoreBundle:Entry')
43 ->getBuilderForAllByUser($user->getId())
47 $output->write(sprintf('Exporting %d entrie(s) for user « <comment>%s</comment> »... ', count($entries), $user->getUserName()));
49 $filePath = $input->getArgument('filepath');
51 $filePath = $this->getContainer()->getParameter('kernel.root_dir') . '/../' . sprintf('%s-export', $user->getUsername());
54 $data = $this->getContainer()->get('wallabag_core.helper.entries_export')
55 ->setEntries($entries)
58 file_put_contents($filePath, $data);
59 } catch (\InvalidArgumentException
$e) {
60 $output->writeln(sprintf('<error>Error: "%s"</error>', $e->getMessage()));
63 $output->writeln('<info>Done.</info>');
67 * Fetches a user from its username.
69 * @param string $username
71 * @return \Wallabag\UserBundle\Entity\User
73 private function getUser($username)
75 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
78 private function getDoctrine()
80 return $this->getContainer()->get('doctrine');