diff options
author | Jeremy Benoist <j0k3r@users.noreply.github.com> | 2016-09-19 07:15:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-19 07:15:40 +0200 |
commit | da18a4682f124b02278860d23ac1d59dee995277 (patch) | |
tree | eabbe9da7203eea41e0cb0ec3c26b9b6599cf58f /src/Wallabag/ImportBundle/Import/ReadabilityImport.php | |
parent | 0ed8ce55b5caf2c88e8330afa83abef6c4aac9a4 (diff) | |
parent | 59b97fae996d8307b9d957d210d46200f6d206bf (diff) | |
download | wallabag-da18a4682f124b02278860d23ac1d59dee995277.tar.gz wallabag-da18a4682f124b02278860d23ac1d59dee995277.tar.zst wallabag-da18a4682f124b02278860d23ac1d59dee995277.zip |
Merge pull request #1941 from wallabag/v2-asynchronous-jobs
Use asynchronous jobs for imports
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/ReadabilityImport.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/ReadabilityImport.php | 149 |
1 files changed, 51 insertions, 98 deletions
diff --git a/src/Wallabag/ImportBundle/Import/ReadabilityImport.php b/src/Wallabag/ImportBundle/Import/ReadabilityImport.php index c7cfe15d..fa2b7053 100644 --- a/src/Wallabag/ImportBundle/Import/ReadabilityImport.php +++ b/src/Wallabag/ImportBundle/Import/ReadabilityImport.php | |||
@@ -3,28 +3,10 @@ | |||
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 | 6 | ||
8 | class ReadabilityImport extends AbstractImport | 7 | class ReadabilityImport extends AbstractImport |
9 | { | 8 | { |
10 | private $user; | ||
11 | private $skippedEntries = 0; | ||
12 | private $importedEntries = 0; | ||
13 | private $filepath; | 9 | private $filepath; |
14 | private $markAsRead; | ||
15 | |||
16 | /** | ||
17 | * We define the user in a custom call because on the import command there is no logged in user. | ||
18 | * So we can't retrieve user from the `security.token_storage` service. | ||
19 | * | ||
20 | * @param User $user | ||
21 | */ | ||
22 | public function setUser(User $user) | ||
23 | { | ||
24 | $this->user = $user; | ||
25 | |||
26 | return $this; | ||
27 | } | ||
28 | 10 | ||
29 | /** | 11 | /** |
30 | * {@inheritdoc} | 12 | * {@inheritdoc} |
@@ -63,37 +45,6 @@ class ReadabilityImport extends AbstractImport | |||
63 | } | 45 | } |
64 | 46 | ||
65 | /** | 47 | /** |
66 | * Set whether articles must be all marked as read. | ||
67 | * | ||
68 | * @param bool $markAsRead | ||
69 | */ | ||
70 | public function setMarkAsRead($markAsRead) | ||
71 | { | ||
72 | $this->markAsRead = $markAsRead; | ||
73 | |||
74 | return $this; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * Get whether articles must be all marked as read. | ||
79 | */ | ||
80 | public function getMarkAsRead() | ||
81 | { | ||
82 | return $this->markAsRead; | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * {@inheritdoc} | ||
87 | */ | ||
88 | public function getSummary() | ||
89 | { | ||
90 | return [ | ||
91 | 'skipped' => $this->skippedEntries, | ||
92 | 'imported' => $this->importedEntries, | ||
93 | ]; | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * {@inheritdoc} | 48 | * {@inheritdoc} |
98 | */ | 49 | */ |
99 | public function import() | 50 | public function import() |
@@ -116,64 +67,66 @@ class ReadabilityImport extends AbstractImport | |||
116 | return false; | 67 | return false; |
117 | } | 68 | } |
118 | 69 | ||
70 | if ($this->producer) { | ||
71 | $this->parseEntriesForProducer($data['bookmarks']); | ||
72 | |||
73 | return true; | ||
74 | } | ||
75 | |||
119 | $this->parseEntries($data['bookmarks']); | 76 | $this->parseEntries($data['bookmarks']); |
120 | 77 | ||
121 | return true; | 78 | return true; |
122 | } | 79 | } |
123 | 80 | ||
124 | /** | 81 | /** |
125 | * Parse and insert all given entries. | 82 | * {@inheritdoc} |
126 | * | ||
127 | * @param $entries | ||
128 | */ | 83 | */ |
129 | protected function parseEntries($entries) | 84 | public function parseEntry(array $importedEntry) |
130 | { | 85 | { |
131 | $i = 1; | 86 | $existingEntry = $this->em |
132 | 87 | ->getRepository('WallabagCoreBundle:Entry') | |
133 | foreach ($entries as $importedEntry) { | 88 | ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId()); |
134 | $existingEntry = $this->em | 89 | |
135 | ->getRepository('WallabagCoreBundle:Entry') | 90 | if (false !== $existingEntry) { |
136 | ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId()); | 91 | ++$this->skippedEntries; |
137 | 92 | ||
138 | if (false !== $existingEntry) { | 93 | return; |
139 | ++$this->skippedEntries; | ||
140 | continue; | ||
141 | } | ||
142 | |||
143 | $data = [ | ||
144 | 'title' => $importedEntry['article__title'], | ||
145 | 'url' => $importedEntry['article__url'], | ||
146 | 'content_type' => '', | ||
147 | 'language' => '', | ||
148 | 'is_archived' => $importedEntry['archive'] || $this->markAsRead, | ||
149 | 'is_starred' => $importedEntry['favorite'], | ||
150 | ]; | ||
151 | |||
152 | $entry = $this->fetchContent( | ||
153 | new Entry($this->user), | ||
154 | $data['url'], | ||
155 | $data | ||
156 | ); | ||
157 | |||
158 | // jump to next entry in case of problem while getting content | ||
159 | if (false === $entry) { | ||
160 | ++$this->skippedEntries; | ||
161 | continue; | ||
162 | } | ||
163 | $entry->setArchived($data['is_archived']); | ||
164 | $entry->setStarred($data['is_starred']); | ||
165 | |||
166 | $this->em->persist($entry); | ||
167 | ++$this->importedEntries; | ||
168 | |||
169 | // flush every 20 entries | ||
170 | if (($i % 20) === 0) { | ||
171 | $this->em->flush(); | ||
172 | } | ||
173 | ++$i; | ||
174 | } | 94 | } |
175 | 95 | ||
176 | $this->em->flush(); | 96 | $data = [ |
177 | $this->em->clear(); | 97 | 'title' => $importedEntry['article__title'], |
98 | 'url' => $importedEntry['article__url'], | ||
99 | 'content_type' => '', | ||
100 | 'language' => '', | ||
101 | 'is_archived' => $importedEntry['archive'] || $this->markAsRead, | ||
102 | 'is_starred' => $importedEntry['favorite'], | ||
103 | 'created_at' => $importedEntry['date_added'], | ||
104 | ]; | ||
105 | |||
106 | $entry = new Entry($this->user); | ||
107 | $entry->setUrl($data['url']); | ||
108 | $entry->setTitle($data['title']); | ||
109 | |||
110 | // update entry with content (in case fetching failed, the given entry will be return) | ||
111 | $entry = $this->fetchContent($entry, $data['url'], $data); | ||
112 | |||
113 | $entry->setArchived($data['is_archived']); | ||
114 | $entry->setStarred($data['is_starred']); | ||
115 | $entry->setCreatedAt(new \DateTime($data['created_at'])); | ||
116 | |||
117 | $this->em->persist($entry); | ||
118 | ++$this->importedEntries; | ||
119 | |||
120 | return $entry; | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * {@inheritdoc} | ||
125 | */ | ||
126 | protected function setEntryAsRead(array $importedEntry) | ||
127 | { | ||
128 | $importedEntry['archive'] = 1; | ||
129 | |||
130 | return $importedEntry; | ||
178 | } | 131 | } |
179 | } | 132 | } |