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