]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/ReadabilityImport.php
Convert other imports to Rabbit
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / ReadabilityImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Wallabag\CoreBundle\Entity\Entry;
6 use Wallabag\UserBundle\Entity\User;
7
8 class ReadabilityImport extends AbstractImport
9 {
10 private $skippedEntries = 0;
11 private $importedEntries = 0;
12 private $filepath;
13
14 /**
15 * {@inheritdoc}
16 */
17 public function getName()
18 {
19 return 'Readability';
20 }
21
22 /**
23 * {@inheritdoc}
24 */
25 public function getUrl()
26 {
27 return 'import_readability';
28 }
29
30 /**
31 * {@inheritdoc}
32 */
33 public function getDescription()
34 {
35 return 'import.readability.description';
36 }
37
38 /**
39 * Set file path to the json file.
40 *
41 * @param string $filepath
42 */
43 public function setFilepath($filepath)
44 {
45 $this->filepath = $filepath;
46
47 return $this;
48 }
49
50 /**
51 * {@inheritdoc}
52 */
53 public function getSummary()
54 {
55 return [
56 'skipped' => $this->skippedEntries,
57 'imported' => $this->importedEntries,
58 ];
59 }
60
61 /**
62 * {@inheritdoc}
63 */
64 public function import()
65 {
66 if (!$this->user) {
67 $this->logger->error('ReadabilityImport: user is not defined');
68
69 return false;
70 }
71
72 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
73 $this->logger->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath]);
74
75 return false;
76 }
77
78 $data = json_decode(file_get_contents($this->filepath), true);
79
80 if (empty($data) || empty($data['bookmarks'])) {
81 return false;
82 }
83
84 if ($this->producer) {
85 $this->parseEntriesForProducer($data['bookmarks']);
86
87 return true;
88 }
89
90 $this->parseEntries($data['bookmarks']);
91
92 return true;
93 }
94
95 public function parseEntry(array $importedEntry)
96 {
97 $existingEntry = $this->em
98 ->getRepository('WallabagCoreBundle:Entry')
99 ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId());
100
101 if (false !== $existingEntry) {
102 ++$this->skippedEntries;
103
104 return;
105 }
106
107 $data = [
108 'title' => $importedEntry['article__title'],
109 'url' => $importedEntry['article__url'],
110 'content_type' => '',
111 'language' => '',
112 'is_archived' => $importedEntry['archive'] || $this->markAsRead,
113 'is_starred' => $importedEntry['favorite'],
114 ];
115
116 $entry = $this->fetchContent(
117 new Entry($this->user),
118 $data['url'],
119 $data
120 );
121
122 // jump to next entry in case of problem while getting content
123 if (false === $entry) {
124 ++$this->skippedEntries;
125
126 return;
127 }
128
129 $entry->setArchived($data['is_archived']);
130 $entry->setStarred($data['is_starred']);
131
132 $this->em->persist($entry);
133 ++$this->importedEntries;
134
135 return $entry;
136 }
137
138 /**
139 * Faster parse entries for Producer.
140 * We don't care to make check at this time. They'll be done by the consumer.
141 *
142 * @param array $entries
143 */
144 protected function parseEntriesForProducer($entries)
145 {
146 foreach ($entries as $importedEntry) {
147 // set userId for the producer (it won't know which user is connected)
148 $importedEntry['userId'] = $this->user->getId();
149
150 if ($this->markAsRead) {
151 $importedEntry['archive'] = 1;
152 }
153
154 ++$this->importedEntries;
155
156 // flush every 20 entries
157 if (($i % 20) === 0) {
158 $this->em->flush();
159 }
160 ++$i;
161 }
162
163 $this->em->flush();
164 $this->em->clear();
165 }
166 }