]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/WallabagImport.php
a1cc085b3e12c14a8ba0ceaccbde48b7f5a3c0ea
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / WallabagImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Wallabag\CoreBundle\Entity\Entry;
6 use Wallabag\UserBundle\Entity\User;
7
8 abstract class WallabagImport extends AbstractImport
9 {
10 protected $user;
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
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
143 $entry = $this->fetchContent(
144 new Entry($this->user),
145 $importedEntry['url'],
146 $data
147 );
148
149 // jump to next entry in case of problem while getting content
150 if (false === $entry) {
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 }
179
180 $this->em->flush();
181 }
182
183 /**
184 * This should return a cleaned array for a given entry to be given to `updateEntry`.
185 *
186 * @param array $entry Data from the imported file
187 * @param bool $markAsRead Should we mark as read content?
188 *
189 * @return array
190 */
191 abstract protected function prepareEntry($entry = [], $markAsRead = false);
192 }