3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
7 class InstapaperImport
extends AbstractImport
14 public function getName()
22 public function getUrl()
24 return 'import_instapaper';
30 public function getDescription()
32 return 'import.instapaper.description';
36 * Set file path to the json file.
38 * @param string $filepath
40 public function setFilepath($filepath)
42 $this->filepath
= $filepath;
50 public function import()
53 $this->logger
->error('InstapaperImport: user is not defined');
58 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
59 $this->logger
->error('InstapaperImport: unable to read file', ['filepath' => $this->filepath
]);
65 $handle = fopen($this->filepath
, 'r');
66 while (false !== ($data = fgetcsv($handle, 10240))) {
67 if ('URL' === $data[0]) {
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
75 if (false === \
in_array($data[3], ['Archive', 'Unread', 'Starred'], true)) {
82 'is_archived' => 'Archive' === $data[3] || 'Starred' === $data[3],
83 'is_starred' => 'Starred' === $data[3],
90 if (empty($entries)) {
91 $this->logger
->error('InstapaperImport: no entries in imported file');
96 // most recent articles are first, which means we should create them at the end so they will show up first
97 // as Instapaper doesn't export the creation date of the article
98 $entries = array_reverse($entries);
100 if ($this->producer
) {
101 $this->parseEntriesForProducer($entries);
106 $this->parseEntries($entries);
114 public function validateEntry(array $importedEntry)
116 if (empty($importedEntry['url'])) {
126 public function parseEntry(array $importedEntry)
128 $existingEntry = $this->em
129 ->getRepository('WallabagCoreBundle:Entry')
130 ->findByUrlAndUserId($importedEntry['url'], $this->user
->getId());
132 if (false !== $existingEntry) {
133 ++
$this->skippedEntries
;
138 $entry = new Entry($this->user
);
139 $entry->setUrl($importedEntry['url']);
140 $entry->setTitle($importedEntry['title']);
142 // update entry with content (in case fetching failed, the given entry will be return)
143 $this->fetchContent($entry, $importedEntry['url'], $importedEntry);
145 if (!empty($importedEntry['tags'])) {
146 $this->tagsAssigner
->assignTagsToEntry(
148 $importedEntry['tags'],
149 $this->em
->getUnitOfWork()->getScheduledEntityInsertions()
153 $entry->updateArchived($importedEntry['is_archived']);
154 $entry->setStarred($importedEntry['is_starred']);
156 $this->em
->persist($entry);
157 ++
$this->importedEntries
;
165 protected function setEntryAsRead(array $importedEntry)
167 $importedEntry['is_archived'] = 1;
169 return $importedEntry;