]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
5 | use Wallabag\CoreBundle\Entity\Entry; | |
6 | ||
7 | class InstapaperImport extends AbstractImport | |
8 | { | |
9 | private $filepath; | |
10 | ||
11 | /** | |
12 | * {@inheritdoc} | |
13 | */ | |
14 | public function getName() | |
15 | { | |
16 | return 'Instapaper'; | |
17 | } | |
18 | ||
19 | /** | |
20 | * {@inheritdoc} | |
21 | */ | |
22 | public function getUrl() | |
23 | { | |
24 | return 'import_instapaper'; | |
25 | } | |
26 | ||
27 | /** | |
28 | * {@inheritdoc} | |
29 | */ | |
30 | public function getDescription() | |
31 | { | |
32 | return 'import.instapaper.description'; | |
33 | } | |
34 | ||
35 | /** | |
36 | * Set file path to the json file. | |
37 | * | |
38 | * @param string $filepath | |
39 | */ | |
40 | public function setFilepath($filepath) | |
41 | { | |
42 | $this->filepath = $filepath; | |
43 | ||
44 | return $this; | |
45 | } | |
46 | ||
47 | /** | |
48 | * {@inheritdoc} | |
49 | */ | |
50 | public function import() | |
51 | { | |
52 | if (!$this->user) { | |
53 | $this->logger->error('InstapaperImport: user is not defined'); | |
54 | ||
55 | return false; | |
56 | } | |
57 | ||
58 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
59 | $this->logger->error('InstapaperImport: unable to read file', ['filepath' => $this->filepath]); | |
60 | ||
61 | return false; | |
62 | } | |
63 | ||
64 | $entries = []; | |
65 | $handle = fopen($this->filepath, 'r'); | |
66 | while (false !== ($data = fgetcsv($handle, 10240))) { | |
67 | if ('URL' === $data[0]) { | |
68 | continue; | |
69 | } | |
70 | ||
71 | // last element in the csv is the folder where the content belong | |
72 | // BUT it can also be the status (since status = folder in Instapaper) | |
73 | // and we don't want archive, unread & starred to become a tag | |
74 | $tags = null; | |
75 | if (false === \in_array($data[3], ['Archive', 'Unread', 'Starred'], true)) { | |
76 | $tags = [$data[3]]; | |
77 | } | |
78 | ||
79 | $entries[] = [ | |
80 | 'url' => $data[0], | |
81 | 'title' => $data[1], | |
82 | 'is_archived' => 'Archive' === $data[3] || 'Starred' === $data[3], | |
83 | 'is_starred' => 'Starred' === $data[3], | |
84 | 'html' => false, | |
85 | 'tags' => $tags, | |
86 | ]; | |
87 | } | |
88 | fclose($handle); | |
89 | ||
90 | if (empty($entries)) { | |
91 | $this->logger->error('InstapaperImport: no entries in imported file'); | |
92 | ||
93 | return false; | |
94 | } | |
95 | ||
96 | // most recent articles are first, which means we should create them at the end so they will show up first | |
97 | // as Instapaper doesn't export the creation date of the article | |
98 | $entries = array_reverse($entries); | |
99 | ||
100 | if ($this->producer) { | |
101 | $this->parseEntriesForProducer($entries); | |
102 | ||
103 | return true; | |
104 | } | |
105 | ||
106 | $this->parseEntries($entries); | |
107 | ||
108 | return true; | |
109 | } | |
110 | ||
111 | /** | |
112 | * {@inheritdoc} | |
113 | */ | |
114 | public function validateEntry(array $importedEntry) | |
115 | { | |
116 | if (empty($importedEntry['url'])) { | |
117 | return false; | |
118 | } | |
119 | ||
120 | return true; | |
121 | } | |
122 | ||
123 | /** | |
124 | * {@inheritdoc} | |
125 | */ | |
126 | public function parseEntry(array $importedEntry) | |
127 | { | |
128 | $existingEntry = $this->em | |
129 | ->getRepository('WallabagCoreBundle:Entry') | |
130 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | |
131 | ||
132 | if (false !== $existingEntry) { | |
133 | ++$this->skippedEntries; | |
134 | ||
135 | return; | |
136 | } | |
137 | ||
138 | $entry = new Entry($this->user); | |
139 | $entry->setUrl($importedEntry['url']); | |
140 | $entry->setTitle($importedEntry['title']); | |
141 | ||
142 | // update entry with content (in case fetching failed, the given entry will be return) | |
143 | $this->fetchContent($entry, $importedEntry['url'], $importedEntry); | |
144 | ||
145 | if (!empty($importedEntry['tags'])) { | |
146 | $this->tagsAssigner->assignTagsToEntry( | |
147 | $entry, | |
148 | $importedEntry['tags'], | |
149 | $this->em->getUnitOfWork()->getScheduledEntityInsertions() | |
150 | ); | |
151 | } | |
152 | ||
153 | $entry->updateArchived($importedEntry['is_archived']); | |
154 | $entry->setStarred($importedEntry['is_starred']); | |
155 | ||
156 | $this->em->persist($entry); | |
157 | ++$this->importedEntries; | |
158 | ||
159 | return $entry; | |
160 | } | |
161 | ||
162 | /** | |
163 | * {@inheritdoc} | |
164 | */ | |
165 | protected function setEntryAsRead(array $importedEntry) | |
166 | { | |
167 | $importedEntry['is_archived'] = 1; | |
168 | ||
169 | return $importedEntry; | |
170 | } | |
171 | } |