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