3 namespace Wallabag\CoreBundle\Command
;
5 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
6 use Symfony\Component\Console\Input\InputArgument
;
7 use Symfony\Component\Console\Input\InputInterface
;
8 use Symfony\Component\Console\Output\OutputInterface
;
9 use Symfony\Component\Console\Helper\ProgressBar
;
10 use Wallabag\CoreBundle\Entity\Entry
;
11 use Wallabag\CoreBundle\Tools\Utils
;
13 class ImportCommand
extends ContainerAwareCommand
15 protected function configure()
18 ->setName('import:json')
19 ->setDescription('Import entries from JSON file')
22 InputArgument
::REQUIRED
,
27 protected function execute(InputInterface
$input, OutputInterface
$output)
29 $now = new \
DateTime();
30 $output->writeln('<comment>Start : '.$now->format('d-m-Y G:i:s').' ---</comment>');
32 // Importing CSV on DB via Doctrine ORM
33 $this->import($input, $output);
35 $now = new \
DateTime();
36 $output->writeln('<comment>End : '.$now->format('d-m-Y G:i:s').' ---</comment>');
39 protected function import(InputInterface
$input, OutputInterface
$output)
41 $userId = $input->getArgument('userId');
43 // Getting php array of data from CSV
44 $data = $this->get($input, $output);
46 $em = $this->getContainer()->get('doctrine')->getManager();
47 // Turning off doctrine default logs queries for saving memory
48 $em->getConnection()->getConfiguration()->setSQLLogger(null);
50 // Define the size of record, the frequency for persisting the data and the current index of records
55 $progress = new ProgressBar($output, $size);
58 $user = $em->getRepository('WallabagCoreBundle:User')
59 ->findOneById($userId);
61 foreach ($data as $object) {
62 $array = (array) $object;
63 $entry = $em->getRepository('WallabagCoreBundle:Entry')
64 ->findOneByUrl($array['url']);
66 if (!is_object($entry)) {
67 $entry = new Entry($user);
68 $entry->setUrl($array['url']);
71 $entry->setTitle($array['title']);
72 $entry->setArchived($array['is_read']);
73 $entry->setStarred($array['is_fav']);
74 $entry->setContent($array['content']);
75 $entry->setReadingTime(Utils
::getReadingTime($array['content']));
79 if (($i %
$batchSize) === 0) {
81 $progress->advance($batchSize);
83 $now = new \
DateTime();
84 $output->writeln(' of entries imported ... | '.$now->format('d-m-Y G:i:s'));
94 protected function convert($filename)
96 if (!file_exists($filename) || !is_readable($filename)) {
103 if (($handle = fopen($filename, 'r')) !== false) {
104 while (($row = fgets($handle)) !== false) {
105 $data = json_decode($row);
113 protected function get(InputInterface
$input, OutputInterface
$output)
115 $fileName = 'web/uploads/import/import.json';
116 $data = $this->convert($fileName);