]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/EntriesExport.php
Merge pull request #4438 from wallabag/dependabot/composer/scheb/two-factor-bundle...
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / EntriesExport.php
CommitLineData
03690d13
TC
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
c6608783 5use Html2Text\Html2Text;
619cc453
JB
6use JMS\Serializer\SerializationContext;
7use JMS\Serializer\SerializerBuilder;
03690d13
TC
8use PHPePub\Core\EPub;
9use PHPePub\Core\Structure\OPF\DublinCore;
add597ba 10use Symfony\Component\HttpFoundation\Response;
b1428a1c 11use Symfony\Component\Translation\TranslatorInterface;
52b84c11 12use Wallabag\CoreBundle\Entity\Entry;
03690d13 13
cceca9ea
JB
14/**
15 * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest.
16 */
03690d13
TC
17class EntriesExport
18{
add597ba
JB
19 private $wallabagUrl;
20 private $logoPath;
b1428a1c 21 private $translator;
add597ba 22 private $title = '';
4094ea47 23 private $entries = [];
07320a2b 24 private $author = 'wallabag';
add597ba 25 private $language = '';
03690d13 26
add597ba 27 /**
9dd67fa3
JB
28 * @param TranslatorInterface $translator Translator service
29 * @param string $wallabagUrl Wallabag instance url
30 * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
add597ba 31 */
dc7fa8df 32 public function __construct(TranslatorInterface $translator, $wallabagUrl, $logoPath)
03690d13 33 {
dc7fa8df 34 $this->translator = $translator;
4b3c983a 35 $this->wallabagUrl = $wallabagUrl;
add597ba
JB
36 $this->logoPath = $logoPath;
37 }
38
39 /**
40 * Define entries.
41 *
42 * @param array|Entry $entries An array of entries or one entry
4094ea47
JB
43 *
44 * @return EntriesExport
add597ba
JB
45 */
46 public function setEntries($entries)
47 {
2a1ceb67 48 if (!\is_array($entries)) {
add597ba 49 $this->language = $entries->getLanguage();
4094ea47 50 $entries = [$entries];
add597ba
JB
51 }
52
03690d13
TC
53 $this->entries = $entries;
54
add597ba 55 return $this;
03690d13
TC
56 }
57
58 /**
59 * Sets the category of which we want to get articles, or just one entry.
60 *
61 * @param string $method Method to get articles
4094ea47
JB
62 *
63 * @return EntriesExport
03690d13 64 */
add597ba 65 public function updateTitle($method)
03690d13 66 {
15a6402f 67 $this->title = $method . ' articles';
add597ba
JB
68
69 if ('entry' === $method) {
70 $this->title = $this->entries[0]->getTitle();
03690d13 71 }
add597ba
JB
72
73 return $this;
03690d13
TC
74 }
75
07320a2b 76 /**
c57f69d9
ÉG
77 * Sets the author for one entry or category.
78 *
79 * The publishers are used, or the domain name if empty.
07320a2b
ÉG
80 *
81 * @param string $method Method to get articles
82 *
83 * @return EntriesExport
84 */
85 public function updateAuthor($method)
86 {
eeabca80 87 if ('entry' !== $method) {
f8108346 88 $this->author = 'Various authors';
07320a2b 89
eeabca80
ÉG
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);
07320a2b
ÉG
98 }
99
100 return $this;
101 }
102
03690d13
TC
103 /**
104 * Sets the output format.
105 *
106 * @param string $format
4094ea47
JB
107 *
108 * @return Response
03690d13
TC
109 */
110 public function exportAs($format)
111 {
15a6402f 112 $functionName = 'produce' . ucfirst($format);
8f336fda
JB
113 if (method_exists($this, $functionName)) {
114 return $this->$functionName();
03690d13 115 }
add597ba
JB
116
117 throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
03690d13
TC
118 }
119
8303b037
TC
120 public function exportJsonData()
121 {
122 return $this->prepareSerializingContent('json');
123 }
124
add597ba
JB
125 /**
126 * Use PHPePub to dump a .epub file.
4094ea47
JB
127 *
128 * @return Response
add597ba 129 */
03690d13
TC
130 private function produceEpub()
131 {
132 /*
133 * Start and End of the book
134 */
135 $content_start =
136 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
15a6402f
JB
137 . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
138 . '<head>'
139 . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
140 . "<title>wallabag articles book</title>\n"
141 . "</head>\n"
142 . "<body>\n";
03690d13
TC
143
144 $bookEnd = "</body>\n</html>\n";
145
146 $book = new EPub(EPub::BOOK_VERSION_EPUB3);
147
148 /*
149 * Book metadata
150 */
151
152 $book->setTitle($this->title);
add597ba
JB
153 // 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.
154 $book->setLanguage($this->language);
155 $book->setDescription('Some articles saved on my wallabag');
03690d13 156
07320a2b 157 $book->setAuthor($this->author, $this->author);
03690d13 158
add597ba
JB
159 // I hope this is a non existant address :)
160 $book->setPublisher('wallabag', 'wallabag');
161 // Strictly not needed as the book date defaults to time().
162 $book->setDate(time());
163 $book->setSourceURL($this->wallabagUrl);
03690d13
TC
164
165 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
166 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');
167
bf22266a 168 $entryIds = [];
edd1825b
KD
169 $entryCount = \count($this->entries);
170 $i = 0;
bf22266a 171
03690d13
TC
172 /*
173 * Adding actual entries
174 */
175
add597ba
JB
176 // set tags as subjects
177 foreach ($this->entries as $entry) {
edd1825b 178 ++$i;
9a7a0e1e
KD
179
180 /*
181 * Front page
182 * Set if there's only one entry in the given set
183 */
184 if (1 === $entryCount && null !== $entry->getPreviewPicture()) {
185 $book->setCoverImage($entry->getPreviewPicture());
186 }
187
b0458874
JB
188 foreach ($entry->getTags() as $tag) {
189 $book->setSubject($tag->getLabel());
add597ba 190 }
41d476d7 191 $filename = sha1(sprintf('%s:%s', $entry->getUrl(), $entry->getTitle()));
45d94a98 192
4944703e 193 $publishedBy = $entry->getPublishedBy();
5e1f2776 194 $authors = $this->translator->trans('export.unknown');
4944703e
KD
195 if (!empty($publishedBy)) {
196 $authors = implode(',', $publishedBy);
4944703e
KD
197 }
198
0bddd348
NL
199 $publishedAt = $entry->getPublishedAt();
200 $publishedDate = $this->translator->trans('export.unknown');
201 if (!empty($publishedAt)) {
202 $publishedDate = $entry->getPublishedAt()->format('Y-m-d');
203 }
204
4944703e
KD
205 $titlepage = $content_start .
206 '<h1>' . $entry->getTitle() . '</h1>' .
207 '<dl>' .
208 '<dt>' . $this->translator->trans('entry.view.published_by') . '</dt><dd>' . $authors . '</dd>' .
0bddd348 209 '<dt>' . $this->translator->trans('entry.metadata.published_on') . '</dt><dd>' . $publishedDate . '</dd>' .
4944703e
KD
210 '<dt>' . $this->translator->trans('entry.metadata.reading_time') . '</dt><dd>' . $this->translator->trans('entry.metadata.reading_time_minutes_short', ['%readingTime%' => $entry->getReadingTime()]) . '</dd>' .
211 '<dt>' . $this->translator->trans('entry.metadata.added_on') . '</dt><dd>' . $entry->getCreatedAt()->format('Y-m-d') . '</dd>' .
212 '<dt>' . $this->translator->trans('entry.metadata.address') . '</dt><dd><a href="' . $entry->getUrl() . '">' . $entry->getUrl() . '</a></dd>' .
213 '</dl>' .
214 $bookEnd;
edd1825b 215 $book->addChapter("Entry {$i} of {$entryCount}", "{$filename}_cover.html", $titlepage, true, EPub::EXTERNAL_REF_ADD);
15a6402f 216 $chapter = $content_start . $entry->getContent() . $bookEnd;
bf22266a
KD
217
218 $entryIds[] = $entry->getId();
edd1825b 219 $book->addChapter($entry->getTitle(), "{$filename}.html", $chapter, true, EPub::EXTERNAL_REF_ADD);
03690d13 220 }
add597ba 221
30cf72bf
KD
222 $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd);
223
bf22266a
KD
224 // Could also be the ISBN number, prefered for published books, or a UUID.
225 $hash = sha1(sprintf('%s:%s', $this->wallabagUrl, implode(',', $entryIds)));
226 $book->setIdentifier(sprintf('urn:wallabag:%s', $hash), EPub::IDENTIFIER_URI);
227
add597ba
JB
228 return Response::create(
229 $book->getBook(),
230 200,
4094ea47 231 [
add597ba
JB
232 'Content-Description' => 'File Transfer',
233 'Content-type' => 'application/epub+zip',
dac93644 234 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.epub"',
add597ba 235 'Content-Transfer-Encoding' => 'binary',
4094ea47 236 ]
f898102c 237 );
03690d13
TC
238 }
239
add597ba
JB
240 /**
241 * Use PHPMobi to dump a .mobi file.
4094ea47
JB
242 *
243 * @return Response
add597ba 244 */
03690d13
TC
245 private function produceMobi()
246 {
247 $mobi = new \MOBI();
248 $content = new \MOBIFile();
249
250 /*
251 * Book metadata
252 */
03690d13 253 $content->set('title', $this->title);
07320a2b 254 $content->set('author', $this->author);
03690d13
TC
255 $content->set('subject', $this->title);
256
257 /*
258 * Front page
259 */
add597ba
JB
260 $content->appendParagraph($this->getExportInformation('PHPMobi'));
261 if (file_exists($this->logoPath)) {
262 $content->appendImage(imagecreatefrompng($this->logoPath));
263 }
03690d13
TC
264 $content->appendPageBreak();
265
266 /*
267 * Adding actual entries
268 */
03690d13
TC
269 foreach ($this->entries as $entry) {
270 $content->appendChapterTitle($entry->getTitle());
271 $content->appendParagraph($entry->getContent());
272 $content->appendPageBreak();
273 }
274 $mobi->setContentProvider($content);
275
add597ba
JB
276 return Response::create(
277 $mobi->toString(),
278 200,
4094ea47 279 [
add597ba
JB
280 'Accept-Ranges' => 'bytes',
281 'Content-Description' => 'File Transfer',
282 'Content-type' => 'application/x-mobipocket-ebook',
dac93644 283 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.mobi"',
add597ba 284 'Content-Transfer-Encoding' => 'binary',
4094ea47 285 ]
f898102c 286 );
03690d13
TC
287 }
288
add597ba
JB
289 /**
290 * Use TCPDF to dump a .pdf file.
4094ea47
JB
291 *
292 * @return Response
add597ba 293 */
8f336fda 294 private function producePdf()
03690d13
TC
295 {
296 $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
297
298 /*
299 * Book metadata
300 */
03690d13 301 $pdf->SetCreator(PDF_CREATOR);
07320a2b 302 $pdf->SetAuthor($this->author);
03690d13
TC
303 $pdf->SetTitle($this->title);
304 $pdf->SetSubject('Articles via wallabag');
305 $pdf->SetKeywords('wallabag');
306
03690d13
TC
307 /*
308 * Adding actual entries
309 */
03690d13 310 foreach ($this->entries as $entry) {
b0458874
JB
311 foreach ($entry->getTags() as $tag) {
312 $pdf->SetKeywords($tag->getLabel());
03690d13
TC
313 }
314
ad5ef8bc 315 $publishedBy = $entry->getPublishedBy();
5e1f2776 316 $authors = $this->translator->trans('export.unknown');
ad5ef8bc
KD
317 if (!empty($publishedBy)) {
318 $authors = implode(',', $publishedBy);
ad5ef8bc
KD
319 }
320
321 $pdf->addPage();
322 $html = '<h1>' . $entry->getTitle() . '</h1>' .
323 '<dl>' .
324 '<dt>' . $this->translator->trans('entry.view.published_by') . '</dt><dd>' . $authors . '</dd>' .
325 '<dt>' . $this->translator->trans('entry.metadata.reading_time') . '</dt><dd>' . $this->translator->trans('entry.metadata.reading_time_minutes_short', ['%readingTime%' => $entry->getReadingTime()]) . '</dd>' .
326 '<dt>' . $this->translator->trans('entry.metadata.added_on') . '</dt><dd>' . $entry->getCreatedAt()->format('Y-m-d') . '</dd>' .
327 '<dt>' . $this->translator->trans('entry.metadata.address') . '</dt><dd><a href="' . $entry->getUrl() . '">' . $entry->getUrl() . '</a></dd>' .
328 '</dl>';
329 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
330
03690d13 331 $pdf->AddPage();
15a6402f 332 $html = '<h1>' . $entry->getTitle() . '</h1>';
03690d13 333 $html .= $entry->getContent();
add597ba 334
03690d13
TC
335 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
336 }
337
ad5ef8bc
KD
338 /*
339 * Last page
340 */
341 $pdf->AddPage();
342 $html = $this->getExportInformation('tcpdf');
343
344 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
345
03690d13
TC
346 // set image scale factor
347 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
348
add597ba
JB
349 return Response::create(
350 $pdf->Output('', 'S'),
351 200,
4094ea47 352 [
add597ba
JB
353 'Content-Description' => 'File Transfer',
354 'Content-type' => 'application/pdf',
dac93644 355 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.pdf"',
add597ba 356 'Content-Transfer-Encoding' => 'binary',
4094ea47 357 ]
f898102c 358 );
03690d13
TC
359 }
360
add597ba
JB
361 /**
362 * Inspired from CsvFileDumper.
4094ea47
JB
363 *
364 * @return Response
add597ba 365 */
8f336fda 366 private function produceCsv()
03690d13 367 {
add597ba
JB
368 $delimiter = ';';
369 $enclosure = '"';
2a1ceb67 370 $handle = fopen('php://memory', 'b+r');
03690d13 371
9401696f 372 fputcsv($handle, ['Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language', 'Creation date'], $delimiter, $enclosure);
03690d13 373
03690d13 374 foreach ($this->entries as $entry) {
add597ba
JB
375 fputcsv(
376 $handle,
4094ea47 377 [
add597ba
JB
378 $entry->getTitle(),
379 $entry->getURL(),
cceca9ea 380 // remove new line to avoid crazy results
4094ea47 381 str_replace(["\r\n", "\r", "\n"], '', $entry->getContent()),
add597ba
JB
382 implode(', ', $entry->getTags()->toArray()),
383 $entry->getMimetype(),
384 $entry->getLanguage(),
9401696f 385 $entry->getCreatedAt()->format('d/m/Y h:i:s'),
4094ea47 386 ],
add597ba
JB
387 $delimiter,
388 $enclosure
389 );
390 }
391
392 rewind($handle);
393 $output = stream_get_contents($handle);
394 fclose($handle);
395
396 return Response::create(
397 $output,
398 200,
4094ea47 399 [
add597ba 400 'Content-type' => 'application/csv',
dac93644 401 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.csv"',
add597ba 402 'Content-Transfer-Encoding' => 'UTF-8',
4094ea47 403 ]
f898102c 404 );
add597ba
JB
405 }
406
4094ea47
JB
407 /**
408 * Dump a JSON file.
409 *
410 * @return Response
411 */
8f336fda 412 private function produceJson()
b3cc1a14 413 {
b3cc1a14 414 return Response::create(
8ac95cbf 415 $this->prepareSerializingContent('json'),
b3cc1a14 416 200,
4094ea47 417 [
b3cc1a14 418 'Content-type' => 'application/json',
dac93644 419 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.json"',
b3cc1a14 420 'Content-Transfer-Encoding' => 'UTF-8',
4094ea47 421 ]
f898102c 422 );
b3cc1a14
TC
423 }
424
4094ea47
JB
425 /**
426 * Dump a XML file.
427 *
428 * @return Response
429 */
8f336fda 430 private function produceXml()
b3cc1a14 431 {
b3cc1a14 432 return Response::create(
8ac95cbf 433 $this->prepareSerializingContent('xml'),
b3cc1a14 434 200,
4094ea47 435 [
b3cc1a14 436 'Content-type' => 'application/xml',
dac93644 437 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.xml"',
b3cc1a14 438 'Content-Transfer-Encoding' => 'UTF-8',
4094ea47 439 ]
f898102c 440 );
b3cc1a14 441 }
8ac95cbf 442
4094ea47
JB
443 /**
444 * Dump a TXT file.
445 *
446 * @return Response
447 */
8f336fda 448 private function produceTxt()
6c08fb68
TC
449 {
450 $content = '';
d3f31ec4 451 $bar = str_repeat('=', 100);
6c08fb68 452 foreach ($this->entries as $entry) {
15a6402f 453 $content .= "\n\n" . $bar . "\n\n" . $entry->getTitle() . "\n\n" . $bar . "\n\n";
c6608783
NH
454 $html = new Html2Text($entry->getContent(), ['do_links' => 'none', 'width' => 100]);
455 $content .= $html->getText();
6c08fb68 456 }
d3f31ec4 457
6c08fb68
TC
458 return Response::create(
459 $content,
460 200,
4094ea47 461 [
6c08fb68 462 'Content-type' => 'text/plain',
dac93644 463 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.txt"',
6c08fb68 464 'Content-Transfer-Encoding' => 'UTF-8',
4094ea47 465 ]
f898102c 466 );
6c08fb68
TC
467 }
468
b3cc1a14
TC
469 /**
470 * Return a Serializer object for producing processes that need it (JSON & XML).
471 *
0e49487b
JB
472 * @param string $format
473 *
52b84c11 474 * @return string
b3cc1a14 475 */
8ac95cbf 476 private function prepareSerializingContent($format)
b3cc1a14 477 {
268e9e72 478 $serializer = SerializerBuilder::create()->build();
b3cc1a14 479
cceca9ea
JB
480 return $serializer->serialize(
481 $this->entries,
482 $format,
4094ea47 483 SerializationContext::create()->setGroups(['entries_for_user'])
cceca9ea 484 );
b3cc1a14
TC
485 }
486
add597ba
JB
487 /**
488 * Return a kind of footer / information for the epub.
489 *
490 * @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi
491 *
492 * @return string
493 */
494 private function getExportInformation($type)
495 {
b1428a1c
NL
496 $info = $this->translator->trans('export.footer_template', [
497 '%method%' => $type,
498 ]);
add597ba
JB
499
500 if ('tcpdf' === $type) {
15a6402f 501 return str_replace('%IMAGE%', '<img src="' . $this->logoPath . '" />', $info);
03690d13 502 }
add597ba
JB
503
504 return str_replace('%IMAGE%', '', $info);
03690d13 505 }
dac93644
KD
506
507 /**
508 * Return a sanitized version of the title by applying translit iconv
509 * and removing non alphanumeric characters, - and space.
510 *
511 * @return string Sanitized filename
512 */
513 private function getSanitizedFilename()
514 {
515 return preg_replace('/[^A-Za-z0-9\- \']/', '', iconv('utf-8', 'us-ascii//TRANSLIT', $this->title));
516 }
03690d13 517}