2015-10-15 20:06:59 +02:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Wallabag\Controller;
|
2015-10-15 20:06:59 +02:00
|
|
|
|
2025-03-10 23:17:43 +01:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
|
2016-10-27 09:41:19 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-08-28 16:59:43 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2015-10-16 10:51:53 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2018-10-04 14:07:20 +02:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2025-03-10 23:17:43 +01:00
|
|
|
use Wallabag\Entity\Entry;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\Helper\EntriesExport;
|
|
|
|
use Wallabag\Repository\EntryRepository;
|
|
|
|
use Wallabag\Repository\TagRepository;
|
2015-10-15 20:06:59 +02:00
|
|
|
|
2015-10-30 20:57:10 +01:00
|
|
|
/**
|
|
|
|
* The try/catch can be removed once all formats will be implemented.
|
|
|
|
* Still need implementation: txt.
|
|
|
|
*/
|
2022-12-19 13:23:56 +01:00
|
|
|
class ExportController extends AbstractController
|
2015-10-15 20:06:59 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-10-16 10:51:53 +02:00
|
|
|
* Gets one entry content.
|
2015-10-15 20:06:59 +02:00
|
|
|
*
|
2025-03-10 23:17:43 +01:00
|
|
|
* @IsGranted("EXPORT", subject="entry")
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-10-15 20:06:59 +02:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/export/{entry}.{format}', name: 'export_entry', methods: ['GET'], requirements: ['format' => 'epub|pdf|json|xml|txt|csv|md', 'entry' => '\d+'])]
|
2025-03-10 23:17:43 +01:00
|
|
|
public function downloadEntryAction(Request $request, EntryRepository $entryRepository, EntriesExport $entriesExport, string $format, Entry $entry)
|
2015-10-15 20:06:59 +02:00
|
|
|
{
|
2015-10-16 10:51:53 +02:00
|
|
|
try {
|
2022-12-19 10:37:22 +01:00
|
|
|
return $entriesExport
|
2015-10-16 10:51:53 +02:00
|
|
|
->setEntries($entry)
|
|
|
|
->updateTitle('entry')
|
2017-07-08 17:55:58 +02:00
|
|
|
->updateAuthor('entry')
|
2015-10-16 10:51:53 +02:00
|
|
|
->exportAs($format);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new NotFoundHttpException($e->getMessage());
|
2015-10-15 20:06:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-16 10:51:53 +02:00
|
|
|
* Export all entries for current user.
|
2015-10-15 20:06:59 +02:00
|
|
|
*
|
2025-03-10 23:17:43 +01:00
|
|
|
* @IsGranted("EXPORT_ENTRIES")
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-10-15 20:06:59 +02:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/export/{category}.{format}', name: 'export_entries', methods: ['GET'], requirements: ['format' => 'epub|pdf|json|xml|txt|csv|md', 'category' => 'all|unread|starred|archive|tag_entries|untagged|search|annotated|same_domain'])]
|
2023-06-29 17:10:23 +02:00
|
|
|
public function downloadEntriesAction(Request $request, EntryRepository $entryRepository, TagRepository $tagRepository, EntriesExport $entriesExport, string $format, string $category, int $entry = 0)
|
2015-10-15 20:06:59 +02:00
|
|
|
{
|
2015-10-16 10:51:53 +02:00
|
|
|
$method = ucfirst($category);
|
2017-07-01 09:52:38 +02:00
|
|
|
$methodBuilder = 'getBuilderFor' . $method . 'ByUser';
|
2019-01-06 20:17:35 +01:00
|
|
|
$title = $method;
|
2016-10-27 09:41:19 +02:00
|
|
|
|
2023-06-29 17:10:23 +02:00
|
|
|
if ('same_domain' === $category) {
|
|
|
|
$entries = $entryRepository->getBuilderForSameDomainByUser(
|
|
|
|
$this->getUser()->getId(),
|
2025-01-19 02:06:54 +01:00
|
|
|
$request->query->get('entry')
|
2023-06-29 17:10:23 +02:00
|
|
|
)->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
$title = 'Same domain';
|
|
|
|
} elseif ('tag_entries' === $category) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$tag = $tagRepository->findOneBySlug($request->query->get('tag'));
|
2016-10-27 09:41:19 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository->findAllByTagId(
|
2017-06-10 12:33:58 +02:00
|
|
|
$this->getUser()->getId(),
|
|
|
|
$tag->getId()
|
|
|
|
);
|
2019-01-06 20:17:35 +01:00
|
|
|
|
|
|
|
$title = 'Tag ' . $tag->getLabel();
|
2020-02-04 22:19:45 +01:00
|
|
|
} elseif ('search' === $category) {
|
2025-04-05 13:14:32 +02:00
|
|
|
$searchTerm = $request->query->all('search_entry')['term'] ?? '';
|
|
|
|
$currentRoute = $request->query->get('currentRoute') ?? '';
|
2020-02-04 22:19:45 +01:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository->getBuilderForSearchByUser(
|
2024-01-01 19:11:01 +01:00
|
|
|
$this->getUser()->getId(),
|
|
|
|
$searchTerm,
|
|
|
|
$currentRoute
|
2020-02-04 22:19:45 +01:00
|
|
|
)->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
$title = 'Search ' . $searchTerm;
|
2020-04-26 14:09:16 +02:00
|
|
|
} elseif ('annotated' === $category) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository->getBuilderForAnnotationsByUser(
|
2020-04-20 19:00:58 +02:00
|
|
|
$this->getUser()->getId()
|
|
|
|
)->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
$title = 'With annotations';
|
2016-10-27 09:41:19 +02:00
|
|
|
} else {
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository
|
2016-10-27 09:41:19 +02:00
|
|
|
->$methodBuilder($this->getUser()->getId())
|
|
|
|
->getQuery()
|
|
|
|
->getResult();
|
|
|
|
}
|
2015-10-16 10:51:53 +02:00
|
|
|
|
|
|
|
try {
|
2022-12-19 10:37:22 +01:00
|
|
|
return $entriesExport
|
2015-10-16 10:51:53 +02:00
|
|
|
->setEntries($entries)
|
2019-01-06 20:17:35 +01:00
|
|
|
->updateTitle($title)
|
2017-07-08 17:55:58 +02:00
|
|
|
->updateAuthor($method)
|
2015-10-16 10:51:53 +02:00
|
|
|
->exportAs($format);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
throw new NotFoundHttpException($e->getMessage());
|
|
|
|
}
|
2015-10-15 20:06:59 +02:00
|
|
|
}
|
|
|
|
}
|