]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/WallabagImport.php
Refacto wallabag import
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / WallabagImport.php
CommitLineData
b787a775
JB
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Psr\Log\LoggerInterface;
6use Psr\Log\NullLogger;
7use Doctrine\ORM\EntityManager;
8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\UserBundle\Entity\User;
10use Wallabag\CoreBundle\Helper\ContentProxy;
11
12abstract class WallabagImport implements ImportInterface
13{
14 protected $user;
15 protected $em;
16 protected $logger;
17 protected $contentProxy;
18 protected $skippedEntries = 0;
19 protected $importedEntries = 0;
20 protected $filepath;
21 protected $markAsRead;
22 // untitled in all languages from v1
23 protected $untitled = [
24 'Untitled',
25 'Sans titre',
26 'podle nadpisu',
27 'Sin título',
28 'با عنوان',
29 'per titolo',
30 'Sem título',
31 'Без названия',
32 'po naslovu',
33 'Без назви',
34 'No title found',
35 '',
36 ];
37
38 public function __construct(EntityManager $em, ContentProxy $contentProxy)
39 {
40 $this->em = $em;
41 $this->logger = new NullLogger();
42 $this->contentProxy = $contentProxy;
43 }
44
45 public function setLogger(LoggerInterface $logger)
46 {
47 $this->logger = $logger;
48 }
49
50 /**
51 * We define the user in a custom call because on the import command there is no logged in user.
52 * So we can't retrieve user from the `security.token_storage` service.
53 *
54 * @param User $user
55 */
56 public function setUser(User $user)
57 {
58 $this->user = $user;
59
60 return $this;
61 }
62
63 /**
64 * {@inheritdoc}
65 */
66 abstract public function getName();
67
68 /**
69 * {@inheritdoc}
70 */
71 abstract public function getUrl();
72
73 /**
74 * {@inheritdoc}
75 */
76 abstract public function getDescription();
77
78 /**
79 * {@inheritdoc}
80 */
81 public function import()
82 {
83 if (!$this->user) {
84 $this->logger->error('WallabagImport: user is not defined');
85
86 return false;
87 }
88
89 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
90 $this->logger->error('WallabagImport: unable to read file', ['filepath' => $this->filepath]);
91
92 return false;
93 }
94
95 $data = json_decode(file_get_contents($this->filepath), true);
96
97 if (empty($data)) {
98 return false;
99 }
100
101 $this->parseEntries($data);
102
103 return true;
104 }
105
106 /**
107 * {@inheritdoc}
108 */
109 public function getSummary()
110 {
111 return [
112 'skipped' => $this->skippedEntries,
113 'imported' => $this->importedEntries,
114 ];
115 }
116
117 /**
118 * Set file path to the json file.
119 *
120 * @param string $filepath
121 */
122 public function setFilepath($filepath)
123 {
124 $this->filepath = $filepath;
125
126 return $this;
127 }
128
129 /**
130 * Set whether articles must be all marked as read.
131 *
132 * @param bool $markAsRead
133 */
134 public function setMarkAsRead($markAsRead)
135 {
136 $this->markAsRead = $markAsRead;
137
138 return $this;
139 }
140
141 /**
142 * Parse and insert all given entries.
143 *
144 * @param $entries
145 */
146 protected function parseEntries($entries)
147 {
148 $i = 1;
149
150 foreach ($entries as $importedEntry) {
151 $existingEntry = $this->em
152 ->getRepository('WallabagCoreBundle:Entry')
153 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
154
155 if (false !== $existingEntry) {
156 ++$this->skippedEntries;
157 continue;
158 }
159
160 $data = $this->prepareEntry($importedEntry, $this->markAsRead);
161
162 $entry = $this->contentProxy->updateEntry(
163 new Entry($this->user),
164 $importedEntry['url'],
165 $data
166 );
167
168 if (array_key_exists('tags', $data)) {
169 $this->contentProxy->assignTagsToEntry(
170 $entry,
171 $data['tags']
172 );
173 }
174
175 if (isset($importedEntry['preview_picture'])) {
176 $entry->setPreviewPicture($importedEntry['preview_picture']);
177 }
178
179 $entry->setArchived($data['is_archived']);
180 $entry->setStarred($data['is_starred']);
181
182 $this->em->persist($entry);
183 ++$this->importedEntries;
184
185 // flush every 20 entries
186 if (($i % 20) === 0) {
187 $this->em->flush();
188 }
189 ++$i;
190 }
191
192 $this->em->flush();
193 }
194
195 /**
196 * This should return a cleaned array for a given entry to be given to `updateEntry`.
197 *
198 * @param array $entry Data from the imported file
199 * @param bool $markAsRead Should we mark as read content?
200 *
201 * @return array
202 */
203 abstract protected function prepareEntry($entry = [], $markAsRead = false);
204}