]>
Commit | Line | Data |
---|---|---|
03e3753f JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
5 | use Wallabag\CoreBundle\Entity\Entry; | |
6 | use Wallabag\UserBundle\Entity\User; | |
7 | ||
8 | class ReadabilityImport extends AbstractImport | |
9 | { | |
10 | private $user; | |
11 | private $client; | |
12 | private $skippedEntries = 0; | |
13 | private $importedEntries = 0; | |
14 | private $filepath; | |
15 | private $markAsRead; | |
16 | ||
17 | /** | |
18 | * We define the user in a custom call because on the import command there is no logged in user. | |
19 | * So we can't retrieve user from the `security.token_storage` service. | |
20 | * | |
21 | * @param User $user | |
22 | */ | |
23 | public function setUser(User $user) | |
24 | { | |
25 | $this->user = $user; | |
26 | ||
27 | return $this; | |
28 | } | |
29 | ||
30 | /** | |
31 | * {@inheritdoc} | |
32 | */ | |
33 | public function getName() | |
34 | { | |
35 | return 'Readability'; | |
36 | } | |
37 | ||
38 | /** | |
39 | * {@inheritdoc} | |
40 | */ | |
41 | public function getUrl() | |
42 | { | |
43 | return 'import_readability'; | |
44 | } | |
45 | ||
46 | /** | |
47 | * {@inheritdoc} | |
48 | */ | |
49 | public function getDescription() | |
50 | { | |
51 | return 'import.readability.description'; | |
52 | } | |
53 | ||
54 | /** | |
55 | * Set file path to the json file. | |
56 | * | |
57 | * @param string $filepath | |
58 | */ | |
59 | public function setFilepath($filepath) | |
60 | { | |
61 | $this->filepath = $filepath; | |
62 | ||
63 | return $this; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Set whether articles must be all marked as read. | |
68 | * | |
69 | * @param bool $markAsRead | |
70 | */ | |
71 | public function setMarkAsRead($markAsRead) | |
72 | { | |
73 | $this->markAsRead = $markAsRead; | |
74 | ||
75 | return $this; | |
76 | } | |
77 | ||
78 | /** | |
79 | * Get whether articles must be all marked as read. | |
80 | */ | |
81 | public function getMarkAsRead() | |
82 | { | |
83 | return $this->markAsRead; | |
84 | } | |
85 | ||
86 | /** | |
87 | * {@inheritdoc} | |
88 | */ | |
89 | public function getSummary() | |
90 | { | |
91 | return [ | |
92 | 'skipped' => $this->skippedEntries, | |
93 | 'imported' => $this->importedEntries, | |
94 | ]; | |
95 | } | |
96 | ||
97 | /** | |
98 | * {@inheritdoc} | |
99 | */ | |
100 | public function import() | |
101 | { | |
102 | if (!$this->user) { | |
103 | $this->logger->error('ReadabilityImport: user is not defined'); | |
104 | ||
105 | return false; | |
106 | } | |
107 | ||
108 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
109 | $this->logger->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath]); | |
110 | ||
111 | return false; | |
112 | } | |
113 | ||
114 | $data = json_decode(file_get_contents($this->filepath), true); | |
115 | ||
116 | if (empty($data) || empty($data['bookmarks'])) { | |
117 | return false; | |
118 | } | |
119 | ||
120 | $this->parseEntries($data['bookmarks']); | |
121 | ||
122 | return true; | |
123 | } | |
124 | ||
125 | /** | |
126 | * Parse and insert all given entries. | |
127 | * | |
128 | * @param $entries | |
129 | */ | |
130 | protected function parseEntries($entries) | |
131 | { | |
132 | $i = 1; | |
133 | ||
134 | foreach ($entries as $importedEntry) { | |
135 | $existingEntry = $this->em | |
136 | ->getRepository('WallabagCoreBundle:Entry') | |
137 | ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId()); | |
138 | ||
139 | if (false !== $existingEntry) { | |
140 | ++$this->skippedEntries; | |
141 | continue; | |
142 | } | |
143 | ||
144 | $data = [ | |
145 | 'title' => $importedEntry['article__title'], | |
146 | // 'html' => $importedEntry['article__excerpt'], | |
147 | 'url' => $importedEntry['article__url'], | |
148 | 'content_type' => '', | |
149 | 'language' => '', | |
150 | 'is_archived' => $importedEntry['archive'] || $this->markAsRead, | |
151 | 'is_starred' => $importedEntry['favorite'], | |
152 | ]; | |
153 | ||
154 | $entry = $this->fetchContent( | |
155 | new Entry($this->user), | |
156 | $data['url'], | |
157 | $data | |
158 | ); | |
159 | ||
160 | // jump to next entry in case of problem while getting content | |
161 | if (false === $entry) { | |
162 | ++$this->skippedEntries; | |
163 | continue; | |
164 | } | |
165 | $entry->setArchived($data['is_archived']); | |
166 | $entry->setStarred($data['is_starred']); | |
167 | ||
168 | $this->em->persist($entry); | |
169 | ++$this->importedEntries; | |
170 | ||
171 | // flush every 20 entries | |
172 | if (($i % 20) === 0) { | |
173 | $this->em->flush(); | |
174 | $this->em->clear($entry); | |
175 | } | |
176 | ++$i; | |
177 | } | |
178 | ||
179 | $this->em->flush(); | |
180 | } | |
181 | } |