aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Import/BrowserImport.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/BrowserImport.php')
-rw-r--r--src/Wallabag/ImportBundle/Import/BrowserImport.php154
1 files changed, 77 insertions, 77 deletions
diff --git a/src/Wallabag/ImportBundle/Import/BrowserImport.php b/src/Wallabag/ImportBundle/Import/BrowserImport.php
index 8bf7d92e..b5593180 100644
--- a/src/Wallabag/ImportBundle/Import/BrowserImport.php
+++ b/src/Wallabag/ImportBundle/Import/BrowserImport.php
@@ -3,8 +3,6 @@
3namespace Wallabag\ImportBundle\Import; 3namespace Wallabag\ImportBundle\Import;
4 4
5use Wallabag\CoreBundle\Entity\Entry; 5use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\CoreBundle\Helper\ContentProxy;
8use Wallabag\CoreBundle\Event\EntrySavedEvent; 6use Wallabag\CoreBundle\Event\EntrySavedEvent;
9 7
10abstract class BrowserImport extends AbstractImport 8abstract class BrowserImport extends AbstractImport
@@ -75,6 +73,80 @@ abstract class BrowserImport extends AbstractImport
75 } 73 }
76 74
77 /** 75 /**
76 * {@inheritdoc}
77 */
78 public function parseEntry(array $importedEntry)
79 {
80 if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
81 if ($this->producer) {
82 $this->parseEntriesForProducer($importedEntry);
83
84 return;
85 }
86
87 $this->parseEntries($importedEntry);
88
89 return;
90 }
91
92 if (array_key_exists('children', $importedEntry)) {
93 if ($this->producer) {
94 $this->parseEntriesForProducer($importedEntry['children']);
95
96 return;
97 }
98
99 $this->parseEntries($importedEntry['children']);
100
101 return;
102 }
103
104 if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) {
105 return;
106 }
107
108 $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
109
110 $existingEntry = $this->em
111 ->getRepository('WallabagCoreBundle:Entry')
112 ->findByUrlAndUserId($url, $this->user->getId());
113
114 if (false !== $existingEntry) {
115 ++$this->skippedEntries;
116
117 return;
118 }
119
120 $data = $this->prepareEntry($importedEntry);
121
122 $entry = new Entry($this->user);
123 $entry->setUrl($data['url']);
124 $entry->setTitle($data['title']);
125
126 // update entry with content (in case fetching failed, the given entry will be return)
127 $this->fetchContent($entry, $data['url'], $data);
128
129 if (array_key_exists('tags', $data)) {
130 $this->tagsAssigner->assignTagsToEntry(
131 $entry,
132 $data['tags']
133 );
134 }
135
136 $entry->setArchived($data['is_archived']);
137
138 if (!empty($data['created_at'])) {
139 $dt = new \DateTime();
140 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
141 }
142
143 $this->em->persist($entry);
144 ++$this->importedEntries;
145
146 return $entry;
147 }
148
149 /**
78 * Parse and insert all given entries. 150 * Parse and insert all given entries.
79 * 151 *
80 * @param $entries 152 * @param $entries
@@ -99,7 +171,7 @@ abstract class BrowserImport extends AbstractImport
99 $entryToBeFlushed[] = $entry; 171 $entryToBeFlushed[] = $entry;
100 172
101 // flush every 20 entries 173 // flush every 20 entries
102 if (($i % 20) === 0) { 174 if (0 === ($i % 20)) {
103 $this->em->flush(); 175 $this->em->flush();
104 176
105 foreach ($entryToBeFlushed as $entry) { 177 foreach ($entryToBeFlushed as $entry) {
@@ -153,84 +225,12 @@ abstract class BrowserImport extends AbstractImport
153 /** 225 /**
154 * {@inheritdoc} 226 * {@inheritdoc}
155 */ 227 */
156 public function parseEntry(array $importedEntry)
157 {
158 if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
159 if ($this->producer) {
160 $this->parseEntriesForProducer($importedEntry);
161
162 return;
163 }
164
165 $this->parseEntries($importedEntry);
166
167 return;
168 }
169
170 if (array_key_exists('children', $importedEntry)) {
171 if ($this->producer) {
172 $this->parseEntriesForProducer($importedEntry['children']);
173
174 return;
175 }
176
177 $this->parseEntries($importedEntry['children']);
178
179 return;
180 }
181
182 if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) {
183 return;
184 }
185
186 $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
187
188 $existingEntry = $this->em
189 ->getRepository('WallabagCoreBundle:Entry')
190 ->findByUrlAndUserId($url, $this->user->getId());
191
192 if (false !== $existingEntry) {
193 ++$this->skippedEntries;
194
195 return;
196 }
197
198 $data = $this->prepareEntry($importedEntry);
199
200 $entry = new Entry($this->user);
201 $entry->setUrl($data['url']);
202 $entry->setTitle($data['title']);
203
204 // update entry with content (in case fetching failed, the given entry will be return)
205 $entry = $this->fetchContent($entry, $data['url'], $data);
206
207 if (array_key_exists('tags', $data)) {
208 $this->contentProxy->assignTagsToEntry(
209 $entry,
210 $data['tags']
211 );
212 }
213
214 $entry->setArchived($data['is_archived']);
215
216 if (!empty($data['created_at'])) {
217 $dt = new \DateTime();
218 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
219 }
220
221 $this->em->persist($entry);
222 ++$this->importedEntries;
223
224 return $entry;
225 }
226
227 /**
228 * {@inheritdoc}
229 */
230 protected function setEntryAsRead(array $importedEntry) 228 protected function setEntryAsRead(array $importedEntry)
231 { 229 {
232 $importedEntry['is_archived'] = 1; 230 $importedEntry['is_archived'] = 1;
233 231
234 return $importedEntry; 232 return $importedEntry;
235 } 233 }
234
235 abstract protected function prepareEntry(array $entry = []);
236} 236}