Add RSS tags feeds

This commit is contained in:
Jeremy Benoist 2017-06-10 13:11:08 +02:00
parent 25203e5081
commit 18c38dffc6
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
5 changed files with 117 additions and 20 deletions

View file

@ -61,6 +61,7 @@ security:
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /(unread|starred|archive).xml$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /tags/(.*).xml$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/share, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/settings, roles: ROLE_SUPER_ADMIN }
- { path: ^/annotations, roles: ROLE_USER }

View file

@ -3,13 +3,16 @@
namespace Wallabag\CoreBundle\Controller;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Pagerfanta\Pagerfanta;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\UserBundle\Entity\User;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@ -31,7 +34,7 @@ class RssController extends Controller
/**
* Shows read entries for current user.
*
* @Route("/{username}/{token}/archive.xml", name="archive_rss")
* @Route("/{username}/{token}/archive.xml", name="archive_rss", defaults={"_format"="xml"})
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
*
* @return \Symfony\Component\HttpFoundation\Response
@ -44,7 +47,7 @@ class RssController extends Controller
/**
* Shows starred entries for current user.
*
* @Route("/{username}/{token}/starred.xml", name="starred_rss")
* @Route("/{username}/{token}/starred.xml", name="starred_rss", defaults={"_format"="xml"})
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
*
* @return \Symfony\Component\HttpFoundation\Response
@ -54,6 +57,65 @@ class RssController extends Controller
return $this->showEntries('starred', $user, $request->query->get('page', 1));
}
/**
* Shows entries associated to a tag for current user.
*
* @Route("/{username}/{token}/tags/{slug}.xml", name="tag_rss", defaults={"_format"="xml"})
* @ParamConverter("user", class="WallabagUserBundle:User", converter="username_rsstoken_converter")
* @ParamConverter("tag", options={"mapping": {"slug": "slug"}})
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function showTagsAction(Request $request, User $user, Tag $tag)
{
$page = $request->query->get('page', 1);
$url = $this->generateUrl(
'tag_rss',
[
'username' => $user->getUsername(),
'token' => $user->getConfig()->getRssToken(),
'slug' => $tag->getSlug(),
],
UrlGeneratorInterface::ABSOLUTE_URL
);
$entriesByTag = $this->get('wallabag_core.entry_repository')->findAllByTagId(
$user->getId(),
$tag->getId()
);
$pagerAdapter = new ArrayAdapter($entriesByTag);
$entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare(
$pagerAdapter,
$user
);
if (null === $entries) {
throw $this->createNotFoundException('No entries found?');
}
try {
$entries->setCurrentPage($page);
} catch (OutOfRangeCurrentPageException $e) {
if ($page > 1) {
return $this->redirect($url.'?page='.$entries->getNbPages(), 302);
}
}
return $this->render(
'@WallabagCore/themes/common/Entry/entries.xml.twig',
[
'url_html' => $this->generateUrl('tag_entries', ['slug' => $tag->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL),
'type' => 'tag ('.$tag->getLabel().')',
'url' => $url,
'entries' => $entries,
],
new Response('', 200, ['Content-Type' => 'application/rss+xml'])
);
}
/**
* Global method to retrieve entries depending on the given type
* It returns the response to be send.
@ -108,10 +170,15 @@ class RssController extends Controller
}
}
return $this->render('@WallabagCore/themes/common/Entry/entries.xml.twig', [
'type' => $type,
'url' => $url,
'entries' => $entries,
]);
return $this->render(
'@WallabagCore/themes/common/Entry/entries.xml.twig',
[
'url_html' => $this->generateUrl($type, [], UrlGeneratorInterface::ABSOLUTE_URL),
'type' => $type,
'url' => $url,
'entries' => $entries,
],
new Response('', 200, ['Content-Type' => 'application/rss+xml'])
);
}
}

View file

@ -4,6 +4,7 @@ namespace Wallabag\CoreBundle\Helper;
use Pagerfanta\Adapter\AdapterInterface;
use Pagerfanta\Pagerfanta;
use Wallabag\UserBundle\Entity\User;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
@ -20,12 +21,15 @@ class PreparePagerForEntries
/**
* @param AdapterInterface $adapter
* @param User $user If user isn't logged in, we can force it (like for rss)
*
* @return null|Pagerfanta
*/
public function prepare(AdapterInterface $adapter)
public function prepare(AdapterInterface $adapter, User $user = null)
{
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
if (null === $user) {
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
}
if (null === $user || !is_object($user)) {
return;

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>wallabag {{type}} feed</title>
<link>{{ url(type) }}</link>
<title>wallabag - {{ type }} feed</title>
<link>{{ url_html }}</link>
<link rel="self" href="{{ app.request.uri }}"/>
{% if entries.hasPreviousPage -%}
<link rel="previous" href="{{ url }}?page={{ entries.previousPage }}"/>
@ -13,7 +13,7 @@
<link rel="last" href="{{ url }}?page={{ entries.nbPages }}"/>
<pubDate>{{ "now"|date('D, d M Y H:i:s') }}</pubDate>
<generator>wallabag</generator>
<description>wallabag {{type}} elements</description>
<description>wallabag {{ type }} elements</description>
{% for entry in entries %}

View file

@ -6,7 +6,7 @@ use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
class RssControllerTest extends WallabagCoreTestCase
{
public function validateDom($xml, $type, $nb = null)
public function validateDom($xml, $type, $urlPagination, $nb = null)
{
$doc = new \DOMDocument();
$doc->loadXML($xml);
@ -34,10 +34,10 @@ class RssControllerTest extends WallabagCoreTestCase
$this->assertEquals('wallabag '.$type.' elements', $xpath->query('/rss/channel/description')->item(0)->nodeValue);
$this->assertEquals(1, $xpath->query('/rss/channel/link[@rel="self"]')->length);
$this->assertContains($type.'.xml', $xpath->query('/rss/channel/link[@rel="self"]')->item(0)->getAttribute('href'));
$this->assertContains($urlPagination.'.xml', $xpath->query('/rss/channel/link[@rel="self"]')->item(0)->getAttribute('href'));
$this->assertEquals(1, $xpath->query('/rss/channel/link[@rel="last"]')->length);
$this->assertContains($type.'.xml?page=', $xpath->query('/rss/channel/link[@rel="last"]')->item(0)->getAttribute('href'));
$this->assertContains($urlPagination.'.xml?page=', $xpath->query('/rss/channel/link[@rel="last"]')->item(0)->getAttribute('href'));
foreach ($xpath->query('//item') as $item) {
$this->assertEquals(1, $xpath->query('title', $item)->length);
@ -94,7 +94,7 @@ class RssControllerTest extends WallabagCoreTestCase
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->validateDom($client->getResponse()->getContent(), 'unread', 2);
$this->validateDom($client->getResponse()->getContent(), 'unread', 'unread', 2);
}
public function testStarred()
@ -116,7 +116,7 @@ class RssControllerTest extends WallabagCoreTestCase
$this->assertEquals(200, $client->getResponse()->getStatusCode(), 1);
$this->validateDom($client->getResponse()->getContent(), 'starred');
$this->validateDom($client->getResponse()->getContent(), 'starred', 'starred');
}
public function testArchives()
@ -138,7 +138,7 @@ class RssControllerTest extends WallabagCoreTestCase
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->validateDom($client->getResponse()->getContent(), 'archive');
$this->validateDom($client->getResponse()->getContent(), 'archive', 'archive');
}
public function testPagination()
@ -159,13 +159,38 @@ class RssControllerTest extends WallabagCoreTestCase
$client->request('GET', '/admin/SUPERTOKEN/unread.xml');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->validateDom($client->getResponse()->getContent(), 'unread');
$this->validateDom($client->getResponse()->getContent(), 'unread', 'unread');
$client->request('GET', '/admin/SUPERTOKEN/unread.xml?page=2');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->validateDom($client->getResponse()->getContent(), 'unread');
$this->validateDom($client->getResponse()->getContent(), 'unread', 'unread');
$client->request('GET', '/admin/SUPERTOKEN/unread.xml?page=3000');
$this->assertEquals(302, $client->getResponse()->getStatusCode());
}
public function testTags()
{
$client = $this->getClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $em
->getRepository('WallabagUserBundle:User')
->findOneByUsername('admin');
$config = $user->getConfig();
$config->setRssToken('SUPERTOKEN');
$config->setRssLimit(null);
$em->persist($config);
$em->flush();
$client = $this->getClient();
$client->request('GET', '/admin/SUPERTOKEN/tags/foo-bar.xml');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->validateDom($client->getResponse()->getContent(), 'tag (foo bar)', 'tags/foo-bar');
$client->request('GET', '/admin/SUPERTOKEN/tags/foo-bar.xml?page=3000');
$this->assertEquals(302, $client->getResponse()->getStatusCode());
}
}