diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-10-23 11:09:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-23 11:09:17 +0200 |
commit | 1953a872932a63792293b4aec087880265ba89f7 (patch) | |
tree | fd16599e737fcdaf193c933ef3ec4a4ee248b117 /src/Wallabag/CoreBundle/Helper/EntriesExport.php | |
parent | d83d25dadec2c38460a32d96f5d2903426fec9d3 (diff) | |
parent | 702f2d67d60ca963492b90dad74cb5f8dcc84e51 (diff) | |
download | wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.gz wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.zst wallabag-1953a872932a63792293b4aec087880265ba89f7.zip |
Merge pull request #3011 from wallabag/2.3
wallabag 2.3.0
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/EntriesExport.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/EntriesExport.php | 107 |
1 files changed, 68 insertions, 39 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index 3d36a4c8..830798b8 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php | |||
@@ -2,12 +2,14 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | 3 | namespace Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use JMS\Serializer; | 5 | use Html2Text\Html2Text; |
6 | use JMS\Serializer\SerializationContext; | 6 | use JMS\Serializer\SerializationContext; |
7 | use JMS\Serializer\SerializerBuilder; | 7 | use JMS\Serializer\SerializerBuilder; |
8 | use PHPePub\Core\EPub; | 8 | use PHPePub\Core\EPub; |
9 | use PHPePub\Core\Structure\OPF\DublinCore; | 9 | use PHPePub\Core\Structure\OPF\DublinCore; |
10 | use Symfony\Component\HttpFoundation\Response; | 10 | use Symfony\Component\HttpFoundation\Response; |
11 | use Symfony\Component\Translation\TranslatorInterface; | ||
12 | use Wallabag\CoreBundle\Entity\Entry; | ||
11 | 13 | ||
12 | /** | 14 | /** |
13 | * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest. | 15 | * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest. |
@@ -16,21 +18,20 @@ class EntriesExport | |||
16 | { | 18 | { |
17 | private $wallabagUrl; | 19 | private $wallabagUrl; |
18 | private $logoPath; | 20 | private $logoPath; |
21 | private $translator; | ||
19 | private $title = ''; | 22 | private $title = ''; |
20 | private $entries = []; | 23 | private $entries = []; |
21 | private $authors = ['wallabag']; | 24 | private $author = 'wallabag'; |
22 | private $language = ''; | 25 | private $language = ''; |
23 | private $footerTemplate = '<div style="text-align:center;"> | ||
24 | <p>Produced by wallabag with %EXPORT_METHOD%</p> | ||
25 | <p>Please open <a href="https://github.com/wallabag/wallabag/issues">an issue</a> if you have trouble with the display of this E-Book on your device.</p> | ||
26 | </div>'; | ||
27 | 26 | ||
28 | /** | 27 | /** |
29 | * @param string $wallabagUrl Wallabag instance url | 28 | * @param TranslatorInterface $translator Translator service |
30 | * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE | 29 | * @param string $wallabagUrl Wallabag instance url |
30 | * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE | ||
31 | */ | 31 | */ |
32 | public function __construct($wallabagUrl, $logoPath) | 32 | public function __construct(TranslatorInterface $translator, $wallabagUrl, $logoPath) |
33 | { | 33 | { |
34 | $this->translator = $translator; | ||
34 | $this->wallabagUrl = $wallabagUrl; | 35 | $this->wallabagUrl = $wallabagUrl; |
35 | $this->logoPath = $logoPath; | 36 | $this->logoPath = $logoPath; |
36 | } | 37 | } |
@@ -63,7 +64,7 @@ class EntriesExport | |||
63 | */ | 64 | */ |
64 | public function updateTitle($method) | 65 | public function updateTitle($method) |
65 | { | 66 | { |
66 | $this->title = $method.' articles'; | 67 | $this->title = $method . ' articles'; |
67 | 68 | ||
68 | if ('entry' === $method) { | 69 | if ('entry' === $method) { |
69 | $this->title = $this->entries[0]->getTitle(); | 70 | $this->title = $this->entries[0]->getTitle(); |
@@ -73,6 +74,33 @@ class EntriesExport | |||
73 | } | 74 | } |
74 | 75 | ||
75 | /** | 76 | /** |
77 | * Sets the author for one entry or category. | ||
78 | * | ||
79 | * The publishers are used, or the domain name if empty. | ||
80 | * | ||
81 | * @param string $method Method to get articles | ||
82 | * | ||
83 | * @return EntriesExport | ||
84 | */ | ||
85 | public function updateAuthor($method) | ||
86 | { | ||
87 | if ('entry' !== $method) { | ||
88 | $this->author = $method . ' authors'; | ||
89 | |||
90 | return $this; | ||
91 | } | ||
92 | |||
93 | $this->author = $this->entries[0]->getDomainName(); | ||
94 | |||
95 | $publishedBy = $this->entries[0]->getPublishedBy(); | ||
96 | if (!empty($publishedBy)) { | ||
97 | $this->author = implode(', ', $publishedBy); | ||
98 | } | ||
99 | |||
100 | return $this; | ||
101 | } | ||
102 | |||
103 | /** | ||
76 | * Sets the output format. | 104 | * Sets the output format. |
77 | * | 105 | * |
78 | * @param string $format | 106 | * @param string $format |
@@ -81,7 +109,7 @@ class EntriesExport | |||
81 | */ | 109 | */ |
82 | public function exportAs($format) | 110 | public function exportAs($format) |
83 | { | 111 | { |
84 | $functionName = 'produce'.ucfirst($format); | 112 | $functionName = 'produce' . ucfirst($format); |
85 | if (method_exists($this, $functionName)) { | 113 | if (method_exists($this, $functionName)) { |
86 | return $this->$functionName(); | 114 | return $this->$functionName(); |
87 | } | 115 | } |
@@ -106,12 +134,12 @@ class EntriesExport | |||
106 | */ | 134 | */ |
107 | $content_start = | 135 | $content_start = |
108 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | 136 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
109 | ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n" | 137 | . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n" |
110 | .'<head>' | 138 | . '<head>' |
111 | ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" | 139 | . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" |
112 | ."<title>wallabag articles book</title>\n" | 140 | . "<title>wallabag articles book</title>\n" |
113 | ."</head>\n" | 141 | . "</head>\n" |
114 | ."<body>\n"; | 142 | . "<body>\n"; |
115 | 143 | ||
116 | $bookEnd = "</body>\n</html>\n"; | 144 | $bookEnd = "</body>\n</html>\n"; |
117 | 145 | ||
@@ -128,9 +156,7 @@ class EntriesExport | |||
128 | $book->setLanguage($this->language); | 156 | $book->setLanguage($this->language); |
129 | $book->setDescription('Some articles saved on my wallabag'); | 157 | $book->setDescription('Some articles saved on my wallabag'); |
130 | 158 | ||
131 | foreach ($this->authors as $author) { | 159 | $book->setAuthor($this->author, $this->author); |
132 | $book->setAuthor($author, $author); | ||
133 | } | ||
134 | 160 | ||
135 | // I hope this is a non existant address :) | 161 | // I hope this is a non existant address :) |
136 | $book->setPublisher('wallabag', 'wallabag'); | 162 | $book->setPublisher('wallabag', 'wallabag'); |
@@ -164,11 +190,11 @@ class EntriesExport | |||
164 | // in filenames, we limit to A-z/0-9 | 190 | // in filenames, we limit to A-z/0-9 |
165 | $filename = preg_replace('/[^A-Za-z0-9\-]/', '', $entry->getTitle()); | 191 | $filename = preg_replace('/[^A-Za-z0-9\-]/', '', $entry->getTitle()); |
166 | 192 | ||
167 | $chapter = $content_start.$entry->getContent().$bookEnd; | 193 | $chapter = $content_start . $entry->getContent() . $bookEnd; |
168 | $book->addChapter($entry->getTitle(), htmlspecialchars($filename).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD); | 194 | $book->addChapter($entry->getTitle(), htmlspecialchars($filename) . '.html', $chapter, true, EPub::EXTERNAL_REF_ADD); |
169 | } | 195 | } |
170 | 196 | ||
171 | $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd); | 197 | $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd); |
172 | 198 | ||
173 | return Response::create( | 199 | return Response::create( |
174 | $book->getBook(), | 200 | $book->getBook(), |
@@ -176,7 +202,7 @@ class EntriesExport | |||
176 | [ | 202 | [ |
177 | 'Content-Description' => 'File Transfer', | 203 | 'Content-Description' => 'File Transfer', |
178 | 'Content-type' => 'application/epub+zip', | 204 | 'Content-type' => 'application/epub+zip', |
179 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.epub"', | 205 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.epub"', |
180 | 'Content-Transfer-Encoding' => 'binary', | 206 | 'Content-Transfer-Encoding' => 'binary', |
181 | ] | 207 | ] |
182 | ); | 208 | ); |
@@ -196,7 +222,7 @@ class EntriesExport | |||
196 | * Book metadata | 222 | * Book metadata |
197 | */ | 223 | */ |
198 | $content->set('title', $this->title); | 224 | $content->set('title', $this->title); |
199 | $content->set('author', implode($this->authors)); | 225 | $content->set('author', $this->author); |
200 | $content->set('subject', $this->title); | 226 | $content->set('subject', $this->title); |
201 | 227 | ||
202 | /* | 228 | /* |
@@ -228,7 +254,7 @@ class EntriesExport | |||
228 | 'Accept-Ranges' => 'bytes', | 254 | 'Accept-Ranges' => 'bytes', |
229 | 'Content-Description' => 'File Transfer', | 255 | 'Content-Description' => 'File Transfer', |
230 | 'Content-type' => 'application/x-mobipocket-ebook', | 256 | 'Content-type' => 'application/x-mobipocket-ebook', |
231 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.mobi"', | 257 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.mobi"', |
232 | 'Content-Transfer-Encoding' => 'binary', | 258 | 'Content-Transfer-Encoding' => 'binary', |
233 | ] | 259 | ] |
234 | ); | 260 | ); |
@@ -247,7 +273,7 @@ class EntriesExport | |||
247 | * Book metadata | 273 | * Book metadata |
248 | */ | 274 | */ |
249 | $pdf->SetCreator(PDF_CREATOR); | 275 | $pdf->SetCreator(PDF_CREATOR); |
250 | $pdf->SetAuthor('wallabag'); | 276 | $pdf->SetAuthor($this->author); |
251 | $pdf->SetTitle($this->title); | 277 | $pdf->SetTitle($this->title); |
252 | $pdf->SetSubject('Articles via wallabag'); | 278 | $pdf->SetSubject('Articles via wallabag'); |
253 | $pdf->SetKeywords('wallabag'); | 279 | $pdf->SetKeywords('wallabag'); |
@@ -256,7 +282,7 @@ class EntriesExport | |||
256 | * Front page | 282 | * Front page |
257 | */ | 283 | */ |
258 | $pdf->AddPage(); | 284 | $pdf->AddPage(); |
259 | $intro = '<h1>'.$this->title.'</h1>'.$this->getExportInformation('tcpdf'); | 285 | $intro = '<h1>' . $this->title . '</h1>' . $this->getExportInformation('tcpdf'); |
260 | 286 | ||
261 | $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true); | 287 | $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true); |
262 | 288 | ||
@@ -269,7 +295,7 @@ class EntriesExport | |||
269 | } | 295 | } |
270 | 296 | ||
271 | $pdf->AddPage(); | 297 | $pdf->AddPage(); |
272 | $html = '<h1>'.$entry->getTitle().'</h1>'; | 298 | $html = '<h1>' . $entry->getTitle() . '</h1>'; |
273 | $html .= $entry->getContent(); | 299 | $html .= $entry->getContent(); |
274 | 300 | ||
275 | $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); | 301 | $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); |
@@ -284,7 +310,7 @@ class EntriesExport | |||
284 | [ | 310 | [ |
285 | 'Content-Description' => 'File Transfer', | 311 | 'Content-Description' => 'File Transfer', |
286 | 'Content-type' => 'application/pdf', | 312 | 'Content-type' => 'application/pdf', |
287 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.pdf"', | 313 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.pdf"', |
288 | 'Content-Transfer-Encoding' => 'binary', | 314 | 'Content-Transfer-Encoding' => 'binary', |
289 | ] | 315 | ] |
290 | ); | 316 | ); |
@@ -330,7 +356,7 @@ class EntriesExport | |||
330 | 200, | 356 | 200, |
331 | [ | 357 | [ |
332 | 'Content-type' => 'application/csv', | 358 | 'Content-type' => 'application/csv', |
333 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.csv"', | 359 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.csv"', |
334 | 'Content-Transfer-Encoding' => 'UTF-8', | 360 | 'Content-Transfer-Encoding' => 'UTF-8', |
335 | ] | 361 | ] |
336 | ); | 362 | ); |
@@ -348,7 +374,7 @@ class EntriesExport | |||
348 | 200, | 374 | 200, |
349 | [ | 375 | [ |
350 | 'Content-type' => 'application/json', | 376 | 'Content-type' => 'application/json', |
351 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"', | 377 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.json"', |
352 | 'Content-Transfer-Encoding' => 'UTF-8', | 378 | 'Content-Transfer-Encoding' => 'UTF-8', |
353 | ] | 379 | ] |
354 | ); | 380 | ); |
@@ -366,7 +392,7 @@ class EntriesExport | |||
366 | 200, | 392 | 200, |
367 | [ | 393 | [ |
368 | 'Content-type' => 'application/xml', | 394 | 'Content-type' => 'application/xml', |
369 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.xml"', | 395 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.xml"', |
370 | 'Content-Transfer-Encoding' => 'UTF-8', | 396 | 'Content-Transfer-Encoding' => 'UTF-8', |
371 | ] | 397 | ] |
372 | ); | 398 | ); |
@@ -382,8 +408,9 @@ class EntriesExport | |||
382 | $content = ''; | 408 | $content = ''; |
383 | $bar = str_repeat('=', 100); | 409 | $bar = str_repeat('=', 100); |
384 | foreach ($this->entries as $entry) { | 410 | foreach ($this->entries as $entry) { |
385 | $content .= "\n\n".$bar."\n\n".$entry->getTitle()."\n\n".$bar."\n\n"; | 411 | $content .= "\n\n" . $bar . "\n\n" . $entry->getTitle() . "\n\n" . $bar . "\n\n"; |
386 | $content .= trim(preg_replace('/\s+/S', ' ', strip_tags($entry->getContent())))."\n\n"; | 412 | $html = new Html2Text($entry->getContent(), ['do_links' => 'none', 'width' => 100]); |
413 | $content .= $html->getText(); | ||
387 | } | 414 | } |
388 | 415 | ||
389 | return Response::create( | 416 | return Response::create( |
@@ -391,7 +418,7 @@ class EntriesExport | |||
391 | 200, | 418 | 200, |
392 | [ | 419 | [ |
393 | 'Content-type' => 'text/plain', | 420 | 'Content-type' => 'text/plain', |
394 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.txt"', | 421 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.txt"', |
395 | 'Content-Transfer-Encoding' => 'UTF-8', | 422 | 'Content-Transfer-Encoding' => 'UTF-8', |
396 | ] | 423 | ] |
397 | ); | 424 | ); |
@@ -402,7 +429,7 @@ class EntriesExport | |||
402 | * | 429 | * |
403 | * @param string $format | 430 | * @param string $format |
404 | * | 431 | * |
405 | * @return Serializer | 432 | * @return string |
406 | */ | 433 | */ |
407 | private function prepareSerializingContent($format) | 434 | private function prepareSerializingContent($format) |
408 | { | 435 | { |
@@ -424,10 +451,12 @@ class EntriesExport | |||
424 | */ | 451 | */ |
425 | private function getExportInformation($type) | 452 | private function getExportInformation($type) |
426 | { | 453 | { |
427 | $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate); | 454 | $info = $this->translator->trans('export.footer_template', [ |
455 | '%method%' => $type, | ||
456 | ]); | ||
428 | 457 | ||
429 | if ('tcpdf' === $type) { | 458 | if ('tcpdf' === $type) { |
430 | return str_replace('%IMAGE%', '<img src="'.$this->logoPath.'" />', $info); | 459 | return str_replace('%IMAGE%', '<img src="' . $this->logoPath . '" />', $info); |
431 | } | 460 | } |
432 | 461 | ||
433 | return str_replace('%IMAGE%', '', $info); | 462 | return str_replace('%IMAGE%', '', $info); |