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