From b1d05721cf37ab94ec1a6837fe79cf19474dd0ff Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 30 Dec 2015 13:26:30 +0100 Subject: Rewrote Wallabag v1 import --- .../ImportBundle/Import/WallabagV1Import.php | 137 +++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/Wallabag/ImportBundle/Import/WallabagV1Import.php (limited to 'src/Wallabag/ImportBundle/Import/WallabagV1Import.php') diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php new file mode 100644 index 00000000..7b012674 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php @@ -0,0 +1,137 @@ +em = $em; + $this->logger = new NullLogger(); + } + + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; + } + + /** + * We define the user in a custom call because on the import command there is no logged in user. + * So we can't retrieve user from the `security.token_storage` service. + * + * @param User $user + */ + public function setUser(User $user) + { + $this->user = $user; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'Wallabag v1'; + } + + /** + * {@inheritdoc} + */ + public function getDescription() + { + return 'This importer will import all your wallabag v1 articles.'; + } + + /** + * {@inheritdoc} + */ + public function import() + { + if (!$this->user) { + $this->logger->error('WallabagV1Import: user is not defined'); + + return false; + } + + if (!file_exists($this->filepath) || !is_readable($this->filepath)) { + $this->logger->error('WallabagV1Import: unable to read file', array('filepath' => $this->filepath)); + + return false; + } + + $this->parseEntries(json_decode(file_get_contents($this->filepath), true)); + + return true; + } + + /** + * {@inheritdoc} + */ + public function getSummary() + { + return [ + 'skipped' => $this->skippedEntries, + 'imported' => $this->importedEntries, + ]; + } + + /** + * Set file path to the json file. + * + * @param string $filepath + */ + public function setFilepath($filepath) + { + $this->filepath = $filepath; + + return $this; + } + + /** + * @param $entries + */ + private function parseEntries($entries) + { + foreach ($entries as $importedEntry) { + $existingEntry = $this->em + ->getRepository('WallabagCoreBundle:Entry') + ->existByUrlAndUserId($importedEntry['url'], $this->user->getId()); + + if (false !== $existingEntry) { + ++$this->skippedEntries; + continue; + } + + // @see ContentProxy->updateEntry + $entry = new Entry($this->user); + $entry->setUrl($importedEntry['url']); + $entry->setTitle($importedEntry['title']); + $entry->setArchived($importedEntry['is_read']); + $entry->setStarred($importedEntry['is_fav']); + $entry->setContent($importedEntry['content']); + $entry->setReadingTime(Utils::getReadingTime($importedEntry['content'])); + $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST)); + + $this->em->persist($entry); + ++$this->importedEntries; + } + + $this->em->flush(); + } +} -- cgit v1.2.3 From 7019c7cf6c6af39c0f458769e20c3f9306477943 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 31 Dec 2015 11:24:46 +0100 Subject: Add tagged services for import - list services in /import - add url to import service - ImportBundle routing are now prefixed by /import - optimize flush in each import (flushing each 20 contents) - improve design of each import - add more tests --- .../ImportBundle/Import/WallabagV1Import.php | 26 ++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/WallabagV1Import.php') diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php index 7b012674..aff5af40 100644 --- a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php +++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php @@ -50,12 +50,20 @@ class WallabagV1Import implements ImportInterface return 'Wallabag v1'; } + /** + * {@inheritdoc} + */ + public function getUrl() + { + return 'import_wallabag_v1'; + } + /** * {@inheritdoc} */ public function getDescription() { - return 'This importer will import all your wallabag v1 articles.'; + return 'This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.'; } /** @@ -75,7 +83,13 @@ class WallabagV1Import implements ImportInterface return false; } - $this->parseEntries(json_decode(file_get_contents($this->filepath), true)); + $data = json_decode(file_get_contents($this->filepath), true); + + if (empty($data)) { + return false; + } + + $this->parseEntries($data); return true; } @@ -108,6 +122,8 @@ class WallabagV1Import implements ImportInterface */ private function parseEntries($entries) { + $i = 1; + foreach ($entries as $importedEntry) { $existingEntry = $this->em ->getRepository('WallabagCoreBundle:Entry') @@ -130,6 +146,12 @@ class WallabagV1Import implements ImportInterface $this->em->persist($entry); ++$this->importedEntries; + + // flush every 20 entries + if (($i % 20) === 0) { + $em->flush(); + } + ++$i; } $this->em->flush(); -- cgit v1.2.3 From 8eedc8cfacc07e998f6f0bcdfe5b76496a215ea2 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 3 Jan 2016 10:59:55 +0100 Subject: Few phpDoc fix And some little mistakes --- src/Wallabag/ImportBundle/Import/WallabagV1Import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/ImportBundle/Import/WallabagV1Import.php') diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php index aff5af40..0866ebe9 100644 --- a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php +++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php @@ -149,7 +149,7 @@ class WallabagV1Import implements ImportInterface // flush every 20 entries if (($i % 20) === 0) { - $em->flush(); + $this->em->flush(); } ++$i; } -- cgit v1.2.3 From d1af8ad4dbf7f3ce5170655c2fa8403406283039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 5 Jan 2016 22:38:09 +0100 Subject: Added french translations --- src/Wallabag/ImportBundle/Import/WallabagV1Import.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/WallabagV1Import.php') diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php index 0866ebe9..68f0574f 100644 --- a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php +++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php @@ -5,6 +5,7 @@ namespace Wallabag\ImportBundle\Import; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use Doctrine\ORM\EntityManager; +use Symfony\Component\Translation\TranslatorInterface; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\UserBundle\Entity\User; use Wallabag\CoreBundle\Tools\Utils; @@ -17,11 +18,13 @@ class WallabagV1Import implements ImportInterface private $skippedEntries = 0; private $importedEntries = 0; private $filepath; + private $translator; - public function __construct(EntityManager $em) + public function __construct(EntityManager $em, TranslatorInterface $translator) { $this->em = $em; $this->logger = new NullLogger(); + $this->translator = $translator; } public function setLogger(LoggerInterface $logger) @@ -47,7 +50,7 @@ class WallabagV1Import implements ImportInterface */ public function getName() { - return 'Wallabag v1'; + return 'wallabag v1'; } /** @@ -63,7 +66,7 @@ class WallabagV1Import implements ImportInterface */ public function getDescription() { - return 'This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.'; + return $this->translator->trans('This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.'); } /** -- cgit v1.2.3 From b88cf91fc8371194df78e690983c61ea94f266cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Wed, 6 Jan 2016 06:34:57 +0100 Subject: updated tests --- src/Wallabag/ImportBundle/Import/WallabagV1Import.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/Wallabag/ImportBundle/Import/WallabagV1Import.php') diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php index 68f0574f..393089d6 100644 --- a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php +++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php @@ -5,7 +5,6 @@ namespace Wallabag\ImportBundle\Import; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use Doctrine\ORM\EntityManager; -use Symfony\Component\Translation\TranslatorInterface; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\UserBundle\Entity\User; use Wallabag\CoreBundle\Tools\Utils; @@ -18,13 +17,11 @@ class WallabagV1Import implements ImportInterface private $skippedEntries = 0; private $importedEntries = 0; private $filepath; - private $translator; - public function __construct(EntityManager $em, TranslatorInterface $translator) + public function __construct(EntityManager $em) { $this->em = $em; $this->logger = new NullLogger(); - $this->translator = $translator; } public function setLogger(LoggerInterface $logger) @@ -66,7 +63,7 @@ class WallabagV1Import implements ImportInterface */ public function getDescription() { - return $this->translator->trans('This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.'); + return 'This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.'; } /** -- cgit v1.2.3