diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-10-23 11:09:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-23 11:09:17 +0200 |
commit | 1953a872932a63792293b4aec087880265ba89f7 (patch) | |
tree | fd16599e737fcdaf193c933ef3ec4a4ee248b117 /src/Wallabag/ImportBundle/Import/BrowserImport.php | |
parent | d83d25dadec2c38460a32d96f5d2903426fec9d3 (diff) | |
parent | 702f2d67d60ca963492b90dad74cb5f8dcc84e51 (diff) | |
download | wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.gz wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.zst wallabag-1953a872932a63792293b4aec087880265ba89f7.zip |
Merge pull request #3011 from wallabag/2.3
wallabag 2.3.0
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/BrowserImport.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/BrowserImport.php | 154 |
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 @@ | |||
3 | namespace Wallabag\ImportBundle\Import; | 3 | namespace Wallabag\ImportBundle\Import; |
4 | 4 | ||
5 | use Wallabag\CoreBundle\Entity\Entry; | 5 | use Wallabag\CoreBundle\Entity\Entry; |
6 | use Wallabag\UserBundle\Entity\User; | ||
7 | use Wallabag\CoreBundle\Helper\ContentProxy; | ||
8 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | 6 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
9 | 7 | ||
10 | abstract class BrowserImport extends AbstractImport | 8 | abstract 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 | } |