diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-09-27 07:57:53 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-09-27 20:27:08 +0200 |
commit | ff1a5362f7254d686864ea53994da6c517b3d3e8 (patch) | |
tree | 1edc0b6b46cf12bd17918355f7dbe090324e6f70 /src/Wallabag/ImportBundle/Import | |
parent | 55345331c47ca6f389e38a842584c3062476601d (diff) | |
download | wallabag-ff1a5362f7254d686864ea53994da6c517b3d3e8.tar.gz wallabag-ff1a5362f7254d686864ea53994da6c517b3d3e8.tar.zst wallabag-ff1a5362f7254d686864ea53994da6c517b3d3e8.zip |
Add Instapaper import
Also update ImportController with latest import (chrome, firefox & instapaper).
Diffstat (limited to 'src/Wallabag/ImportBundle/Import')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/AbstractImport.php | 4 | ||||
-rw-r--r-- | src/Wallabag/ImportBundle/Import/InstapaperImport.php | 134 |
2 files changed, 138 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Import/AbstractImport.php b/src/Wallabag/ImportBundle/Import/AbstractImport.php index a1a14576..764b390a 100644 --- a/src/Wallabag/ImportBundle/Import/AbstractImport.php +++ b/src/Wallabag/ImportBundle/Import/AbstractImport.php | |||
@@ -106,6 +106,10 @@ abstract class AbstractImport implements ImportInterface | |||
106 | $i = 1; | 106 | $i = 1; |
107 | 107 | ||
108 | foreach ($entries as $importedEntry) { | 108 | foreach ($entries as $importedEntry) { |
109 | if ($this->markAsRead) { | ||
110 | $importedEntry = $this->setEntryAsRead($importedEntry); | ||
111 | } | ||
112 | |||
109 | $entry = $this->parseEntry($importedEntry); | 113 | $entry = $this->parseEntry($importedEntry); |
110 | 114 | ||
111 | if (null === $entry) { | 115 | if (null === $entry) { |
diff --git a/src/Wallabag/ImportBundle/Import/InstapaperImport.php b/src/Wallabag/ImportBundle/Import/InstapaperImport.php new file mode 100644 index 00000000..356acf23 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/InstapaperImport.php | |||
@@ -0,0 +1,134 @@ | |||
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 (($data = fgetcsv($handle, 10240)) !== false) { | ||
67 | if ('URL' === $data[0]) { | ||
68 | continue; | ||
69 | } | ||
70 | |||
71 | $entries[] = [ | ||
72 | 'url' => $data[0], | ||
73 | 'title' => $data[1], | ||
74 | 'status' => $data[3], | ||
75 | 'is_archived' => $data[3] === 'Archive' || $data[3] === 'Starred', | ||
76 | 'is_starred' => $data[3] === 'Starred', | ||
77 | 'content_type' => '', | ||
78 | 'language' => '', | ||
79 | ]; | ||
80 | } | ||
81 | fclose($handle); | ||
82 | |||
83 | if ($this->producer) { | ||
84 | $this->parseEntriesForProducer($entries); | ||
85 | |||
86 | return true; | ||
87 | } | ||
88 | |||
89 | $this->parseEntries($entries); | ||
90 | |||
91 | return true; | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * {@inheritdoc} | ||
96 | */ | ||
97 | public function parseEntry(array $importedEntry) | ||
98 | { | ||
99 | $existingEntry = $this->em | ||
100 | ->getRepository('WallabagCoreBundle:Entry') | ||
101 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | ||
102 | |||
103 | if (false !== $existingEntry) { | ||
104 | ++$this->skippedEntries; | ||
105 | |||
106 | return; | ||
107 | } | ||
108 | |||
109 | $entry = new Entry($this->user); | ||
110 | $entry->setUrl($importedEntry['url']); | ||
111 | $entry->setTitle($importedEntry['title']); | ||
112 | |||
113 | // update entry with content (in case fetching failed, the given entry will be return) | ||
114 | $entry = $this->fetchContent($entry, $importedEntry['url'], $importedEntry); | ||
115 | |||
116 | $entry->setArchived($importedEntry['is_archived']); | ||
117 | $entry->setStarred($importedEntry['is_starred']); | ||
118 | |||
119 | $this->em->persist($entry); | ||
120 | ++$this->importedEntries; | ||
121 | |||
122 | return $entry; | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * {@inheritdoc} | ||
127 | */ | ||
128 | protected function setEntryAsRead(array $importedEntry) | ||
129 | { | ||
130 | $importedEntry['is_archived'] = 1; | ||
131 | |||
132 | return $importedEntry; | ||
133 | } | ||
134 | } | ||