1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wallabag\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Pagerfanta\Adapter\ArrayAdapter; |
6
|
|
|
use Pagerfanta\Doctrine\ORM\QueryAdapter as 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\HttpKernel\Exception\BadRequestHttpException; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
16
|
|
|
use Wallabag\CoreBundle\Entity\Tag; |
17
|
|
|
use Wallabag\UserBundle\Entity\User; |
18
|
|
|
|
19
|
|
|
class FeedController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Shows unread entries for current user. |
23
|
|
|
* |
24
|
|
|
* @Route("/feed/{username}/{token}/unread/{page}", name="unread_feed", defaults={"page"=1, "_format"="xml"}) |
25
|
|
|
* |
26
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter") |
27
|
|
|
* |
28
|
|
|
* @param $page |
29
|
|
|
* |
30
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
31
|
|
|
*/ |
32
|
|
|
public function showUnreadFeedAction(User $user, $page) |
33
|
|
|
{ |
34
|
|
|
return $this->showEntries('unread', $user, $page); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Shows read entries for current user. |
39
|
|
|
* |
40
|
|
|
* @Route("/feed/{username}/{token}/archive/{page}", name="archive_feed", defaults={"page"=1, "_format"="xml"}) |
41
|
|
|
* |
42
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter") |
43
|
|
|
* |
44
|
|
|
* @param $page |
45
|
|
|
* |
46
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
47
|
|
|
*/ |
48
|
|
|
public function showArchiveFeedAction(User $user, $page) |
49
|
|
|
{ |
50
|
|
|
return $this->showEntries('archive', $user, $page); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Shows starred entries for current user. |
55
|
|
|
* |
56
|
|
|
* @Route("/feed/{username}/{token}/starred/{page}", name="starred_feed", defaults={"page"=1, "_format"="xml"}) |
57
|
|
|
* |
58
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter") |
59
|
|
|
* |
60
|
|
|
* @param $page |
61
|
|
|
* |
62
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
63
|
|
|
*/ |
64
|
|
|
public function showStarredFeedAction(User $user, $page) |
65
|
|
|
{ |
66
|
|
|
return $this->showEntries('starred', $user, $page); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Shows all entries for current user. |
71
|
|
|
* |
72
|
|
|
* @Route("/feed/{username}/{token}/all/{page}", name="all_feed", defaults={"page"=1, "_format"="xml"}) |
73
|
|
|
* |
74
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter") |
75
|
|
|
* |
76
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
77
|
|
|
*/ |
78
|
|
|
public function showAllFeedAction(User $user, $page) |
79
|
|
|
{ |
80
|
|
|
return $this->showEntries('all', $user, $page); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Shows entries associated to a tag for current user. |
85
|
|
|
* |
86
|
|
|
* @Route("/feed/{username}/{token}/tags/{slug}/{page}", name="tag_feed", defaults={"page"=1, "_format"="xml"}) |
87
|
|
|
* |
88
|
|
|
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_feed_token_converter") |
89
|
|
|
* @ParamConverter("tag", options={"mapping": {"slug": "slug"}}) |
90
|
|
|
* |
91
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
92
|
|
|
*/ |
93
|
|
|
public function showTagsFeedAction(Request $request, User $user, Tag $tag, $page) |
94
|
|
|
{ |
95
|
|
|
$sort = $request->query->get('sort', 'created'); |
96
|
|
|
|
97
|
|
|
$sorts = [ |
98
|
|
|
'created' => 'createdAt', |
99
|
|
|
'updated' => 'updatedAt', |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
if (!isset($sorts[$sort])) { |
103
|
|
|
throw new BadRequestHttpException(sprintf('Sort "%s" is not available.', $sort)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$url = $this->generateUrl( |
107
|
|
|
'tag_feed', |
108
|
|
|
[ |
109
|
|
|
'username' => $user->getUsername(), |
110
|
|
|
'token' => $user->getConfig()->getFeedToken(), |
111
|
|
|
'slug' => $tag->getSlug(), |
112
|
|
|
], |
113
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId( |
117
|
|
|
$user->getId(), |
118
|
|
|
$tag->getId(), |
119
|
|
|
$sorts[$sort] |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$pagerAdapter = new ArrayAdapter($entriesByTag); |
123
|
|
|
|
124
|
|
|
$entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare( |
125
|
|
|
$pagerAdapter, |
126
|
|
|
$user |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$perPage = $user->getConfig()->getFeedLimit() ?: $this->getParameter('wallabag_core.feed_limit'); |
130
|
|
|
$entries->setMaxPerPage($perPage); |
131
|
|
|
|
132
|
|
|
if (null === $entries) { |
133
|
|
|
throw $this->createNotFoundException('No entries found?'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
$entries->setCurrentPage($page); |
138
|
|
|
} catch (OutOfRangeCurrentPageException $e) { |
139
|
|
|
if ($page > 1) { |
140
|
|
|
return $this->redirect($url . '?page=' . $entries->getNbPages(), 302); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->render( |
145
|
|
|
'@WallabagCore/themes/common/Entry/entries.xml.twig', |
146
|
|
|
[ |
147
|
|
|
'type' => 'tag', |
148
|
|
|
'url' => $url, |
149
|
|
|
'entries' => $entries, |
150
|
|
|
'user' => $user->getUsername(), |
151
|
|
|
'domainName' => $this->getParameter('domain_name'), |
152
|
|
|
'version' => $this->getParameter('wallabag_core.version'), |
153
|
|
|
'tag' => $tag->getSlug(), |
154
|
|
|
'updated' => $this->prepareFeedUpdatedDate($entries, $sort), |
155
|
|
|
], |
156
|
|
|
new Response('', 200, ['Content-Type' => 'application/atom+xml']) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function prepareFeedUpdatedDate(Pagerfanta $entries, $sort = 'created') |
161
|
|
|
{ |
162
|
|
|
$currentPageResults = $entries->getCurrentPageResults(); |
163
|
|
|
|
164
|
|
|
if (isset($currentPageResults[0])) { |
165
|
|
|
$firstEntry = $currentPageResults[0]; |
166
|
|
|
if ('created' === $sort) { |
167
|
|
|
return $firstEntry->getCreatedAt(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $firstEntry->getUpdatedAt(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Global method to retrieve entries depending on the given type |
178
|
|
|
* It returns the response to be send. |
179
|
|
|
* |
180
|
|
|
* @param string $type Entries type: unread, starred or archive |
181
|
|
|
* @param int $page |
182
|
|
|
* |
183
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
184
|
|
|
*/ |
185
|
|
|
private function showEntries($type, User $user, $page = 1) |
186
|
|
|
{ |
187
|
|
|
$repository = $this->get('wallabag_core.entry_repository'); |
188
|
|
|
|
189
|
|
|
switch ($type) { |
190
|
|
|
case 'starred': |
191
|
|
|
$qb = $repository->getBuilderForStarredByUser($user->getId()); |
192
|
|
|
break; |
193
|
|
|
case 'archive': |
194
|
|
|
$qb = $repository->getBuilderForArchiveByUser($user->getId()); |
195
|
|
|
break; |
196
|
|
|
case 'unread': |
197
|
|
|
$qb = $repository->getBuilderForUnreadByUser($user->getId()); |
198
|
|
|
break; |
199
|
|
|
case 'all': |
200
|
|
|
$qb = $repository->getBuilderForAllByUser($user->getId()); |
201
|
|
|
break; |
202
|
|
|
default: |
203
|
|
|
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); |
207
|
|
|
$entries = new Pagerfanta($pagerAdapter); |
208
|
|
|
|
209
|
|
|
$perPage = $user->getConfig()->getFeedLimit() ?: $this->getParameter('wallabag_core.feed_limit'); |
210
|
|
|
$entries->setMaxPerPage($perPage); |
211
|
|
|
|
212
|
|
|
$url = $this->generateUrl( |
213
|
|
|
$type . '_feed', |
214
|
|
|
[ |
215
|
|
|
'username' => $user->getUsername(), |
216
|
|
|
'token' => $user->getConfig()->getFeedToken(), |
217
|
|
|
], |
218
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
try { |
222
|
|
|
$entries->setCurrentPage((int) $page); |
223
|
|
|
} catch (OutOfRangeCurrentPageException $e) { |
224
|
|
|
if ($page > 1) { |
225
|
|
|
return $this->redirect($url . '/' . $entries->getNbPages()); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [ |
230
|
|
|
'type' => $type, |
231
|
|
|
'url' => $url, |
232
|
|
|
'entries' => $entries, |
233
|
|
|
'user' => $user->getUsername(), |
234
|
|
|
'domainName' => $this->getParameter('domain_name'), |
235
|
|
|
'version' => $this->getParameter('wallabag_core.version'), |
236
|
|
|
'updated' => $this->prepareFeedUpdatedDate($entries), |
237
|
|
|
], |
238
|
|
|
new Response('', 200, ['Content-Type' => 'application/atom+xml']) |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|