1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wallabag\ImportBundle\Import; |
4
|
|
|
|
5
|
|
|
use Wallabag\CoreBundle\Entity\Entry; |
6
|
|
|
|
7
|
|
|
class PinboardImport extends AbstractImport |
8
|
|
|
{ |
9
|
|
|
private $filepath; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
public function getName() |
15
|
|
|
{ |
16
|
|
|
return 'Pinboard'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function getUrl() |
23
|
|
|
{ |
24
|
|
|
return 'import_pinboard'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function getDescription() |
31
|
|
|
{ |
32
|
|
|
return 'import.pinboard.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('PinboardImport: user is not defined'); |
54
|
|
|
|
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!file_exists($this->filepath) || !is_readable($this->filepath)) { |
59
|
|
|
$this->logger->error('PinboardImport: unable to read file', ['filepath' => $this->filepath]); |
60
|
|
|
|
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$data = json_decode(file_get_contents($this->filepath), true); |
65
|
|
|
|
66
|
|
|
if (empty($data)) { |
67
|
|
|
$this->logger->error('PinboardImport: no entries in imported file'); |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($this->producer) { |
73
|
|
|
$this->parseEntriesForProducer($data); |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->parseEntries($data); |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function validateEntry(array $importedEntry) |
87
|
|
|
{ |
88
|
|
|
if (empty($importedEntry['href'])) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function parseEntry(array $importedEntry) |
99
|
|
|
{ |
100
|
|
|
$existingEntry = $this->em |
101
|
|
|
->getRepository('WallabagCoreBundle:Entry') |
102
|
|
|
->findByUrlAndUserId($importedEntry['href'], $this->user->getId()); |
103
|
|
|
|
104
|
|
|
if (false !== $existingEntry) { |
105
|
|
|
++$this->skippedEntries; |
106
|
|
|
|
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$data = [ |
111
|
|
|
'title' => $importedEntry['description'], |
112
|
|
|
'url' => $importedEntry['href'], |
113
|
|
|
'is_archived' => ('no' === $importedEntry['toread']) || $this->markAsRead, |
114
|
|
|
'is_starred' => false, |
115
|
|
|
'created_at' => $importedEntry['time'], |
116
|
|
|
'tags' => explode(' ', $importedEntry['tags']), |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
$entry = new Entry($this->user); |
120
|
|
|
$entry->setUrl($data['url']); |
121
|
|
|
$entry->setTitle($data['title']); |
122
|
|
|
|
123
|
|
|
// update entry with content (in case fetching failed, the given entry will be return) |
124
|
|
|
$this->fetchContent($entry, $data['url'], $data); |
125
|
|
|
|
126
|
|
|
if (!empty($data['tags'])) { |
127
|
|
|
$this->tagsAssigner->assignTagsToEntry( |
128
|
|
|
$entry, |
129
|
|
|
$data['tags'], |
130
|
|
|
$this->em->getUnitOfWork()->getScheduledEntityInsertions() |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$entry->setArchived($data['is_archived']); |
135
|
|
|
$entry->setStarred($data['is_starred']); |
136
|
|
|
$entry->setCreatedAt(new \DateTime($data['created_at'])); |
137
|
|
|
|
138
|
|
|
$this->em->persist($entry); |
139
|
|
|
++$this->importedEntries; |
140
|
|
|
|
141
|
|
|
return $entry; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
protected function setEntryAsRead(array $importedEntry) |
148
|
|
|
{ |
149
|
|
|
$importedEntry['toread'] = 'no'; |
150
|
|
|
|
151
|
|
|
return $importedEntry; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|