]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/InstapaperImport.php
Merge pull request #3526 from wallabag/add-random-article
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / InstapaperImport.php
CommitLineData
ff1a5362
JB
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Wallabag\CoreBundle\Entity\Entry;
6
7class 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 = [];
3afc8742 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;
2a1ceb67 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],
3ef055ce
JB
82 'is_archived' => 'Archive' === $data[3] || 'Starred' === $data[3],
83 'is_starred' => 'Starred' === $data[3],
36e6ef52 84 'html' => false,
7a8ed3ce 85 'tags' => $tags,
ff1a5362
JB
86 ];
87 }
88 fclose($handle);
89
c7ea9b41
JB
90 if (empty($entries)) {
91 $this->logger->error('InstapaperImport: no entries in imported file');
92
93 return false;
94 }
95
ff1a5362
JB
96 if ($this->producer) {
97 $this->parseEntriesForProducer($entries);
98
99 return true;
100 }
101
102 $this->parseEntries($entries);
103
104 return true;
105 }
106
9f8f188d
JB
107 /**
108 * {@inheritdoc}
109 */
110 public function validateEntry(array $importedEntry)
111 {
112 if (empty($importedEntry['url'])) {
113 return false;
114 }
115
116 return true;
117 }
118
ff1a5362
JB
119 /**
120 * {@inheritdoc}
121 */
122 public function parseEntry(array $importedEntry)
123 {
124 $existingEntry = $this->em
125 ->getRepository('WallabagCoreBundle:Entry')
126 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
127
128 if (false !== $existingEntry) {
129 ++$this->skippedEntries;
130
131 return;
132 }
133
134 $entry = new Entry($this->user);
135 $entry->setUrl($importedEntry['url']);
136 $entry->setTitle($importedEntry['title']);
137
138 // update entry with content (in case fetching failed, the given entry will be return)
7aba665e 139 $this->fetchContent($entry, $importedEntry['url'], $importedEntry);
ff1a5362 140
7a8ed3ce
JB
141 if (!empty($importedEntry['tags'])) {
142 $this->tagsAssigner->assignTagsToEntry(
143 $entry,
144 $importedEntry['tags'],
145 $this->em->getUnitOfWork()->getScheduledEntityInsertions()
146 );
147 }
148
7975395d 149 $entry->updateArchived($importedEntry['is_archived']);
ff1a5362
JB
150 $entry->setStarred($importedEntry['is_starred']);
151
152 $this->em->persist($entry);
153 ++$this->importedEntries;
154
155 return $entry;
156 }
157
158 /**
159 * {@inheritdoc}
160 */
161 protected function setEntryAsRead(array $importedEntry)
162 {
163 $importedEntry['is_archived'] = 1;
164
165 return $importedEntry;
166 }
167}