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, 'rb'); |
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
|
|
|
'status' => $data[3], |
83
|
|
|
'is_archived' => 'Archive' === $data[3] || 'Starred' === $data[3], |
84
|
|
|
'is_starred' => 'Starred' === $data[3], |
85
|
|
|
'html' => false, |
86
|
|
|
'tags' => $tags, |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
fclose($handle); |
90
|
|
|
|
91
|
|
|
if (empty($entries)) { |
92
|
|
|
$this->logger->error('InstapaperImport: no entries in imported file'); |
93
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
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 validateEntry(array $importedEntry) |
112
|
|
|
{ |
113
|
|
|
if (empty($importedEntry['url'])) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function parseEntry(array $importedEntry) |
124
|
|
|
{ |
125
|
|
|
$existingEntry = $this->em |
126
|
|
|
->getRepository('WallabagCoreBundle:Entry') |
127
|
|
|
->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); |
128
|
|
|
|
129
|
|
|
if (false !== $existingEntry) { |
130
|
|
|
++$this->skippedEntries; |
131
|
|
|
|
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$entry = new Entry($this->user); |
136
|
|
|
$entry->setUrl($importedEntry['url']); |
137
|
|
|
$entry->setTitle($importedEntry['title']); |
138
|
|
|
|
139
|
|
|
// update entry with content (in case fetching failed, the given entry will be return) |
140
|
|
|
$this->fetchContent($entry, $importedEntry['url'], $importedEntry); |
141
|
|
|
|
142
|
|
|
if (!empty($importedEntry['tags'])) { |
143
|
|
|
$this->tagsAssigner->assignTagsToEntry( |
144
|
|
|
$entry, |
145
|
|
|
$importedEntry['tags'], |
146
|
|
|
$this->em->getUnitOfWork()->getScheduledEntityInsertions() |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$entry->setArchived($importedEntry['is_archived']); |
151
|
|
|
$entry->setStarred($importedEntry['is_starred']); |
152
|
|
|
|
153
|
|
|
$this->em->persist($entry); |
154
|
|
|
++$this->importedEntries; |
155
|
|
|
|
156
|
|
|
return $entry; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
protected function setEntryAsRead(array $importedEntry) |
163
|
|
|
{ |
164
|
|
|
$importedEntry['is_archived'] = 1; |
165
|
|
|
|
166
|
|
|
return $importedEntry; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|