]>
Commit | Line | Data |
---|---|---|
ff1a5362 JB |
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'); | |
3ef055ce | 66 | while (false !== ($data = fgetcsv($handle, 10240))) { |
ff1a5362 JB |
67 | if ('URL' === $data[0]) { |
68 | continue; | |
69 | } | |
70 | ||
7a8ed3ce JB |
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; | |
f808b016 | 75 | if (false === in_array($data[3], ['Archive', 'Unread', 'Starred'], true)) { |
7a8ed3ce JB |
76 | $tags = [$data[3]]; |
77 | } | |
78 | ||
ff1a5362 JB |
79 | $entries[] = [ |
80 | 'url' => $data[0], | |
81 | 'title' => $data[1], | |
82 | 'status' => $data[3], | |
3ef055ce JB |
83 | 'is_archived' => 'Archive' === $data[3] || 'Starred' === $data[3], |
84 | 'is_starred' => 'Starred' === $data[3], | |
36e6ef52 | 85 | 'html' => false, |
7a8ed3ce | 86 | 'tags' => $tags, |
ff1a5362 JB |
87 | ]; |
88 | } | |
89 | fclose($handle); | |
90 | ||
c7ea9b41 JB |
91 | if (empty($entries)) { |
92 | $this->logger->error('InstapaperImport: no entries in imported file'); | |
93 | ||
94 | return false; | |
95 | } | |
96 | ||
ff1a5362 JB |
97 | if ($this->producer) { |
98 | $this->parseEntriesForProducer($entries); | |
99 | ||
100 | return true; | |
101 | } | |
102 | ||
103 | $this->parseEntries($entries); | |
104 | ||
105 | return true; | |
106 | } | |
107 | ||
108 | /** | |
109 | * {@inheritdoc} | |
110 | */ | |
111 | public function parseEntry(array $importedEntry) | |
112 | { | |
113 | $existingEntry = $this->em | |
114 | ->getRepository('WallabagCoreBundle:Entry') | |
115 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | |
116 | ||
117 | if (false !== $existingEntry) { | |
118 | ++$this->skippedEntries; | |
119 | ||
120 | return; | |
121 | } | |
122 | ||
123 | $entry = new Entry($this->user); | |
124 | $entry->setUrl($importedEntry['url']); | |
125 | $entry->setTitle($importedEntry['title']); | |
126 | ||
127 | // update entry with content (in case fetching failed, the given entry will be return) | |
7aba665e | 128 | $this->fetchContent($entry, $importedEntry['url'], $importedEntry); |
ff1a5362 | 129 | |
7a8ed3ce JB |
130 | if (!empty($importedEntry['tags'])) { |
131 | $this->tagsAssigner->assignTagsToEntry( | |
132 | $entry, | |
133 | $importedEntry['tags'], | |
134 | $this->em->getUnitOfWork()->getScheduledEntityInsertions() | |
135 | ); | |
136 | } | |
137 | ||
ff1a5362 JB |
138 | $entry->setArchived($importedEntry['is_archived']); |
139 | $entry->setStarred($importedEntry['is_starred']); | |
140 | ||
141 | $this->em->persist($entry); | |
142 | ++$this->importedEntries; | |
143 | ||
144 | return $entry; | |
145 | } | |
146 | ||
147 | /** | |
148 | * {@inheritdoc} | |
149 | */ | |
150 | protected function setEntryAsRead(array $importedEntry) | |
151 | { | |
152 | $importedEntry['is_archived'] = 1; | |
153 | ||
154 | return $importedEntry; | |
155 | } | |
156 | } |