3 namespace Wallabag\CoreBundle\Controller
;
5 use Pagerfanta\Adapter\ArrayAdapter
;
6 use Pagerfanta\Adapter\DoctrineORMAdapter
;
7 use Pagerfanta\Exception\OutOfRangeCurrentPageException
;
8 use Pagerfanta\Pagerfanta
;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
10 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
11 use Symfony\Component\HttpFoundation\Request
;
12 use Symfony\Component\HttpFoundation\Response
;
13 use Symfony\Component\Routing\Annotation\Route
;
14 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
15 use Wallabag\CoreBundle\Entity\Tag
;
16 use Wallabag\UserBundle\Entity\User
;
18 class RssController
extends Controller
21 * Shows unread entries for current user.
23 * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
24 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
26 * @return \Symfony\Component\HttpFoundation\Response
28 public function showUnreadRSSAction(Request
$request, User
$user)
30 return $this->showEntries('unread', $user, $request->query
->get('page', 1));
34 * Shows read entries for current user.
36 * @Route("/{username}/{token}/archive.xml", name="archive_rss", defaults={"_format"="xml"})
37 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
39 * @return \Symfony\Component\HttpFoundation\Response
41 public function showArchiveRSSAction(Request
$request, User
$user)
43 return $this->showEntries('archive', $user, $request->query
->get('page', 1));
47 * Shows starred entries for current user.
49 * @Route("/{username}/{token}/starred.xml", name="starred_rss", defaults={"_format"="xml"})
50 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
52 * @return \Symfony\Component\HttpFoundation\Response
54 public function showStarredRSSAction(Request
$request, User
$user)
56 return $this->showEntries('starred', $user, $request->query
->get('page', 1));
60 * Shows all entries for current user.
62 * @Route("/{username}/{token}/all.xml", name="all_rss", defaults={"_format"="xml"})
63 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
65 * @return \Symfony\Component\HttpFoundation\Response
67 public function showAllRSSAction(Request
$request, User
$user)
69 return $this->showEntries('all', $user, $request->query
->get('page', 1));
73 * Shows entries associated to a tag for current user.
75 * @Route("/{username}/{token}/tags/{slug}.xml", name="tag_rss", defaults={"_format"="xml"})
76 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
77 * @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
79 * @return \Symfony\Component\HttpFoundation\Response
81 public function showTagsAction(Request
$request, User
$user, Tag
$tag)
83 $page = $request->query
->get('page', 1);
85 $url = $this->generateUrl(
88 'username' => $user->getUsername(),
89 'token' => $user->getConfig()->getRssToken(),
90 'slug' => $tag->getSlug(),
92 UrlGeneratorInterface
::ABSOLUTE_URL
95 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
100 $pagerAdapter = new ArrayAdapter($entriesByTag);
102 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare(
107 if (null === $entries) {
108 throw $this->createNotFoundException('No entries found?');
112 $entries->setCurrentPage($page);
113 } catch (OutOfRangeCurrentPageException
$e) {
115 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
119 return $this->render(
120 '@WallabagCore/themes/common/Entry/entries.xml.twig',
122 'url_html' => $this->generateUrl('tag_entries', ['slug' => $tag->getSlug()], UrlGeneratorInterface
::ABSOLUTE_URL
),
123 'type' => 'tag (' . $tag->getLabel() . ')',
125 'entries' => $entries,
127 new Response('', 200, ['Content-Type' => 'application/rss+xml'])
132 * Global method to retrieve entries depending on the given type
133 * It returns the response to be send.
135 * @param string $type Entries type: unread, starred or archive
139 * @return \Symfony\Component\HttpFoundation\Response
141 private function showEntries($type, User
$user, $page = 1)
143 $repository = $this->get('wallabag_core.entry_repository');
147 $qb = $repository->getBuilderForStarredByUser($user->getId());
150 $qb = $repository->getBuilderForArchiveByUser($user->getId());
153 $qb = $repository->getBuilderForUnreadByUser($user->getId());
156 $qb = $repository->getBuilderForAllByUser($user->getId());
159 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
162 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
163 $entries = new Pagerfanta($pagerAdapter);
165 $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit');
166 $entries->setMaxPerPage($perPage);
168 $url = $this->generateUrl(
171 'username' => $user->getUsername(),
172 'token' => $user->getConfig()->getRssToken(),
174 UrlGeneratorInterface
::ABSOLUTE_URL
178 $entries->setCurrentPage((int) $page);
179 } catch (OutOfRangeCurrentPageException
$e) {
181 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
185 return $this->render(
186 '@WallabagCore/themes/common/Entry/entries.xml.twig',
188 'url_html' => $this->generateUrl($type, [], UrlGeneratorInterface
::ABSOLUTE_URL
),
191 'entries' => $entries,
193 new Response('', 200, ['Content-Type' => 'application/rss+xml'])