]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/EntriesExport.php
e16168b147ee0bc2f9568e792236f108e95a1f33
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / EntriesExport.php
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 $this->author = $method.' authors';
87
88 if ('entry' === $method) {
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 }
95 }
96
97 return $this;
98 }
99
100 /**
101 * Sets the output format.
102 *
103 * @param string $format
104 *
105 * @return Response
106 */
107 public function exportAs($format)
108 {
109 $functionName = 'produce' . ucfirst($format);
110 if (method_exists($this, $functionName)) {
111 return $this->$functionName();
112 }
113
114 throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
115 }
116
117 public function exportJsonData()
118 {
119 return $this->prepareSerializingContent('json');
120 }
121
122 /**
123 * Use PHPePub to dump a .epub file.
124 *
125 * @return Response
126 */
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"
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";
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);
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');
155
156 $book->setAuthor($this->author, $this->author);
157
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);
163
164 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
165 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');
166
167 /*
168 * Front page
169 */
170 if (file_exists($this->logoPath)) {
171 $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png');
172 }
173
174 $book->buildTOC();
175
176 /*
177 * Adding actual entries
178 */
179
180 // set tags as subjects
181 foreach ($this->entries as $entry) {
182 foreach ($entry->getTags() as $tag) {
183 $book->setSubject($tag->getLabel());
184 }
185
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
190 $chapter = $content_start . $entry->getContent() . $bookEnd;
191 $book->addChapter($entry->getTitle(), htmlspecialchars($filename) . '.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
192 }
193
194 $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd);
195
196 return Response::create(
197 $book->getBook(),
198 200,
199 [
200 'Content-Description' => 'File Transfer',
201 'Content-type' => 'application/epub+zip',
202 'Content-Disposition' => 'attachment; filename="' . $this->title . '.epub"',
203 'Content-Transfer-Encoding' => 'binary',
204 ]
205 );
206 }
207
208 /**
209 * Use PHPMobi to dump a .mobi file.
210 *
211 * @return Response
212 */
213 private function produceMobi()
214 {
215 $mobi = new \MOBI();
216 $content = new \MOBIFile();
217
218 /*
219 * Book metadata
220 */
221 $content->set('title', $this->title);
222 $content->set('author', $this->author);
223 $content->set('subject', $this->title);
224
225 /*
226 * Front page
227 */
228 $content->appendParagraph($this->getExportInformation('PHPMobi'));
229 if (file_exists($this->logoPath)) {
230 $content->appendImage(imagecreatefrompng($this->logoPath));
231 }
232 $content->appendPageBreak();
233
234 /*
235 * Adding actual entries
236 */
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
247 return Response::create(
248 $mobi->toString(),
249 200,
250 [
251 'Accept-Ranges' => 'bytes',
252 'Content-Description' => 'File Transfer',
253 'Content-type' => 'application/x-mobipocket-ebook',
254 'Content-Disposition' => 'attachment; filename="' . $this->title . '.mobi"',
255 'Content-Transfer-Encoding' => 'binary',
256 ]
257 );
258 }
259
260 /**
261 * Use TCPDF to dump a .pdf file.
262 *
263 * @return Response
264 */
265 private function producePdf()
266 {
267 $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
268
269 /*
270 * Book metadata
271 */
272 $pdf->SetCreator(PDF_CREATOR);
273 $pdf->SetAuthor($this->author);
274 $pdf->SetTitle($this->title);
275 $pdf->SetSubject('Articles via wallabag');
276 $pdf->SetKeywords('wallabag');
277
278 /*
279 * Front page
280 */
281 $pdf->AddPage();
282 $intro = '<h1>' . $this->title . '</h1>' . $this->getExportInformation('tcpdf');
283
284 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
285
286 /*
287 * Adding actual entries
288 */
289 foreach ($this->entries as $entry) {
290 foreach ($entry->getTags() as $tag) {
291 $pdf->SetKeywords($tag->getLabel());
292 }
293
294 $pdf->AddPage();
295 $html = '<h1>' . $entry->getTitle() . '</h1>';
296 $html .= $entry->getContent();
297
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
304 return Response::create(
305 $pdf->Output('', 'S'),
306 200,
307 [
308 'Content-Description' => 'File Transfer',
309 'Content-type' => 'application/pdf',
310 'Content-Disposition' => 'attachment; filename="' . $this->title . '.pdf"',
311 'Content-Transfer-Encoding' => 'binary',
312 ]
313 );
314 }
315
316 /**
317 * Inspired from CsvFileDumper.
318 *
319 * @return Response
320 */
321 private function produceCsv()
322 {
323 $delimiter = ';';
324 $enclosure = '"';
325 $handle = fopen('php://memory', 'rb+');
326
327 fputcsv($handle, ['Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language', 'Creation date'], $delimiter, $enclosure);
328
329 foreach ($this->entries as $entry) {
330 fputcsv(
331 $handle,
332 [
333 $entry->getTitle(),
334 $entry->getURL(),
335 // remove new line to avoid crazy results
336 str_replace(["\r\n", "\r", "\n"], '', $entry->getContent()),
337 implode(', ', $entry->getTags()->toArray()),
338 $entry->getMimetype(),
339 $entry->getLanguage(),
340 $entry->getCreatedAt()->format('d/m/Y h:i:s'),
341 ],
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,
354 [
355 'Content-type' => 'application/csv',
356 'Content-Disposition' => 'attachment; filename="' . $this->title . '.csv"',
357 'Content-Transfer-Encoding' => 'UTF-8',
358 ]
359 );
360 }
361
362 /**
363 * Dump a JSON file.
364 *
365 * @return Response
366 */
367 private function produceJson()
368 {
369 return Response::create(
370 $this->prepareSerializingContent('json'),
371 200,
372 [
373 'Content-type' => 'application/json',
374 'Content-Disposition' => 'attachment; filename="' . $this->title . '.json"',
375 'Content-Transfer-Encoding' => 'UTF-8',
376 ]
377 );
378 }
379
380 /**
381 * Dump a XML file.
382 *
383 * @return Response
384 */
385 private function produceXml()
386 {
387 return Response::create(
388 $this->prepareSerializingContent('xml'),
389 200,
390 [
391 'Content-type' => 'application/xml',
392 'Content-Disposition' => 'attachment; filename="' . $this->title . '.xml"',
393 'Content-Transfer-Encoding' => 'UTF-8',
394 ]
395 );
396 }
397
398 /**
399 * Dump a TXT file.
400 *
401 * @return Response
402 */
403 private function produceTxt()
404 {
405 $content = '';
406 $bar = str_repeat('=', 100);
407 foreach ($this->entries as $entry) {
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";
410 }
411
412 return Response::create(
413 $content,
414 200,
415 [
416 'Content-type' => 'text/plain',
417 'Content-Disposition' => 'attachment; filename="' . $this->title . '.txt"',
418 'Content-Transfer-Encoding' => 'UTF-8',
419 ]
420 );
421 }
422
423 /**
424 * Return a Serializer object for producing processes that need it (JSON & XML).
425 *
426 * @param string $format
427 *
428 * @return Serializer
429 */
430 private function prepareSerializingContent($format)
431 {
432 $serializer = SerializerBuilder::create()->build();
433
434 return $serializer->serialize(
435 $this->entries,
436 $format,
437 SerializationContext::create()->setGroups(['entries_for_user'])
438 );
439 }
440
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) {
453 return str_replace('%IMAGE%', '<img src="' . $this->logoPath . '" />', $info);
454 }
455
456 return str_replace('%IMAGE%', '', $info);
457 }
458 }