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