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