diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-11-04 22:44:31 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-11-04 22:44:31 +0100 |
commit | 9ab024b4f5388e2a41c50a8a2b79e4033788782a (patch) | |
tree | ae01ddb121b5037aae89e892d1b971208b5108dd /src/Wallabag/ImportBundle/Import | |
parent | b5571b52ccbc11d71637e1ccb07421cf606ac846 (diff) | |
download | wallabag-9ab024b4f5388e2a41c50a8a2b79e4033788782a.tar.gz wallabag-9ab024b4f5388e2a41c50a8a2b79e4033788782a.tar.zst wallabag-9ab024b4f5388e2a41c50a8a2b79e4033788782a.zip |
Add Pinboard import
Diffstat (limited to 'src/Wallabag/ImportBundle/Import')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/PinboardImport.php | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Import/PinboardImport.php b/src/Wallabag/ImportBundle/Import/PinboardImport.php new file mode 100644 index 00000000..9bcfbc36 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/PinboardImport.php | |||
@@ -0,0 +1,143 @@ | |||
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 parseEntry(array $importedEntry) | ||
87 | { | ||
88 | $existingEntry = $this->em | ||
89 | ->getRepository('WallabagCoreBundle:Entry') | ||
90 | ->findByUrlAndUserId($importedEntry['href'], $this->user->getId()); | ||
91 | |||
92 | if (false !== $existingEntry) { | ||
93 | ++$this->skippedEntries; | ||
94 | |||
95 | return; | ||
96 | } | ||
97 | |||
98 | $data = [ | ||
99 | 'title' => $importedEntry['description'], | ||
100 | 'url' => $importedEntry['href'], | ||
101 | 'content_type' => '', | ||
102 | 'language' => '', | ||
103 | 'is_archived' => ('no' === $importedEntry['toread']) || $this->markAsRead, | ||
104 | 'is_starred' => false, | ||
105 | 'created_at' => $importedEntry['time'], | ||
106 | 'tags' => explode(' ', $importedEntry['tags']), | ||
107 | ]; | ||
108 | |||
109 | $entry = new Entry($this->user); | ||
110 | $entry->setUrl($data['url']); | ||
111 | $entry->setTitle($data['title']); | ||
112 | |||
113 | // update entry with content (in case fetching failed, the given entry will be return) | ||
114 | $entry = $this->fetchContent($entry, $data['url'], $data); | ||
115 | |||
116 | if (!empty($data['tags'])) { | ||
117 | $this->contentProxy->assignTagsToEntry( | ||
118 | $entry, | ||
119 | $data['tags'], | ||
120 | $this->em->getUnitOfWork()->getScheduledEntityInsertions() | ||
121 | ); | ||
122 | } | ||
123 | |||
124 | $entry->setArchived($data['is_archived']); | ||
125 | $entry->setStarred($data['is_starred']); | ||
126 | $entry->setCreatedAt(new \DateTime($data['created_at'])); | ||
127 | |||
128 | $this->em->persist($entry); | ||
129 | ++$this->importedEntries; | ||
130 | |||
131 | return $entry; | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * {@inheritdoc} | ||
136 | */ | ||
137 | protected function setEntryAsRead(array $importedEntry) | ||
138 | { | ||
139 | $importedEntry['toread'] = 'no'; | ||
140 | |||
141 | return $importedEntry; | ||
142 | } | ||
143 | } | ||