aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Command/ImportCommand.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Command/ImportCommand.php')
-rw-r--r--src/Wallabag/ImportBundle/Command/ImportCommand.php107
1 files changed, 18 insertions, 89 deletions
diff --git a/src/Wallabag/ImportBundle/Command/ImportCommand.php b/src/Wallabag/ImportBundle/Command/ImportCommand.php
index 3fb8927d..dfbfc2f7 100644
--- a/src/Wallabag/ImportBundle/Command/ImportCommand.php
+++ b/src/Wallabag/ImportBundle/Command/ImportCommand.php
@@ -7,118 +7,47 @@ use Symfony\Component\Config\Definition\Exception\Exception;
7use Symfony\Component\Console\Input\InputArgument; 7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface; 8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface; 9use Symfony\Component\Console\Output\OutputInterface;
10use Symfony\Component\Console\Helper\ProgressBar;
11use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Tools\Utils;
13 10
14class ImportCommand extends ContainerAwareCommand 11class ImportCommand extends ContainerAwareCommand
15{ 12{
16 protected function configure() 13 protected function configure()
17 { 14 {
18 $this 15 $this
19 ->setName('wallabag:import') 16 ->setName('wallabag:import-v1')
20 ->setDescription('Import entries from JSON file') 17 ->setDescription('Import entries from a JSON export from a wallabag v1 instance')
21 ->addArgument( 18 ->addArgument('userId', InputArgument::REQUIRED, 'User ID to populate')
22 'userId', 19 ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file')
23 InputArgument::REQUIRED, 20 ;
24 'user ID to populate'
25 );
26 } 21 }
27 22
28 protected function execute(InputInterface $input, OutputInterface $output) 23 protected function execute(InputInterface $input, OutputInterface $output)
29 { 24 {
30 $now = new \DateTime(); 25 $output->writeln('Start : '.(new \DateTime())->format('d-m-Y G:i:s').' ---');
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 26
45 $em = $this->getContainer()->get('doctrine')->getManager(); 27 $em = $this->getContainer()->get('doctrine')->getManager();
46 // Turning off doctrine default logs queries for saving memory 28 // Turning off doctrine default logs queries for saving memory
47 $em->getConnection()->getConfiguration()->setSQLLogger(null); 29 $em->getConnection()->getConfiguration()->setSQLLogger(null);
48 30
49 // Define the size of record, the frequency for persisting the data and the current index of records 31 $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('userId'));
50 $size = count($data);
51 $batchSize = 20;
52 $i = 1;
53
54 $user = $em->getRepository('WallabagUserBundle:User')
55 ->findOneById($input->getArgument('userId'));
56 32
57 if (!is_object($user)) { 33 if (!is_object($user)) {
58 throw new Exception('User not found'); 34 throw new Exception(sprintf('User with id "%s" not found', $input->getArgument('userId')));
59 } 35 }
60 36
61 $progress = new ProgressBar($output, $size); 37 $wallabag = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
62 $progress->start(); 38 $res = $wallabag
39 ->setUser($user)
40 ->setFilepath($input->getArgument('filepath'))
41 ->import();
63 42
64 foreach ($data as $object) { 43 if (true === $res) {
65 $array = (array) $object; 44 $summary = $wallabag->getSummary();
66 $entry = $em->getRepository('WallabagCoreBundle:Entry') 45 $output->writeln('<info>'.$summary['imported'].' imported</info>');
67 ->findOneByUrl($array['url']); 46 $output->writeln('<comment>'.$summary['skipped'].' already saved</comment>');
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 } 47 }
91 48
92 $em->flush();
93 $em->clear(); 49 $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 50
122 return $data; 51 $output->writeln('End : '.(new \DateTime())->format('d-m-Y G:i:s').' ---');
123 } 52 }
124} 53}