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
This commit is contained in:
Jeremy Benoist
2015-12-31 11:24:46 +01:00
parent b1d05721cf
commit 7019c7cf6c
25 changed files with 394 additions and 43 deletions

View File

@ -0,0 +1,34 @@
<?php
namespace Wallabag\ImportBundle\Import;
class ImportChain
{
private $imports;
public function __construct()
{
$this->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<ImportInterface>
*/
public function getAll()
{
return $this->imports;
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Wallabag\ImportBundle\Import;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
class ImportCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->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']]
);
}
}
}
}

View File

@ -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.
*

View File

@ -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 <a href="https://getpocket.com">Pocket</a> data.';
return 'This importer will import all your <a href="https://getpocket.com">Pocket</a> 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();

View File

@ -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();