From 03690d138792dde6405e3d2eb3c53f6572eb3c43 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 15 Oct 2015 20:06:59 +0200 Subject: Start work on export For now: - ebook - mobi - pdf - csv --- .../CoreBundle/Controller/ExportController.php | 65 +++++ src/Wallabag/CoreBundle/Helper/EntriesExport.php | 263 +++++++++++++++++++++ .../views/themes/material/Entry/entries.html.twig | 18 ++ .../views/themes/material/Entry/entry.html.twig | 9 +- .../views/themes/material/layout.html.twig | 1 + .../views/themes/material/public/js/init.js | 9 + 6 files changed, 361 insertions(+), 4 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Controller/ExportController.php create mode 100644 src/Wallabag/CoreBundle/Helper/EntriesExport.php (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Controller/ExportController.php b/src/Wallabag/CoreBundle/Controller/ExportController.php new file mode 100644 index 00000000..123e491a --- /dev/null +++ b/src/Wallabag/CoreBundle/Controller/ExportController.php @@ -0,0 +1,65 @@ +getDoctrine()->getRepository('WallabagCoreBundle:Entry'); + switch ($category) { + case 'all': + $method = 'All'; + break; + + case 'unread': + $method = 'Unread'; + break; + + case 'starred': + $method = 'Starred'; + break; + + case 'archive': + $method = 'Archive'; + break; + + default: + break; + } + + $methodBuilder = 'getBuilderFor'.$method.'ByUser'; + $qb = $repository->$methodBuilder($this->getUser()->getId()); + $entries = $qb->getQuery()->getResult(); + + $export = new EntriesExport($entries); + $export->setMethod($method); + $export->exportAs($format); + } + + /** + * Gets one entry content. + * + * @param Entry $entry + * + * @Route("/export/id/{id}.{format}", requirements={"id" = "\d+"}, name="ebook_entry") + */ + public function getEntryAction(Entry $entry, $format) + { + $export = new EntriesExport(array($entry)); + $export->setMethod('entry'); + $export->exportAs($format); + } +} diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php new file mode 100644 index 00000000..fad0bb97 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -0,0 +1,263 @@ +entries = $entries; + + foreach ($entries as $entry) { + $this->tags[] = $entry->getTags(); + } + if (count($entries) === 1) { + $this->language = $entries[0]->getLanguage(); + } + } + + /** + * Sets the category of which we want to get articles, or just one entry. + * + * @param string $method Method to get articles + */ + public function setMethod($method) + { + $this->method = $method; + + switch ($this->method) { + case 'All': + $this->title = 'All Articles'; + break; + case 'Unread': + $this->title = 'Unread articles'; + break; + case 'Starred': + $this->title = 'Starred articles'; + break; + case 'Archive': + $this->title = 'Archived articles'; + break; + case 'entry': + $this->title = $this->entries[0]->getTitle(); + break; + default: + break; + } + } + + /** + * Sets the output format. + * + * @param string $format + */ + public function exportAs($format) + { + $this->format = $format; + + switch ($this->format) { + case 'epub': + $this->produceEpub(); + break; + + case 'mobi': + $this->produceMobi(); + break; + + case 'pdf': + $this->producePDF(); + break; + + case 'csv': + $this->produceCSV(); + break; + + default: + break; + } + } + + private function produceEpub() + { + /* + * Start and End of the book + */ + $content_start = + "\n" + ."\n" + .'' + ."\n" + .''._('wallabag articles book')."\n" + ."\n" + ."\n"; + + $bookEnd = "\n\n"; + + $book = new EPub(EPub::BOOK_VERSION_EPUB3); + + /* + * Book metadata + */ + + $book->setTitle($this->title); + $book->setIdentifier($this->title, EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID. + $book->setLanguage($this->language); // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc. + $book->setDescription(_('Some articles saved on my wallabag')); + + foreach ($this->authors as $author) { + $book->setAuthor($author, $author); + } + + $book->setPublisher('wallabag', 'wallabag'); // I hope this is a non existant address :) + $book->setDate(time()); // Strictly not needed as the book date defaults to time(). + $book->setSourceURL("http://$_SERVER[HTTP_HOST]"); + + $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP'); + $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag'); + + /* + * Front page + */ + + $book->setCoverImage('Cover.png', file_get_contents('themes/_global/img/appicon/apple-touch-icon-152.png'), 'image/png'); + + $cover = $content_start.'

'._('Produced by wallabag with PHPePub').'

'._('Please open an issue if you have trouble with the display of this E-Book on your device.').'

'.$bookEnd; + + $book->addChapter('Notices', 'Cover2.html', $cover); + + $book->buildTOC(); + + /* + * Adding actual entries + */ + + foreach ($this->entries as $entry) { //set tags as subjects + foreach ($this->tags as $tag) { + $book->setSubject($tag['value']); + } + + $chapter = $content_start.$entry->getContent().$bookEnd; + $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD); + } + $book->finalize(); + $book->sendBook($this->title); + } + + private function produceMobi() + { + $mobi = new \MOBI(); + $content = new \MOBIFile(); + + /* + * Book metadata + */ + + $content->set('title', $this->title); + $content->set('author', implode($this->authors)); + $content->set('subject', $this->title); + + /* + * Front page + */ + + $content->appendParagraph('

'._('Produced by wallabag with PHPMobi').'

'._('Please open an issue if you have trouble with the display of this E-Book on your device.').'

'); + $content->appendImage(imagecreatefrompng('themes/_global/img/appicon/apple-touch-icon-152.png')); + $content->appendPageBreak(); + + /* + * Adding actual entries + */ + + foreach ($this->entries as $entry) { + $content->appendChapterTitle($entry->getTitle()); + $content->appendParagraph($entry->getContent()); + $content->appendPageBreak(); + } + $mobi->setContentProvider($content); + + // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9 + $this->title = preg_replace('/[^A-Za-z0-9\-]/', '', $this->title); + + // we offer file to download + $mobi->download($this->title.'.mobi'); + } + + private function producePDF() + { + $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); + + /* + * Book metadata + */ + + $pdf->SetCreator(PDF_CREATOR); + $pdf->SetAuthor('wallabag'); + $pdf->SetTitle($this->title); + $pdf->SetSubject('Articles via wallabag'); + $pdf->SetKeywords('wallabag'); + + /* + * Front page + */ + + $pdf->AddPage(); + $intro = '

'.$this->title.'

+

'._('Produced by wallabag with tcpdf').'

+

'._('Please open an issue if you have trouble with the display of this E-Book on your device.').'

+
'; + + $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true); + + /* + * Adding actual entries + */ + + foreach ($this->entries as $entry) { + foreach ($this->tags as $tag) { + $pdf->SetKeywords($tag['value']); + } + + $pdf->AddPage(); + $html = '

'.$entry->getTitle().'

'; + $html .= $entry->getContent(); + $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); + } + + // set image scale factor + $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); + + $pdf->Output($this->title.'.pdf', 'D'); + } + + private function produceCSV() + { + header('Content-type: application/csv'); + header('Content-Disposition: attachment; filename="'.$this->title.'.csv"'); + header('Content-Transfer-Encoding: UTF-8'); + + $output = fopen('php://output', 'a'); + + fputcsv($output, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language')); + foreach ($this->entries as $entry) { + fputcsv($output, array($entry->getTitle(), + $entry->getURL(), + $entry->getContent(), + implode(', ', $entry->getTags()->toArray()), + $entry->getMimetype(), + $entry->getLanguage(), )); + } + fclose($output); + exit(); + } +} 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 668824bc..5a231c86 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 @@ -91,6 +91,24 @@ {% endfor %} + +
+ {% set currentRoute = app.request.attributes.get('_route') %} + {% if currentRoute == 'homepage' %} + {% set currentRoute = 'unread' %} + {% 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 7230506c..bece099a 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 @@ -102,13 +102,14 @@
  • - {% trans %}Download{% endtrans %} + {% trans %}Download{% endtrans %}
      - {% if export_epub %}
    • EPUB
    • {% endif %} - {% if export_mobi %}
    • MOBI
    • {% endif %} - {% if export_pdf %}
    • PDF
    • {% endif %} + {% if export_epub %}
    • EPUB
    • {% endif %} + {% if export_mobi %}
    • MOBI
    • {% endif %} + {% if export_pdf %}
    • PDF
    • {% endif %} +
    • CSV
  • diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig index 95b3977c..f426e25b 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig @@ -59,6 +59,7 @@
  • +
  • diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/public/js/init.js b/src/Wallabag/CoreBundle/Resources/views/themes/material/public/js/init.js index edfdee82..491a7916 100755 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/public/js/init.js +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/public/js/init.js @@ -11,6 +11,14 @@ function init_filters() { } } +function init_export() { + // no display if export not aviable + if ($("div").is("#export")) { + $('#button_export').show(); + $('.button-collapse-right').sideNav({ edge: 'right' }); + } +} + $(document).ready(function(){ // sideNav $('.button-collapse').sideNav(); @@ -26,6 +34,7 @@ $(document).ready(function(){ format: 'dd/mm/yyyy', }); init_filters(); + init_export(); $('#nav-btn-add-tag').on('click', function(){ $(".nav-panel-add-tag").toggle(100); -- cgit v1.2.3 From add597bad95b30dbecab3aecc8362a1ccd427976 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 16 Oct 2015 10:51:53 +0200 Subject: Rework on export - all export now return a `HttpFoundation\Response` - return a 404 on unsupported format - add tests --- .../CoreBundle/Controller/ExportController.php | 77 +++--- src/Wallabag/CoreBundle/Helper/EntriesExport.php | 258 +++++++++++++-------- .../CoreBundle/Resources/config/services.yml | 6 + .../Tests/Controller/ExportControllerTest.php | 116 +++++++++ 4 files changed, 324 insertions(+), 133 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Controller/ExportController.php b/src/Wallabag/CoreBundle/Controller/ExportController.php index 123e491a..dd3cb7ca 100644 --- a/src/Wallabag/CoreBundle/Controller/ExportController.php +++ b/src/Wallabag/CoreBundle/Controller/ExportController.php @@ -4,62 +4,55 @@ namespace Wallabag\CoreBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Helper\EntriesExport; class ExportController extends Controller { /** - * Gets all entries for current user. + * Gets one entry content. * - * @Route("/export/{category}.{format}", name="ebook", requirements={ - * "_format": "epub|mobi|pdf|json|xml|txt|csv" - * }) + * @param Entry $entry + * + * @Route("/export/{id}.{format}", requirements={"id" = "\d+"}, name="export_entry") */ - public function getEntriesAction($format, $category) + public function downloadEntryAction(Entry $entry, $format) { - $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); - switch ($category) { - case 'all': - $method = 'All'; - break; - - case 'unread': - $method = 'Unread'; - break; - - case 'starred': - $method = 'Starred'; - break; - - case 'archive': - $method = 'Archive'; - break; - - default: - break; + try { + return $this->get('wallabag_core.helper.entries_export') + ->setEntries($entry) + ->updateTitle('entry') + ->exportAs($format); + } catch (\InvalidArgumentException $e) { + throw new NotFoundHttpException($e->getMessage()); } - - $methodBuilder = 'getBuilderFor'.$method.'ByUser'; - $qb = $repository->$methodBuilder($this->getUser()->getId()); - $entries = $qb->getQuery()->getResult(); - - $export = new EntriesExport($entries); - $export->setMethod($method); - $export->exportAs($format); } /** - * Gets one entry content. - * - * @param Entry $entry + * Export all entries for current user. * - * @Route("/export/id/{id}.{format}", requirements={"id" = "\d+"}, name="ebook_entry") + * @Route("/export/{category}.{format}", name="export_entries", requirements={ + * "_format": "epub|mobi|pdf|json|xml|txt|csv", + * "category": "all|unread|starred|archive" + * }) */ - public function getEntryAction(Entry $entry, $format) + public function downloadEntriesAction($format, $category) { - $export = new EntriesExport(array($entry)); - $export->setMethod('entry'); - $export->exportAs($format); + $method = ucfirst($category); + $methodBuilder = 'getBuilderFor'.$method.'ByUser'; + $entries = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->$methodBuilder($this->getUser()->getId()) + ->getQuery() + ->getResult(); + + try { + return $this->get('wallabag_core.helper.entries_export') + ->setEntries($entries) + ->updateTitle($method) + ->exportAs($format); + } catch (\InvalidArgumentException $e) { + throw new NotFoundHttpException($e->getMessage()); + } } } diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index fad0bb97..806319b1 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -4,27 +4,51 @@ namespace Wallabag\CoreBundle\Helper; use PHPePub\Core\EPub; use PHPePub\Core\Structure\OPF\DublinCore; +use Symfony\Component\HttpFoundation\Response; class EntriesExport { - private $format; - private $method; - private $title; - private $entries; + private $wallabagUrl; + private $logoPath; + private $title = ''; + private $entries = array(); private $authors = array('wallabag'); - private $language; - private $tags; + private $language = ''; + private $tags = array(); + private $footerTemplate = '
    +

    Produced by wallabag with %EXPORT_METHOD%

    +

    Please open an issue if you have trouble with the display of this E-Book on your device.

    + wallabagUrl = $wallabagUrl; + $this->logoPath = $logoPath; + } + + /** + * Define entries. + * + * @param array|Entry $entries An array of entries or one entry + */ + public function setEntries($entries) + { + if (!is_array($entries)) { + $this->language = $entries->getLanguage(); + $entries = array($entries); + } + $this->entries = $entries; foreach ($entries as $entry) { $this->tags[] = $entry->getTags(); } - if (count($entries) === 1) { - $this->language = $entries[0]->getLanguage(); - } + + return $this; } /** @@ -32,29 +56,15 @@ class EntriesExport * * @param string $method Method to get articles */ - public function setMethod($method) + public function updateTitle($method) { - $this->method = $method; - - switch ($this->method) { - case 'All': - $this->title = 'All Articles'; - break; - case 'Unread': - $this->title = 'Unread articles'; - break; - case 'Starred': - $this->title = 'Starred articles'; - break; - case 'Archive': - $this->title = 'Archived articles'; - break; - case 'entry': - $this->title = $this->entries[0]->getTitle(); - break; - default: - break; + $this->title = $method.' articles'; + + if ('entry' === $method) { + $this->title = $this->entries[0]->getTitle(); } + + return $this; } /** @@ -64,30 +74,26 @@ class EntriesExport */ public function exportAs($format) { - $this->format = $format; - - switch ($this->format) { + switch ($format) { case 'epub': - $this->produceEpub(); - break; + return $this->produceEpub(); case 'mobi': - $this->produceMobi(); - break; + return $this->produceMobi(); case 'pdf': - $this->producePDF(); - break; + return $this->producePDF(); case 'csv': - $this->produceCSV(); - break; - - default: - break; + return $this->produceCSV(); } + + throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); } + /** + * Use PHPePub to dump a .epub file. + */ private function produceEpub() { /* @@ -98,7 +104,7 @@ class EntriesExport ."\n" .'' ."\n" - .''._('wallabag articles book')."\n" + ."wallabag articles book\n" ."\n" ."\n"; @@ -111,17 +117,21 @@ class EntriesExport */ $book->setTitle($this->title); - $book->setIdentifier($this->title, EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID. - $book->setLanguage($this->language); // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc. - $book->setDescription(_('Some articles saved on my wallabag')); + // Could also be the ISBN number, prefered for published books, or a UUID. + $book->setIdentifier($this->title, EPub::IDENTIFIER_URI); + // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc. + $book->setLanguage($this->language); + $book->setDescription('Some articles saved on my wallabag'); foreach ($this->authors as $author) { $book->setAuthor($author, $author); } - $book->setPublisher('wallabag', 'wallabag'); // I hope this is a non existant address :) - $book->setDate(time()); // Strictly not needed as the book date defaults to time(). - $book->setSourceURL("http://$_SERVER[HTTP_HOST]"); + // I hope this is a non existant address :) + $book->setPublisher('wallabag', 'wallabag'); + // Strictly not needed as the book date defaults to time(). + $book->setDate(time()); + $book->setSourceURL($this->wallabagUrl); $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP'); $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag'); @@ -129,12 +139,11 @@ class EntriesExport /* * Front page */ + if (file_exists($this->logoPath)) { + $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png'); + } - $book->setCoverImage('Cover.png', file_get_contents('themes/_global/img/appicon/apple-touch-icon-152.png'), 'image/png'); - - $cover = $content_start.'

    '._('Produced by wallabag with PHPePub').'

    '._('Please open an issue if you have trouble with the display of this E-Book on your device.').'

    '.$bookEnd; - - $book->addChapter('Notices', 'Cover2.html', $cover); + $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd); $book->buildTOC(); @@ -142,18 +151,31 @@ class EntriesExport * Adding actual entries */ - foreach ($this->entries as $entry) { //set tags as subjects - foreach ($this->tags as $tag) { - $book->setSubject($tag['value']); - } + // set tags as subjects + foreach ($this->entries as $entry) { + foreach ($this->tags as $tag) { + $book->setSubject($tag['value']); + } $chapter = $content_start.$entry->getContent().$bookEnd; $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD); } - $book->finalize(); - $book->sendBook($this->title); + + return Response::create( + $book->getBook(), + 200, + array( + 'Content-Description' => 'File Transfer', + 'Content-type' => 'application/epub+zip', + 'Content-Disposition' => 'attachment; filename="'.$this->title.'.epub"', + 'Content-Transfer-Encoding' => 'binary', + ) + )->send(); } + /** + * Use PHPMobi to dump a .mobi file. + */ private function produceMobi() { $mobi = new \MOBI(); @@ -162,7 +184,6 @@ class EntriesExport /* * Book metadata */ - $content->set('title', $this->title); $content->set('author', implode($this->authors)); $content->set('subject', $this->title); @@ -170,15 +191,15 @@ class EntriesExport /* * Front page */ - - $content->appendParagraph('

    '._('Produced by wallabag with PHPMobi').'

    '._('Please open an issue if you have trouble with the display of this E-Book on your device.').'

    '); - $content->appendImage(imagecreatefrompng('themes/_global/img/appicon/apple-touch-icon-152.png')); + $content->appendParagraph($this->getExportInformation('PHPMobi')); + if (file_exists($this->logoPath)) { + $content->appendImage(imagecreatefrompng($this->logoPath)); + } $content->appendPageBreak(); /* * Adding actual entries */ - foreach ($this->entries as $entry) { $content->appendChapterTitle($entry->getTitle()); $content->appendParagraph($entry->getContent()); @@ -189,10 +210,22 @@ class EntriesExport // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9 $this->title = preg_replace('/[^A-Za-z0-9\-]/', '', $this->title); - // we offer file to download - $mobi->download($this->title.'.mobi'); + return Response::create( + $mobi->toString(), + 200, + array( + 'Accept-Ranges' => 'bytes', + 'Content-Description' => 'File Transfer', + 'Content-type' => 'application/x-mobipocket-ebook', + 'Content-Disposition' => 'attachment; filename="'.$this->title.'.mobi"', + 'Content-Transfer-Encoding' => 'binary', + ) + )->send(); } + /** + * Use TCPDF to dump a .pdf file. + */ private function producePDF() { $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); @@ -200,7 +233,6 @@ class EntriesExport /* * Book metadata */ - $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('wallabag'); $pdf->SetTitle($this->title); @@ -210,19 +242,14 @@ class EntriesExport /* * Front page */ - $pdf->AddPage(); - $intro = '

    '.$this->title.'

    -

    '._('Produced by wallabag with tcpdf').'

    -

    '._('Please open an issue if you have trouble with the display of this E-Book on your device.').'

    -
    '; + $intro = '

    '.$this->title.'

    '.$this->getExportInformation('tcpdf'); $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true); /* * Adding actual entries */ - foreach ($this->entries as $entry) { foreach ($this->tags as $tag) { $pdf->SetKeywords($tag['value']); @@ -231,33 +258,82 @@ class EntriesExport $pdf->AddPage(); $html = '

    '.$entry->getTitle().'

    '; $html .= $entry->getContent(); + $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); } // set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); - $pdf->Output($this->title.'.pdf', 'D'); + return Response::create( + $pdf->Output('', 'S'), + 200, + array( + 'Content-Description' => 'File Transfer', + 'Content-type' => 'application/pdf', + 'Content-Disposition' => 'attachment; filename="'.$this->title.'.pdf"', + 'Content-Transfer-Encoding' => 'binary', + ) + )->send(); } + /** + * Inspired from CsvFileDumper. + */ private function produceCSV() { - header('Content-type: application/csv'); - header('Content-Disposition: attachment; filename="'.$this->title.'.csv"'); - header('Content-Transfer-Encoding: UTF-8'); + $delimiter = ';'; + $enclosure = '"'; + $handle = fopen('php://memory', 'rb+'); - $output = fopen('php://output', 'a'); + fputcsv($handle, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'), $delimiter, $enclosure); - fputcsv($output, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language')); foreach ($this->entries as $entry) { - fputcsv($output, array($entry->getTitle(), - $entry->getURL(), - $entry->getContent(), - implode(', ', $entry->getTags()->toArray()), - $entry->getMimetype(), - $entry->getLanguage(), )); + fputcsv( + $handle, + array( + $entry->getTitle(), + $entry->getURL(), + $entry->getContent(), + implode(', ', $entry->getTags()->toArray()), + $entry->getMimetype(), + $entry->getLanguage(), + ), + $delimiter, + $enclosure + ); + } + + rewind($handle); + $output = stream_get_contents($handle); + fclose($handle); + + return Response::create( + $output, + 200, + array( + 'Content-type' => 'application/csv', + 'Content-Disposition' => 'attachment; filename="'.$this->title.'.csv"', + 'Content-Transfer-Encoding' => 'UTF-8', + ) + )->send(); + } + + /** + * Return a kind of footer / information for the epub. + * + * @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi + * + * @return string + */ + private function getExportInformation($type) + { + $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate); + + if ('tcpdf' === $type) { + return str_replace('%IMAGE%', '', $info); } - fclose($output); - exit(); + + return str_replace('%IMAGE%', '', $info); } } diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 65c2c8d8..8e21b052 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -64,3 +64,9 @@ services: - %language% tags: - { name: kernel.event_subscriber } + + wallabag_core.helper.entries_export: + class: Wallabag\CoreBundle\Helper\EntriesExport + arguments: + - %wallabag_url% + - src/Wallabag/CoreBundle/Resources/views/themes/_global/public/img/appicon/apple-touch-icon-152.png diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php new file mode 100644 index 00000000..19d5f01d --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php @@ -0,0 +1,116 @@ +getClient(); + + $client->request('GET', '/export/unread.csv'); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + $this->assertContains('login', $client->getResponse()->headers->get('location')); + } + + public function testUnknownCategoryExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/export/awesomeness.epub'); + + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + } + + public function testUnknownFormatExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/export/unread.xslx'); + + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + } + + public function testEpubExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + ob_start(); + $crawler = $client->request('GET', '/export/archive.epub'); + ob_end_clean(); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $headers = $client->getResponse()->headers; + $this->assertEquals('application/epub+zip', $headers->get('content-type')); + $this->assertEquals('attachment; filename="Archive articles.epub"', $headers->get('content-disposition')); + $this->assertEquals('binary', $headers->get('content-transfer-encoding')); + } + + public function testMobiExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $content = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); + + ob_start(); + $crawler = $client->request('GET', '/export/'.$content->getId().'.mobi'); + ob_end_clean(); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $headers = $client->getResponse()->headers; + $this->assertEquals('application/x-mobipocket-ebook', $headers->get('content-type')); + $this->assertEquals('attachment; filename="testtitleentry1.mobi"', $headers->get('content-disposition')); + $this->assertEquals('binary', $headers->get('content-transfer-encoding')); + } + + public function testPdfExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + ob_start(); + $crawler = $client->request('GET', '/export/all.pdf'); + ob_end_clean(); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $headers = $client->getResponse()->headers; + $this->assertEquals('application/pdf', $headers->get('content-type')); + $this->assertEquals('attachment; filename="All articles.pdf"', $headers->get('content-disposition')); + $this->assertEquals('binary', $headers->get('content-transfer-encoding')); + } + + public function testCsvExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + ob_start(); + $crawler = $client->request('GET', '/export/unread.csv'); + ob_end_clean(); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $headers = $client->getResponse()->headers; + $this->assertEquals('application/csv', $headers->get('content-type')); + $this->assertEquals('attachment; filename="Unread articles.csv"', $headers->get('content-disposition')); + $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); + + $csv = str_getcsv($client->getResponse()->getContent(), "\n"); + + $this->assertGreaterThan(1, $csv); + $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]); + } +} -- cgit v1.2.3 From 33c36f6b482dd512a43b86c0e965bc09a67cf555 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 16 Oct 2015 10:56:24 +0200 Subject: Fix tests on pgsql --- src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php index 19d5f01d..3f749aae 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php @@ -71,7 +71,7 @@ class ExportControllerTest extends WallabagCoreTestCase $headers = $client->getResponse()->headers; $this->assertEquals('application/x-mobipocket-ebook', $headers->get('content-type')); - $this->assertEquals('attachment; filename="testtitleentry1.mobi"', $headers->get('content-disposition')); + $this->assertEquals('attachment; filename="'.preg_replace('/[^A-Za-z0-9\-]/', '', $content->getTitle()).'.mobi"', $headers->get('content-disposition')); $this->assertEquals('binary', $headers->get('content-transfer-encoding')); } -- cgit v1.2.3 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/Wallabag') 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 @@
      - {% if export_epub %}
    • EPUB
    • {% endif %} - {% if export_mobi %}
    • MOBI
    • {% endif %} - {% if export_pdf %}
    • PDF
    • {% endif %} -
    • CSV
    • + {% if export_epub %}
    • EPUB
    • {% endif %} + {% if export_mobi %}
    • MOBI
    • {% endif %} + {% if export_pdf %}
    • PDF
    • {% endif %} +
    • CSV
    • +
    • JSON
    • +
    • XML
    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 From 8ac95cbfcc2fa3e115bef369be216f7aa30f7311 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 18 Oct 2015 15:59:15 +0200 Subject: improved function --- src/Wallabag/CoreBundle/Helper/EntriesExport.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index 33ff6311..e073606c 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -331,11 +331,8 @@ class EntriesExport private function produceJSON() { - $serializer = $this->prepareSerializingContent(); - $jsonContent = $serializer->serialize($this->entries, 'json'); - return Response::create( - $jsonContent, + $this->prepareSerializingContent('json'), 200, array( 'Content-type' => 'application/json', @@ -347,11 +344,8 @@ class EntriesExport private function produceXML() { - $serializer = $this->prepareSerializingContent(); - $xmlContent = $serializer->serialize($this->entries, 'xml'); - return Response::create( - $xmlContent, + $this->prepareSerializingContent('xml'), 200, array( 'Content-type' => 'application/xml', @@ -360,18 +354,20 @@ class EntriesExport ) )->send(); } + /** * Return a Serializer object for producing processes that need it (JSON & XML). * * @return Serializer */ - private function prepareSerializingContent() + private function prepareSerializingContent($format) { $encoders = array(new XmlEncoder(), new JsonEncoder()); $normalizers = array(new ObjectNormalizer()); $normalizers[0]->setIgnoredAttributes(array('user', 'createdAt', 'updatedAt')); + $serializer = new Serializer($normalizers, $encoders); - return new Serializer($normalizers, $encoders); + return $serializer->serialize($this->entries, $format); } /** -- cgit v1.2.3 From 5b7da07620116a91d3c36ccd728d1899bc3ccb46 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 19 Oct 2015 20:31:30 +0200 Subject: use the groups annotation instead of setIgnoredAttributes --- src/Wallabag/CoreBundle/Entity/Entry.php | 35 ++++++++++++++++++++++++ src/Wallabag/CoreBundle/Helper/EntriesExport.php | 11 +++++--- 2 files changed, 42 insertions(+), 4 deletions(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 9e5446a6..a2bb507e 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -6,6 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Hateoas\Configuration\Annotation as Hateoas; +use Symfony\Component\Serializer\Annotation\Groups; use JMS\Serializer\Annotation\XmlRoot; use Wallabag\UserBundle\Entity\User; @@ -27,6 +28,8 @@ class Entry * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") + * + * @Groups({"entries_for_user", "export_all"}) */ private $id; @@ -34,6 +37,8 @@ class Entry * @var string * * @ORM\Column(name="title", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $title; @@ -42,6 +47,8 @@ class Entry * * @Assert\NotBlank() * @ORM\Column(name="url", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $url; @@ -49,6 +56,8 @@ class Entry * @var bool * * @ORM\Column(name="is_archived", type="boolean") + * + * @Groups({"entries_for_user", "export_all"}) */ private $isArchived = false; @@ -56,6 +65,8 @@ class Entry * @var bool * * @ORM\Column(name="is_starred", type="boolean") + * + * @Groups({"entries_for_user", "export_all"}) */ private $isStarred = false; @@ -63,6 +74,8 @@ class Entry * @var string * * @ORM\Column(name="content", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $content; @@ -70,6 +83,8 @@ class Entry * @var date * * @ORM\Column(name="created_at", type="datetime") + * + * @Groups({"export_all"}) */ private $createdAt; @@ -77,6 +92,8 @@ class Entry * @var date * * @ORM\Column(name="updated_at", type="datetime") + * + * @Groups({"export_all"}) */ private $updatedAt; @@ -84,6 +101,8 @@ class Entry * @var string * * @ORM\Column(name="comments", type="text", nullable=true) + * + * @Groups({"export_all"}) */ private $comments; @@ -91,6 +110,8 @@ class Entry * @var string * * @ORM\Column(name="mimetype", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $mimetype; @@ -98,6 +119,8 @@ class Entry * @var string * * @ORM\Column(name="language", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $language; @@ -105,6 +128,8 @@ class Entry * @var int * * @ORM\Column(name="reading_time", type="integer", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $readingTime; @@ -112,6 +137,8 @@ class Entry * @var string * * @ORM\Column(name="domain_name", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $domainName; @@ -119,6 +146,8 @@ class Entry * @var string * * @ORM\Column(name="preview_picture", type="text", nullable=true) + * + * @Groups({"entries_for_user", "export_all"}) */ private $previewPicture; @@ -126,17 +155,23 @@ class Entry * @var bool * * @ORM\Column(name="is_public", type="boolean", nullable=true, options={"default" = false}) + * + * @Groups({"export_all"}) */ private $isPublic; /** * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries") + * + * @Groups({"export_all"}) */ private $user; /** * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) * @ORM\JoinTable + * + * @Groups({"entries_for_user", "export_all"}) */ private $tags; diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index e073606c..f073ed7f 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -8,7 +8,10 @@ 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; +use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; +use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; +use Doctrine\Common\Annotations\AnnotationReader; class EntriesExport { @@ -363,11 +366,11 @@ class EntriesExport private function prepareSerializingContent($format) { $encoders = array(new XmlEncoder(), new JsonEncoder()); - $normalizers = array(new ObjectNormalizer()); - $normalizers[0]->setIgnoredAttributes(array('user', 'createdAt', 'updatedAt')); + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); + $normalizers = array(new PropertyNormalizer($classMetadataFactory)); $serializer = new Serializer($normalizers, $encoders); - return $serializer->serialize($this->entries, $format); + return $serializer->serialize($this->entries, $format, array('groups' => array('entries_for_user'))); } /** -- cgit v1.2.3 From 268e9e7277d470dbd65b4eaa70c247ef35a95a3d Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 19 Oct 2015 21:17:30 +0200 Subject: use JMS Serializer --- src/Wallabag/CoreBundle/Entity/Entry.php | 2 +- src/Wallabag/CoreBundle/Helper/EntriesExport.php | 17 +++++------------ 2 files changed, 6 insertions(+), 13 deletions(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index a2bb507e..5aa582f8 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -6,7 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Hateoas\Configuration\Annotation as Hateoas; -use Symfony\Component\Serializer\Annotation\Groups; +use JMS\Serializer\Annotation\Groups; use JMS\Serializer\Annotation\XmlRoot; use Wallabag\UserBundle\Entity\User; diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index f073ed7f..c14f9d72 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -5,13 +5,9 @@ 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\PropertyNormalizer; -use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; -use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; -use Doctrine\Common\Annotations\AnnotationReader; +use JMS\Serializer; +use JMS\Serializer\SerializerBuilder; +use JMS\Serializer\SerializationContext; class EntriesExport { @@ -365,12 +361,9 @@ class EntriesExport */ private function prepareSerializingContent($format) { - $encoders = array(new XmlEncoder(), new JsonEncoder()); - $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); - $normalizers = array(new PropertyNormalizer($classMetadataFactory)); - $serializer = new Serializer($normalizers, $encoders); + $serializer = SerializerBuilder::create()->build(); - return $serializer->serialize($this->entries, $format, array('groups' => array('entries_for_user'))); + return $serializer->serialize($this->entries, $format, SerializationContext::create()->setGroups(array('entries_for_user'))); } /** -- cgit v1.2.3 From cceca9ea1d93ccf1420c2506330a16dc07f6433c Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 30 Oct 2015 20:57:10 +0100 Subject: Fix route parameters Improve export tests Improve CSV export --- .../CoreBundle/Controller/ExportController.php | 11 ++- src/Wallabag/CoreBundle/Helper/EntriesExport.php | 12 ++- .../Tests/Controller/ExportControllerTest.php | 93 +++++++++++++++++++++- 3 files changed, 108 insertions(+), 8 deletions(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/Controller/ExportController.php b/src/Wallabag/CoreBundle/Controller/ExportController.php index dd3cb7ca..c8ef49a2 100644 --- a/src/Wallabag/CoreBundle/Controller/ExportController.php +++ b/src/Wallabag/CoreBundle/Controller/ExportController.php @@ -7,6 +7,10 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Wallabag\CoreBundle\Entity\Entry; +/** + * The try/catch can be removed once all formats will be implemented. + * Still need implementation: txt. + */ class ExportController extends Controller { /** @@ -14,7 +18,10 @@ class ExportController extends Controller * * @param Entry $entry * - * @Route("/export/{id}.{format}", requirements={"id" = "\d+"}, name="export_entry") + * @Route("/export/{id}.{format}", name="export_entry", requirements={ + * "format": "epub|mobi|pdf|json|xml|txt|csv", + * "id": "\d+" + * }) */ public function downloadEntryAction(Entry $entry, $format) { @@ -32,7 +39,7 @@ class ExportController extends Controller * Export all entries for current user. * * @Route("/export/{category}.{format}", name="export_entries", requirements={ - * "_format": "epub|mobi|pdf|json|xml|txt|csv", + * "format": "epub|mobi|pdf|json|xml|txt|csv", * "category": "all|unread|starred|archive" * }) */ diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index c14f9d72..d6a4d094 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -9,6 +9,9 @@ use JMS\Serializer; use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializationContext; +/** + * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest. + */ class EntriesExport { private $wallabagUrl; @@ -303,7 +306,8 @@ class EntriesExport array( $entry->getTitle(), $entry->getURL(), - $entry->getContent(), + // remove new line to avoid crazy results + str_replace(array("\r\n", "\r", "\n"), '', $entry->getContent()), implode(', ', $entry->getTags()->toArray()), $entry->getMimetype(), $entry->getLanguage(), @@ -363,7 +367,11 @@ class EntriesExport { $serializer = SerializerBuilder::create()->build(); - return $serializer->serialize($this->entries, $format, SerializationContext::create()->setGroups(array('entries_for_user'))); + return $serializer->serialize( + $this->entries, + $format, + SerializationContext::create()->setGroups(array('entries_for_user')) + ); } /** diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php index febdf4d4..3d3b97a9 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php @@ -21,7 +21,7 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $crawler = $client->request('GET', '/export/awesomeness.epub'); + $client->request('GET', '/export/awesomeness.epub'); $this->assertEquals(404, $client->getResponse()->getStatusCode()); } @@ -31,7 +31,34 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $crawler = $client->request('GET', '/export/unread.xslx'); + $client->request('GET', '/export/unread.xslx'); + + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + } + + public function testUnsupportedFormatExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $client->request('GET', '/export/unread.txt'); + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + + $content = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); + + $client->request('GET', '/export/'.$content->getId().'.txt'); + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + } + + public function testBadEntryId() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $client->request('GET', '/export/0.mobi'); $this->assertEquals(404, $client->getResponse()->getStatusCode()); } @@ -97,20 +124,33 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); + // to be sure results are the same + $contentInDB = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', 'admin') + ->andWhere('e.isArchived = true') + ->getQuery() + ->getArrayResult(); + ob_start(); - $crawler = $client->request('GET', '/export/unread.csv'); + $crawler = $client->request('GET', '/export/archive.csv'); ob_end_clean(); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $headers = $client->getResponse()->headers; $this->assertEquals('application/csv', $headers->get('content-type')); - $this->assertEquals('attachment; filename="Unread articles.csv"', $headers->get('content-disposition')); + $this->assertEquals('attachment; filename="Archive articles.csv"', $headers->get('content-disposition')); $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); $csv = str_getcsv($client->getResponse()->getContent(), "\n"); $this->assertGreaterThan(1, $csv); + // +1 for title line + $this->assertEquals(count($contentInDB)+1, count($csv)); $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]); } @@ -119,6 +159,16 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); + // to be sure results are the same + $contentInDB = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', 'admin') + ->getQuery() + ->getArrayResult(); + ob_start(); $crawler = $client->request('GET', '/export/all.json'); ob_end_clean(); @@ -129,6 +179,21 @@ class ExportControllerTest extends WallabagCoreTestCase $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')); + + $content = json_decode($client->getResponse()->getContent(), true); + $this->assertEquals(count($contentInDB), count($content)); + $this->assertArrayHasKey('id', $content[0]); + $this->assertArrayHasKey('title', $content[0]); + $this->assertArrayHasKey('url', $content[0]); + $this->assertArrayHasKey('is_archived', $content[0]); + $this->assertArrayHasKey('is_starred', $content[0]); + $this->assertArrayHasKey('content', $content[0]); + $this->assertArrayHasKey('mimetype', $content[0]); + $this->assertArrayHasKey('language', $content[0]); + $this->assertArrayHasKey('reading_time', $content[0]); + $this->assertArrayHasKey('domain_name', $content[0]); + $this->assertArrayHasKey('preview_picture', $content[0]); + $this->assertArrayHasKey('tags', $content[0]); } public function testXmlExport() @@ -136,6 +201,17 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); + // to be sure results are the same + $contentInDB = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', 'admin') + ->andWhere('e.isArchived = false') + ->getQuery() + ->getArrayResult(); + ob_start(); $crawler = $client->request('GET', '/export/unread.xml'); ob_end_clean(); @@ -146,5 +222,14 @@ class ExportControllerTest extends WallabagCoreTestCase $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')); + + $content = new \SimpleXMLElement($client->getResponse()->getContent()); + $this->assertGreaterThan(0, $content->count()); + $this->assertEquals(count($contentInDB), $content->count()); + $this->assertNotEmpty('id', (string) $content->entry[0]->id); + $this->assertNotEmpty('title', (string) $content->entry[0]->title); + $this->assertNotEmpty('url', (string) $content->entry[0]->url); + $this->assertNotEmpty('content', (string) $content->entry[0]->content); + $this->assertNotEmpty('domain_name', (string) $content->entry[0]->domain_name); } } -- cgit v1.2.3 From fba3f536a5557b9094393728d360bf78260ab4da Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 31 Oct 2015 09:26:00 +0100 Subject: Fix tests --- src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php | 6 ++++++ src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php | 1 - 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php index 7e64c5e1..176c529e 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php @@ -19,6 +19,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry1->setUrl('http://0.0.0.0'); $entry1->setReadingTime(11); $entry1->setDomainName('domain.io'); + $entry1->setMimetype('text/html'); $entry1->setTitle('test title entry1'); $entry1->setContent('This is my content /o/'); $entry1->setLanguage('en'); @@ -31,6 +32,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry2->setUrl('http://0.0.0.0'); $entry2->setReadingTime(1); $entry2->setDomainName('domain.io'); + $entry2->setMimetype('text/html'); $entry2->setTitle('test title entry2'); $entry2->setContent('This is my content /o/'); $entry2->setLanguage('fr'); @@ -43,6 +45,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry3->setUrl('http://0.0.0.0'); $entry3->setReadingTime(1); $entry3->setDomainName('domain.io'); + $entry3->setMimetype('text/html'); $entry3->setTitle('test title entry3'); $entry3->setContent('This is my content /o/'); $entry3->setLanguage('en'); @@ -63,6 +66,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry4->setUrl('http://0.0.0.0'); $entry4->setReadingTime(12); $entry4->setDomainName('domain.io'); + $entry4->setMimetype('text/html'); $entry4->setTitle('test title entry4'); $entry4->setContent('This is my content /o/'); $entry4->setLanguage('en'); @@ -83,6 +87,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry5->setUrl('http://0.0.0.0'); $entry5->setReadingTime(12); $entry5->setDomainName('domain.io'); + $entry5->setMimetype('text/html'); $entry5->setTitle('test title entry5'); $entry5->setContent('This is my content /o/'); $entry5->setStarred(true); @@ -97,6 +102,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry6->setUrl('http://0.0.0.0'); $entry6->setReadingTime(12); $entry6->setDomainName('domain.io'); + $entry6->setMimetype('text/html'); $entry6->setTitle('test title entry6'); $entry6->setContent('This is my content /o/'); $entry6->setArchived(true); diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php index 3d3b97a9..739b2dec 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php @@ -192,7 +192,6 @@ class ExportControllerTest extends WallabagCoreTestCase $this->assertArrayHasKey('language', $content[0]); $this->assertArrayHasKey('reading_time', $content[0]); $this->assertArrayHasKey('domain_name', $content[0]); - $this->assertArrayHasKey('preview_picture', $content[0]); $this->assertArrayHasKey('tags', $content[0]); } -- cgit v1.2.3