aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-12-30 10:06:45 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-01-02 23:27:41 +0100
commit77a7752a592af9ac821621a34d9955533baf40a0 (patch)
tree304b29f1d01a93a61f75f3206aabf845ff3fbe35 /src/Wallabag/CoreBundle
parent7ec2897ee0ad190dcb9f77032d785f2f9661b754 (diff)
downloadwallabag-77a7752a592af9ac821621a34d9955533baf40a0.tar.gz
wallabag-77a7752a592af9ac821621a34d9955533baf40a0.tar.zst
wallabag-77a7752a592af9ac821621a34d9955533baf40a0.zip
Update after previous merge
PR #1443 was merged into this branch to handle all import type in the same place.
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r--src/Wallabag/CoreBundle/Command/ImportCommand.php124
-rw-r--r--src/Wallabag/CoreBundle/Controller/ImportController.php64
-rw-r--r--src/Wallabag/CoreBundle/DependencyInjection/Configuration.php13
-rw-r--r--src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php1
-rw-r--r--src/Wallabag/CoreBundle/Form/Type/UploadImportType.php29
-rw-r--r--src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig30
6 files changed, 0 insertions, 261 deletions
diff --git a/src/Wallabag/CoreBundle/Command/ImportCommand.php b/src/Wallabag/CoreBundle/Command/ImportCommand.php
deleted file mode 100644
index 6be6f5e1..00000000
--- a/src/Wallabag/CoreBundle/Command/ImportCommand.php
+++ /dev/null
@@ -1,124 +0,0 @@
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6use Symfony\Component\Config\Definition\Exception\Exception;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
10use Symfony\Component\Console\Helper\ProgressBar;
11use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Tools\Utils;
13
14class ImportCommand extends ContainerAwareCommand
15{
16 protected function configure()
17 {
18 $this
19 ->setName('wallabag:import')
20 ->setDescription('Import entries from JSON file')
21 ->addArgument(
22 'userId',
23 InputArgument::REQUIRED,
24 'user ID to populate'
25 );
26 }
27
28 protected function execute(InputInterface $input, OutputInterface $output)
29 {
30 $now = new \DateTime();
31 $output->writeln('<comment>Start : '.$now->format('d-m-Y G:i:s').' ---</comment>');
32
33 // Importing CSV on DB via Doctrine ORM
34 $this->import($input, $output);
35
36 $now = new \DateTime();
37 $output->writeln('<comment>End : '.$now->format('d-m-Y G:i:s').' ---</comment>');
38 }
39
40 protected function import(InputInterface $input, OutputInterface $output)
41 {
42 // Getting php array of data from CSV
43 $data = $this->get($input, $output);
44
45 $em = $this->getContainer()->get('doctrine')->getManager();
46 // Turning off doctrine default logs queries for saving memory
47 $em->getConnection()->getConfiguration()->setSQLLogger(null);
48
49 // Define the size of record, the frequency for persisting the data and the current index of records
50 $size = count($data);
51 $batchSize = 20;
52 $i = 1;
53
54 $user = $em->getRepository('WallabagUserBundle:User')
55 ->findOneById($input->getArgument('userId'));
56
57 if (!is_object($user)) {
58 throw new Exception('User not found');
59 }
60
61 $progress = new ProgressBar($output, $size);
62 $progress->start();
63
64 foreach ($data as $object) {
65 $array = (array) $object;
66 $entry = $em->getRepository('WallabagCoreBundle:Entry')
67 ->findOneByUrl($array['url']);
68
69 if (!is_object($entry)) {
70 $entry = new Entry($user);
71 $entry->setUrl($array['url']);
72 }
73
74 $entry->setTitle($array['title']);
75 $entry->setArchived($array['is_read']);
76 $entry->setStarred($array['is_fav']);
77 $entry->setContent($array['content']);
78 $entry->setReadingTime(Utils::getReadingTime($array['content']));
79
80 $em->persist($entry);
81
82 if (($i % $batchSize) === 0) {
83 $em->flush();
84 $progress->advance($batchSize);
85
86 $now = new \DateTime();
87 $output->writeln(' of entries imported ... | '.$now->format('d-m-Y G:i:s'));
88 }
89 ++$i;
90 }
91
92 $em->flush();
93 $em->clear();
94 $progress->finish();
95 }
96
97 protected function convert($filename)
98 {
99 if (!file_exists($filename) || !is_readable($filename)) {
100 return false;
101 }
102
103 $header = null;
104 $data = array();
105
106 if (($handle = fopen($filename, 'r')) !== false) {
107 while (($row = fgets($handle)) !== false) {
108 $data = json_decode($row);
109 }
110 fclose($handle);
111 }
112
113 return $data;
114 }
115
116 protected function get(InputInterface $input, OutputInterface $output)
117 {
118 $filename = __DIR__.'/../../../../web/uploads/import/'.$input->getArgument('userId').'.json';
119
120 $data = $this->convert($filename);
121
122 return $data;
123 }
124}
diff --git a/src/Wallabag/CoreBundle/Controller/ImportController.php b/src/Wallabag/CoreBundle/Controller/ImportController.php
deleted file mode 100644
index 33087710..00000000
--- a/src/Wallabag/CoreBundle/Controller/ImportController.php
+++ /dev/null
@@ -1,64 +0,0 @@
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\Console\Input\ArrayInput;
8use Symfony\Component\Console\Output\NullOutput;
9use Symfony\Component\HttpFoundation\Request;
10use Wallabag\CoreBundle\Command\ImportCommand;
11use Wallabag\CoreBundle\Form\Type\UploadImportType;
12
13class ImportController extends Controller
14{
15 /**
16 * @param Request $request
17 *
18 * @Route("/import", name="import")
19 */
20 public function importAction(Request $request)
21 {
22 $importForm = $this->createForm(new UploadImportType());
23 $importForm->handleRequest($request);
24 $user = $this->getUser();
25 $importConfig = $this->container->getParameter('wallabag_core.import');
26
27 if ($importForm->isValid()) {
28 $file = $importForm->get('file')->getData();
29 $name = $user->getId().'.json';
30 $dir = __DIR__.'/../../../../web/uploads/import';
31
32 if (in_array($file->getMimeType(), $importConfig['allow_mimetypes']) && $file->move($dir, $name)) {
33 $command = new ImportCommand();
34 $command->setContainer($this->container);
35 $input = new ArrayInput(array('userId' => $user->getId()));
36 $return = $command->run($input, new NullOutput());
37
38 if ($return == 0) {
39 $this->get('session')->getFlashBag()->add(
40 'notice',
41 'Import successful'
42 );
43 } else {
44 $this->get('session')->getFlashBag()->add(
45 'notice',
46 'Import failed'
47 );
48 }
49
50 return $this->redirect('/');
51 } else {
52 $this->get('session')->getFlashBag()->add(
53 'notice',
54 'Error while processing import. Please verify your import file.'
55 );
56 }
57 }
58
59 return $this->render('WallabagCoreBundle:Import:index.html.twig', array(
60 'form' => array(
61 'import' => $importForm->createView(), ),
62 ));
63 }
64}
diff --git a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
index fb1941b8..32acd1f1 100644
--- a/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
+++ b/src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
@@ -4,7 +4,6 @@ namespace Wallabag\CoreBundle\DependencyInjection;
4 4
5use Symfony\Component\Config\Definition\Builder\TreeBuilder; 5use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6use Symfony\Component\Config\Definition\ConfigurationInterface; 6use Symfony\Component\Config\Definition\ConfigurationInterface;
7use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8 7
9class Configuration implements ConfigurationInterface 8class Configuration implements ConfigurationInterface
10{ 9{
@@ -18,21 +17,9 @@ class Configuration implements ConfigurationInterface
18 ->arrayNode('languages') 17 ->arrayNode('languages')
19 ->prototype('scalar')->end() 18 ->prototype('scalar')->end()
20 ->end() 19 ->end()
21 ->arrayNode('import')
22 ->append($this->getAllowMimetypes())
23 ->end()
24 ->end() 20 ->end()
25 ; 21 ;
26 22
27 return $treeBuilder; 23 return $treeBuilder;
28 } 24 }
29
30 private function getAllowMimetypes()
31 {
32 $node = new ArrayNodeDefinition('allow_mimetypes');
33
34 $node->prototype('scalar')->end();
35
36 return $node;
37 }
38} 25}
diff --git a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
index e2a3ad55..330cc957 100644
--- a/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
+++ b/src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
@@ -14,7 +14,6 @@ class WallabagCoreExtension extends Extension
14 $configuration = new Configuration(); 14 $configuration = new Configuration();
15 $config = $this->processConfiguration($configuration, $configs); 15 $config = $this->processConfiguration($configuration, $configs);
16 $container->setParameter('wallabag_core.languages', $config['languages']); 16 $container->setParameter('wallabag_core.languages', $config['languages']);
17 $container->setParameter('wallabag_core.import', $config['import']);
18 17
19 $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 18 $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
20 $loader->load('services.yml'); 19 $loader->load('services.yml');
diff --git a/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php b/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php
deleted file mode 100644
index b9a9c465..00000000
--- a/src/Wallabag/CoreBundle/Form/Type/UploadImportType.php
+++ /dev/null
@@ -1,29 +0,0 @@
1<?php
2
3namespace Wallabag\CoreBundle\Form\Type;
4
5use Symfony\Component\Form\AbstractType;
6use Symfony\Component\Form\FormBuilderInterface;
7
8class UploadImportType extends AbstractType
9{
10 public function buildForm(FormBuilderInterface $builder, array $options)
11 {
12 $builder
13 ->add('file', 'file')
14 ->add('save', 'submit')
15 ;
16 }
17
18 public function getDefaultOptions(array $options)
19 {
20 return array(
21 'csrf_protection' => false,
22 );
23 }
24
25 public function getName()
26 {
27 return 'upload_import_file';
28 }
29}
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig
deleted file mode 100644
index 47004144..00000000
--- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Import/index.html.twig
+++ /dev/null
@@ -1,30 +0,0 @@
1{% extends "WallabagCoreBundle::layout.html.twig" %}
2
3{% block title %}{% trans %}import{% endtrans %}{% endblock %}
4
5{% block content %}
6<div class="row">
7 <div class="col s12">
8 <div class="card-panel settings">
9 <div class="row">
10 <div class="col s12">
11 <form action="{{ path('import') }}" method="post" {{ form_enctype(form.import) }}>
12 {{ form_errors(form.import) }}
13 <div class="row">
14 <div class="input-field col s12">
15 <p>{% trans %}Please select your wallabag export and click on the below button to upload and import it.{% endtrans %}</p>
16 {{ form_errors(form.import.file) }}
17 {{ form_widget(form.import.file) }}
18 </div>
19 </div>
20 <div class="hidden">{{ form_rest(form.import) }}</div>
21 <button class="btn waves-effect waves-light" type="submit" name="action">
22 {% trans %}Upload file{% endtrans %}
23 </button>
24 </form>
25 </div>
26 </div>
27 </div>
28 </div>
29</div>
30{% endblock %}