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