]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/InstapaperImport.php
Imported entries which fail to fetch get standard error body
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / InstapaperImport.php
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 'html' => false,
80 ];
81 }
82 fclose($handle);
83
84 if (empty($entries)) {
85 $this->logger->error('InstapaperImport: no entries in imported file');
86
87 return false;
88 }
89
90 if ($this->producer) {
91 $this->parseEntriesForProducer($entries);
92
93 return true;
94 }
95
96 $this->parseEntries($entries);
97
98 return true;
99 }
100
101 /**
102 * {@inheritdoc}
103 */
104 public function parseEntry(array $importedEntry)
105 {
106 $existingEntry = $this->em
107 ->getRepository('WallabagCoreBundle:Entry')
108 ->findByUrlAndUserId($importedEntry['url'], $this->user->getId());
109
110 if (false !== $existingEntry) {
111 ++$this->skippedEntries;
112
113 return;
114 }
115
116 $entry = new Entry($this->user);
117 $entry->setUrl($importedEntry['url']);
118 $entry->setTitle($importedEntry['title']);
119
120 // update entry with content (in case fetching failed, the given entry will be return)
121 $entry = $this->fetchContent($entry, $importedEntry['url'], $importedEntry);
122
123 $entry->setArchived($importedEntry['is_archived']);
124 $entry->setStarred($importedEntry['is_starred']);
125
126 $this->em->persist($entry);
127 ++$this->importedEntries;
128
129 return $entry;
130 }
131
132 /**
133 * {@inheritdoc}
134 */
135 protected function setEntryAsRead(array $importedEntry)
136 {
137 $importedEntry['is_archived'] = 1;
138
139 return $importedEntry;
140 }
141 }