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