aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle')
-rw-r--r--src/Wallabag/ImportBundle/Controller/ReadabilityController.php65
-rw-r--r--src/Wallabag/ImportBundle/Import/ReadabilityImport.php179
-rw-r--r--src/Wallabag/ImportBundle/Resources/config/services.yml10
-rw-r--r--src/Wallabag/ImportBundle/Resources/views/Readability/index.html.twig43
4 files changed, 297 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Controller/ReadabilityController.php b/src/Wallabag/ImportBundle/Controller/ReadabilityController.php
new file mode 100644
index 00000000..b61aa99c
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Controller/ReadabilityController.php
@@ -0,0 +1,65 @@
1<?php
2
3namespace Wallabag\ImportBundle\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7use Symfony\Component\HttpFoundation\Request;
8use Wallabag\ImportBundle\Form\Type\UploadImportType;
9
10class ReadabilityController extends Controller
11{
12 /**
13 * @Route("/readability", name="import_readability")
14 */
15 public function indexAction(Request $request)
16 {
17 $form = $this->createForm(UploadImportType::class);
18 $form->handleRequest($request);
19
20 $readability = $this->get('wallabag_import.readability.import');
21
22 if ($form->isValid()) {
23 $file = $form->get('file')->getData();
24 $markAsRead = $form->get('mark_as_read')->getData();
25 $name = 'readability_'.$this->getUser()->getId().'.json';
26
27 if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
28 $res = $readability
29 ->setUser($this->getUser())
30 ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
31 ->setMarkAsRead($markAsRead)
32 ->import();
33
34 $message = 'flashes.import.notice.failed';
35
36 if (true === $res) {
37 $summary = $readability->getSummary();
38 $message = $this->get('translator')->trans('flashes.import.notice.summary', [
39 '%imported%' => $summary['imported'],
40 '%skipped%' => $summary['skipped'],
41 ]);
42
43 unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name);
44 }
45
46 $this->get('session')->getFlashBag()->add(
47 'notice',
48 $message
49 );
50
51 return $this->redirect($this->generateUrl('homepage'));
52 } else {
53 $this->get('session')->getFlashBag()->add(
54 'notice',
55 'flashes.import.notice.failed_on_file'
56 );
57 }
58 }
59
60 return $this->render('WallabagImportBundle:Readability:index.html.twig', [
61 'form' => $form->createView(),
62 'import' => $readability,
63 ]);
64 }
65}
diff --git a/src/Wallabag/ImportBundle/Import/ReadabilityImport.php b/src/Wallabag/ImportBundle/Import/ReadabilityImport.php
new file mode 100644
index 00000000..37b160c5
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Import/ReadabilityImport.php
@@ -0,0 +1,179 @@
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\UserBundle\Entity\User;
7
8class ReadabilityImport extends AbstractImport
9{
10 private $user;
11 private $skippedEntries = 0;
12 private $importedEntries = 0;
13 private $filepath;
14 private $markAsRead;
15
16 /**
17 * We define the user in a custom call because on the import command there is no logged in user.
18 * So we can't retrieve user from the `security.token_storage` service.
19 *
20 * @param User $user
21 */
22 public function setUser(User $user)
23 {
24 $this->user = $user;
25
26 return $this;
27 }
28
29 /**
30 * {@inheritdoc}
31 */
32 public function getName()
33 {
34 return 'Readability';
35 }
36
37 /**
38 * {@inheritdoc}
39 */
40 public function getUrl()
41 {
42 return 'import_readability';
43 }
44
45 /**
46 * {@inheritdoc}
47 */
48 public function getDescription()
49 {
50 return 'import.readability.description';
51 }
52
53 /**
54 * Set file path to the json file.
55 *
56 * @param string $filepath
57 */
58 public function setFilepath($filepath)
59 {
60 $this->filepath = $filepath;
61
62 return $this;
63 }
64
65 /**
66 * Set whether articles must be all marked as read.
67 *
68 * @param bool $markAsRead
69 */
70 public function setMarkAsRead($markAsRead)
71 {
72 $this->markAsRead = $markAsRead;
73
74 return $this;
75 }
76
77 /**
78 * Get whether articles must be all marked as read.
79 */
80 public function getMarkAsRead()
81 {
82 return $this->markAsRead;
83 }
84
85 /**
86 * {@inheritdoc}
87 */
88 public function getSummary()
89 {
90 return [
91 'skipped' => $this->skippedEntries,
92 'imported' => $this->importedEntries,
93 ];
94 }
95
96 /**
97 * {@inheritdoc}
98 */
99 public function import()
100 {
101 if (!$this->user) {
102 $this->logger->error('ReadabilityImport: user is not defined');
103
104 return false;
105 }
106
107 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
108 $this->logger->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath]);
109
110 return false;
111 }
112
113 $data = json_decode(file_get_contents($this->filepath), true);
114
115 if (empty($data) || empty($data['bookmarks'])) {
116 return false;
117 }
118
119 $this->parseEntries($data['bookmarks']);
120
121 return true;
122 }
123
124 /**
125 * Parse and insert all given entries.
126 *
127 * @param $entries
128 */
129 protected function parseEntries($entries)
130 {
131 $i = 1;
132
133 foreach ($entries as $importedEntry) {
134 $existingEntry = $this->em
135 ->getRepository('WallabagCoreBundle:Entry')
136 ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId());
137
138 if (false !== $existingEntry) {
139 ++$this->skippedEntries;
140 continue;
141 }
142
143 $data = [
144 'title' => $importedEntry['article__title'],
145 'url' => $importedEntry['article__url'],
146 'content_type' => '',
147 'language' => '',
148 'is_archived' => $importedEntry['archive'] || $this->markAsRead,
149 'is_starred' => $importedEntry['favorite'],
150 ];
151
152 $entry = $this->fetchContent(
153 new Entry($this->user),
154 $data['url'],
155 $data
156 );
157
158 // jump to next entry in case of problem while getting content
159 if (false === $entry) {
160 ++$this->skippedEntries;
161 continue;
162 }
163 $entry->setArchived($data['is_archived']);
164 $entry->setStarred($data['is_starred']);
165
166 $this->em->persist($entry);
167 ++$this->importedEntries;
168
169 // flush every 20 entries
170 if (($i % 20) === 0) {
171 $this->em->flush();
172 $this->em->clear($entry);
173 }
174 ++$i;
175 }
176
177 $this->em->flush();
178 }
179}
diff --git a/src/Wallabag/ImportBundle/Resources/config/services.yml b/src/Wallabag/ImportBundle/Resources/config/services.yml
index 86b44cb3..520d43af 100644
--- a/src/Wallabag/ImportBundle/Resources/config/services.yml
+++ b/src/Wallabag/ImportBundle/Resources/config/services.yml
@@ -43,3 +43,13 @@ services:
43 - [ setLogger, [ "@logger" ]] 43 - [ setLogger, [ "@logger" ]]
44 tags: 44 tags:
45 - { name: wallabag_import.import, alias: wallabag_v2 } 45 - { name: wallabag_import.import, alias: wallabag_v2 }
46
47 wallabag_import.readability.import:
48 class: Wallabag\ImportBundle\Import\ReadabilityImport
49 arguments:
50 - "@doctrine.orm.entity_manager"
51 - "@wallabag_core.content_proxy"
52 calls:
53 - [ setLogger, [ "@logger" ]]
54 tags:
55 - { name: wallabag_import.import, alias: readability }
diff --git a/src/Wallabag/ImportBundle/Resources/views/Readability/index.html.twig b/src/Wallabag/ImportBundle/Resources/views/Readability/index.html.twig
new file mode 100644
index 00000000..f527d309
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Resources/views/Readability/index.html.twig
@@ -0,0 +1,43 @@
1{% extends "WallabagCoreBundle::layout.html.twig" %}
2
3{% block title %}{{ 'import.readability.page_title'|trans }}{% 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 <blockquote>{{ import.description|trans }}</blockquote>
11 <p>{{ 'import.readability.how_to'|trans }}</p>
12
13 <div class="col s12">
14 {{ form_start(form, {'method': 'POST'}) }}
15 {{ form_errors(form) }}
16 <div class="row">
17 <div class="file-field input-field col s12">
18 {{ form_errors(form.file) }}
19 <div class="btn">
20 <span>{{ form.file.vars.label|trans }}</span>
21 {{ form_widget(form.file) }}
22 </div>
23 <div class="file-path-wrapper">
24 <input class="file-path validate" type="text">
25 </div>
26 </div>
27 <div class="input-field col s6 with-checkbox">
28 <h6>{{ 'import.form.mark_as_read_title'|trans }}</h6>
29 {{ form_widget(form.mark_as_read) }}
30 {{ form_label(form.mark_as_read) }}
31 </div>
32 </div>
33
34 {{ form_widget(form.save, { 'attr': {'class': 'btn waves-effect waves-light'} }) }}
35
36 {{ form_rest(form) }}
37 </form>
38 </div>
39 </div>
40 </div>
41 </div>
42</div>
43{% endblock %}