]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/WallabagImport.php
Fix error on EntityManager clear
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / WallabagImport.php
CommitLineData
b787a775
JB
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
b787a775
JB
5use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\UserBundle\Entity\User;
b787a775 7
19d9efab 8abstract class WallabagImport extends AbstractImport
b787a775
JB
9{
10 protected $user;
b787a775
JB
11 protected $skippedEntries = 0;
12 protected $importedEntries = 0;
13 protected $filepath;
14 protected $markAsRead;
15 // untitled in all languages from v1
16 protected $untitled = [
17 'Untitled',
18 'Sans titre',
19 'podle nadpisu',
20 'Sin título',
21 'با عنوان',
22 'per titolo',
23 'Sem título',
24 'Без названия',
25 'po naslovu',
26 'Без назви',
27 'No title found',
28 '',
29 ];
30
b787a775
JB
31 /**
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}
46 */
47 abstract public function getName();
48
49 /**
50 * {@inheritdoc}
51 */
52 abstract public function getUrl();
53
54 /**
55 * {@inheritdoc}
56 */
57 abstract public function getDescription();
58
59 /**
60 * {@inheritdoc}
61 */
62 public function import()
63 {
64 if (!$this->user) {
65 $this->logger->error('WallabagImport: user is not defined');
66
67 return false;
68 }
69
70 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
71 $this->logger->error('WallabagImport: unable to read file', ['filepath' => $this->filepath]);
72
73 return false;
74 }
75
76 $data = json_decode(file_get_contents($this->filepath), true);
77
78 if (empty($data)) {
79 return false;
80 }
81
82 $this->parseEntries($data);
83
84 return true;
85 }
86
87 /**
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.
100 *
101 * @param string $filepath
102 */
103 public function setFilepath($filepath)
104 {
105 $this->filepath = $filepath;
106
107 return $this;
108 }
109
110 /**
111 * Set whether articles must be all marked as read.
112 *
113 * @param bool $markAsRead
114 */
115 public function setMarkAsRead($markAsRead)
116 {
117 $this->markAsRead = $markAsRead;
118
119 return $this;
120 }
121
122 /**
123 * Parse and insert all given entries.
124 *
125 * @param $entries
126 */
127 protected function parseEntries($entries)
128 {
129 $i = 1;
130
131 foreach ($entries as $importedEntry) {
132 $existingEntry = $this->em
133 ->getRepository('WallabagCoreBundle:Entry')
134 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
135
136 if (false !== $existingEntry) {
137 ++$this->skippedEntries;
138 continue;
139 }
140
141 $data = $this->prepareEntry($importedEntry, $this->markAsRead);
142
19d9efab 143 $entry = $this->fetchContent(
b787a775
JB
144 new Entry($this->user),
145 $importedEntry['url'],
146 $data
147 );
148
19d9efab
JB
149 // jump to next entry in case of problem while getting content
150 if (false === $entry) {
151 ++$this->skippedEntries;
152 continue;
153 }
154
b787a775
JB
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();
58fadbc9
JB
175
176 // clear only affected entities
177 $this->em->clear(Entry::class);
178 $this->em->clear(Tag::class);
b787a775
JB
179 }
180 ++$i;
181 }
182
183 $this->em->flush();
184 }
185
186 /**
187 * This should return a cleaned array for a given entry to be given to `updateEntry`.
188 *
189 * @param array $entry Data from the imported file
190 * @param bool $markAsRead Should we mark as read content?
191 *
192 * @return array
193 */
194 abstract protected function prepareEntry($entry = [], $markAsRead = false);
195}