]>
Commit | Line | Data |
---|---|---|
b1d05721 JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
5 | use Psr\Log\LoggerInterface; | |
6 | use Psr\Log\NullLogger; | |
7 | use Doctrine\ORM\EntityManager; | |
d1af8ad4 | 8 | use Symfony\Component\Translation\TranslatorInterface; |
b1d05721 JB |
9 | use Wallabag\CoreBundle\Entity\Entry; |
10 | use Wallabag\UserBundle\Entity\User; | |
11 | use Wallabag\CoreBundle\Tools\Utils; | |
12 | ||
13 | class WallabagV1Import implements ImportInterface | |
14 | { | |
15 | private $user; | |
16 | private $em; | |
17 | private $logger; | |
18 | private $skippedEntries = 0; | |
19 | private $importedEntries = 0; | |
20 | private $filepath; | |
d1af8ad4 | 21 | private $translator; |
b1d05721 | 22 | |
d1af8ad4 | 23 | public function __construct(EntityManager $em, TranslatorInterface $translator) |
b1d05721 JB |
24 | { |
25 | $this->em = $em; | |
26 | $this->logger = new NullLogger(); | |
d1af8ad4 | 27 | $this->translator = $translator; |
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 | { | |
d1af8ad4 | 69 | return $this->translator->trans('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.'); |
b1d05721 JB |
70 | } |
71 | ||
72 | /** | |
73 | * {@inheritdoc} | |
74 | */ | |
75 | public function import() | |
76 | { | |
77 | if (!$this->user) { | |
78 | $this->logger->error('WallabagV1Import: user is not defined'); | |
79 | ||
80 | return false; | |
81 | } | |
82 | ||
83 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
84 | $this->logger->error('WallabagV1Import: unable to read file', array('filepath' => $this->filepath)); | |
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 | ||
123 | /** | |
124 | * @param $entries | |
125 | */ | |
126 | private function parseEntries($entries) | |
127 | { | |
7019c7cf JB |
128 | $i = 1; |
129 | ||
b1d05721 JB |
130 | foreach ($entries as $importedEntry) { |
131 | $existingEntry = $this->em | |
132 | ->getRepository('WallabagCoreBundle:Entry') | |
133 | ->existByUrlAndUserId($importedEntry['url'], $this->user->getId()); | |
134 | ||
135 | if (false !== $existingEntry) { | |
136 | ++$this->skippedEntries; | |
137 | continue; | |
138 | } | |
139 | ||
140 | // @see ContentProxy->updateEntry | |
141 | $entry = new Entry($this->user); | |
142 | $entry->setUrl($importedEntry['url']); | |
143 | $entry->setTitle($importedEntry['title']); | |
144 | $entry->setArchived($importedEntry['is_read']); | |
145 | $entry->setStarred($importedEntry['is_fav']); | |
146 | $entry->setContent($importedEntry['content']); | |
147 | $entry->setReadingTime(Utils::getReadingTime($importedEntry['content'])); | |
148 | $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST)); | |
149 | ||
150 | $this->em->persist($entry); | |
151 | ++$this->importedEntries; | |
7019c7cf JB |
152 | |
153 | // flush every 20 entries | |
154 | if (($i % 20) === 0) { | |
8eedc8cf | 155 | $this->em->flush(); |
7019c7cf JB |
156 | } |
157 | ++$i; | |
b1d05721 JB |
158 | } |
159 | ||
160 | $this->em->flush(); | |
161 | } | |
162 | } |