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