diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/ReadabilityImport.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/ReadabilityImport.php | 127 |
1 files changed, 57 insertions, 70 deletions
diff --git a/src/Wallabag/ImportBundle/Import/ReadabilityImport.php b/src/Wallabag/ImportBundle/Import/ReadabilityImport.php index c7cfe15d..18a6631a 100644 --- a/src/Wallabag/ImportBundle/Import/ReadabilityImport.php +++ b/src/Wallabag/ImportBundle/Import/ReadabilityImport.php | |||
@@ -7,24 +7,9 @@ use Wallabag\UserBundle\Entity\User; | |||
7 | 7 | ||
8 | class ReadabilityImport extends AbstractImport | 8 | class ReadabilityImport extends AbstractImport |
9 | { | 9 | { |
10 | private $user; | ||
11 | private $skippedEntries = 0; | 10 | private $skippedEntries = 0; |
12 | private $importedEntries = 0; | 11 | private $importedEntries = 0; |
13 | private $filepath; | 12 | 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 | 13 | ||
29 | /** | 14 | /** |
30 | * {@inheritdoc} | 15 | * {@inheritdoc} |
@@ -63,26 +48,6 @@ class ReadabilityImport extends AbstractImport | |||
63 | } | 48 | } |
64 | 49 | ||
65 | /** | 50 | /** |
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} | 51 | * {@inheritdoc} |
87 | */ | 52 | */ |
88 | public function getSummary() | 53 | public function getSummary() |
@@ -116,54 +81,76 @@ class ReadabilityImport extends AbstractImport | |||
116 | return false; | 81 | return false; |
117 | } | 82 | } |
118 | 83 | ||
84 | if ($this->producer) { | ||
85 | $this->parseEntriesForProducer($data['bookmarks']); | ||
86 | |||
87 | return true; | ||
88 | } | ||
89 | |||
119 | $this->parseEntries($data['bookmarks']); | 90 | $this->parseEntries($data['bookmarks']); |
120 | 91 | ||
121 | return true; | 92 | return true; |
122 | } | 93 | } |
123 | 94 | ||
95 | public function parseEntry(array $importedEntry) | ||
96 | { | ||
97 | $existingEntry = $this->em | ||
98 | ->getRepository('WallabagCoreBundle:Entry') | ||
99 | ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId()); | ||
100 | |||
101 | if (false !== $existingEntry) { | ||
102 | ++$this->skippedEntries; | ||
103 | |||
104 | return; | ||
105 | } | ||
106 | |||
107 | $data = [ | ||
108 | 'title' => $importedEntry['article__title'], | ||
109 | 'url' => $importedEntry['article__url'], | ||
110 | 'content_type' => '', | ||
111 | 'language' => '', | ||
112 | 'is_archived' => $importedEntry['archive'] || $this->markAsRead, | ||
113 | 'is_starred' => $importedEntry['favorite'], | ||
114 | ]; | ||
115 | |||
116 | $entry = $this->fetchContent( | ||
117 | new Entry($this->user), | ||
118 | $data['url'], | ||
119 | $data | ||
120 | ); | ||
121 | |||
122 | // jump to next entry in case of problem while getting content | ||
123 | if (false === $entry) { | ||
124 | ++$this->skippedEntries; | ||
125 | |||
126 | return; | ||
127 | } | ||
128 | |||
129 | $entry->setArchived($data['is_archived']); | ||
130 | $entry->setStarred($data['is_starred']); | ||
131 | |||
132 | $this->em->persist($entry); | ||
133 | ++$this->importedEntries; | ||
134 | |||
135 | return $entry; | ||
136 | } | ||
137 | |||
124 | /** | 138 | /** |
125 | * Parse and insert all given entries. | 139 | * Faster parse entries for Producer. |
140 | * We don't care to make check at this time. They'll be done by the consumer. | ||
126 | * | 141 | * |
127 | * @param $entries | 142 | * @param array $entries |
128 | */ | 143 | */ |
129 | protected function parseEntries($entries) | 144 | protected function parseEntriesForProducer($entries) |
130 | { | 145 | { |
131 | $i = 1; | ||
132 | |||
133 | foreach ($entries as $importedEntry) { | 146 | foreach ($entries as $importedEntry) { |
134 | $existingEntry = $this->em | 147 | // set userId for the producer (it won't know which user is connected) |
135 | ->getRepository('WallabagCoreBundle:Entry') | 148 | $importedEntry['userId'] = $this->user->getId(); |
136 | ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId()); | ||
137 | |||
138 | if (false !== $existingEntry) { | ||
139 | ++$this->skippedEntries; | ||
140 | continue; | ||
141 | } | ||
142 | 149 | ||
143 | $data = [ | 150 | if ($this->markAsRead) { |
144 | 'title' => $importedEntry['article__title'], | 151 | $importedEntry['archive'] = 1; |
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 | } | 152 | } |
163 | $entry->setArchived($data['is_archived']); | ||
164 | $entry->setStarred($data['is_starred']); | ||
165 | 153 | ||
166 | $this->em->persist($entry); | ||
167 | ++$this->importedEntries; | 154 | ++$this->importedEntries; |
168 | 155 | ||
169 | // flush every 20 entries | 156 | // flush every 20 entries |