]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/WallabagV1Import.php
Rewrote Wallabag v1 import
[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;
11
12class WallabagV1Import implements ImportInterface
13{
14 private $user;
15 private $em;
16 private $logger;
17 private $skippedEntries = 0;
18 private $importedEntries = 0;
19 private $filepath;
20
21 public function __construct(EntityManager $em)
22 {
23 $this->em = $em;
24 $this->logger = new NullLogger();
25 }
26
27 public function setLogger(LoggerInterface $logger)
28 {
29 $this->logger = $logger;
30 }
31
32 /**
33 * We define the user in a custom call because on the import command there is no logged in user.
34 * So we can't retrieve user from the `security.token_storage` service.
35 *
36 * @param User $user
37 */
38 public function setUser(User $user)
39 {
40 $this->user = $user;
41
42 return $this;
43 }
44
45 /**
46 * {@inheritdoc}
47 */
48 public function getName()
49 {
50 return 'Wallabag v1';
51 }
52
53 /**
54 * {@inheritdoc}
55 */
56 public function getDescription()
57 {
58 return 'This importer will import all your wallabag v1 articles.';
59 }
60
61 /**
62 * {@inheritdoc}
63 */
64 public function import()
65 {
66 if (!$this->user) {
67 $this->logger->error('WallabagV1Import: user is not defined');
68
69 return false;
70 }
71
72 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
73 $this->logger->error('WallabagV1Import: unable to read file', array('filepath' => $this->filepath));
74
75 return false;
76 }
77
78 $this->parseEntries(json_decode(file_get_contents($this->filepath), true));
79
80 return true;
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function getSummary()
87 {
88 return [
89 'skipped' => $this->skippedEntries,
90 'imported' => $this->importedEntries,
91 ];
92 }
93
94 /**
95 * Set file path to the json file.
96 *
97 * @param string $filepath
98 */
99 public function setFilepath($filepath)
100 {
101 $this->filepath = $filepath;
102
103 return $this;
104 }
105
106 /**
107 * @param $entries
108 */
109 private function parseEntries($entries)
110 {
111 foreach ($entries as $importedEntry) {
112 $existingEntry = $this->em
113 ->getRepository('WallabagCoreBundle:Entry')
114 ->existByUrlAndUserId($importedEntry['url'], $this->user->getId());
115
116 if (false !== $existingEntry) {
117 ++$this->skippedEntries;
118 continue;
119 }
120
121 // @see ContentProxy->updateEntry
122 $entry = new Entry($this->user);
123 $entry->setUrl($importedEntry['url']);
124 $entry->setTitle($importedEntry['title']);
125 $entry->setArchived($importedEntry['is_read']);
126 $entry->setStarred($importedEntry['is_fav']);
127 $entry->setContent($importedEntry['content']);
128 $entry->setReadingTime(Utils::getReadingTime($importedEntry['content']));
129 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST));
130
131 $this->em->persist($entry);
132 ++$this->importedEntries;
133 }
134
135 $this->em->flush();
136 }
137}