]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
add json & xml
authorThomas Citharel <tcit@tcit.fr>
Sun, 18 Oct 2015 13:49:00 +0000 (15:49 +0200)
committerNicolas LÅ“uillet <nicolas.loeuillet@smile.fr>
Mon, 9 Nov 2015 15:32:48 +0000 (16:32 +0100)
src/Wallabag/CoreBundle/Helper/EntriesExport.php
src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig
src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig
src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php

index 806319b1a28e24778f21d5640930ead4565b241e..33ff6311b571dbccd55abcc7cc1073ca6a4c553b 100644 (file)
@@ -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.
      *
index 5a231c865f3b44c8cc4cb374906dc23b8dc355b8..bf38bff8245e839d53c6cbb71fdd229e58836501 100644 (file)
     {% endif %}
         <h4 class="center">{% trans %}Export{% endtrans %}</h4>
         <ul>
-            <li class="bold"><a class="waves-effect" href="{{ path('ebook', { 'category': currentRoute, 'format': 'epub' }) }}">{% trans %}EPUB{% endtrans %}</a></li>
-            <li class="bold"><a class="waves-effect" href="{{ path('ebook', { 'category': currentRoute, 'format': 'mobi' }) }}">{% trans %}MOBI{% endtrans %}</a></li>
-            <li class="bold"><a class="waves-effect" href="{{ path('ebook', { 'category': currentRoute, 'format': 'pdf' }) }}">{% trans %}PDF{% endtrans %}</a></li>
-            <li class="bold"><del><a class="waves-effect" href="{{ path('ebook', { 'category': currentRoute, 'format': 'xml' }) }}">{% trans %}XML{% endtrans %}</a></del></li>
-            <li class="bold"><del><a class="waves-effect" href="{{ path('ebook', { 'category': currentRoute, 'format': 'json' }) }}">{% trans %}JSON{% endtrans %}</a></del></li>
-            <li class="bold"><a class="waves-effect" href="{{ path('ebook', { 'category': currentRoute, 'format': 'csv' }) }}">{% trans %}CSV{% endtrans %}</a></li>
-            <li class="bold"><del><a class="waves-effect" href="{{ path('ebook', { 'category': currentRoute, 'format': 'txt' }) }}">{% trans %}TXT{% endtrans %}</a></del></li>
+            <li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'epub' }) }}">{% trans %}EPUB{% endtrans %}</a></li>
+            <li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'mobi' }) }}">{% trans %}MOBI{% endtrans %}</a></li>
+            <li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'pdf' }) }}">{% trans %}PDF{% endtrans %}</a></li>
+            <li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'xml' }) }}">{% trans %}XML{% endtrans %}</a></li>
+            <li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'json' }) }}">{% trans %}JSON{% endtrans %}</a></li>
+            <li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'csv' }) }}">{% trans %}CSV{% endtrans %}</a></li>
+            <li class="bold"><del><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'txt' }) }}">{% trans %}TXT{% endtrans %}</a></del></li>
         </ul>
     </div>
 
index bece099a595a02409ebf5269ff43120606f4e7d9..fd84d984edb970d82b206c7f83d94ac633be1397 100644 (file)
             </a>
             <div class="collapsible-body">
                 <ul>
-                    {% if export_epub %}<li><a href="{{ path('ebook_entry', { 'id': entry.id, 'format': 'epub' }) }}" title="Generate ePub file">EPUB</a></li>{% endif %}
-                    {% if export_mobi %}<li><a href="{{ path('ebook_entry', { 'id': entry.id, 'format': 'mobi' }) }}" title="Generate Mobi file">MOBI</a></li>{% endif %}
-                    {% if export_pdf %}<li><a href="{{ path('ebook_entry', { 'id': entry.id, 'format': 'pdf' }) }}" title="Generate PDF file">PDF</a></li>{% endif %}
-                    <li><a href="{{ path('ebook_entry', { 'id': entry.id, 'format': 'csv' }) }}" title="Generate CSV file">CSV</a></li>
+                    {% if export_epub %}<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'epub' }) }}" title="Generate ePub file">EPUB</a></li>{% endif %}
+                    {% if export_mobi %}<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'mobi' }) }}" title="Generate Mobi file">MOBI</a></li>{% endif %}
+                    {% if export_pdf %}<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'pdf' }) }}" title="Generate PDF file">PDF</a></li>{% endif %}
+                    <li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'csv' }) }}" title="Generate CSV file">CSV</a></li>
+                    <li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'json' }) }}" title="Generate JSON file">JSON</a></li>
+                    <li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'xml' }) }}" title="Generate XML file">XML</a></li>
                 </ul>
             </div>
         </li>
index 3f749aaeec08138db145c50f0ecf7d7811feafec..febdf4d4ca2db38a84f4aae8e73f8cb104435706 100644 (file)
@@ -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'));
+    }
 }