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