From ff1a5362f7254d686864ea53994da6c517b3d3e8 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 27 Sep 2016 07:57:53 +0200 Subject: Add Instapaper import Also update ImportController with latest import (chrome, firefox & instapaper). --- .../ImportBundle/Import/InstapaperImport.php | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/Wallabag/ImportBundle/Import/InstapaperImport.php (limited to 'src/Wallabag/ImportBundle/Import/InstapaperImport.php') 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 @@ +filepath = $filepath; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function import() + { + if (!$this->user) { + $this->logger->error('InstapaperImport: user is not defined'); + + return false; + } + + if (!file_exists($this->filepath) || !is_readable($this->filepath)) { + $this->logger->error('InstapaperImport: unable to read file', ['filepath' => $this->filepath]); + + return false; + } + + $entries = []; + $handle = fopen($this->filepath, 'r'); + while (($data = fgetcsv($handle, 10240)) !== false) { + if ('URL' === $data[0]) { + continue; + } + + $entries[] = [ + 'url' => $data[0], + 'title' => $data[1], + 'status' => $data[3], + 'is_archived' => $data[3] === 'Archive' || $data[3] === 'Starred', + 'is_starred' => $data[3] === 'Starred', + 'content_type' => '', + 'language' => '', + ]; + } + fclose($handle); + + if ($this->producer) { + $this->parseEntriesForProducer($entries); + + return true; + } + + $this->parseEntries($entries); + + return true; + } + + /** + * {@inheritdoc} + */ + public function parseEntry(array $importedEntry) + { + $existingEntry = $this->em + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); + + if (false !== $existingEntry) { + ++$this->skippedEntries; + + return; + } + + $entry = new Entry($this->user); + $entry->setUrl($importedEntry['url']); + $entry->setTitle($importedEntry['title']); + + // update entry with content (in case fetching failed, the given entry will be return) + $entry = $this->fetchContent($entry, $importedEntry['url'], $importedEntry); + + $entry->setArchived($importedEntry['is_archived']); + $entry->setStarred($importedEntry['is_starred']); + + $this->em->persist($entry); + ++$this->importedEntries; + + return $entry; + } + + /** + * {@inheritdoc} + */ + protected function setEntryAsRead(array $importedEntry) + { + $importedEntry['is_archived'] = 1; + + return $importedEntry; + } +} -- cgit v1.2.3 From c7ea9b41f32f222fef6a59734ea0b1176bfa1f41 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 27 Sep 2016 17:01:14 +0200 Subject: Add controller test for Instapaper --- src/Wallabag/ImportBundle/Import/InstapaperImport.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/Wallabag/ImportBundle/Import/InstapaperImport.php') diff --git a/src/Wallabag/ImportBundle/Import/InstapaperImport.php b/src/Wallabag/ImportBundle/Import/InstapaperImport.php index 356acf23..cf4c785c 100644 --- a/src/Wallabag/ImportBundle/Import/InstapaperImport.php +++ b/src/Wallabag/ImportBundle/Import/InstapaperImport.php @@ -80,6 +80,12 @@ class InstapaperImport extends AbstractImport } fclose($handle); + if (empty($entries)) { + $this->logger->error('InstapaperImport: no entries in imported file'); + + return false; + } + if ($this->producer) { $this->parseEntriesForProducer($entries); -- cgit v1.2.3