aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Command
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Command')
-rw-r--r--src/Wallabag/ImportBundle/Command/ImportCommand.php124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Command/ImportCommand.php b/src/Wallabag/ImportBundle/Command/ImportCommand.php
new file mode 100644
index 00000000..3fb8927d
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Command/ImportCommand.php
@@ -0,0 +1,124 @@
1<?php
2
3namespace Wallabag\ImportBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6use Symfony\Component\Config\Definition\Exception\Exception;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
10use Symfony\Component\Console\Helper\ProgressBar;
11use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Tools\Utils;
13
14class ImportCommand extends ContainerAwareCommand
15{
16 protected function configure()
17 {
18 $this
19 ->setName('wallabag:import')
20 ->setDescription('Import entries from JSON file')
21 ->addArgument(
22 'userId',
23 InputArgument::REQUIRED,
24 'user ID to populate'
25 );
26 }
27
28 protected function execute(InputInterface $input, OutputInterface $output)
29 {
30 $now = new \DateTime();
31 $output->writeln('<comment>Start : '.$now->format('d-m-Y G:i:s').' ---</comment>');
32
33 // Importing CSV on DB via Doctrine ORM
34 $this->import($input, $output);
35
36 $now = new \DateTime();
37 $output->writeln('<comment>End : '.$now->format('d-m-Y G:i:s').' ---</comment>');
38 }
39
40 protected function import(InputInterface $input, OutputInterface $output)
41 {
42 // Getting php array of data from CSV
43 $data = $this->get($input, $output);
44
45 $em = $this->getContainer()->get('doctrine')->getManager();
46 // Turning off doctrine default logs queries for saving memory
47 $em->getConnection()->getConfiguration()->setSQLLogger(null);
48
49 // Define the size of record, the frequency for persisting the data and the current index of records
50 $size = count($data);
51 $batchSize = 20;
52 $i = 1;
53
54 $user = $em->getRepository('WallabagUserBundle:User')
55 ->findOneById($input->getArgument('userId'));
56
57 if (!is_object($user)) {
58 throw new Exception('User not found');
59 }
60
61 $progress = new ProgressBar($output, $size);
62 $progress->start();
63
64 foreach ($data as $object) {
65 $array = (array) $object;
66 $entry = $em->getRepository('WallabagCoreBundle:Entry')
67 ->findOneByUrl($array['url']);
68
69 if (!is_object($entry)) {
70 $entry = new Entry($user);
71 $entry->setUrl($array['url']);
72 }
73
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']));
79
80 $em->persist($entry);
81
82 if (($i % $batchSize) === 0) {
83 $em->flush();
84 $progress->advance($batchSize);
85
86 $now = new \DateTime();
87 $output->writeln(' of entries imported ... | '.$now->format('d-m-Y G:i:s'));
88 }
89 ++$i;
90 }
91
92 $em->flush();
93 $em->clear();
94 $progress->finish();
95 }
96
97 protected function convert($filename)
98 {
99 if (!file_exists($filename) || !is_readable($filename)) {
100 return false;
101 }
102
103 $header = null;
104 $data = array();
105
106 if (($handle = fopen($filename, 'r')) !== false) {
107 while (($row = fgets($handle)) !== false) {
108 $data = json_decode($row);
109 }
110 fclose($handle);
111 }
112
113 return $data;
114 }
115
116 protected function get(InputInterface $input, OutputInterface $output)
117 {
118 $filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json';
119
120 $data = $this->convert($filename);
121
122 return $data;
123 }
124}