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 --- src/Wallabag/CoreBundle/Helper/EntriesExport.php | 263 +++++++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 src/Wallabag/CoreBundle/Helper/EntriesExport.php (limited to 'src/Wallabag/CoreBundle/Helper') 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(); + } +} -- 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 --- src/Wallabag/CoreBundle/Helper/EntriesExport.php | 258 +++++++++++++++-------- 1 file changed, 167 insertions(+), 91 deletions(-) (limited to 'src/Wallabag/CoreBundle/Helper') 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); } } -- 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 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src/Wallabag/CoreBundle/Helper') 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. * -- 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/CoreBundle/Helper') 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/Helper/EntriesExport.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/Wallabag/CoreBundle/Helper') 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/Helper/EntriesExport.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'src/Wallabag/CoreBundle/Helper') 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 --- src/Wallabag/CoreBundle/Helper/EntriesExport.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/CoreBundle/Helper') 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')) + ); } /** -- cgit v1.2.3