Update after previous merge

PR #1443 was merged into this branch to handle all import type in the same place.
This commit is contained in:
Jeremy Benoist
2015-12-30 10:06:45 +01:00
parent 7ec2897ee0
commit 77a7752a59
11 changed files with 86 additions and 114 deletions

View File

@ -1,124 +0,0 @@
<?php
namespace Wallabag\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Tools\Utils;
class ImportCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('wallabag:import')
->setDescription('Import entries from JSON file')
->addArgument(
'userId',
InputArgument::REQUIRED,
'user ID to populate'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$now = new \DateTime();
$output->writeln('<comment>Start : '.$now->format('d-m-Y G:i:s').' ---</comment>');
// Importing CSV on DB via Doctrine ORM
$this->import($input, $output);
$now = new \DateTime();
$output->writeln('<comment>End : '.$now->format('d-m-Y G:i:s').' ---</comment>');
}
protected function import(InputInterface $input, OutputInterface $output)
{
// Getting php array of data from CSV
$data = $this->get($input, $output);
$em = $this->getContainer()->get('doctrine')->getManager();
// Turning off doctrine default logs queries for saving memory
$em->getConnection()->getConfiguration()->setSQLLogger(null);
// Define the size of record, the frequency for persisting the data and the current index of records
$size = count($data);
$batchSize = 20;
$i = 1;
$user = $em->getRepository('WallabagUserBundle:User')
->findOneById($input->getArgument('userId'));
if (!is_object($user)) {
throw new Exception('User not found');
}
$progress = new ProgressBar($output, $size);
$progress->start();
foreach ($data as $object) {
$array = (array) $object;
$entry = $em->getRepository('WallabagCoreBundle:Entry')
->findOneByUrl($array['url']);
if (!is_object($entry)) {
$entry = new Entry($user);
$entry->setUrl($array['url']);
}
$entry->setTitle($array['title']);
$entry->setArchived($array['is_read']);
$entry->setStarred($array['is_fav']);
$entry->setContent($array['content']);
$entry->setReadingTime(Utils::getReadingTime($array['content']));
$em->persist($entry);
if (($i % $batchSize) === 0) {
$em->flush();
$progress->advance($batchSize);
$now = new \DateTime();
$output->writeln(' of entries imported ... | '.$now->format('d-m-Y G:i:s'));
}
++$i;
}
$em->flush();
$em->clear();
$progress->finish();
}
protected function convert($filename)
{
if (!file_exists($filename) || !is_readable($filename)) {
return false;
}
$header = null;
$data = array();
if (($handle = fopen($filename, 'r')) !== false) {
while (($row = fgets($handle)) !== false) {
$data = json_decode($row);
}
fclose($handle);
}
return $data;
}
protected function get(InputInterface $input, OutputInterface $output)
{
$filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json';
$data = $this->convert($filename);
return $data;
}
}

View File

@ -1,64 +0,0 @@
<?php
namespace Wallabag\CoreBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\HttpFoundation\Request;
use Wallabag\CoreBundle\Command\ImportCommand;
use Wallabag\CoreBundle\Form\Type\UploadImportType;
class ImportController extends Controller
{
/**
* @param Request $request
*
* @Route("/import", name="import")
*/
public function importAction(Request $request)
{
$importForm = $this->createForm(new UploadImportType());
$importForm->handleRequest($request);
$user = $this->getUser();
$importConfig = $this->container->getParameter('wallabag_core.import');
if ($importForm->isValid()) {
$file = $importForm->get('file')->getData();
$name = $user->getId().'.json';
$dir = __DIR__.'/../../../../web/uploads/import';
if (in_array($file->getMimeType(), $importConfig['allow_mimetypes']) && $file->move($dir, $name)) {
$command = new ImportCommand();
$command->setContainer($this->container);
$input = new ArrayInput(array('userId' => $user->getId()));
$return = $command->run($input, new NullOutput());
if ($return == 0) {
$this->get('session')->getFlashBag()->add(
'notice',
'Import successful'
);
} else {
$this->get('session')->getFlashBag()->add(
'notice',
'Import failed'
);
}
return $this->redirect('/');
} else {
$this->get('session')->getFlashBag()->add(
'notice',
'Error while processing import. Please verify your import file.'
);
}
}
return $this->render('WallabagCoreBundle:Import:index.html.twig', array(
'form' => array(
'import' => $importForm->createView(), ),
));
}
}

View File

@ -4,7 +4,6 @@ namespace Wallabag\CoreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
class Configuration implements ConfigurationInterface
{
@ -18,21 +17,9 @@ class Configuration implements ConfigurationInterface
->arrayNode('languages')
->prototype('scalar')->end()
->end()
->arrayNode('import')
->append($this->getAllowMimetypes())
->end()
->end()
;
return $treeBuilder;
}
private function getAllowMimetypes()
{
$node = new ArrayNodeDefinition('allow_mimetypes');
$node->prototype('scalar')->end();
return $node;
}
}

View File

@ -14,7 +14,6 @@ class WallabagCoreExtension extends Extension
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('wallabag_core.languages', $config['languages']);
$container->setParameter('wallabag_core.import', $config['import']);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

View File

@ -1,29 +0,0 @@
<?php
namespace Wallabag\CoreBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class UploadImportType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file')
->add('save', 'submit')
;
}
public function getDefaultOptions(array $options)
{
return array(
'csrf_protection' => false,
);
}
public function getName()
{
return 'upload_import_file';
}
}

View File

@ -1,30 +0,0 @@
{% extends "WallabagCoreBundle::layout.html.twig" %}
{% block title %}{% trans %}import{% endtrans %}{% endblock %}
{% block content %}
<div class="row">
<div class="col s12">
<div class="card-panel settings">
<div class="row">
<div class="col s12">
<form action="{{ path('import') }}" method="post" {{ form_enctype(form.import) }}>
{{ form_errors(form.import) }}
<div class="row">
<div class="input-field col s12">
<p>{% trans %}Please select your wallabag export and click on the below button to upload and import it.{% endtrans %}</p>
{{ form_errors(form.import.file) }}
{{ form_widget(form.import.file) }}
</div>
</div>
<div class="hidden">{{ form_rest(form.import) }}</div>
<button class="btn waves-effect waves-light" type="submit" name="action">
{% trans %}Upload file{% endtrans %}
</button>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}