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