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