]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Helper/EntriesExport.php
Exported entries were added twice in export file
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / EntriesExport.php
index e073606cf9dfe155df5f9d176f4f3f243efc6896..6ecdf019672a65ac003f28ab320f145c7ddf7e13 100644 (file)
@@ -2,14 +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\ObjectNormalizer;
+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;
@@ -25,12 +28,12 @@ class EntriesExport
         </div';
 
     /**
-     * @param string $wallabagUrl Wallabag instance url
+     * @param Config $craueConfig CraueConfig instance to get wallabag instance url from database
      * @param string $logoPath    Path to the logo FROM THE BUNDLE SCOPE
      */
-    public function __construct($wallabagUrl, $logoPath)
+    public function __construct(Config $craueConfig, $logoPath)
     {
-        $this->wallabagUrl = $wallabagUrl;
+        $this->wallabagUrl = $craueConfig->get('wallabag_url');
         $this->logoPath = $logoPath;
     }
 
@@ -96,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));
@@ -180,7 +186,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.epub"',
                 'Content-Transfer-Encoding' => 'binary',
             )
-        )->send();
+        );
     }
 
     /**
@@ -230,7 +236,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.mobi"',
                 'Content-Transfer-Encoding' => 'binary',
             )
-        )->send();
+        );
     }
 
     /**
@@ -284,7 +290,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.pdf"',
                 'Content-Transfer-Encoding' => 'binary',
             )
-        )->send();
+        );
     }
 
     /**
@@ -304,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(),
@@ -326,7 +333,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.csv"',
                 'Content-Transfer-Encoding' => 'UTF-8',
             )
-        )->send();
+        );
     }
 
     private function produceJSON()
@@ -339,7 +346,7 @@ class EntriesExport
                 'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"',
                 'Content-Transfer-Encoding' => 'UTF-8',
             )
-        )->send();
+        );
     }
 
     private function produceXML()
@@ -352,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',
+            )
+        );
     }
 
     /**
@@ -362,12 +389,13 @@ class EntriesExport
      */
     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);
+        $serializer = SerializerBuilder::create()->build();
 
-        return $serializer->serialize($this->entries, $format);
+        return $serializer->serialize(
+            $this->entries,
+            $format,
+            SerializationContext::create()->setGroups(array('entries_for_user'))
+        );
     }
 
     /**