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.php131
1 files changed, 41 insertions, 90 deletions
diff --git a/src/Wallabag/ImportBundle/Import/WallabagImport.php b/src/Wallabag/ImportBundle/Import/WallabagImport.php
index a1cc085b..043bb0a2 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();
@@ -79,23 +61,18 @@ abstract class WallabagImport extends AbstractImport
79 return false; 61 return false;
80 } 62 }
81 63
64 if ($this->producer) {
65 $this->parseEntriesForProducer($data);
66
67 return true;
68 }
69
82 $this->parseEntries($data); 70 $this->parseEntries($data);
83 71
84 return true; 72 return true;
85 } 73 }
86 74
87 /** 75 /**
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. 76 * Set file path to the json file.
100 * 77 *
101 * @param string $filepath 78 * @param string $filepath
@@ -108,85 +85,59 @@ abstract class WallabagImport extends AbstractImport
108 } 85 }
109 86
110 /** 87 /**
111 * Set whether articles must be all marked as read. 88 * {@inheritdoc}
112 *
113 * @param bool $markAsRead
114 */ 89 */
115 public function setMarkAsRead($markAsRead) 90 public function parseEntry(array $importedEntry)
116 { 91 {
117 $this->markAsRead = $markAsRead; 92 $existingEntry = $this->em
93 ->getRepository('WallabagCoreBundle:Entry')
94 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
118 95
119 return $this; 96 if (false !== $existingEntry) {
120 } 97 ++$this->skippedEntries;
121 98
122 /** 99 return;
123 * Parse and insert all given entries. 100 }
124 *
125 * @param $entries
126 */
127 protected function parseEntries($entries)
128 {
129 $i = 1;
130 101
131 foreach ($entries as $importedEntry) { 102 $data = $this->prepareEntry($importedEntry);
132 $existingEntry = $this->em
133 ->getRepository('WallabagCoreBundle:Entry')
134 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
135 103
136 if (false !== $existingEntry) { 104 $entry = new Entry($this->user);
137 ++$this->skippedEntries; 105 $entry->setUrl($data['url']);
138 continue; 106 $entry->setTitle($data['title']);
139 }
140 107
141 $data = $this->prepareEntry($importedEntry, $this->markAsRead); 108 // update entry with content (in case fetching failed, the given entry will be return)
109 $entry = $this->fetchContent($entry, $data['url'], $data);
142 110
143 $entry = $this->fetchContent( 111 if (array_key_exists('tags', $data)) {
144 new Entry($this->user), 112 $this->contentProxy->assignTagsToEntry(
145 $importedEntry['url'], 113 $entry,
146 $data 114 $data['tags']
147 ); 115 );
116 }
148 117
149 // jump to next entry in case of problem while getting content 118 if (isset($importedEntry['preview_picture'])) {
150 if (false === $entry) { 119 $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 } 120 }
179 121
180 $this->em->flush(); 122 $entry->setArchived($data['is_archived']);
123 $entry->setStarred($data['is_starred']);
124
125 if (!empty($data['created_at'])) {
126 $entry->setCreatedAt(new \DateTime($data['created_at']));
127 }
128
129 $this->em->persist($entry);
130 ++$this->importedEntries;
131
132 return $entry;
181 } 133 }
182 134
183 /** 135 /**
184 * This should return a cleaned array for a given entry to be given to `updateEntry`. 136 * This should return a cleaned array for a given entry to be given to `updateEntry`.
185 * 137 *
186 * @param array $entry Data from the imported file 138 * @param array $entry Data from the imported file
187 * @param bool $markAsRead Should we mark as read content?
188 * 139 *
189 * @return array 140 * @return array
190 */ 141 */
191 abstract protected function prepareEntry($entry = [], $markAsRead = false); 142 abstract protected function prepareEntry($entry = []);
192} 143}