2015-03-28 14:27:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Wallabag\Controller;
|
2015-03-28 14:27:45 +01:00
|
|
|
|
2017-06-10 13:11:08 +02:00
|
|
|
use Pagerfanta\Adapter\ArrayAdapter;
|
2020-07-29 06:36:43 +02:00
|
|
|
use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
|
2016-11-19 14:53:28 +01:00
|
|
|
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
|
2015-12-22 10:16:34 +01:00
|
|
|
use Pagerfanta\Pagerfanta;
|
2025-03-10 23:09:48 +01:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
|
2015-03-28 14:27:45 +01:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
2022-03-10 08:06:55 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2017-06-10 13:11:08 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2022-03-10 08:06:55 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
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;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\Entity\Tag;
|
|
|
|
|
use Wallabag\Entity\User;
|
|
|
|
|
use Wallabag\Helper\PreparePagerForEntries;
|
|
|
|
|
use Wallabag\Repository\EntryRepository;
|
2015-03-28 14:27:45 +01:00
|
|
|
|
2022-12-19 13:23:56 +01:00
|
|
|
class FeedController extends AbstractController
|
2015-03-28 14:27:45 +01:00
|
|
|
{
|
2025-04-05 13:54:27 +02:00
|
|
|
public function __construct(
|
2025-04-05 13:59:36 +02:00
|
|
|
private readonly EntryRepository $entryRepository,
|
2025-11-23 02:16:01 +01:00
|
|
|
private readonly int $feedLimit,
|
|
|
|
|
private readonly string $version,
|
2025-04-05 13:54:27 +02:00
|
|
|
) {
|
2022-12-19 10:37:22 +01:00
|
|
|
}
|
|
|
|
|
|
2015-03-28 14:27:45 +01:00
|
|
|
/**
|
2015-05-30 13:52:26 +02:00
|
|
|
* Shows unread entries for current user.
|
2015-03-28 14:27:45 +01:00
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-03-28 14:27:45 +01:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/feed/{username}/{token}/unread/{page}', name: 'unread_feed', methods: ['GET'], defaults: ['page' => 1, '_format' => 'xml'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('PUBLIC_ACCESS')]
|
|
|
|
|
#[ParamConverter('user', class: User::class, converter: 'username_feed_token_converter')]
|
2017-06-13 18:48:10 +02:00
|
|
|
public function showUnreadFeedAction(User $user, $page)
|
2015-03-28 14:27:45 +01:00
|
|
|
{
|
2017-06-13 18:48:10 +02:00
|
|
|
return $this->showEntries('unread', $user, $page);
|
2015-03-28 14:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-30 13:52:26 +02:00
|
|
|
* Shows read entries for current user.
|
2015-03-28 14:27:45 +01:00
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-03-28 14:27:45 +01:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/feed/{username}/{token}/archive/{page}', name: 'archive_feed', methods: ['GET'], defaults: ['page' => 1, '_format' => 'xml'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('PUBLIC_ACCESS')]
|
|
|
|
|
#[ParamConverter('user', class: User::class, converter: 'username_feed_token_converter')]
|
2017-06-13 18:48:10 +02:00
|
|
|
public function showArchiveFeedAction(User $user, $page)
|
2015-03-28 14:27:45 +01:00
|
|
|
{
|
2017-06-13 18:48:10 +02:00
|
|
|
return $this->showEntries('archive', $user, $page);
|
2015-03-28 14:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-30 13:52:26 +02:00
|
|
|
* Shows starred entries for current user.
|
2015-03-28 14:27:45 +01:00
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-03-28 14:27:45 +01:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/feed/{username}/{token}/starred/{page}', name: 'starred_feed', methods: ['GET'], defaults: ['page' => 1, '_format' => 'xml'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('PUBLIC_ACCESS')]
|
|
|
|
|
#[ParamConverter('user', class: User::class, converter: 'username_feed_token_converter')]
|
2017-06-13 18:48:10 +02:00
|
|
|
public function showStarredFeedAction(User $user, $page)
|
2015-03-28 14:27:45 +01:00
|
|
|
{
|
2017-06-13 18:48:10 +02:00
|
|
|
return $this->showEntries('starred', $user, $page);
|
2015-08-20 20:10:06 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 18:29:46 +02:00
|
|
|
/**
|
|
|
|
|
* Shows all entries for current user.
|
|
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2017-06-20 18:29:46 +02:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/feed/{username}/{token}/all/{page}', name: 'all_feed', methods: ['GET'], defaults: ['page' => 1, '_format' => 'xml'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('PUBLIC_ACCESS')]
|
|
|
|
|
#[ParamConverter('user', class: User::class, converter: 'username_feed_token_converter')]
|
2019-04-25 14:12:56 +02:00
|
|
|
public function showAllFeedAction(User $user, $page)
|
2017-06-20 18:29:46 +02:00
|
|
|
{
|
2019-04-25 14:12:56 +02:00
|
|
|
return $this->showEntries('all', $user, $page);
|
2017-06-20 18:29:46 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-10 13:11:08 +02:00
|
|
|
/**
|
|
|
|
|
* Shows entries associated to a tag for current user.
|
|
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2017-06-10 13:11:08 +02:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/feed/{username}/{token}/tags/{slug}/{page}', name: 'tag_feed', methods: ['GET'], defaults: ['page' => 1, '_format' => 'xml'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('PUBLIC_ACCESS')]
|
|
|
|
|
#[ParamConverter('user', class: User::class, converter: 'username_feed_token_converter')]
|
|
|
|
|
#[ParamConverter('tag', options: ['mapping' => ['slug' => 'slug']])]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function showTagsFeedAction(Request $request, User $user, Tag $tag, PreparePagerForEntries $preparePagerForEntries, $page)
|
2017-06-10 13:11:08 +02:00
|
|
|
{
|
2022-03-10 08:06:55 +01:00
|
|
|
$sort = $request->query->get('sort', 'created');
|
|
|
|
|
|
|
|
|
|
$sorts = [
|
|
|
|
|
'created' => 'createdAt',
|
|
|
|
|
'updated' => 'updatedAt',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (!isset($sorts[$sort])) {
|
2024-08-14 16:39:36 +02:00
|
|
|
throw new BadRequestHttpException(\sprintf('Sort "%s" is not available.', $sort));
|
2022-03-10 08:06:55 +01:00
|
|
|
}
|
|
|
|
|
|
2017-06-10 13:11:08 +02:00
|
|
|
$url = $this->generateUrl(
|
2017-06-13 18:48:10 +02:00
|
|
|
'tag_feed',
|
2017-06-10 13:11:08 +02:00
|
|
|
[
|
|
|
|
|
'username' => $user->getUsername(),
|
2017-06-13 18:48:10 +02:00
|
|
|
'token' => $user->getConfig()->getFeedToken(),
|
2017-06-10 13:11:08 +02:00
|
|
|
'slug' => $tag->getSlug(),
|
|
|
|
|
],
|
|
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL
|
|
|
|
|
);
|
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$entriesByTag = $this->entryRepository->findAllByTagId(
|
2017-06-10 13:11:08 +02:00
|
|
|
$user->getId(),
|
2022-03-10 08:06:55 +01:00
|
|
|
$tag->getId(),
|
|
|
|
|
$sorts[$sort]
|
2017-06-10 13:11:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$pagerAdapter = new ArrayAdapter($entriesByTag);
|
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $preparePagerForEntries->prepare(
|
2017-06-10 13:11:08 +02:00
|
|
|
$pagerAdapter,
|
|
|
|
|
$user
|
|
|
|
|
);
|
2022-01-31 01:18:04 -08:00
|
|
|
|
2025-11-23 02:16:01 +01:00
|
|
|
$perPage = $user->getConfig()->getFeedLimit() ?: $this->feedLimit;
|
2022-01-30 18:11:18 +01:00
|
|
|
$entries->setMaxPerPage($perPage);
|
2017-06-10 13:11:08 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$entries->setCurrentPage($page);
|
2025-04-05 13:56:56 +02:00
|
|
|
} catch (OutOfRangeCurrentPageException) {
|
2017-06-10 13:11:08 +02:00
|
|
|
if ($page > 1) {
|
2017-07-01 09:52:38 +02:00
|
|
|
return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
|
2017-06-10 13:11:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->render(
|
2024-02-19 00:03:14 +01:00
|
|
|
'Entry/entries.xml.twig',
|
2017-06-10 13:11:08 +02:00
|
|
|
[
|
2017-06-13 18:48:10 +02:00
|
|
|
'type' => 'tag',
|
2017-06-10 13:11:08 +02:00
|
|
|
'url' => $url,
|
|
|
|
|
'entries' => $entries,
|
2017-06-13 18:48:10 +02:00
|
|
|
'user' => $user->getUsername(),
|
2025-11-23 02:16:01 +01:00
|
|
|
'version' => $this->version,
|
2017-06-13 18:48:10 +02:00
|
|
|
'tag' => $tag->getSlug(),
|
2022-03-10 08:06:55 +01:00
|
|
|
'updated' => $this->prepareFeedUpdatedDate($entries, $sort),
|
2017-06-10 13:11:08 +02:00
|
|
|
],
|
2017-06-13 18:48:10 +02:00
|
|
|
new Response('', 200, ['Content-Type' => 'application/atom+xml'])
|
2017-06-10 13:11:08 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 08:06:55 +01:00
|
|
|
private function prepareFeedUpdatedDate(Pagerfanta $entries, $sort = 'created')
|
|
|
|
|
{
|
|
|
|
|
$currentPageResults = $entries->getCurrentPageResults();
|
|
|
|
|
|
|
|
|
|
if (isset($currentPageResults[0])) {
|
|
|
|
|
$firstEntry = $currentPageResults[0];
|
|
|
|
|
if ('created' === $sort) {
|
|
|
|
|
return $firstEntry->getCreatedAt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $firstEntry->getUpdatedAt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 20:10:06 +02:00
|
|
|
/**
|
|
|
|
|
* Global method to retrieve entries depending on the given type
|
|
|
|
|
* It returns the response to be send.
|
|
|
|
|
*
|
|
|
|
|
* @param string $type Entries type: unread, starred or archive
|
2016-11-19 14:53:28 +01:00
|
|
|
* @param int $page
|
2015-08-20 20:10:06 +02:00
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-08-20 20:10:06 +02:00
|
|
|
*/
|
2022-12-19 10:37:22 +01:00
|
|
|
private function showEntries(string $type, User $user, $page = 1)
|
2015-08-20 20:10:06 +02:00
|
|
|
{
|
2025-04-05 13:56:56 +02:00
|
|
|
$qb = match ($type) {
|
|
|
|
|
'starred' => $this->entryRepository->getBuilderForStarredByUser($user->getId()),
|
|
|
|
|
'archive' => $this->entryRepository->getBuilderForArchiveByUser($user->getId()),
|
|
|
|
|
'unread' => $this->entryRepository->getBuilderForUnreadByUser($user->getId()),
|
|
|
|
|
'all' => $this->entryRepository->getBuilderForAllByUser($user->getId()),
|
|
|
|
|
default => throw new \InvalidArgumentException(\sprintf('Type "%s" is not implemented.', $type)),
|
|
|
|
|
};
|
2015-03-28 14:27:45 +01:00
|
|
|
|
2016-12-14 09:00:14 +01:00
|
|
|
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
|
2015-08-07 22:20:30 +02:00
|
|
|
$entries = new Pagerfanta($pagerAdapter);
|
|
|
|
|
|
2025-11-23 02:16:01 +01:00
|
|
|
$perPage = $user->getConfig()->getFeedLimit() ?: $this->feedLimit;
|
2015-07-27 23:20:32 +02:00
|
|
|
$entries->setMaxPerPage($perPage);
|
|
|
|
|
|
2016-11-19 14:53:28 +01:00
|
|
|
$url = $this->generateUrl(
|
2017-06-13 18:48:10 +02:00
|
|
|
$type . '_feed',
|
2016-11-19 14:53:28 +01:00
|
|
|
[
|
|
|
|
|
'username' => $user->getUsername(),
|
2017-06-13 18:48:10 +02:00
|
|
|
'token' => $user->getConfig()->getFeedToken(),
|
2016-11-19 14:53:28 +01:00
|
|
|
],
|
|
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$entries->setCurrentPage((int) $page);
|
2025-04-05 13:56:56 +02:00
|
|
|
} catch (OutOfRangeCurrentPageException) {
|
2016-11-19 14:53:28 +01:00
|
|
|
if ($page > 1) {
|
2017-06-13 18:48:10 +02:00
|
|
|
return $this->redirect($url . '/' . $entries->getNbPages());
|
2016-11-19 14:53:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 00:03:14 +01:00
|
|
|
return $this->render('Entry/entries.xml.twig', [
|
2017-06-13 18:48:10 +02:00
|
|
|
'type' => $type,
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'entries' => $entries,
|
|
|
|
|
'user' => $user->getUsername(),
|
2025-11-23 02:16:01 +01:00
|
|
|
'version' => $this->version,
|
2022-03-10 08:06:55 +01:00
|
|
|
'updated' => $this->prepareFeedUpdatedDate($entries),
|
2024-01-01 19:11:01 +01:00
|
|
|
], new Response('', 200, ['Content-Type' => 'application/atom+xml']));
|
2015-03-28 14:27:45 +01:00
|
|
|
}
|
|
|
|
|
}
|