3 namespace Wallabag\CoreBundle\Command
;
5 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
6 use Symfony\Component\Console\Input\InputInterface
;
7 use Symfony\Component\Console\Output\OutputInterface
;
8 use Symfony\Component\Console\Helper\ProgressBar
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Tools\Utils
;
12 class ImportCommand
extends ContainerAwareCommand
14 protected function configure()
17 ->setName('import:json')
18 ->setDescription('Import entries from JSON file');
21 protected function execute(InputInterface
$input, OutputInterface
$output)
23 $now = new \
DateTime();
24 $output->writeln('<comment>Start : '.$now->format('d-m-Y G:i:s').' ---</comment>');
26 // Importing CSV on DB via Doctrine ORM
27 $this->import($input, $output);
29 $now = new \
DateTime();
30 $output->writeln('<comment>End : '.$now->format('d-m-Y G:i:s').' ---</comment>');
33 protected function import(InputInterface
$input, OutputInterface
$output)
35 // Getting php array of data from CSV
36 $data = $this->get($input, $output);
38 $em = $this->getContainer()->get('doctrine')->getManager();
39 // Turning off doctrine default logs queries for saving memory
40 $em->getConnection()->getConfiguration()->setSQLLogger(null);
42 // Define the size of record, the frequency for persisting the data and the current index of records
47 $progress = new ProgressBar($output, $size);
50 $user = $em->getRepository('WallabagCoreBundle:User')
53 foreach ($data as $object) {
54 $array = (array) $object;
55 $entry = $em->getRepository('WallabagCoreBundle:Entry')
56 ->findOneByUrl($array['url']);
58 if (!is_object($entry)) {
59 $entry = new Entry($user);
60 $entry->setUrl($array['url']);
63 $entry->setTitle($array['title']);
64 $entry->setArchived($array['is_read']);
65 $entry->setStarred($array['is_fav']);
66 $entry->setContent($array['content']);
67 $entry->setReadingTime(Utils
::getReadingTime($array['content']));
71 if (($i %
$batchSize) === 0) {
73 $progress->advance($batchSize);
75 $now = new \
DateTime();
76 $output->writeln(' of entries imported ... | '.$now->format('d-m-Y G:i:s'));
86 protected function convert($filename)
88 if (!file_exists($filename) || !is_readable($filename)) {
95 if (($handle = fopen($filename, 'r')) !== false) {
96 while (($row = fgets($handle)) !== false) {
97 $data = json_decode($row);
105 protected function get(InputInterface
$input, OutputInterface
$output)
107 $fileName = 'web/uploads/import/import.json';
108 $data = $this->convert($fileName);