]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/ImportCommand.php
First test on PocketImport
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / ImportCommand.php
CommitLineData
56ea1de9
NL
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8c3c77c1 6use Symfony\Component\Config\Definition\Exception\Exception;
a1bb1b3c 7use Symfony\Component\Console\Input\InputArgument;
56ea1de9
NL
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
8c3c77c1 19 ->setName('wallabag:import')
a1bb1b3c
NL
20 ->setDescription('Import entries from JSON file')
21 ->addArgument(
22 'userId',
23 InputArgument::REQUIRED,
24 'user ID to populate'
25 );
56ea1de9
NL
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
d275bdf4 54 $user = $em->getRepository('WallabagUserBundle:User')
8c3c77c1
NL
55 ->findOneById($input->getArgument('userId'));
56
57 if (!is_object($user)) {
58 throw new Exception('User not found');
59 }
60
56ea1de9
NL
61 $progress = new ProgressBar($output, $size);
62 $progress->start();
63
56ea1de9
NL
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 {
8c3c77c1 118 $filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json';
d275bdf4 119
8c3c77c1 120 $data = $this->convert($filename);
56ea1de9
NL
121
122 return $data;
123 }
124}