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