diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/EntriesExport.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/EntriesExport.php | 100 |
1 files changed, 72 insertions, 28 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index cbf1037b..64591687 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php | |||
@@ -85,7 +85,7 @@ class EntriesExport | |||
85 | public function updateAuthor($method) | 85 | public function updateAuthor($method) |
86 | { | 86 | { |
87 | if ('entry' !== $method) { | 87 | if ('entry' !== $method) { |
88 | $this->author = $method . ' authors'; | 88 | $this->author = 'Various authors'; |
89 | 89 | ||
90 | return $this; | 90 | return $this; |
91 | } | 91 | } |
@@ -150,8 +150,6 @@ class EntriesExport | |||
150 | */ | 150 | */ |
151 | 151 | ||
152 | $book->setTitle($this->title); | 152 | $book->setTitle($this->title); |
153 | // Could also be the ISBN number, prefered for published books, or a UUID. | ||
154 | $book->setIdentifier($this->title, EPub::IDENTIFIER_URI); | ||
155 | // 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. | 153 | // 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. |
156 | $book->setLanguage($this->language); | 154 | $book->setLanguage($this->language); |
157 | $book->setDescription('Some articles saved on my wallabag'); | 155 | $book->setDescription('Some articles saved on my wallabag'); |
@@ -174,27 +172,49 @@ class EntriesExport | |||
174 | $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png'); | 172 | $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png'); |
175 | } | 173 | } |
176 | 174 | ||
175 | $entryIds = []; | ||
176 | $entryCount = \count($this->entries); | ||
177 | $i = 0; | ||
178 | |||
177 | /* | 179 | /* |
178 | * Adding actual entries | 180 | * Adding actual entries |
179 | */ | 181 | */ |
180 | 182 | ||
181 | // set tags as subjects | 183 | // set tags as subjects |
182 | foreach ($this->entries as $entry) { | 184 | foreach ($this->entries as $entry) { |
185 | ++$i; | ||
183 | foreach ($entry->getTags() as $tag) { | 186 | foreach ($entry->getTags() as $tag) { |
184 | $book->setSubject($tag->getLabel()); | 187 | $book->setSubject($tag->getLabel()); |
185 | } | 188 | } |
189 | $filename = sha1($entry->getTitle()); | ||
186 | 190 | ||
187 | // the reader in Kobo Devices doesn't likes special caracters | 191 | $publishedBy = $entry->getPublishedBy(); |
188 | // in filenames, we limit to A-z/0-9 | 192 | $authors = $this->translator->trans('export.unknown'); |
189 | $filename = preg_replace('/[^A-Za-z0-9\-]/', '', $entry->getTitle()); | 193 | if (!empty($publishedBy)) { |
194 | $authors = implode(',', $publishedBy); | ||
195 | } | ||
190 | 196 | ||
191 | $titlepage = $content_start . '<h1>' . $entry->getTitle() . '</h1>' . $this->getExportInformation('PHPePub') . $bookEnd; | 197 | $titlepage = $content_start . |
192 | $book->addChapter('Title', 'Title.html', $titlepage, true, EPub::EXTERNAL_REF_ADD); | 198 | '<h1>' . $entry->getTitle() . '</h1>' . |
199 | '<dl>' . | ||
200 | '<dt>' . $this->translator->trans('entry.view.published_by') . '</dt><dd>' . $authors . '</dd>' . | ||
201 | '<dt>' . $this->translator->trans('entry.metadata.reading_time') . '</dt><dd>' . $this->translator->trans('entry.metadata.reading_time_minutes_short', ['%readingTime%' => $entry->getReadingTime()]) . '</dd>' . | ||
202 | '<dt>' . $this->translator->trans('entry.metadata.added_on') . '</dt><dd>' . $entry->getCreatedAt()->format('Y-m-d') . '</dd>' . | ||
203 | '<dt>' . $this->translator->trans('entry.metadata.address') . '</dt><dd><a href="' . $entry->getUrl() . '">' . $entry->getUrl() . '</a></dd>' . | ||
204 | '</dl>' . | ||
205 | $bookEnd; | ||
206 | $book->addChapter("Entry {$i} of {$entryCount}", "{$filename}_cover.html", $titlepage, true, EPub::EXTERNAL_REF_ADD); | ||
193 | $chapter = $content_start . $entry->getContent() . $bookEnd; | 207 | $chapter = $content_start . $entry->getContent() . $bookEnd; |
194 | $book->addChapter($entry->getTitle(), htmlspecialchars($filename) . '.html', $chapter, true, EPub::EXTERNAL_REF_ADD); | 208 | |
209 | $entryIds[] = $entry->getId(); | ||
210 | $book->addChapter($entry->getTitle(), "{$filename}.html", $chapter, true, EPub::EXTERNAL_REF_ADD); | ||
195 | } | 211 | } |
196 | 212 | ||
197 | $book->buildTOC(); | 213 | $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd); |
214 | |||
215 | // Could also be the ISBN number, prefered for published books, or a UUID. | ||
216 | $hash = sha1(sprintf('%s:%s', $this->wallabagUrl, implode(',', $entryIds))); | ||
217 | $book->setIdentifier(sprintf('urn:wallabag:%s', $hash), EPub::IDENTIFIER_URI); | ||
198 | 218 | ||
199 | return Response::create( | 219 | return Response::create( |
200 | $book->getBook(), | 220 | $book->getBook(), |
@@ -202,7 +222,7 @@ class EntriesExport | |||
202 | [ | 222 | [ |
203 | 'Content-Description' => 'File Transfer', | 223 | 'Content-Description' => 'File Transfer', |
204 | 'Content-type' => 'application/epub+zip', | 224 | 'Content-type' => 'application/epub+zip', |
205 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.epub"', | 225 | 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.epub"', |
206 | 'Content-Transfer-Encoding' => 'binary', | 226 | 'Content-Transfer-Encoding' => 'binary', |
207 | ] | 227 | ] |
208 | ); | 228 | ); |
@@ -244,9 +264,6 @@ class EntriesExport | |||
244 | } | 264 | } |
245 | $mobi->setContentProvider($content); | 265 | $mobi->setContentProvider($content); |
246 | 266 | ||
247 | // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9 | ||
248 | $this->title = preg_replace('/[^A-Za-z0-9\-]/', '', $this->title); | ||
249 | |||
250 | return Response::create( | 267 | return Response::create( |
251 | $mobi->toString(), | 268 | $mobi->toString(), |
252 | 200, | 269 | 200, |
@@ -254,7 +271,7 @@ class EntriesExport | |||
254 | 'Accept-Ranges' => 'bytes', | 271 | 'Accept-Ranges' => 'bytes', |
255 | 'Content-Description' => 'File Transfer', | 272 | 'Content-Description' => 'File Transfer', |
256 | 'Content-type' => 'application/x-mobipocket-ebook', | 273 | 'Content-type' => 'application/x-mobipocket-ebook', |
257 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.mobi"', | 274 | 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.mobi"', |
258 | 'Content-Transfer-Encoding' => 'binary', | 275 | 'Content-Transfer-Encoding' => 'binary', |
259 | ] | 276 | ] |
260 | ); | 277 | ); |
@@ -279,14 +296,6 @@ class EntriesExport | |||
279 | $pdf->SetKeywords('wallabag'); | 296 | $pdf->SetKeywords('wallabag'); |
280 | 297 | ||
281 | /* | 298 | /* |
282 | * Front page | ||
283 | */ | ||
284 | $pdf->AddPage(); | ||
285 | $intro = '<h1>' . $this->title . '</h1>' . $this->getExportInformation('tcpdf'); | ||
286 | |||
287 | $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true); | ||
288 | |||
289 | /* | ||
290 | * Adding actual entries | 299 | * Adding actual entries |
291 | */ | 300 | */ |
292 | foreach ($this->entries as $entry) { | 301 | foreach ($this->entries as $entry) { |
@@ -294,6 +303,22 @@ class EntriesExport | |||
294 | $pdf->SetKeywords($tag->getLabel()); | 303 | $pdf->SetKeywords($tag->getLabel()); |
295 | } | 304 | } |
296 | 305 | ||
306 | $publishedBy = $entry->getPublishedBy(); | ||
307 | $authors = $this->translator->trans('export.unknown'); | ||
308 | if (!empty($publishedBy)) { | ||
309 | $authors = implode(',', $publishedBy); | ||
310 | } | ||
311 | |||
312 | $pdf->addPage(); | ||
313 | $html = '<h1>' . $entry->getTitle() . '</h1>' . | ||
314 | '<dl>' . | ||
315 | '<dt>' . $this->translator->trans('entry.view.published_by') . '</dt><dd>' . $authors . '</dd>' . | ||
316 | '<dt>' . $this->translator->trans('entry.metadata.reading_time') . '</dt><dd>' . $this->translator->trans('entry.metadata.reading_time_minutes_short', ['%readingTime%' => $entry->getReadingTime()]) . '</dd>' . | ||
317 | '<dt>' . $this->translator->trans('entry.metadata.added_on') . '</dt><dd>' . $entry->getCreatedAt()->format('Y-m-d') . '</dd>' . | ||
318 | '<dt>' . $this->translator->trans('entry.metadata.address') . '</dt><dd><a href="' . $entry->getUrl() . '">' . $entry->getUrl() . '</a></dd>' . | ||
319 | '</dl>'; | ||
320 | $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); | ||
321 | |||
297 | $pdf->AddPage(); | 322 | $pdf->AddPage(); |
298 | $html = '<h1>' . $entry->getTitle() . '</h1>'; | 323 | $html = '<h1>' . $entry->getTitle() . '</h1>'; |
299 | $html .= $entry->getContent(); | 324 | $html .= $entry->getContent(); |
@@ -301,6 +326,14 @@ class EntriesExport | |||
301 | $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); | 326 | $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); |
302 | } | 327 | } |
303 | 328 | ||
329 | /* | ||
330 | * Last page | ||
331 | */ | ||
332 | $pdf->AddPage(); | ||
333 | $html = $this->getExportInformation('tcpdf'); | ||
334 | |||
335 | $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); | ||
336 | |||
304 | // set image scale factor | 337 | // set image scale factor |
305 | $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); | 338 | $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); |
306 | 339 | ||
@@ -310,7 +343,7 @@ class EntriesExport | |||
310 | [ | 343 | [ |
311 | 'Content-Description' => 'File Transfer', | 344 | 'Content-Description' => 'File Transfer', |
312 | 'Content-type' => 'application/pdf', | 345 | 'Content-type' => 'application/pdf', |
313 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.pdf"', | 346 | 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.pdf"', |
314 | 'Content-Transfer-Encoding' => 'binary', | 347 | 'Content-Transfer-Encoding' => 'binary', |
315 | ] | 348 | ] |
316 | ); | 349 | ); |
@@ -356,7 +389,7 @@ class EntriesExport | |||
356 | 200, | 389 | 200, |
357 | [ | 390 | [ |
358 | 'Content-type' => 'application/csv', | 391 | 'Content-type' => 'application/csv', |
359 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.csv"', | 392 | 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.csv"', |
360 | 'Content-Transfer-Encoding' => 'UTF-8', | 393 | 'Content-Transfer-Encoding' => 'UTF-8', |
361 | ] | 394 | ] |
362 | ); | 395 | ); |
@@ -374,7 +407,7 @@ class EntriesExport | |||
374 | 200, | 407 | 200, |
375 | [ | 408 | [ |
376 | 'Content-type' => 'application/json', | 409 | 'Content-type' => 'application/json', |
377 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.json"', | 410 | 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.json"', |
378 | 'Content-Transfer-Encoding' => 'UTF-8', | 411 | 'Content-Transfer-Encoding' => 'UTF-8', |
379 | ] | 412 | ] |
380 | ); | 413 | ); |
@@ -392,7 +425,7 @@ class EntriesExport | |||
392 | 200, | 425 | 200, |
393 | [ | 426 | [ |
394 | 'Content-type' => 'application/xml', | 427 | 'Content-type' => 'application/xml', |
395 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.xml"', | 428 | 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.xml"', |
396 | 'Content-Transfer-Encoding' => 'UTF-8', | 429 | 'Content-Transfer-Encoding' => 'UTF-8', |
397 | ] | 430 | ] |
398 | ); | 431 | ); |
@@ -418,7 +451,7 @@ class EntriesExport | |||
418 | 200, | 451 | 200, |
419 | [ | 452 | [ |
420 | 'Content-type' => 'text/plain', | 453 | 'Content-type' => 'text/plain', |
421 | 'Content-Disposition' => 'attachment; filename="' . $this->title . '.txt"', | 454 | 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.txt"', |
422 | 'Content-Transfer-Encoding' => 'UTF-8', | 455 | 'Content-Transfer-Encoding' => 'UTF-8', |
423 | ] | 456 | ] |
424 | ); | 457 | ); |
@@ -461,4 +494,15 @@ class EntriesExport | |||
461 | 494 | ||
462 | return str_replace('%IMAGE%', '', $info); | 495 | return str_replace('%IMAGE%', '', $info); |
463 | } | 496 | } |
497 | |||
498 | /** | ||
499 | * Return a sanitized version of the title by applying translit iconv | ||
500 | * and removing non alphanumeric characters, - and space. | ||
501 | * | ||
502 | * @return string Sanitized filename | ||
503 | */ | ||
504 | private function getSanitizedFilename() | ||
505 | { | ||
506 | return preg_replace('/[^A-Za-z0-9\- \']/', '', iconv('utf-8', 'us-ascii//TRANSLIT', $this->title)); | ||
507 | } | ||
464 | } | 508 | } |