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