From b3cc1a14e7b9939fdaf7e71fac40ed7c42727854 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 18 Oct 2015 15:49:00 +0200 Subject: add json & xml --- src/Wallabag/CoreBundle/Helper/EntriesExport.php | 55 ++++++++++++++++++++++ .../views/themes/material/Entry/entries.html.twig | 14 +++--- .../views/themes/material/Entry/entry.html.twig | 10 ++-- .../Tests/Controller/ExportControllerTest.php | 34 +++++++++++++ 4 files changed, 102 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index 806319b1..33ff6311 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -5,6 +5,10 @@ namespace Wallabag\CoreBundle\Helper; use PHPePub\Core\EPub; use PHPePub\Core\Structure\OPF\DublinCore; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Serializer\Serializer; +use Symfony\Component\Serializer\Encoder\XmlEncoder; +use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; class EntriesExport { @@ -86,6 +90,12 @@ class EntriesExport case 'csv': return $this->produceCSV(); + + case 'json': + return $this->produceJSON(); + + case 'xml': + return $this->produceXML(); } throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); @@ -319,6 +329,51 @@ class EntriesExport )->send(); } + private function produceJSON() + { + $serializer = $this->prepareSerializingContent(); + $jsonContent = $serializer->serialize($this->entries, 'json'); + + return Response::create( + $jsonContent, + 200, + array( + 'Content-type' => 'application/json', + 'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"', + 'Content-Transfer-Encoding' => 'UTF-8', + ) + )->send(); + } + + private function produceXML() + { + $serializer = $this->prepareSerializingContent(); + $xmlContent = $serializer->serialize($this->entries, 'xml'); + + return Response::create( + $xmlContent, + 200, + array( + 'Content-type' => 'application/xml', + 'Content-Disposition' => 'attachment; filename="'.$this->title.'.xml"', + 'Content-Transfer-Encoding' => 'UTF-8', + ) + )->send(); + } + /** + * Return a Serializer object for producing processes that need it (JSON & XML). + * + * @return Serializer + */ + private function prepareSerializingContent() + { + $encoders = array(new XmlEncoder(), new JsonEncoder()); + $normalizers = array(new ObjectNormalizer()); + $normalizers[0]->setIgnoredAttributes(array('user', 'createdAt', 'updatedAt')); + + return new Serializer($normalizers, $encoders); + } + /** * Return a kind of footer / information for the epub. * diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig index 5a231c86..bf38bff8 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig @@ -99,13 +99,13 @@ {% endif %}

{% trans %}Export{% endtrans %}

diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig index bece099a..fd84d984 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig @@ -106,10 +106,12 @@
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php index 3f749aae..febdf4d4 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php @@ -113,4 +113,38 @@ class ExportControllerTest extends WallabagCoreTestCase $this->assertGreaterThan(1, $csv); $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]); } + + public function testJsonExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + ob_start(); + $crawler = $client->request('GET', '/export/all.json'); + ob_end_clean(); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $headers = $client->getResponse()->headers; + $this->assertEquals('application/json', $headers->get('content-type')); + $this->assertEquals('attachment; filename="All articles.json"', $headers->get('content-disposition')); + $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); + } + + public function testXmlExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + ob_start(); + $crawler = $client->request('GET', '/export/unread.xml'); + ob_end_clean(); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $headers = $client->getResponse()->headers; + $this->assertEquals('application/xml', $headers->get('content-type')); + $this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition')); + $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); + } } -- cgit v1.2.3