2015-10-20 13:58:13 +02:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Wallabag\Controller\Import;
|
2015-10-20 13:58:13 +02:00
|
|
|
|
2022-08-28 02:01:46 +02:00
|
|
|
use Craue\ConfigBundle\Util\Config;
|
2022-12-19 10:37:22 +01:00
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\Producer as RabbitMqProducer;
|
2025-03-11 00:54:02 +01:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
|
2016-02-13 14:32:16 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
2017-07-01 09:52:38 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-08-28 02:01:46 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2018-10-04 14:07:20 +02:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2017-07-01 09:52:38 +02:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2022-12-19 10:37:22 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\Controller\AbstractController;
|
|
|
|
use Wallabag\Import\PocketImport;
|
|
|
|
use Wallabag\Redis\Producer as RedisProducer;
|
2016-02-13 14:32:16 +01:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
class PocketController extends AbstractController
|
2015-10-20 13:58:13 +02:00
|
|
|
{
|
2025-04-05 13:54:27 +02:00
|
|
|
public function __construct(
|
2025-04-05 13:59:36 +02:00
|
|
|
private readonly Config $craueConfig,
|
|
|
|
private readonly RabbitMqProducer $rabbitMqProducer,
|
|
|
|
private readonly RedisProducer $redisProducer,
|
|
|
|
private readonly SessionInterface $session,
|
2025-04-05 13:54:27 +02:00
|
|
|
) {
|
2022-12-19 10:37:22 +01:00
|
|
|
}
|
|
|
|
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/import/pocket', name: 'import_pocket', methods: ['GET'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('IMPORT_ENTRIES')]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function indexAction(PocketImport $pocketImport)
|
2015-10-20 13:58:13 +02:00
|
|
|
{
|
2025-10-02 11:54:12 +02:00
|
|
|
if (!$pocketImport->isEnabled()) {
|
|
|
|
throw $this->createNotFoundException('Import is disabled');
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$pocket = $this->getPocketImportService($pocketImport);
|
|
|
|
|
2016-02-13 14:32:16 +01:00
|
|
|
$form = $this->createFormBuilder($pocket)
|
2016-04-12 11:36:01 +02:00
|
|
|
->add('mark_as_read', CheckboxType::class, [
|
2016-03-09 08:59:08 +01:00
|
|
|
'label' => 'import.form.mark_as_read_label',
|
2016-03-04 10:04:51 +01:00
|
|
|
'required' => false,
|
2016-04-12 11:36:01 +02:00
|
|
|
])
|
2016-02-13 14:32:16 +01:00
|
|
|
->getForm();
|
|
|
|
|
2024-02-19 00:03:14 +01:00
|
|
|
return $this->render('Import/Pocket/index.html.twig', [
|
2022-12-19 10:37:22 +01:00
|
|
|
'import' => $pocket,
|
2016-09-16 22:22:25 +02:00
|
|
|
'has_consumer_key' => '' === trim($this->getUser()->getConfig()->getPocketConsumerKey()) ? false : true,
|
2016-02-13 14:32:16 +01:00
|
|
|
'form' => $form->createView(),
|
2015-12-31 11:24:46 +01:00
|
|
|
]);
|
2015-10-20 13:58:13 +02:00
|
|
|
}
|
|
|
|
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/import/pocket/auth', name: 'import_pocket_auth', methods: ['POST'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('IMPORT_ENTRIES')]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function authAction(Request $request, PocketImport $pocketImport)
|
2015-10-20 13:58:13 +02:00
|
|
|
{
|
2025-10-02 11:54:12 +02:00
|
|
|
if (!$pocketImport->isEnabled()) {
|
|
|
|
throw $this->createNotFoundException('Import is disabled');
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$requestToken = $this->getPocketImportService($pocketImport)
|
2016-04-12 11:36:01 +02:00
|
|
|
->getRequestToken($this->generateUrl('import', [], UrlGeneratorInterface::ABSOLUTE_URL));
|
2015-12-30 12:23:51 +01:00
|
|
|
|
2016-03-27 20:35:56 +02:00
|
|
|
if (false === $requestToken) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->addFlash(
|
2016-03-27 20:35:56 +02:00
|
|
|
'notice',
|
|
|
|
'flashes.import.notice.failed'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->redirect($this->generateUrl('import_pocket'));
|
|
|
|
}
|
|
|
|
|
2025-04-05 17:34:00 +02:00
|
|
|
$form = $request->request->all('form');
|
2020-04-04 17:11:25 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->session->set('import.pocket.code', $requestToken);
|
2025-04-05 17:34:00 +02:00
|
|
|
if (\array_key_exists('mark_as_read', $form)) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->session->set('mark_as_read', $form['mark_as_read']);
|
2020-04-04 17:11:25 +02:00
|
|
|
}
|
2015-10-20 13:58:13 +02:00
|
|
|
|
2015-12-30 12:23:51 +01:00
|
|
|
return $this->redirect(
|
2017-07-01 09:52:38 +02:00
|
|
|
'https://getpocket.com/auth/authorize?request_token=' . $requestToken . '&redirect_uri=' . $this->generateUrl('import_pocket_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
|
2015-12-30 12:23:51 +01:00
|
|
|
301
|
|
|
|
);
|
2015-10-20 13:58:13 +02:00
|
|
|
}
|
|
|
|
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/import/pocket/callback', name: 'import_pocket_callback', methods: ['GET'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('IMPORT_ENTRIES')]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function callbackAction(PocketImport $pocketImport, TranslatorInterface $translator)
|
2015-10-20 13:58:13 +02:00
|
|
|
{
|
2025-10-02 11:54:12 +02:00
|
|
|
if (!$pocketImport->isEnabled()) {
|
|
|
|
throw $this->createNotFoundException('Import is disabled');
|
|
|
|
}
|
|
|
|
|
2016-03-11 14:48:46 +01:00
|
|
|
$message = 'flashes.import.notice.failed';
|
2022-12-19 10:37:22 +01:00
|
|
|
$pocket = $this->getPocketImportService($pocketImport);
|
2016-03-11 14:48:46 +01:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$markAsRead = $this->session->get('mark_as_read');
|
|
|
|
$this->session->remove('mark_as_read');
|
2015-12-30 12:23:51 +01:00
|
|
|
|
|
|
|
// something bad happend on pocket side
|
2022-12-19 10:37:22 +01:00
|
|
|
if (false === $pocket->authorize($this->session->get('import.pocket.code'))) {
|
|
|
|
$this->addFlash('notice', $message);
|
2015-12-30 12:23:51 +01:00
|
|
|
|
|
|
|
return $this->redirect($this->generateUrl('import_pocket'));
|
|
|
|
}
|
|
|
|
|
2016-02-13 14:32:16 +01:00
|
|
|
if (true === $pocket->setMarkAsRead($markAsRead)->import()) {
|
2015-12-30 12:23:51 +01:00
|
|
|
$summary = $pocket->getSummary();
|
2022-12-19 10:37:22 +01:00
|
|
|
$message = $translator->trans('flashes.import.notice.summary', [
|
2020-04-04 17:11:25 +02:00
|
|
|
'%imported%' => null !== $summary && \array_key_exists('imported', $summary) ? $summary['imported'] : 0,
|
|
|
|
'%skipped%' => null !== $summary && \array_key_exists('skipped', $summary) ? $summary['skipped'] : 0,
|
2016-04-12 11:36:01 +02:00
|
|
|
]);
|
2016-09-13 21:09:05 +02:00
|
|
|
|
2020-04-04 17:11:25 +02:00
|
|
|
if (null !== $summary && \array_key_exists('queued', $summary) && 0 < $summary['queued']) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$message = $translator->trans('flashes.import.notice.summary_with_queue', [
|
2016-09-13 21:09:05 +02:00
|
|
|
'%queued%' => $summary['queued'],
|
|
|
|
]);
|
|
|
|
}
|
2015-12-30 12:23:51 +01:00
|
|
|
}
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->addFlash('notice', $message);
|
2015-10-20 13:58:13 +02:00
|
|
|
|
|
|
|
return $this->redirect($this->generateUrl('homepage'));
|
|
|
|
}
|
2017-07-01 09:52:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return Pocket Import Service with or without RabbitMQ enabled.
|
|
|
|
*/
|
2022-12-19 10:37:22 +01:00
|
|
|
private function getPocketImportService(PocketImport $pocketImport): PocketImport
|
2017-07-01 09:52:38 +02:00
|
|
|
{
|
2022-12-19 10:37:22 +01:00
|
|
|
$pocketImport->setUser($this->getUser());
|
2017-07-01 09:52:38 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
if ($this->craueConfig->get('import_with_rabbitmq')) {
|
|
|
|
$pocketImport->setProducer($this->rabbitMqProducer);
|
|
|
|
} elseif ($this->craueConfig->get('import_with_redis')) {
|
|
|
|
$pocketImport->setProducer($this->redisProducer);
|
2017-07-01 09:52:38 +02:00
|
|
|
}
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
return $pocketImport;
|
2017-07-01 09:52:38 +02:00
|
|
|
}
|
2015-10-20 13:58:13 +02:00
|
|
|
}
|