3 namespace Wallabag\CoreBundle\Command
;
5 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
6 use Symfony\Component\Config\Definition\Exception\Exception
;
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\Helper\ProgressBar
;
11 use Wallabag\CoreBundle\Entity\Entry
;
12 use Wallabag\CoreBundle\Tools\Utils
;
14 class ImportCommand
extends ContainerAwareCommand
16 protected function configure()
19 ->setName('wallabag:import')
20 ->setDescription('Import entries from JSON file')
23 InputArgument
::REQUIRED
,
28 protected function execute(InputInterface
$input, OutputInterface
$output)
30 $now = new \
DateTime();
31 $output->writeln('<comment>Start : '.$now->format('d-m-Y G:i:s').' ---</comment>');
33 // Importing CSV on DB via Doctrine ORM
34 $this->import($input, $output);
36 $now = new \
DateTime();
37 $output->writeln('<comment>End : '.$now->format('d-m-Y G:i:s').' ---</comment>');
40 protected function import(InputInterface
$input, OutputInterface
$output)
42 // Getting php array of data from CSV
43 $data = $this->get($input, $output);
45 $em = $this->getContainer()->get('doctrine')->getManager();
46 // Turning off doctrine default logs queries for saving memory
47 $em->getConnection()->getConfiguration()->setSQLLogger(null);
49 // Define the size of record, the frequency for persisting the data and the current index of records
54 $user = $em->getRepository('WallabagUserBundle:User')
55 ->findOneById($input->getArgument('userId'));
57 if (!is_object($user)) {
58 throw new Exception('User not found');
61 $progress = new ProgressBar($output, $size);
64 foreach ($data as $object) {
65 $array = (array) $object;
66 $entry = $em->getRepository('WallabagCoreBundle:Entry')
67 ->findOneByUrl($array['url']);
69 if (!is_object($entry)) {
70 $entry = new Entry($user);
71 $entry->setUrl($array['url']);
74 $entry->setTitle($array['title']);
75 $entry->setArchived($array['is_read']);
76 $entry->setStarred($array['is_fav']);
77 $entry->setContent($array['content']);
78 $entry->setReadingTime(Utils
::getReadingTime($array['content']));
82 if (($i %
$batchSize) === 0) {
84 $progress->advance($batchSize);
86 $now = new \
DateTime();
87 $output->writeln(' of entries imported ... | '.$now->format('d-m-Y G:i:s'));
97 protected function convert($filename)
99 if (!file_exists($filename) || !is_readable($filename)) {
106 if (($handle = fopen($filename, 'r')) !== false) {
107 while (($row = fgets($handle)) !== false) {
108 $data = json_decode($row);
116 protected function get(InputInterface
$input, OutputInterface
$output)
118 $filename = __DIR__
.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json';
120 $data = $this->convert($filename);