]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/RssController.php
Update messages.pl.yml
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / RssController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
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;
17
18 class RssController extends Controller
19 {
20 /**
21 * Shows unread entries for current user.
22 *
23 * @Route("/{username}/{token}/unread.xml", name="unread_rss", defaults={"_format"="xml"})
24 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
25 *
26 * @return \Symfony\Component\HttpFoundation\Response
27 */
28 public function showUnreadRSSAction(Request $request, User $user)
29 {
30 return $this->showEntries('unread', $user, $request->query->get('page', 1));
31 }
32
33 /**
34 * Shows read entries for current user.
35 *
36 * @Route("/{username}/{token}/archive.xml", name="archive_rss", defaults={"_format"="xml"})
37 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
38 *
39 * @return \Symfony\Component\HttpFoundation\Response
40 */
41 public function showArchiveRSSAction(Request $request, User $user)
42 {
43 return $this->showEntries('archive', $user, $request->query->get('page', 1));
44 }
45
46 /**
47 * Shows starred entries for current user.
48 *
49 * @Route("/{username}/{token}/starred.xml", name="starred_rss", defaults={"_format"="xml"})
50 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
51 *
52 * @return \Symfony\Component\HttpFoundation\Response
53 */
54 public function showStarredRSSAction(Request $request, User $user)
55 {
56 return $this->showEntries('starred', $user, $request->query->get('page', 1));
57 }
58
59 /**
60 * Shows all entries for current user.
61 *
62 * @Route("/{username}/{token}/all.xml", name="all_rss", defaults={"_format"="xml"})
63 * @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
64 *
65 * @return \Symfony\Component\HttpFoundation\Response
66 */
67 public function showAllRSSAction(Request $request, User $user)
68 {
69 return $this->showEntries('all', $user, $request->query->get('page', 1));
70 }
71
72 /**
73 * Shows entries associated to a tag for current user.
74 *
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"}})
78 *
79 * @return \Symfony\Component\HttpFoundation\Response
80 */
81 public function showTagsAction(Request $request, User $user, Tag $tag)
82 {
83 $page = $request->query->get('page', 1);
84
85 $url = $this->generateUrl(
86 'tag_rss',
87 [
88 'username' => $user->getUsername(),
89 'token' => $user->getConfig()->getRssToken(),
90 'slug' => $tag->getSlug(),
91 ],
92 UrlGeneratorInterface::ABSOLUTE_URL
93 );
94
95 $entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
96 $user->getId(),
97 $tag->getId()
98 );
99
100 $pagerAdapter = new ArrayAdapter($entriesByTag);
101
102 $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare(
103 $pagerAdapter,
104 $user
105 );
106
107 if (null === $entries) {
108 throw $this->createNotFoundException('No entries found?');
109 }
110
111 try {
112 $entries->setCurrentPage($page);
113 } catch (OutOfRangeCurrentPageException $e) {
114 if ($page > 1) {
115 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
116 }
117 }
118
119 return $this->render(
120 '@WallabagCore/themes/common/Entry/entries.xml.twig',
121 [
122 'url_html' => $this->generateUrl('tag_entries', ['slug' => $tag->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL),
123 'type' => 'tag (' . $tag->getLabel() . ')',
124 'url' => $url,
125 'entries' => $entries,
126 ],
127 new Response('', 200, ['Content-Type' => 'application/rss+xml'])
128 );
129 }
130
131 /**
132 * Global method to retrieve entries depending on the given type
133 * It returns the response to be send.
134 *
135 * @param string $type Entries type: unread, starred or archive
136 * @param User $user
137 * @param int $page
138 *
139 * @return \Symfony\Component\HttpFoundation\Response
140 */
141 private function showEntries($type, User $user, $page = 1)
142 {
143 $repository = $this->get('wallabag_core.entry_repository');
144
145 switch ($type) {
146 case 'starred':
147 $qb = $repository->getBuilderForStarredByUser($user->getId());
148 break;
149 case 'archive':
150 $qb = $repository->getBuilderForArchiveByUser($user->getId());
151 break;
152 case 'unread':
153 $qb = $repository->getBuilderForUnreadByUser($user->getId());
154 break;
155 case 'all':
156 $qb = $repository->getBuilderForAllByUser($user->getId());
157 break;
158 default:
159 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
160 }
161
162 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
163 $entries = new Pagerfanta($pagerAdapter);
164
165 $perPage = $user->getConfig()->getRssLimit() ?: $this->getParameter('wallabag_core.rss_limit');
166 $entries->setMaxPerPage($perPage);
167
168 $url = $this->generateUrl(
169 $type . '_rss',
170 [
171 'username' => $user->getUsername(),
172 'token' => $user->getConfig()->getRssToken(),
173 ],
174 UrlGeneratorInterface::ABSOLUTE_URL
175 );
176
177 try {
178 $entries->setCurrentPage((int) $page);
179 } catch (OutOfRangeCurrentPageException $e) {
180 if ($page > 1) {
181 return $this->redirect($url . '?page=' . $entries->getNbPages(), 302);
182 }
183 }
184
185 return $this->render(
186 '@WallabagCore/themes/common/Entry/entries.xml.twig',
187 [
188 'url_html' => $this->generateUrl($type, [], UrlGeneratorInterface::ABSOLUTE_URL),
189 'type' => $type,
190 'url' => $url,
191 'entries' => $entries,
192 ],
193 new Response('', 200, ['Content-Type' => 'application/rss+xml'])
194 );
195 }
196 }