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