aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Import/WallabagImport.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/WallabagImport.php')
-rw-r--r--src/Wallabag/ImportBundle/Import/WallabagImport.php134
1 files changed, 44 insertions, 90 deletions
diff --git a/src/Wallabag/ImportBundle/Import/WallabagImport.php b/src/Wallabag/ImportBundle/Import/WallabagImport.php
index a1cc085b..702da057 100644
--- a/src/Wallabag/ImportBundle/Import/WallabagImport.php
+++ b/src/Wallabag/ImportBundle/Import/WallabagImport.php
@@ -3,15 +3,10 @@
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;
7 6
8abstract class WallabagImport extends AbstractImport 7abstract class WallabagImport extends AbstractImport
9{ 8{
10 protected $user;
11 protected $skippedEntries = 0;
12 protected $importedEntries = 0;
13 protected $filepath; 9 protected $filepath;
14 protected $markAsRead;
15 // untitled in all languages from v1 10 // untitled in all languages from v1
16 protected $untitled = [ 11 protected $untitled = [
17 'Untitled', 12 'Untitled',
@@ -29,19 +24,6 @@ abstract class WallabagImport extends AbstractImport
29 ]; 24 ];
30 25
31 /** 26 /**
32 * We define the user in a custom call because on the import command there is no logged in user.
33 * So we can't retrieve user from the `security.token_storage` service.
34 *
35 * @param User $user
36 */
37 public function setUser(User $user)
38 {
39 $this->user = $user;
40
41 return $this;
42 }
43
44 /**
45 * {@inheritdoc} 27 * {@inheritdoc}
46 */ 28 */
47 abstract public function getName(); 29 abstract public function getName();
@@ -76,26 +58,23 @@ abstract class WallabagImport extends AbstractImport
76 $data = json_decode(file_get_contents($this->filepath), true); 58 $data = json_decode(file_get_contents($this->filepath), true);
77 59
78 if (empty($data)) { 60 if (empty($data)) {
61 $this->logger->error('WallabagImport: no entries in imported file');
62
79 return false; 63 return false;
80 } 64 }
81 65
66 if ($this->producer) {
67 $this->parseEntriesForProducer($data);
68
69 return true;
70 }
71
82 $this->parseEntries($data); 72 $this->parseEntries($data);
83 73
84 return true; 74 return true;
85 } 75 }
86 76
87 /** 77 /**
88 * {@inheritdoc}
89 */
90 public function getSummary()
91 {
92 return [
93 'skipped' => $this->skippedEntries,
94 'imported' => $this->importedEntries,
95 ];
96 }
97
98 /**
99 * Set file path to the json file. 78 * Set file path to the json file.
100 * 79 *
101 * @param string $filepath 80 * @param string $filepath
@@ -108,85 +87,60 @@ abstract class WallabagImport extends AbstractImport
108 } 87 }
109 88
110 /** 89 /**
111 * Set whether articles must be all marked as read. 90 * {@inheritdoc}
112 *
113 * @param bool $markAsRead
114 */ 91 */
115 public function setMarkAsRead($markAsRead) 92 public function parseEntry(array $importedEntry)
116 { 93 {
117 $this->markAsRead = $markAsRead; 94 $existingEntry = $this->em
95 ->getRepository('WallabagCoreBundle:Entry')
96 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
118 97
119 return $this; 98 if (false !== $existingEntry) {
120 } 99 ++$this->skippedEntries;
121 100
122 /** 101 return;
123 * Parse and insert all given entries. 102 }
124 *
125 * @param $entries
126 */
127 protected function parseEntries($entries)
128 {
129 $i = 1;
130 103
131 foreach ($entries as $importedEntry) { 104 $data = $this->prepareEntry($importedEntry);
132 $existingEntry = $this->em
133 ->getRepository('WallabagCoreBundle:Entry')
134 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
135 105
136 if (false !== $existingEntry) { 106 $entry = new Entry($this->user);
137 ++$this->skippedEntries; 107 $entry->setUrl($data['url']);
138 continue; 108 $entry->setTitle($data['title']);
139 }
140 109
141 $data = $this->prepareEntry($importedEntry, $this->markAsRead); 110 // update entry with content (in case fetching failed, the given entry will be return)
111 $entry = $this->fetchContent($entry, $data['url'], $data);
142 112
143 $entry = $this->fetchContent( 113 if (array_key_exists('tags', $data)) {
144 new Entry($this->user), 114 $this->contentProxy->assignTagsToEntry(
145 $importedEntry['url'], 115 $entry,
146 $data 116 $data['tags'],
117 $this->em->getUnitOfWork()->getScheduledEntityInsertions()
147 ); 118 );
119 }
148 120
149 // jump to next entry in case of problem while getting content 121 if (isset($importedEntry['preview_picture'])) {
150 if (false === $entry) { 122 $entry->setPreviewPicture($importedEntry['preview_picture']);
151 ++$this->skippedEntries;
152 continue;
153 }
154
155 if (array_key_exists('tags', $data)) {
156 $this->contentProxy->assignTagsToEntry(
157 $entry,
158 $data['tags']
159 );
160 }
161
162 if (isset($importedEntry['preview_picture'])) {
163 $entry->setPreviewPicture($importedEntry['preview_picture']);
164 }
165
166 $entry->setArchived($data['is_archived']);
167 $entry->setStarred($data['is_starred']);
168
169 $this->em->persist($entry);
170 ++$this->importedEntries;
171
172 // flush every 20 entries
173 if (($i % 20) === 0) {
174 $this->em->flush();
175 $this->em->clear($entry);
176 }
177 ++$i;
178 } 123 }
179 124
180 $this->em->flush(); 125 $entry->setArchived($data['is_archived']);
126 $entry->setStarred($data['is_starred']);
127
128 if (!empty($data['created_at'])) {
129 $entry->setCreatedAt(new \DateTime($data['created_at']));
130 }
131
132 $this->em->persist($entry);
133 ++$this->importedEntries;
134
135 return $entry;
181 } 136 }
182 137
183 /** 138 /**
184 * This should return a cleaned array for a given entry to be given to `updateEntry`. 139 * This should return a cleaned array for a given entry to be given to `updateEntry`.
185 * 140 *
186 * @param array $entry Data from the imported file 141 * @param array $entry Data from the imported file
187 * @param bool $markAsRead Should we mark as read content?
188 * 142 *
189 * @return array 143 * @return array
190 */ 144 */
191 abstract protected function prepareEntry($entry = [], $markAsRead = false); 145 abstract protected function prepareEntry($entry = []);
192} 146}