diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/WallabagImport.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/WallabagImport.php | 118 |
1 files changed, 43 insertions, 75 deletions
diff --git a/src/Wallabag/ImportBundle/Import/WallabagImport.php b/src/Wallabag/ImportBundle/Import/WallabagImport.php index 581ec178..6ad14e8c 100644 --- a/src/Wallabag/ImportBundle/Import/WallabagImport.php +++ b/src/Wallabag/ImportBundle/Import/WallabagImport.php | |||
@@ -11,7 +11,6 @@ abstract class WallabagImport extends AbstractImport | |||
11 | protected $skippedEntries = 0; | 11 | protected $skippedEntries = 0; |
12 | protected $importedEntries = 0; | 12 | protected $importedEntries = 0; |
13 | protected $filepath; | 13 | protected $filepath; |
14 | protected $markAsRead; | ||
15 | // untitled in all languages from v1 | 14 | // untitled in all languages from v1 |
16 | protected $untitled = [ | 15 | protected $untitled = [ |
17 | 'Untitled', | 16 | 'Untitled', |
@@ -29,19 +28,6 @@ abstract class WallabagImport extends AbstractImport | |||
29 | ]; | 28 | ]; |
30 | 29 | ||
31 | /** | 30 | /** |
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} | 31 | * {@inheritdoc} |
46 | */ | 32 | */ |
47 | abstract public function getName(); | 33 | abstract public function getName(); |
@@ -79,6 +65,12 @@ abstract class WallabagImport extends AbstractImport | |||
79 | return false; | 65 | return false; |
80 | } | 66 | } |
81 | 67 | ||
68 | if ($this->producer) { | ||
69 | $this->parseEntriesForProducer($data); | ||
70 | |||
71 | return true; | ||
72 | } | ||
73 | |||
82 | $this->parseEntries($data); | 74 | $this->parseEntries($data); |
83 | 75 | ||
84 | return true; | 76 | return true; |
@@ -108,85 +100,61 @@ abstract class WallabagImport extends AbstractImport | |||
108 | } | 100 | } |
109 | 101 | ||
110 | /** | 102 | /** |
111 | * Set whether articles must be all marked as read. | 103 | * {@inheritdoc} |
112 | * | ||
113 | * @param bool $markAsRead | ||
114 | */ | 104 | */ |
115 | public function setMarkAsRead($markAsRead) | 105 | public function parseEntry(array $importedEntry) |
116 | { | 106 | { |
117 | $this->markAsRead = $markAsRead; | 107 | $existingEntry = $this->em |
108 | ->getRepository('WallabagCoreBundle:Entry') | ||
109 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | ||
118 | 110 | ||
119 | return $this; | 111 | if (false !== $existingEntry) { |
120 | } | 112 | ++$this->skippedEntries; |
121 | 113 | ||
122 | /** | 114 | return; |
123 | * Parse and insert all given entries. | 115 | } |
124 | * | 116 | |
125 | * @param $entries | 117 | $data = $this->prepareEntry($importedEntry); |
126 | */ | ||
127 | protected function parseEntries($entries) | ||
128 | { | ||
129 | $i = 1; | ||
130 | 118 | ||
131 | foreach ($entries as $importedEntry) { | 119 | $entry = $this->fetchContent( |
132 | $existingEntry = $this->em | 120 | new Entry($this->user), |
133 | ->getRepository('WallabagCoreBundle:Entry') | 121 | $importedEntry['url'], |
134 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | 122 | $data |
123 | ); | ||
135 | 124 | ||
136 | if (false !== $existingEntry) { | 125 | // jump to next entry in case of problem while getting content |
137 | ++$this->skippedEntries; | 126 | if (false === $entry) { |
138 | continue; | 127 | ++$this->skippedEntries; |
139 | } | ||
140 | 128 | ||
141 | $data = $this->prepareEntry($importedEntry, $this->markAsRead); | 129 | return; |
130 | } | ||
142 | 131 | ||
143 | $entry = $this->fetchContent( | 132 | if (array_key_exists('tags', $data)) { |
144 | new Entry($this->user), | 133 | $this->contentProxy->assignTagsToEntry( |
145 | $importedEntry['url'], | 134 | $entry, |
146 | $data | 135 | $data['tags'] |
147 | ); | 136 | ); |
137 | } | ||
148 | 138 | ||
149 | // jump to next entry in case of problem while getting content | 139 | if (isset($importedEntry['preview_picture'])) { |
150 | if (false === $entry) { | 140 | $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 | } | ||
176 | ++$i; | ||
177 | } | 141 | } |
178 | 142 | ||
179 | $this->em->flush(); | 143 | $entry->setArchived($data['is_archived']); |
180 | $this->em->clear(); | 144 | $entry->setStarred($data['is_starred']); |
145 | |||
146 | $this->em->persist($entry); | ||
147 | ++$this->importedEntries; | ||
148 | |||
149 | return $entry; | ||
181 | } | 150 | } |
182 | 151 | ||
183 | /** | 152 | /** |
184 | * This should return a cleaned array for a given entry to be given to `updateEntry`. | 153 | * This should return a cleaned array for a given entry to be given to `updateEntry`. |
185 | * | 154 | * |
186 | * @param array $entry Data from the imported file | 155 | * @param array $entry Data from the imported file |
187 | * @param bool $markAsRead Should we mark as read content? | ||
188 | * | 156 | * |
189 | * @return array | 157 | * @return array |
190 | */ | 158 | */ |
191 | abstract protected function prepareEntry($entry = [], $markAsRead = false); | 159 | abstract protected function prepareEntry($entry = []); |
192 | } | 160 | } |