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 --- src/Wallabag/ImportBundle/Import/ImportChain.php | 34 ++++++++++++++++++++++ .../ImportBundle/Import/ImportCompilerPass.php | 33 +++++++++++++++++++++ .../ImportBundle/Import/ImportInterface.php | 7 +++++ src/Wallabag/ImportBundle/Import/PocketImport.php | 18 +++++++++++- .../ImportBundle/Import/WallabagV1Import.php | 26 +++++++++++++++-- 5 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 src/Wallabag/ImportBundle/Import/ImportChain.php create mode 100644 src/Wallabag/ImportBundle/Import/ImportCompilerPass.php (limited to 'src/Wallabag/ImportBundle/Import') diff --git a/src/Wallabag/ImportBundle/Import/ImportChain.php b/src/Wallabag/ImportBundle/Import/ImportChain.php new file mode 100644 index 00000000..9dd77956 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/ImportChain.php @@ -0,0 +1,34 @@ +imports = []; + } + + /** + * Add an import to the chain. + * + * @param ImportInterface $import + * @param string $alias + */ + public function addImport(ImportInterface $import, $alias) + { + $this->imports[$alias] = $import; + } + + /** + * Get all imports. + * + * @return array + */ + public function getAll() + { + return $this->imports; + } +} diff --git a/src/Wallabag/ImportBundle/Import/ImportCompilerPass.php b/src/Wallabag/ImportBundle/Import/ImportCompilerPass.php new file mode 100644 index 00000000..a363a566 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/ImportCompilerPass.php @@ -0,0 +1,33 @@ +hasDefinition('wallabag_import.chain')) { + return; + } + + $definition = $container->getDefinition( + 'wallabag_import.chain' + ); + + $taggedServices = $container->findTaggedServiceIds( + 'wallabag_import.import' + ); + foreach ($taggedServices as $id => $tagAttributes) { + foreach ($tagAttributes as $attributes) { + $definition->addMethodCall( + 'addImport', + [new Reference($id), $attributes['alias']] + ); + } + } + } +} diff --git a/src/Wallabag/ImportBundle/Import/ImportInterface.php b/src/Wallabag/ImportBundle/Import/ImportInterface.php index 8cf238aa..25dc0d85 100644 --- a/src/Wallabag/ImportBundle/Import/ImportInterface.php +++ b/src/Wallabag/ImportBundle/Import/ImportInterface.php @@ -13,6 +13,13 @@ interface ImportInterface extends LoggerAwareInterface */ public function getName(); + /** + * Url to start the import. + * + * @return string + */ + public function getUrl(); + /** * Description of the import. * diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index aeccc7bd..b1c5bb00 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -45,12 +45,20 @@ class PocketImport implements ImportInterface return 'Pocket'; } + /** + * {@inheritdoc} + */ + public function getUrl() + { + return 'import_pocket'; + } + /** * {@inheritdoc} */ public function getDescription() { - return 'This importer will import all your Pocket data.'; + return 'This importer will import all your Pocket data. Pocket doesn\'t allow us to retrieve content from their service, so the readable content of each article will be re-fetched by Wallabag.'; } /** @@ -196,6 +204,8 @@ class PocketImport implements ImportInterface */ private function parseEntries($entries) { + $i = 1; + foreach ($entries as $pocketEntry) { $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url']; @@ -241,6 +251,12 @@ class PocketImport implements ImportInterface $this->em->persist($entry); ++$this->importedEntries; + + // flush every 20 entries + if (($i % 20) === 0) { + $em->flush(); + } + ++$i; } $this->em->flush(); 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