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