]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use JMS\Serializer; | |
6 | use JMS\Serializer\SerializationContext; | |
7 | use JMS\Serializer\SerializerBuilder; | |
8 | use PHPePub\Core\EPub; | |
9 | use PHPePub\Core\Structure\OPF\DublinCore; | |
10 | use Symfony\Component\HttpFoundation\Response; | |
11 | ||
12 | /** | |
13 | * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest. | |
14 | */ | |
15 | class EntriesExport | |
16 | { | |
17 | private $wallabagUrl; | |
18 | private $logoPath; | |
19 | private $title = ''; | |
20 | private $entries = []; | |
21 | private $author = 'wallabag'; | |
22 | private $language = ''; | |
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> | |
26 | </div>'; | |
27 | ||
28 | /** | |
29 | * @param string $wallabagUrl Wallabag instance url | |
30 | * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE | |
31 | */ | |
32 | public function __construct($wallabagUrl, $logoPath) | |
33 | { | |
34 | $this->wallabagUrl = $wallabagUrl; | |
35 | $this->logoPath = $logoPath; | |
36 | } | |
37 | ||
38 | /** | |
39 | * Define entries. | |
40 | * | |
41 | * @param array|Entry $entries An array of entries or one entry | |
42 | * | |
43 | * @return EntriesExport | |
44 | */ | |
45 | public function setEntries($entries) | |
46 | { | |
47 | if (!is_array($entries)) { | |
48 | $this->language = $entries->getLanguage(); | |
49 | $entries = [$entries]; | |
50 | } | |
51 | ||
52 | $this->entries = $entries; | |
53 | ||
54 | return $this; | |
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 | |
61 | * | |
62 | * @return EntriesExport | |
63 | */ | |
64 | public function updateTitle($method) | |
65 | { | |
66 | $this->title = $method . ' articles'; | |
67 | ||
68 | if ('entry' === $method) { | |
69 | $this->title = $this->entries[0]->getTitle(); | |
70 | } | |
71 | ||
72 | return $this; | |
73 | } | |
74 | ||
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 | /** | |
103 | * Sets the output format. | |
104 | * | |
105 | * @param string $format | |
106 | * | |
107 | * @return Response | |
108 | */ | |
109 | public function exportAs($format) | |
110 | { | |
111 | $functionName = 'produce' . ucfirst($format); | |
112 | if (method_exists($this, $functionName)) { | |
113 | return $this->$functionName(); | |
114 | } | |
115 | ||
116 | throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); | |
117 | } | |
118 | ||
119 | public function exportJsonData() | |
120 | { | |
121 | return $this->prepareSerializingContent('json'); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Use PHPePub to dump a .epub file. | |
126 | * | |
127 | * @return Response | |
128 | */ | |
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" | |
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"; | |
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); | |
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'); | |
157 | ||
158 | $book->setAuthor($this->author, $this->author); | |
159 | ||
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); | |
165 | ||
166 | $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP'); | |
167 | $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag'); | |
168 | ||
169 | /* | |
170 | * Front page | |
171 | */ | |
172 | if (file_exists($this->logoPath)) { | |
173 | $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png'); | |
174 | } | |
175 | ||
176 | $book->buildTOC(); | |
177 | ||
178 | /* | |
179 | * Adding actual entries | |
180 | */ | |
181 | ||
182 | // set tags as subjects | |
183 | foreach ($this->entries as $entry) { | |
184 | foreach ($entry->getTags() as $tag) { | |
185 | $book->setSubject($tag->getLabel()); | |
186 | } | |
187 | ||
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 | ||
192 | $chapter = $content_start . $entry->getContent() . $bookEnd; | |
193 | $book->addChapter($entry->getTitle(), htmlspecialchars($filename) . '.html', $chapter, true, EPub::EXTERNAL_REF_ADD); | |
194 | } | |
195 | ||
196 | $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd); | |
197 | ||
198 | return Response::create( | |
199 | $book->getBook(), | |
200 | 200, | |
201 | [ | |
202 | 'Content-Description' => 'File Transfer', | |
203 | 'Content-type' => 'application/epub+zip', | |
204 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.epub"', | |
205 | 'Content-Transfer-Encoding' => 'binary', | |
206 | ] | |
207 | ); | |
208 | } | |
209 | ||
210 | /** | |
211 | * Use PHPMobi to dump a .mobi file. | |
212 | * | |
213 | * @return Response | |
214 | */ | |
215 | private function produceMobi() | |
216 | { | |
217 | $mobi = new \MOBI(); | |
218 | $content = new \MOBIFile(); | |
219 | ||
220 | /* | |
221 | * Book metadata | |
222 | */ | |
223 | $content->set('title', $this->title); | |
224 | $content->set('author', $this->author); | |
225 | $content->set('subject', $this->title); | |
226 | ||
227 | /* | |
228 | * Front page | |
229 | */ | |
230 | $content->appendParagraph($this->getExportInformation('PHPMobi')); | |
231 | if (file_exists($this->logoPath)) { | |
232 | $content->appendImage(imagecreatefrompng($this->logoPath)); | |
233 | } | |
234 | $content->appendPageBreak(); | |
235 | ||
236 | /* | |
237 | * Adding actual entries | |
238 | */ | |
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 | ||
249 | return Response::create( | |
250 | $mobi->toString(), | |
251 | 200, | |
252 | [ | |
253 | 'Accept-Ranges' => 'bytes', | |
254 | 'Content-Description' => 'File Transfer', | |
255 | 'Content-type' => 'application/x-mobipocket-ebook', | |
256 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.mobi"', | |
257 | 'Content-Transfer-Encoding' => 'binary', | |
258 | ] | |
259 | ); | |
260 | } | |
261 | ||
262 | /** | |
263 | * Use TCPDF to dump a .pdf file. | |
264 | * | |
265 | * @return Response | |
266 | */ | |
267 | private function producePdf() | |
268 | { | |
269 | $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); | |
270 | ||
271 | /* | |
272 | * Book metadata | |
273 | */ | |
274 | $pdf->SetCreator(PDF_CREATOR); | |
275 | $pdf->SetAuthor($this->author); | |
276 | $pdf->SetTitle($this->title); | |
277 | $pdf->SetSubject('Articles via wallabag'); | |
278 | $pdf->SetKeywords('wallabag'); | |
279 | ||
280 | /* | |
281 | * Front page | |
282 | */ | |
283 | $pdf->AddPage(); | |
284 | $intro = '<h1>' . $this->title . '</h1>' . $this->getExportInformation('tcpdf'); | |
285 | ||
286 | $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true); | |
287 | ||
288 | /* | |
289 | * Adding actual entries | |
290 | */ | |
291 | foreach ($this->entries as $entry) { | |
292 | foreach ($entry->getTags() as $tag) { | |
293 | $pdf->SetKeywords($tag->getLabel()); | |
294 | } | |
295 | ||
296 | $pdf->AddPage(); | |
297 | $html = '<h1>' . $entry->getTitle() . '</h1>'; | |
298 | $html .= $entry->getContent(); | |
299 | ||
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 | ||
306 | return Response::create( | |
307 | $pdf->Output('', 'S'), | |
308 | 200, | |
309 | [ | |
310 | 'Content-Description' => 'File Transfer', | |
311 | 'Content-type' => 'application/pdf', | |
312 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.pdf"', | |
313 | 'Content-Transfer-Encoding' => 'binary', | |
314 | ] | |
315 | ); | |
316 | } | |
317 | ||
318 | /** | |
319 | * Inspired from CsvFileDumper. | |
320 | * | |
321 | * @return Response | |
322 | */ | |
323 | private function produceCsv() | |
324 | { | |
325 | $delimiter = ';'; | |
326 | $enclosure = '"'; | |
327 | $handle = fopen('php://memory', 'rb+'); | |
328 | ||
329 | fputcsv($handle, ['Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language', 'Creation date'], $delimiter, $enclosure); | |
330 | ||
331 | foreach ($this->entries as $entry) { | |
332 | fputcsv( | |
333 | $handle, | |
334 | [ | |
335 | $entry->getTitle(), | |
336 | $entry->getURL(), | |
337 | // remove new line to avoid crazy results | |
338 | str_replace(["\r\n", "\r", "\n"], '', $entry->getContent()), | |
339 | implode(', ', $entry->getTags()->toArray()), | |
340 | $entry->getMimetype(), | |
341 | $entry->getLanguage(), | |
342 | $entry->getCreatedAt()->format('d/m/Y h:i:s'), | |
343 | ], | |
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, | |
356 | [ | |
357 | 'Content-type' => 'application/csv', | |
358 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.csv"', | |
359 | 'Content-Transfer-Encoding' => 'UTF-8', | |
360 | ] | |
361 | ); | |
362 | } | |
363 | ||
364 | /** | |
365 | * Dump a JSON file. | |
366 | * | |
367 | * @return Response | |
368 | */ | |
369 | private function produceJson() | |
370 | { | |
371 | return Response::create( | |
372 | $this->prepareSerializingContent('json'), | |
373 | 200, | |
374 | [ | |
375 | 'Content-type' => 'application/json', | |
376 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.json"', | |
377 | 'Content-Transfer-Encoding' => 'UTF-8', | |
378 | ] | |
379 | ); | |
380 | } | |
381 | ||
382 | /** | |
383 | * Dump a XML file. | |
384 | * | |
385 | * @return Response | |
386 | */ | |
387 | private function produceXml() | |
388 | { | |
389 | return Response::create( | |
390 | $this->prepareSerializingContent('xml'), | |
391 | 200, | |
392 | [ | |
393 | 'Content-type' => 'application/xml', | |
394 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.xml"', | |
395 | 'Content-Transfer-Encoding' => 'UTF-8', | |
396 | ] | |
397 | ); | |
398 | } | |
399 | ||
400 | /** | |
401 | * Dump a TXT file. | |
402 | * | |
403 | * @return Response | |
404 | */ | |
405 | private function produceTxt() | |
406 | { | |
407 | $content = ''; | |
408 | $bar = str_repeat('=', 100); | |
409 | foreach ($this->entries as $entry) { | |
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"; | |
412 | } | |
413 | ||
414 | return Response::create( | |
415 | $content, | |
416 | 200, | |
417 | [ | |
418 | 'Content-type' => 'text/plain', | |
419 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.txt"', | |
420 | 'Content-Transfer-Encoding' => 'UTF-8', | |
421 | ] | |
422 | ); | |
423 | } | |
424 | ||
425 | /** | |
426 | * Return a Serializer object for producing processes that need it (JSON & XML). | |
427 | * | |
428 | * @param string $format | |
429 | * | |
430 | * @return Serializer | |
431 | */ | |
432 | private function prepareSerializingContent($format) | |
433 | { | |
434 | $serializer = SerializerBuilder::create()->build(); | |
435 | ||
436 | return $serializer->serialize( | |
437 | $this->entries, | |
438 | $format, | |
439 | SerializationContext::create()->setGroups(['entries_for_user']) | |
440 | ); | |
441 | } | |
442 | ||
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) { | |
455 | return str_replace('%IMAGE%', '<img src="' . $this->logoPath . '" />', $info); | |
456 | } | |
457 | ||
458 | return str_replace('%IMAGE%', '', $info); | |
459 | } | |
460 | } |