diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-09-27 07:57:53 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-09-27 20:27:08 +0200 |
commit | ff1a5362f7254d686864ea53994da6c517b3d3e8 (patch) | |
tree | 1edc0b6b46cf12bd17918355f7dbe090324e6f70 /src/Wallabag/ImportBundle | |
parent | 55345331c47ca6f389e38a842584c3062476601d (diff) | |
download | wallabag-ff1a5362f7254d686864ea53994da6c517b3d3e8.tar.gz wallabag-ff1a5362f7254d686864ea53994da6c517b3d3e8.tar.zst wallabag-ff1a5362f7254d686864ea53994da6c517b3d3e8.zip |
Add Instapaper import
Also update ImportController with latest import (chrome, firefox & instapaper).
Diffstat (limited to 'src/Wallabag/ImportBundle')
9 files changed, 308 insertions, 3 deletions
diff --git a/src/Wallabag/ImportBundle/Command/RedisWorkerCommand.php b/src/Wallabag/ImportBundle/Command/RedisWorkerCommand.php index 5f90e00f..c2c11f11 100644 --- a/src/Wallabag/ImportBundle/Command/RedisWorkerCommand.php +++ b/src/Wallabag/ImportBundle/Command/RedisWorkerCommand.php | |||
@@ -17,7 +17,7 @@ class RedisWorkerCommand extends ContainerAwareCommand | |||
17 | $this | 17 | $this |
18 | ->setName('wallabag:import:redis-worker') | 18 | ->setName('wallabag:import:redis-worker') |
19 | ->setDescription('Launch Redis worker') | 19 | ->setDescription('Launch Redis worker') |
20 | ->addArgument('serviceName', InputArgument::REQUIRED, 'Service to use: wallabag_v1, wallabag_v2, pocket or readability') | 20 | ->addArgument('serviceName', InputArgument::REQUIRED, 'Service to use: wallabag_v1, wallabag_v2, pocket, readability, firefox, chrome or instapaper') |
21 | ->addOption('maxIterations', '', InputOption::VALUE_OPTIONAL, 'Number of iterations before stoping', false) | 21 | ->addOption('maxIterations', '', InputOption::VALUE_OPTIONAL, 'Number of iterations before stoping', false) |
22 | ; | 22 | ; |
23 | } | 23 | } |
diff --git a/src/Wallabag/ImportBundle/Controller/ImportController.php b/src/Wallabag/ImportBundle/Controller/ImportController.php index 36a2a399..15de75ff 100644 --- a/src/Wallabag/ImportBundle/Controller/ImportController.php +++ b/src/Wallabag/ImportBundle/Controller/ImportController.php | |||
@@ -38,7 +38,11 @@ class ImportController extends Controller | |||
38 | $nbRabbitMessages = $this->getTotalMessageInRabbitQueue('pocket') | 38 | $nbRabbitMessages = $this->getTotalMessageInRabbitQueue('pocket') |
39 | + $this->getTotalMessageInRabbitQueue('readability') | 39 | + $this->getTotalMessageInRabbitQueue('readability') |
40 | + $this->getTotalMessageInRabbitQueue('wallabag_v1') | 40 | + $this->getTotalMessageInRabbitQueue('wallabag_v1') |
41 | + $this->getTotalMessageInRabbitQueue('wallabag_v2'); | 41 | + $this->getTotalMessageInRabbitQueue('wallabag_v2') |
42 | + $this->getTotalMessageInRabbitQueue('firefox') | ||
43 | + $this->getTotalMessageInRabbitQueue('chrome') | ||
44 | + $this->getTotalMessageInRabbitQueue('instapaper') | ||
45 | ; | ||
42 | } catch (\Exception $e) { | 46 | } catch (\Exception $e) { |
43 | $rabbitNotInstalled = true; | 47 | $rabbitNotInstalled = true; |
44 | } | 48 | } |
@@ -49,7 +53,11 @@ class ImportController extends Controller | |||
49 | $nbRedisMessages = $redis->llen('wallabag.import.pocket') | 53 | $nbRedisMessages = $redis->llen('wallabag.import.pocket') |
50 | + $redis->llen('wallabag.import.readability') | 54 | + $redis->llen('wallabag.import.readability') |
51 | + $redis->llen('wallabag.import.wallabag_v1') | 55 | + $redis->llen('wallabag.import.wallabag_v1') |
52 | + $redis->llen('wallabag.import.wallabag_v2'); | 56 | + $redis->llen('wallabag.import.wallabag_v2') |
57 | + $redis->llen('wallabag.import.firefox') | ||
58 | + $redis->llen('wallabag.import.chrome') | ||
59 | + $redis->llen('wallabag.import.instapaper') | ||
60 | ; | ||
53 | } catch (\Exception $e) { | 61 | } catch (\Exception $e) { |
54 | $redisNotInstalled = true; | 62 | $redisNotInstalled = true; |
55 | } | 63 | } |
diff --git a/src/Wallabag/ImportBundle/Controller/InstapaperController.php b/src/Wallabag/ImportBundle/Controller/InstapaperController.php new file mode 100644 index 00000000..c3fc8a39 --- /dev/null +++ b/src/Wallabag/ImportBundle/Controller/InstapaperController.php | |||
@@ -0,0 +1,77 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Controller; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
6 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
7 | use Symfony\Component\HttpFoundation\Request; | ||
8 | use Wallabag\ImportBundle\Form\Type\UploadImportType; | ||
9 | |||
10 | class InstapaperController extends Controller | ||
11 | { | ||
12 | /** | ||
13 | * @Route("/instapaper", name="import_instapaper") | ||
14 | */ | ||
15 | public function indexAction(Request $request) | ||
16 | { | ||
17 | $form = $this->createForm(UploadImportType::class); | ||
18 | $form->handleRequest($request); | ||
19 | |||
20 | $instapaper = $this->get('wallabag_import.instapaper.import'); | ||
21 | $instapaper->setUser($this->getUser()); | ||
22 | |||
23 | if ($this->get('craue_config')->get('import_with_rabbitmq')) { | ||
24 | $instapaper->setProducer($this->get('old_sound_rabbit_mq.import_instapaper_producer')); | ||
25 | } elseif ($this->get('craue_config')->get('import_with_redis')) { | ||
26 | $instapaper->setProducer($this->get('wallabag_import.producer.redis.instapaper')); | ||
27 | } | ||
28 | |||
29 | if ($form->isValid()) { | ||
30 | $file = $form->get('file')->getData(); | ||
31 | $markAsRead = $form->get('mark_as_read')->getData(); | ||
32 | $name = 'instapaper_'.$this->getUser()->getId().'.csv'; | ||
33 | |||
34 | if (null !== $file && in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) { | ||
35 | $res = $instapaper | ||
36 | ->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name) | ||
37 | ->setMarkAsRead($markAsRead) | ||
38 | ->import(); | ||
39 | |||
40 | $message = 'flashes.import.notice.failed'; | ||
41 | |||
42 | if (true === $res) { | ||
43 | $summary = $instapaper->getSummary(); | ||
44 | $message = $this->get('translator')->trans('flashes.import.notice.summary', [ | ||
45 | '%imported%' => $summary['imported'], | ||
46 | '%skipped%' => $summary['skipped'], | ||
47 | ]); | ||
48 | |||
49 | if (0 < $summary['queued']) { | ||
50 | $message = $this->get('translator')->trans('flashes.import.notice.summary_with_queue', [ | ||
51 | '%queued%' => $summary['queued'], | ||
52 | ]); | ||
53 | } | ||
54 | |||
55 | unlink($this->getParameter('wallabag_import.resource_dir').'/'.$name); | ||
56 | } | ||
57 | |||
58 | $this->get('session')->getFlashBag()->add( | ||
59 | 'notice', | ||
60 | $message | ||
61 | ); | ||
62 | |||
63 | return $this->redirect($this->generateUrl('homepage')); | ||
64 | } else { | ||
65 | $this->get('session')->getFlashBag()->add( | ||
66 | 'notice', | ||
67 | 'flashes.import.notice.failed_on_file' | ||
68 | ); | ||
69 | } | ||
70 | } | ||
71 | |||
72 | return $this->render('WallabagImportBundle:Instapaper:index.html.twig', [ | ||
73 | 'form' => $form->createView(), | ||
74 | 'import' => $instapaper, | ||
75 | ]); | ||
76 | } | ||
77 | } | ||
diff --git a/src/Wallabag/ImportBundle/Import/AbstractImport.php b/src/Wallabag/ImportBundle/Import/AbstractImport.php index a1a14576..764b390a 100644 --- a/src/Wallabag/ImportBundle/Import/AbstractImport.php +++ b/src/Wallabag/ImportBundle/Import/AbstractImport.php | |||
@@ -106,6 +106,10 @@ abstract class AbstractImport implements ImportInterface | |||
106 | $i = 1; | 106 | $i = 1; |
107 | 107 | ||
108 | foreach ($entries as $importedEntry) { | 108 | foreach ($entries as $importedEntry) { |
109 | if ($this->markAsRead) { | ||
110 | $importedEntry = $this->setEntryAsRead($importedEntry); | ||
111 | } | ||
112 | |||
109 | $entry = $this->parseEntry($importedEntry); | 113 | $entry = $this->parseEntry($importedEntry); |
110 | 114 | ||
111 | if (null === $entry) { | 115 | if (null === $entry) { |
diff --git a/src/Wallabag/ImportBundle/Import/InstapaperImport.php b/src/Wallabag/ImportBundle/Import/InstapaperImport.php new file mode 100644 index 00000000..356acf23 --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/InstapaperImport.php | |||
@@ -0,0 +1,134 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | |||
7 | class InstapaperImport extends AbstractImport | ||
8 | { | ||
9 | private $filepath; | ||
10 | |||
11 | /** | ||
12 | * {@inheritdoc} | ||
13 | */ | ||
14 | public function getName() | ||
15 | { | ||
16 | return 'Instapaper'; | ||
17 | } | ||
18 | |||
19 | /** | ||
20 | * {@inheritdoc} | ||
21 | */ | ||
22 | public function getUrl() | ||
23 | { | ||
24 | return 'import_instapaper'; | ||
25 | } | ||
26 | |||
27 | /** | ||
28 | * {@inheritdoc} | ||
29 | */ | ||
30 | public function getDescription() | ||
31 | { | ||
32 | return 'import.instapaper.description'; | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Set file path to the json file. | ||
37 | * | ||
38 | * @param string $filepath | ||
39 | */ | ||
40 | public function setFilepath($filepath) | ||
41 | { | ||
42 | $this->filepath = $filepath; | ||
43 | |||
44 | return $this; | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * {@inheritdoc} | ||
49 | */ | ||
50 | public function import() | ||
51 | { | ||
52 | if (!$this->user) { | ||
53 | $this->logger->error('InstapaperImport: user is not defined'); | ||
54 | |||
55 | return false; | ||
56 | } | ||
57 | |||
58 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | ||
59 | $this->logger->error('InstapaperImport: unable to read file', ['filepath' => $this->filepath]); | ||
60 | |||
61 | return false; | ||
62 | } | ||
63 | |||
64 | $entries = []; | ||
65 | $handle = fopen($this->filepath, 'r'); | ||
66 | while (($data = fgetcsv($handle, 10240)) !== false) { | ||
67 | if ('URL' === $data[0]) { | ||
68 | continue; | ||
69 | } | ||
70 | |||
71 | $entries[] = [ | ||
72 | 'url' => $data[0], | ||
73 | 'title' => $data[1], | ||
74 | 'status' => $data[3], | ||
75 | 'is_archived' => $data[3] === 'Archive' || $data[3] === 'Starred', | ||
76 | 'is_starred' => $data[3] === 'Starred', | ||
77 | 'content_type' => '', | ||
78 | 'language' => '', | ||
79 | ]; | ||
80 | } | ||
81 | fclose($handle); | ||
82 | |||
83 | if ($this->producer) { | ||
84 | $this->parseEntriesForProducer($entries); | ||
85 | |||
86 | return true; | ||
87 | } | ||
88 | |||
89 | $this->parseEntries($entries); | ||
90 | |||
91 | return true; | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * {@inheritdoc} | ||
96 | */ | ||
97 | public function parseEntry(array $importedEntry) | ||
98 | { | ||
99 | $existingEntry = $this->em | ||
100 | ->getRepository('WallabagCoreBundle:Entry') | ||
101 | ->findByUrlAndUserId($importedEntry['url'], $this->user->getId()); | ||
102 | |||
103 | if (false !== $existingEntry) { | ||
104 | ++$this->skippedEntries; | ||
105 | |||
106 | return; | ||
107 | } | ||
108 | |||
109 | $entry = new Entry($this->user); | ||
110 | $entry->setUrl($importedEntry['url']); | ||
111 | $entry->setTitle($importedEntry['title']); | ||
112 | |||
113 | // update entry with content (in case fetching failed, the given entry will be return) | ||
114 | $entry = $this->fetchContent($entry, $importedEntry['url'], $importedEntry); | ||
115 | |||
116 | $entry->setArchived($importedEntry['is_archived']); | ||
117 | $entry->setStarred($importedEntry['is_starred']); | ||
118 | |||
119 | $this->em->persist($entry); | ||
120 | ++$this->importedEntries; | ||
121 | |||
122 | return $entry; | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * {@inheritdoc} | ||
127 | */ | ||
128 | protected function setEntryAsRead(array $importedEntry) | ||
129 | { | ||
130 | $importedEntry['is_archived'] = 1; | ||
131 | |||
132 | return $importedEntry; | ||
133 | } | ||
134 | } | ||
diff --git a/src/Wallabag/ImportBundle/Resources/config/rabbit.yml b/src/Wallabag/ImportBundle/Resources/config/rabbit.yml index 6ada6302..70b8a0d4 100644 --- a/src/Wallabag/ImportBundle/Resources/config/rabbit.yml +++ b/src/Wallabag/ImportBundle/Resources/config/rabbit.yml | |||
@@ -14,6 +14,13 @@ services: | |||
14 | - "@wallabag_user.user_repository" | 14 | - "@wallabag_user.user_repository" |
15 | - "@wallabag_import.readability.import" | 15 | - "@wallabag_import.readability.import" |
16 | - "@logger" | 16 | - "@logger" |
17 | wallabag_import.consumer.amqp.instapaper: | ||
18 | class: Wallabag\ImportBundle\Consumer\AMQPEntryConsumer | ||
19 | arguments: | ||
20 | - "@doctrine.orm.entity_manager" | ||
21 | - "@wallabag_user.user_repository" | ||
22 | - "@wallabag_import.instapaper.import" | ||
23 | - "@logger" | ||
17 | wallabag_import.consumer.amqp.wallabag_v1: | 24 | wallabag_import.consumer.amqp.wallabag_v1: |
18 | class: Wallabag\ImportBundle\Consumer\AMQPEntryConsumer | 25 | class: Wallabag\ImportBundle\Consumer\AMQPEntryConsumer |
19 | arguments: | 26 | arguments: |
diff --git a/src/Wallabag/ImportBundle/Resources/config/redis.yml b/src/Wallabag/ImportBundle/Resources/config/redis.yml index c9c2cf26..0a81e1b5 100644 --- a/src/Wallabag/ImportBundle/Resources/config/redis.yml +++ b/src/Wallabag/ImportBundle/Resources/config/redis.yml | |||
@@ -20,6 +20,26 @@ services: | |||
20 | - "@wallabag_import.readability.import" | 20 | - "@wallabag_import.readability.import" |
21 | - "@logger" | 21 | - "@logger" |
22 | 22 | ||
23 | # instapaper | ||
24 | wallabag_import.queue.redis.instapaper: | ||
25 | class: Simpleue\Queue\RedisQueue | ||
26 | arguments: | ||
27 | - "@wallabag_core.redis.client" | ||
28 | - "wallabag.import.instapaper" | ||
29 | |||
30 | wallabag_import.producer.redis.instapaper: | ||
31 | class: Wallabag\ImportBundle\Redis\Producer | ||
32 | arguments: | ||
33 | - "@wallabag_import.queue.redis.instapaper" | ||
34 | |||
35 | wallabag_import.consumer.redis.instapaper: | ||
36 | class: Wallabag\ImportBundle\Consumer\RedisEntryConsumer | ||
37 | arguments: | ||
38 | - "@doctrine.orm.entity_manager" | ||
39 | - "@wallabag_user.user_repository" | ||
40 | - "@wallabag_import.instapaper.import" | ||
41 | - "@logger" | ||
42 | |||
23 | 43 | ||
24 | wallabag_import.queue.redis.pocket: | 44 | wallabag_import.queue.redis.pocket: |
25 | class: Simpleue\Queue\RedisQueue | 45 | class: Simpleue\Queue\RedisQueue |
diff --git a/src/Wallabag/ImportBundle/Resources/config/services.yml b/src/Wallabag/ImportBundle/Resources/config/services.yml index 990f336d..89adc71b 100644 --- a/src/Wallabag/ImportBundle/Resources/config/services.yml +++ b/src/Wallabag/ImportBundle/Resources/config/services.yml | |||
@@ -57,6 +57,16 @@ services: | |||
57 | tags: | 57 | tags: |
58 | - { name: wallabag_import.import, alias: readability } | 58 | - { name: wallabag_import.import, alias: readability } |
59 | 59 | ||
60 | wallabag_import.instapaper.import: | ||
61 | class: Wallabag\ImportBundle\Import\InstapaperImport | ||
62 | arguments: | ||
63 | - "@doctrine.orm.entity_manager" | ||
64 | - "@wallabag_core.content_proxy" | ||
65 | calls: | ||
66 | - [ setLogger, [ "@logger" ]] | ||
67 | tags: | ||
68 | - { name: wallabag_import.import, alias: instapaper } | ||
69 | |||
60 | wallabag_import.firefox.import: | 70 | wallabag_import.firefox.import: |
61 | class: Wallabag\ImportBundle\Import\FirefoxImport | 71 | class: Wallabag\ImportBundle\Import\FirefoxImport |
62 | arguments: | 72 | arguments: |
diff --git a/src/Wallabag/ImportBundle/Resources/views/Instapaper/index.html.twig b/src/Wallabag/ImportBundle/Resources/views/Instapaper/index.html.twig new file mode 100644 index 00000000..5789361f --- /dev/null +++ b/src/Wallabag/ImportBundle/Resources/views/Instapaper/index.html.twig | |||
@@ -0,0 +1,45 @@ | |||
1 | {% extends "WallabagCoreBundle::layout.html.twig" %} | ||
2 | |||
3 | {% block title %}{{ 'import.instapaper.page_title'|trans }}{% endblock %} | ||
4 | |||
5 | {% block content %} | ||
6 | <div class="row"> | ||
7 | <div class="col s12"> | ||
8 | <div class="card-panel settings"> | ||
9 | {% include 'WallabagImportBundle:Import:_workerEnabled.html.twig' %} | ||
10 | |||
11 | <div class="row"> | ||
12 | <blockquote>{{ import.description|trans }}</blockquote> | ||
13 | <p>{{ 'import.instapaper.how_to'|trans }}</p> | ||
14 | |||
15 | <div class="col s12"> | ||
16 | {{ form_start(form, {'method': 'POST'}) }} | ||
17 | {{ form_errors(form) }} | ||
18 | <div class="row"> | ||
19 | <div class="file-field input-field col s12"> | ||
20 | {{ form_errors(form.file) }} | ||
21 | <div class="btn"> | ||
22 | <span>{{ form.file.vars.label|trans }}</span> | ||
23 | {{ form_widget(form.file) }} | ||
24 | </div> | ||
25 | <div class="file-path-wrapper"> | ||
26 | <input class="file-path validate" type="text"> | ||
27 | </div> | ||
28 | </div> | ||
29 | <div class="input-field col s6 with-checkbox"> | ||
30 | <h6>{{ 'import.form.mark_as_read_title'|trans }}</h6> | ||
31 | {{ form_widget(form.mark_as_read) }} | ||
32 | {{ form_label(form.mark_as_read) }} | ||
33 | </div> | ||
34 | </div> | ||
35 | |||
36 | {{ form_widget(form.save, { 'attr': {'class': 'btn waves-effect waves-light'} }) }} | ||
37 | |||
38 | {{ form_rest(form) }} | ||
39 | </form> | ||
40 | </div> | ||
41 | </div> | ||
42 | </div> | ||
43 | </div> | ||
44 | </div> | ||
45 | {% endblock %} | ||