]> git.immae.eu Git - github/wallabag/wallabag.git/blame_incremental - src/Wallabag/CoreBundle/Helper/EntriesExport.php
Merge pull request #3106 from wallabag/api-content
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / EntriesExport.php
... / ...
CommitLineData
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use JMS\Serializer;
6use JMS\Serializer\SerializationContext;
7use JMS\Serializer\SerializerBuilder;
8use PHPePub\Core\EPub;
9use PHPePub\Core\Structure\OPF\DublinCore;
10use 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 */
15class EntriesExport
16{
17 private $wallabagUrl;
18 private $logoPath;
19 private $title = '';
20 private $entries = [];
21 private $authors = ['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 output format.
77 *
78 * @param string $format
79 *
80 * @return Response
81 */
82 public function exportAs($format)
83 {
84 $functionName = 'produce'.ucfirst($format);
85 if (method_exists($this, $functionName)) {
86 return $this->$functionName();
87 }
88
89 throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
90 }
91
92 public function exportJsonData()
93 {
94 return $this->prepareSerializingContent('json');
95 }
96
97 /**
98 * Use PHPePub to dump a .epub file.
99 *
100 * @return Response
101 */
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"
112 ."<title>wallabag articles book</title>\n"
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);
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');
130
131 foreach ($this->authors as $author) {
132 $book->setAuthor($author, $author);
133 }
134
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);
140
141 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
142 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');
143
144 /*
145 * Front page
146 */
147 if (file_exists($this->logoPath)) {
148 $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png');
149 }
150
151 $book->buildTOC();
152
153 /*
154 * Adding actual entries
155 */
156
157 // set tags as subjects
158 foreach ($this->entries as $entry) {
159 foreach ($entry->getTags() as $tag) {
160 $book->setSubject($tag->getLabel());
161 }
162
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
167 $chapter = $content_start.$entry->getContent().$bookEnd;
168 $book->addChapter($entry->getTitle(), htmlspecialchars($filename).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
169 }
170
171 $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
172
173 return Response::create(
174 $book->getBook(),
175 200,
176 [
177 'Content-Description' => 'File Transfer',
178 'Content-type' => 'application/epub+zip',
179 'Content-Disposition' => 'attachment; filename="'.$this->title.'.epub"',
180 'Content-Transfer-Encoding' => 'binary',
181 ]
182 );
183 }
184
185 /**
186 * Use PHPMobi to dump a .mobi file.
187 *
188 * @return Response
189 */
190 private function produceMobi()
191 {
192 $mobi = new \MOBI();
193 $content = new \MOBIFile();
194
195 /*
196 * Book metadata
197 */
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 */
205 $content->appendParagraph($this->getExportInformation('PHPMobi'));
206 if (file_exists($this->logoPath)) {
207 $content->appendImage(imagecreatefrompng($this->logoPath));
208 }
209 $content->appendPageBreak();
210
211 /*
212 * Adding actual entries
213 */
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
224 return Response::create(
225 $mobi->toString(),
226 200,
227 [
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',
233 ]
234 );
235 }
236
237 /**
238 * Use TCPDF to dump a .pdf file.
239 *
240 * @return Response
241 */
242 private function producePdf()
243 {
244 $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
245
246 /*
247 * Book metadata
248 */
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 */
258 $pdf->AddPage();
259 $intro = '<h1>'.$this->title.'</h1>'.$this->getExportInformation('tcpdf');
260
261 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
262
263 /*
264 * Adding actual entries
265 */
266 foreach ($this->entries as $entry) {
267 foreach ($entry->getTags() as $tag) {
268 $pdf->SetKeywords($tag->getLabel());
269 }
270
271 $pdf->AddPage();
272 $html = '<h1>'.$entry->getTitle().'</h1>';
273 $html .= $entry->getContent();
274
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
281 return Response::create(
282 $pdf->Output('', 'S'),
283 200,
284 [
285 'Content-Description' => 'File Transfer',
286 'Content-type' => 'application/pdf',
287 'Content-Disposition' => 'attachment; filename="'.$this->title.'.pdf"',
288 'Content-Transfer-Encoding' => 'binary',
289 ]
290 );
291 }
292
293 /**
294 * Inspired from CsvFileDumper.
295 *
296 * @return Response
297 */
298 private function produceCsv()
299 {
300 $delimiter = ';';
301 $enclosure = '"';
302 $handle = fopen('php://memory', 'rb+');
303
304 fputcsv($handle, ['Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language', 'Creation date'], $delimiter, $enclosure);
305
306 foreach ($this->entries as $entry) {
307 fputcsv(
308 $handle,
309 [
310 $entry->getTitle(),
311 $entry->getURL(),
312 // remove new line to avoid crazy results
313 str_replace(["\r\n", "\r", "\n"], '', $entry->getContent()),
314 implode(', ', $entry->getTags()->toArray()),
315 $entry->getMimetype(),
316 $entry->getLanguage(),
317 $entry->getCreatedAt()->format('d/m/Y h:i:s'),
318 ],
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,
331 [
332 'Content-type' => 'application/csv',
333 'Content-Disposition' => 'attachment; filename="'.$this->title.'.csv"',
334 'Content-Transfer-Encoding' => 'UTF-8',
335 ]
336 );
337 }
338
339 /**
340 * Dump a JSON file.
341 *
342 * @return Response
343 */
344 private function produceJson()
345 {
346 return Response::create(
347 $this->prepareSerializingContent('json'),
348 200,
349 [
350 'Content-type' => 'application/json',
351 'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"',
352 'Content-Transfer-Encoding' => 'UTF-8',
353 ]
354 );
355 }
356
357 /**
358 * Dump a XML file.
359 *
360 * @return Response
361 */
362 private function produceXml()
363 {
364 return Response::create(
365 $this->prepareSerializingContent('xml'),
366 200,
367 [
368 'Content-type' => 'application/xml',
369 'Content-Disposition' => 'attachment; filename="'.$this->title.'.xml"',
370 'Content-Transfer-Encoding' => 'UTF-8',
371 ]
372 );
373 }
374
375 /**
376 * Dump a TXT file.
377 *
378 * @return Response
379 */
380 private function produceTxt()
381 {
382 $content = '';
383 $bar = str_repeat('=', 100);
384 foreach ($this->entries as $entry) {
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";
387 }
388
389 return Response::create(
390 $content,
391 200,
392 [
393 'Content-type' => 'text/plain',
394 'Content-Disposition' => 'attachment; filename="'.$this->title.'.txt"',
395 'Content-Transfer-Encoding' => 'UTF-8',
396 ]
397 );
398 }
399
400 /**
401 * Return a Serializer object for producing processes that need it (JSON & XML).
402 *
403 * @param string $format
404 *
405 * @return Serializer
406 */
407 private function prepareSerializingContent($format)
408 {
409 $serializer = SerializerBuilder::create()->build();
410
411 return $serializer->serialize(
412 $this->entries,
413 $format,
414 SerializationContext::create()->setGroups(['entries_for_user'])
415 );
416 }
417
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);
431 }
432
433 return str_replace('%IMAGE%', '', $info);
434 }
435}