]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/WallabagV1Import.php
Merge pull request #1658 from wallabag/v2-import-v1-tags
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / WallabagV1Import.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Psr\Log\LoggerInterface;
6 use Psr\Log\NullLogger;
7 use Doctrine\ORM\EntityManager;
8 use Wallabag\CoreBundle\Entity\Entry;
9 use Wallabag\CoreBundle\Entity\Tag;
10 use Wallabag\UserBundle\Entity\User;
11 use Wallabag\CoreBundle\Tools\Utils;
12 use Wallabag\CoreBundle\Helper\ContentProxy;
13
14 class WallabagV1Import implements ImportInterface
15 {
16 protected $user;
17 protected $em;
18 protected $logger;
19 protected $contentProxy;
20 protected $skippedEntries = 0;
21 protected $importedEntries = 0;
22 protected $filepath;
23
24 public function __construct(EntityManager $em, ContentProxy $contentProxy)
25 {
26 $this->em = $em;
27 $this->logger = new NullLogger();
28 $this->contentProxy = $contentProxy;
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 {
54 return 'wallabag v1';
55 }
56
57 /**
58 * {@inheritdoc}
59 */
60 public function getUrl()
61 {
62 return 'import_wallabag_v1';
63 }
64
65 /**
66 * {@inheritdoc}
67 */
68 public function getDescription()
69 {
70 return 'This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.';
71 }
72
73 /**
74 * {@inheritdoc}
75 */
76 public function import()
77 {
78 if (!$this->user) {
79 $this->logger->error('WallabagImport: user is not defined');
80
81 return false;
82 }
83
84 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
85 $this->logger->error('WallabagImport: unable to read file', array('filepath' => $this->filepath));
86
87 return false;
88 }
89
90 $data = json_decode(file_get_contents($this->filepath), true);
91
92 if (empty($data)) {
93 return false;
94 }
95
96 $this->parseEntries($data);
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
124 /**
125 * @param $entries
126 */
127 protected function parseEntries($entries)
128 {
129 $i = 1;
130
131 //Untitled in all languages from v1. This should never have been translated
132 $untitled = array('Untitled', 'Sans titre', 'podle nadpisu', 'Sin título', 'با عنوان', 'per titolo', 'Sem título', 'Без названия', 'po naslovu', 'Без назви', 'No title found', '');
133
134 foreach ($entries as $importedEntry) {
135 $existingEntry = $this->em
136 ->getRepository('WallabagCoreBundle:Entry')
137 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
138
139 if (false !== $existingEntry) {
140 ++$this->skippedEntries;
141 continue;
142 }
143
144 // @see ContentProxy->updateEntry
145 $entry = new Entry($this->user);
146 $entry->setUrl($importedEntry['url']);
147 if (in_array($importedEntry['title'], $untitled)) {
148 $entry = $this->contentProxy->updateEntry($entry, $importedEntry['url']);
149 } else {
150 $entry->setContent($importedEntry['content']);
151 $entry->setTitle($importedEntry['title']);
152 $entry->setReadingTime(Utils::getReadingTime($importedEntry['content']));
153 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST));
154 }
155 if (array_key_exists('tags', $importedEntry) && $importedEntry['tags'] != '') {
156 $tags = explode(',', $importedEntry['tags']);
157 $this->assignTagsToEntry($entry, $tags);
158 }
159 $entry->setArchived($importedEntry['is_read']);
160 $entry->setStarred($importedEntry['is_fav']);
161
162 $this->em->persist($entry);
163 ++$this->importedEntries;
164
165 // flush every 20 entries
166 if (($i % 20) === 0) {
167 $this->em->flush();
168 }
169 ++$i;
170 }
171
172 $this->em->flush();
173 }
174
175 private function assignTagsToEntry(Entry $entry, $tags)
176 {
177 foreach ($tags as $tag) {
178 $label = trim($tag);
179 $tagEntity = $this->em
180 ->getRepository('WallabagCoreBundle:Tag')
181 ->findOneByLabel($label);
182 if (is_object($tagEntity)) {
183 $entry->addTag($tagEntity);
184 } else {
185 $newTag = new Tag();
186 $newTag->setLabel($label);
187 $entry->addTag($newTag);
188 }
189 $this->em->flush();
190 }
191 }
192 }