aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/EntriesExport.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/EntriesExport.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/EntriesExport.php87
1 files changed, 56 insertions, 31 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php
index 3d36a4c8..de259e7f 100644
--- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php
+++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php
@@ -2,12 +2,12 @@
2 2
3namespace Wallabag\CoreBundle\Helper; 3namespace Wallabag\CoreBundle\Helper;
4 4
5use JMS\Serializer;
6use JMS\Serializer\SerializationContext; 5use JMS\Serializer\SerializationContext;
7use JMS\Serializer\SerializerBuilder; 6use JMS\Serializer\SerializerBuilder;
8use PHPePub\Core\EPub; 7use PHPePub\Core\EPub;
9use PHPePub\Core\Structure\OPF\DublinCore; 8use PHPePub\Core\Structure\OPF\DublinCore;
10use Symfony\Component\HttpFoundation\Response; 9use Symfony\Component\HttpFoundation\Response;
10use Wallabag\CoreBundle\Entity\Entry;
11 11
12/** 12/**
13 * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest. 13 * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest.
@@ -18,7 +18,7 @@ class EntriesExport
18 private $logoPath; 18 private $logoPath;
19 private $title = ''; 19 private $title = '';
20 private $entries = []; 20 private $entries = [];
21 private $authors = ['wallabag']; 21 private $author = 'wallabag';
22 private $language = ''; 22 private $language = '';
23 private $footerTemplate = '<div style="text-align:center;"> 23 private $footerTemplate = '<div style="text-align:center;">
24 <p>Produced by wallabag with %EXPORT_METHOD%</p> 24 <p>Produced by wallabag with %EXPORT_METHOD%</p>
@@ -63,7 +63,7 @@ class EntriesExport
63 */ 63 */
64 public function updateTitle($method) 64 public function updateTitle($method)
65 { 65 {
66 $this->title = $method.' articles'; 66 $this->title = $method . ' articles';
67 67
68 if ('entry' === $method) { 68 if ('entry' === $method) {
69 $this->title = $this->entries[0]->getTitle(); 69 $this->title = $this->entries[0]->getTitle();
@@ -73,6 +73,33 @@ class EntriesExport
73 } 73 }
74 74
75 /** 75 /**
76 * Sets the author for one entry or category.
77 *
78 * The publishers are used, or the domain name if empty.
79 *
80 * @param string $method Method to get articles
81 *
82 * @return EntriesExport
83 */
84 public function updateAuthor($method)
85 {
86 if ('entry' !== $method) {
87 $this->author = $method . ' authors';
88
89 return $this;
90 }
91
92 $this->author = $this->entries[0]->getDomainName();
93
94 $publishedBy = $this->entries[0]->getPublishedBy();
95 if (!empty($publishedBy)) {
96 $this->author = implode(', ', $publishedBy);
97 }
98
99 return $this;
100 }
101
102 /**
76 * Sets the output format. 103 * Sets the output format.
77 * 104 *
78 * @param string $format 105 * @param string $format
@@ -81,7 +108,7 @@ class EntriesExport
81 */ 108 */
82 public function exportAs($format) 109 public function exportAs($format)
83 { 110 {
84 $functionName = 'produce'.ucfirst($format); 111 $functionName = 'produce' . ucfirst($format);
85 if (method_exists($this, $functionName)) { 112 if (method_exists($this, $functionName)) {
86 return $this->$functionName(); 113 return $this->$functionName();
87 } 114 }
@@ -106,12 +133,12 @@ class EntriesExport
106 */ 133 */
107 $content_start = 134 $content_start =
108 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 135 "<?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" 136 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
110 .'<head>' 137 . '<head>'
111 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" 138 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
112 ."<title>wallabag articles book</title>\n" 139 . "<title>wallabag articles book</title>\n"
113 ."</head>\n" 140 . "</head>\n"
114 ."<body>\n"; 141 . "<body>\n";
115 142
116 $bookEnd = "</body>\n</html>\n"; 143 $bookEnd = "</body>\n</html>\n";
117 144
@@ -128,9 +155,7 @@ class EntriesExport
128 $book->setLanguage($this->language); 155 $book->setLanguage($this->language);
129 $book->setDescription('Some articles saved on my wallabag'); 156 $book->setDescription('Some articles saved on my wallabag');
130 157
131 foreach ($this->authors as $author) { 158 $book->setAuthor($this->author, $this->author);
132 $book->setAuthor($author, $author);
133 }
134 159
135 // I hope this is a non existant address :) 160 // I hope this is a non existant address :)
136 $book->setPublisher('wallabag', 'wallabag'); 161 $book->setPublisher('wallabag', 'wallabag');
@@ -164,11 +189,11 @@ class EntriesExport
164 // in filenames, we limit to A-z/0-9 189 // in filenames, we limit to A-z/0-9
165 $filename = preg_replace('/[^A-Za-z0-9\-]/', '', $entry->getTitle()); 190 $filename = preg_replace('/[^A-Za-z0-9\-]/', '', $entry->getTitle());
166 191
167 $chapter = $content_start.$entry->getContent().$bookEnd; 192 $chapter = $content_start . $entry->getContent() . $bookEnd;
168 $book->addChapter($entry->getTitle(), htmlspecialchars($filename).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD); 193 $book->addChapter($entry->getTitle(), htmlspecialchars($filename) . '.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
169 } 194 }
170 195
171 $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd); 196 $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd);
172 197
173 return Response::create( 198 return Response::create(
174 $book->getBook(), 199 $book->getBook(),
@@ -176,7 +201,7 @@ class EntriesExport
176 [ 201 [
177 'Content-Description' => 'File Transfer', 202 'Content-Description' => 'File Transfer',
178 'Content-type' => 'application/epub+zip', 203 'Content-type' => 'application/epub+zip',
179 'Content-Disposition' => 'attachment; filename="'.$this->title.'.epub"', 204 'Content-Disposition' => 'attachment; filename="' . $this->title . '.epub"',
180 'Content-Transfer-Encoding' => 'binary', 205 'Content-Transfer-Encoding' => 'binary',
181 ] 206 ]
182 ); 207 );
@@ -196,7 +221,7 @@ class EntriesExport
196 * Book metadata 221 * Book metadata
197 */ 222 */
198 $content->set('title', $this->title); 223 $content->set('title', $this->title);
199 $content->set('author', implode($this->authors)); 224 $content->set('author', $this->author);
200 $content->set('subject', $this->title); 225 $content->set('subject', $this->title);
201 226
202 /* 227 /*
@@ -228,7 +253,7 @@ class EntriesExport
228 'Accept-Ranges' => 'bytes', 253 'Accept-Ranges' => 'bytes',
229 'Content-Description' => 'File Transfer', 254 'Content-Description' => 'File Transfer',
230 'Content-type' => 'application/x-mobipocket-ebook', 255 'Content-type' => 'application/x-mobipocket-ebook',
231 'Content-Disposition' => 'attachment; filename="'.$this->title.'.mobi"', 256 'Content-Disposition' => 'attachment; filename="' . $this->title . '.mobi"',
232 'Content-Transfer-Encoding' => 'binary', 257 'Content-Transfer-Encoding' => 'binary',
233 ] 258 ]
234 ); 259 );
@@ -247,7 +272,7 @@ class EntriesExport
247 * Book metadata 272 * Book metadata
248 */ 273 */
249 $pdf->SetCreator(PDF_CREATOR); 274 $pdf->SetCreator(PDF_CREATOR);
250 $pdf->SetAuthor('wallabag'); 275 $pdf->SetAuthor($this->author);
251 $pdf->SetTitle($this->title); 276 $pdf->SetTitle($this->title);
252 $pdf->SetSubject('Articles via wallabag'); 277 $pdf->SetSubject('Articles via wallabag');
253 $pdf->SetKeywords('wallabag'); 278 $pdf->SetKeywords('wallabag');
@@ -256,7 +281,7 @@ class EntriesExport
256 * Front page 281 * Front page
257 */ 282 */
258 $pdf->AddPage(); 283 $pdf->AddPage();
259 $intro = '<h1>'.$this->title.'</h1>'.$this->getExportInformation('tcpdf'); 284 $intro = '<h1>' . $this->title . '</h1>' . $this->getExportInformation('tcpdf');
260 285
261 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true); 286 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
262 287
@@ -269,7 +294,7 @@ class EntriesExport
269 } 294 }
270 295
271 $pdf->AddPage(); 296 $pdf->AddPage();
272 $html = '<h1>'.$entry->getTitle().'</h1>'; 297 $html = '<h1>' . $entry->getTitle() . '</h1>';
273 $html .= $entry->getContent(); 298 $html .= $entry->getContent();
274 299
275 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); 300 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
@@ -284,7 +309,7 @@ class EntriesExport
284 [ 309 [
285 'Content-Description' => 'File Transfer', 310 'Content-Description' => 'File Transfer',
286 'Content-type' => 'application/pdf', 311 'Content-type' => 'application/pdf',
287 'Content-Disposition' => 'attachment; filename="'.$this->title.'.pdf"', 312 'Content-Disposition' => 'attachment; filename="' . $this->title . '.pdf"',
288 'Content-Transfer-Encoding' => 'binary', 313 'Content-Transfer-Encoding' => 'binary',
289 ] 314 ]
290 ); 315 );
@@ -330,7 +355,7 @@ class EntriesExport
330 200, 355 200,
331 [ 356 [
332 'Content-type' => 'application/csv', 357 'Content-type' => 'application/csv',
333 'Content-Disposition' => 'attachment; filename="'.$this->title.'.csv"', 358 'Content-Disposition' => 'attachment; filename="' . $this->title . '.csv"',
334 'Content-Transfer-Encoding' => 'UTF-8', 359 'Content-Transfer-Encoding' => 'UTF-8',
335 ] 360 ]
336 ); 361 );
@@ -348,7 +373,7 @@ class EntriesExport
348 200, 373 200,
349 [ 374 [
350 'Content-type' => 'application/json', 375 'Content-type' => 'application/json',
351 'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"', 376 'Content-Disposition' => 'attachment; filename="' . $this->title . '.json"',
352 'Content-Transfer-Encoding' => 'UTF-8', 377 'Content-Transfer-Encoding' => 'UTF-8',
353 ] 378 ]
354 ); 379 );
@@ -366,7 +391,7 @@ class EntriesExport
366 200, 391 200,
367 [ 392 [
368 'Content-type' => 'application/xml', 393 'Content-type' => 'application/xml',
369 'Content-Disposition' => 'attachment; filename="'.$this->title.'.xml"', 394 'Content-Disposition' => 'attachment; filename="' . $this->title . '.xml"',
370 'Content-Transfer-Encoding' => 'UTF-8', 395 'Content-Transfer-Encoding' => 'UTF-8',
371 ] 396 ]
372 ); 397 );
@@ -382,8 +407,8 @@ class EntriesExport
382 $content = ''; 407 $content = '';
383 $bar = str_repeat('=', 100); 408 $bar = str_repeat('=', 100);
384 foreach ($this->entries as $entry) { 409 foreach ($this->entries as $entry) {
385 $content .= "\n\n".$bar."\n\n".$entry->getTitle()."\n\n".$bar."\n\n"; 410 $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"; 411 $content .= trim(preg_replace('/\s+/S', ' ', strip_tags($entry->getContent()))) . "\n\n";
387 } 412 }
388 413
389 return Response::create( 414 return Response::create(
@@ -391,7 +416,7 @@ class EntriesExport
391 200, 416 200,
392 [ 417 [
393 'Content-type' => 'text/plain', 418 'Content-type' => 'text/plain',
394 'Content-Disposition' => 'attachment; filename="'.$this->title.'.txt"', 419 'Content-Disposition' => 'attachment; filename="' . $this->title . '.txt"',
395 'Content-Transfer-Encoding' => 'UTF-8', 420 'Content-Transfer-Encoding' => 'UTF-8',
396 ] 421 ]
397 ); 422 );
@@ -402,7 +427,7 @@ class EntriesExport
402 * 427 *
403 * @param string $format 428 * @param string $format
404 * 429 *
405 * @return Serializer 430 * @return string
406 */ 431 */
407 private function prepareSerializingContent($format) 432 private function prepareSerializingContent($format)
408 { 433 {
@@ -427,7 +452,7 @@ class EntriesExport
427 $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate); 452 $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate);
428 453
429 if ('tcpdf' === $type) { 454 if ('tcpdf' === $type) {
430 return str_replace('%IMAGE%', '<img src="'.$this->logoPath.'" />', $info); 455 return str_replace('%IMAGE%', '<img src="' . $this->logoPath . '" />', $info);
431 } 456 }
432 457
433 return str_replace('%IMAGE%', '', $info); 458 return str_replace('%IMAGE%', '', $info);