X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FHelper%2FEntriesExport.php;h=6ecdf019672a65ac003f28ab320f145c7ddf7e13;hb=cfc90f8422fc6ff2625bd338afe8fbf18f917e95;hp=f073ed7f46447b655d8459fd0fe1c81e6d1198f9;hpb=5b7da07620116a91d3c36ccd728d1899bc3ccb46;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index f073ed7f..6ecdf019 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -2,17 +2,17 @@ namespace Wallabag\CoreBundle\Helper; +use JMS\Serializer; +use JMS\Serializer\SerializationContext; +use JMS\Serializer\SerializerBuilder; 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 Craue\ConfigBundle\Util\Config; +/** + * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest. + */ class EntriesExport { private $wallabagUrl; @@ -28,12 +28,12 @@ class EntriesExport wallabagUrl = $wallabagUrl; + $this->wallabagUrl = $craueConfig->get('wallabag_url'); $this->logoPath = $logoPath; } @@ -99,6 +99,9 @@ class EntriesExport case 'xml': return $this->produceXML(); + + case 'txt': + return $this->produceTXT(); } throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); @@ -183,7 +186,7 @@ class EntriesExport 'Content-Disposition' => 'attachment; filename="'.$this->title.'.epub"', 'Content-Transfer-Encoding' => 'binary', ) - )->send(); + ); } /** @@ -233,7 +236,7 @@ class EntriesExport 'Content-Disposition' => 'attachment; filename="'.$this->title.'.mobi"', 'Content-Transfer-Encoding' => 'binary', ) - )->send(); + ); } /** @@ -287,7 +290,7 @@ class EntriesExport 'Content-Disposition' => 'attachment; filename="'.$this->title.'.pdf"', 'Content-Transfer-Encoding' => 'binary', ) - )->send(); + ); } /** @@ -307,7 +310,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(), @@ -329,7 +333,7 @@ class EntriesExport 'Content-Disposition' => 'attachment; filename="'.$this->title.'.csv"', 'Content-Transfer-Encoding' => 'UTF-8', ) - )->send(); + ); } private function produceJSON() @@ -342,7 +346,7 @@ class EntriesExport 'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"', 'Content-Transfer-Encoding' => 'UTF-8', ) - )->send(); + ); } private function produceXML() @@ -355,7 +359,27 @@ class EntriesExport 'Content-Disposition' => 'attachment; filename="'.$this->title.'.xml"', 'Content-Transfer-Encoding' => 'UTF-8', ) - )->send(); + ); + } + + private function produceTXT() + { + $content = ''; + $bar = str_repeat('=', 100); + foreach ($this->entries as $entry) { + $content .= "\n\n".$bar."\n\n".$entry->getTitle()."\n\n".$bar."\n\n"; + $content .= trim(preg_replace('/\s+/S', ' ', strip_tags($entry->getContent())))."\n\n"; + } + + return Response::create( + $content, + 200, + array( + 'Content-type' => 'text/plain', + 'Content-Disposition' => 'attachment; filename="'.$this->title.'.txt"', + 'Content-Transfer-Encoding' => 'UTF-8', + ) + ); } /** @@ -365,12 +389,13 @@ 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')) + ); } /**