2015-03-28 13:27:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Controller;
|
|
|
|
|
2017-06-10 11:11:08 +00:00
|
|
|
use Pagerfanta\Adapter\ArrayAdapter;
|
2020-07-29 04:36:43 +00:00
|
|
|
use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
|
2016-11-19 13:53:28 +00:00
|
|
|
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
|
2015-12-22 09:16:34 +00:00
|
|
|
use Pagerfanta\Pagerfanta;
|
2015-03-28 13:27:45 +00:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
2022-03-10 07:06:55 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2017-06-10 11:11:08 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2022-03-10 07:06:55 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2018-10-04 12:07:20 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2017-06-10 11:11:08 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Tag;
|
2022-04-24 15:48:59 +00:00
|
|
|
use Wallabag\CoreBundle\Helper\PreparePagerForEntries;
|
2022-04-24 15:58:57 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\EntryRepository;
|
2015-12-22 09:16:34 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2015-03-28 13:27:45 +00:00
|
|
|
|
2017-06-13 16:48:10 +00:00
|
|
|
class FeedController extends Controller
|
2015-03-28 13:27:45 +00:00
|
|
|
{
|
|
|
|
/**
|
2015-05-30 11:52:26 +00:00
|
|
|
* Shows unread entries for current user.
|
2015-03-28 13:27:45 +00:00
|
|
|
*
|
2019-04-25 12:12:56 +00:00
|
|
|
* @Route("/feed/{username}/{token}/unread/{page}", name="unread_feed", defaults={"page"=1, "_format"="xml"})
|
|
|
|
*
|
2017-06-13 16:48:10 +00:00
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
|
|
|
|
*
|
|
|
|
* @param $page
|
2015-03-28 13:27:45 +00:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2017-06-13 16:48:10 +00:00
|
|
|
public function showUnreadFeedAction(User $user, $page)
|
2015-03-28 13:27:45 +00:00
|
|
|
{
|
2017-06-13 16:48:10 +00:00
|
|
|
return $this->showEntries('unread', $user, $page);
|
2015-03-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-30 11:52:26 +00:00
|
|
|
* Shows read entries for current user.
|
2015-03-28 13:27:45 +00:00
|
|
|
*
|
2019-04-25 12:12:56 +00:00
|
|
|
* @Route("/feed/{username}/{token}/archive/{page}", name="archive_feed", defaults={"page"=1, "_format"="xml"})
|
|
|
|
*
|
2017-06-13 16:48:10 +00:00
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
|
|
|
|
*
|
|
|
|
* @param $page
|
2015-03-28 13:27:45 +00:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2017-06-13 16:48:10 +00:00
|
|
|
public function showArchiveFeedAction(User $user, $page)
|
2015-03-28 13:27:45 +00:00
|
|
|
{
|
2017-06-13 16:48:10 +00:00
|
|
|
return $this->showEntries('archive', $user, $page);
|
2015-03-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-30 11:52:26 +00:00
|
|
|
* Shows starred entries for current user.
|
2015-03-28 13:27:45 +00:00
|
|
|
*
|
2019-04-25 12:12:56 +00:00
|
|
|
* @Route("/feed/{username}/{token}/starred/{page}", name="starred_feed", defaults={"page"=1, "_format"="xml"})
|
|
|
|
*
|
2017-06-13 16:48:10 +00:00
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
|
|
|
|
*
|
|
|
|
* @param $page
|
2015-03-28 13:27:45 +00:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2017-06-13 16:48:10 +00:00
|
|
|
public function showStarredFeedAction(User $user, $page)
|
2015-03-28 13:27:45 +00:00
|
|
|
{
|
2017-06-13 16:48:10 +00:00
|
|
|
return $this->showEntries('starred', $user, $page);
|
2015-08-20 18:10:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 16:29:46 +00:00
|
|
|
/**
|
|
|
|
* Shows all entries for current user.
|
|
|
|
*
|
2019-04-25 12:12:56 +00:00
|
|
|
* @Route("/feed/{username}/{token}/all/{page}", name="all_feed", defaults={"page"=1, "_format"="xml"})
|
|
|
|
*
|
2017-06-13 16:48:10 +00:00
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
|
2017-06-20 16:29:46 +00:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2019-04-25 12:12:56 +00:00
|
|
|
public function showAllFeedAction(User $user, $page)
|
2017-06-20 16:29:46 +00:00
|
|
|
{
|
2019-04-25 12:12:56 +00:00
|
|
|
return $this->showEntries('all', $user, $page);
|
2017-06-20 16:29:46 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 11:11:08 +00:00
|
|
|
/**
|
|
|
|
* Shows entries associated to a tag for current user.
|
|
|
|
*
|
2019-04-25 12:12:56 +00:00
|
|
|
* @Route("/feed/{username}/{token}/tags/{slug}/{page}", name="tag_feed", defaults={"page"=1, "_format"="xml"})
|
|
|
|
*
|
2017-06-13 16:48:10 +00:00
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter")
|
2017-06-10 11:11:08 +00:00
|
|
|
* @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2022-03-10 07:06:55 +00:00
|
|
|
public function showTagsFeedAction(Request $request, User $user, Tag $tag, $page)
|
2017-06-10 11:11:08 +00:00
|
|
|
{
|
2022-03-10 07:06:55 +00:00
|
|
|
$sort = $request->query->get('sort', 'created');
|
|
|
|
|
|
|
|
$sorts = [
|
|
|
|
'created' => 'createdAt',
|
|
|
|
'updated' => 'updatedAt',
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!isset($sorts[$sort])) {
|
|
|
|
throw new BadRequestHttpException(sprintf('Sort "%s" is not available.', $sort));
|
|
|
|
}
|
|
|
|
|
2017-06-10 11:11:08 +00:00
|
|
|
$url = $this->generateUrl(
|
2017-06-13 16:48:10 +00:00
|
|
|
'tag_feed',
|
2017-06-10 11:11:08 +00:00
|
|
|
[
|
|
|
|
'username' => $user->getUsername(),
|
2017-06-13 16:48:10 +00:00
|
|
|
'token' => $user->getConfig()->getFeedToken(),
|
2017-06-10 11:11:08 +00:00
|
|
|
'slug' => $tag->getSlug(),
|
|
|
|
],
|
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL
|
|
|
|
);
|
|
|
|
|
2022-04-24 15:58:57 +00:00
|
|
|
$entriesByTag = $this->get(EntryRepository::class)->findAllByTagId(
|
2017-06-10 11:11:08 +00:00
|
|
|
$user->getId(),
|
2022-03-10 07:06:55 +00:00
|
|
|
$tag->getId(),
|
|
|
|
$sorts[$sort]
|
2017-06-10 11:11:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$pagerAdapter = new ArrayAdapter($entriesByTag);
|
|
|
|
|
2022-04-24 15:48:59 +00:00
|
|
|
$entries = $this->get(PreparePagerForEntries::class)->prepare(
|
2017-06-10 11:11:08 +00:00
|
|
|
$pagerAdapter,
|
|
|
|
$user
|
|
|
|
);
|
2022-01-31 09:18:04 +00:00
|
|
|
|
2022-01-30 17:11:18 +00:00
|
|
|
$perPage = $user->getConfig()->getFeedLimit() ?: $this->getParameter('wallabag_core.feed_limit');
|
|
|
|
$entries->setMaxPerPage($perPage);
|
2017-06-10 11:11:08 +00:00
|
|
|
|
|
|
|
if (null === $entries) {
|
|
|
|
throw $this->createNotFoundException('No entries found?');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$entries->setCurrentPage($page);
|
|
|
|
} catch (OutOfRangeCurrentPageException $e) {
|
|
|
|
if ($page > 1) {
|
2017-07-01 07:52:38 +00:00
|
|
|
return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
|
2017-06-10 11:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render(
|
|
|
|
'@WallabagCore/themes/common/Entry/entries.xml.twig',
|
|
|
|
[
|
2017-06-13 16:48:10 +00:00
|
|
|
'type' => 'tag',
|
2017-06-10 11:11:08 +00:00
|
|
|
'url' => $url,
|
|
|
|
'entries' => $entries,
|
2017-06-13 16:48:10 +00:00
|
|
|
'user' => $user->getUsername(),
|
|
|
|
'domainName' => $this->getParameter('domain_name'),
|
|
|
|
'version' => $this->getParameter('wallabag_core.version'),
|
|
|
|
'tag' => $tag->getSlug(),
|
2022-03-10 07:06:55 +00:00
|
|
|
'updated' => $this->prepareFeedUpdatedDate($entries, $sort),
|
2017-06-10 11:11:08 +00:00
|
|
|
],
|
2017-06-13 16:48:10 +00:00
|
|
|
new Response('', 200, ['Content-Type' => 'application/atom+xml'])
|
2017-06-10 11:11:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-10 07:06:55 +00: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 18:10:06 +00: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 13:53:28 +00:00
|
|
|
* @param int $page
|
2015-08-20 18:10:06 +00:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
2016-11-19 13:53:28 +00:00
|
|
|
private function showEntries($type, User $user, $page = 1)
|
2015-08-20 18:10:06 +00:00
|
|
|
{
|
2022-04-24 15:58:57 +00:00
|
|
|
$repository = $this->get(EntryRepository::class);
|
2015-08-20 18:10:06 +00:00
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'starred':
|
|
|
|
$qb = $repository->getBuilderForStarredByUser($user->getId());
|
|
|
|
break;
|
|
|
|
case 'archive':
|
|
|
|
$qb = $repository->getBuilderForArchiveByUser($user->getId());
|
|
|
|
break;
|
|
|
|
case 'unread':
|
|
|
|
$qb = $repository->getBuilderForUnreadByUser($user->getId());
|
|
|
|
break;
|
2017-06-20 16:29:46 +00:00
|
|
|
case 'all':
|
|
|
|
$qb = $repository->getBuilderForAllByUser($user->getId());
|
|
|
|
break;
|
2015-08-20 18:10:06 +00:00
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
|
|
|
|
}
|
2015-03-28 13:27:45 +00:00
|
|
|
|
2016-12-14 08:00:14 +00:00
|
|
|
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
|
2015-08-07 20:20:30 +00:00
|
|
|
$entries = new Pagerfanta($pagerAdapter);
|
|
|
|
|
2019-06-21 10:46:53 +00:00
|
|
|
$perPage = $user->getConfig()->getFeedLimit() ?: $this->getParameter('wallabag_core.feed_limit');
|
2015-07-27 21:20:32 +00:00
|
|
|
$entries->setMaxPerPage($perPage);
|
|
|
|
|
2016-11-19 13:53:28 +00:00
|
|
|
$url = $this->generateUrl(
|
2017-06-13 16:48:10 +00:00
|
|
|
$type . '_feed',
|
2016-11-19 13:53:28 +00:00
|
|
|
[
|
|
|
|
'username' => $user->getUsername(),
|
2017-06-13 16:48:10 +00:00
|
|
|
'token' => $user->getConfig()->getFeedToken(),
|
2016-11-19 13:53:28 +00:00
|
|
|
],
|
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$entries->setCurrentPage((int) $page);
|
|
|
|
} catch (OutOfRangeCurrentPageException $e) {
|
|
|
|
if ($page > 1) {
|
2017-06-13 16:48:10 +00:00
|
|
|
return $this->redirect($url . '/' . $entries->getNbPages());
|
2016-11-19 13:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 16:48:10 +00:00
|
|
|
return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [
|
|
|
|
'type' => $type,
|
|
|
|
'url' => $url,
|
|
|
|
'entries' => $entries,
|
|
|
|
'user' => $user->getUsername(),
|
|
|
|
'domainName' => $this->getParameter('domain_name'),
|
|
|
|
'version' => $this->getParameter('wallabag_core.version'),
|
2022-03-10 07:06:55 +00:00
|
|
|
'updated' => $this->prepareFeedUpdatedDate($entries),
|
2017-06-13 16:48:10 +00:00
|
|
|
],
|
|
|
|
new Response('', 200, ['Content-Type' => 'application/atom+xml'])
|
2017-06-10 11:11:08 +00:00
|
|
|
);
|
2015-03-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
}
|