mirror of
https://github.com/wallabag/wallabag.git
synced 2025-03-30 06:45:29 +00:00
add json & xml
This commit is contained in:
parent
33c36f6b48
commit
b3cc1a14e7
4 changed files with 102 additions and 11 deletions
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -99,13 +99,13 @@
|
|||
{% 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>
|
||||
|
||||
|
|
|
@ -106,10 +106,12 @@
|
|||
</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>
|
||||
|
|
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue