aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/EntriesExport.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/EntriesExport.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/EntriesExport.php92
1 files changed, 65 insertions, 27 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php
index 6082f6b9..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 }
@@ -173,6 +173,8 @@ class EntriesExport
173 } 173 }
174 174
175 $entryIds = []; 175 $entryIds = [];
176 $entryCount = \count($this->entries);
177 $i = 0;
176 178
177 /* 179 /*
178 * Adding actual entries 180 * Adding actual entries
@@ -180,35 +182,47 @@ class EntriesExport
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);
195 208
196 $entryIds[] = $entry->getId(); 209 $entryIds[] = $entry->getId();
210 $book->addChapter($entry->getTitle(), "{$filename}.html", $chapter, true, EPub::EXTERNAL_REF_ADD);
197 } 211 }
198 212
213 $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd);
214
199 // Could also be the ISBN number, prefered for published books, or a UUID. 215 // Could also be the ISBN number, prefered for published books, or a UUID.
200 $hash = sha1(sprintf('%s:%s', $this->wallabagUrl, implode(',', $entryIds))); 216 $hash = sha1(sprintf('%s:%s', $this->wallabagUrl, implode(',', $entryIds)));
201 $book->setIdentifier(sprintf('urn:wallabag:%s', $hash), EPub::IDENTIFIER_URI); 217 $book->setIdentifier(sprintf('urn:wallabag:%s', $hash), EPub::IDENTIFIER_URI);
202 218
203 $book->buildTOC();
204
205 return Response::create( 219 return Response::create(
206 $book->getBook(), 220 $book->getBook(),
207 200, 221 200,
208 [ 222 [
209 'Content-Description' => 'File Transfer', 223 'Content-Description' => 'File Transfer',
210 'Content-type' => 'application/epub+zip', 224 'Content-type' => 'application/epub+zip',
211 'Content-Disposition' => 'attachment; filename="' . $this->title . '.epub"', 225 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.epub"',
212 'Content-Transfer-Encoding' => 'binary', 226 'Content-Transfer-Encoding' => 'binary',
213 ] 227 ]
214 ); 228 );
@@ -250,9 +264,6 @@ class EntriesExport
250 } 264 }
251 $mobi->setContentProvider($content); 265 $mobi->setContentProvider($content);
252 266
253 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
254 $this->title = preg_replace('/[^A-Za-z0-9\-]/', '', $this->title);
255
256 return Response::create( 267 return Response::create(
257 $mobi->toString(), 268 $mobi->toString(),
258 200, 269 200,
@@ -260,7 +271,7 @@ class EntriesExport
260 'Accept-Ranges' => 'bytes', 271 'Accept-Ranges' => 'bytes',
261 'Content-Description' => 'File Transfer', 272 'Content-Description' => 'File Transfer',
262 'Content-type' => 'application/x-mobipocket-ebook', 273 'Content-type' => 'application/x-mobipocket-ebook',
263 'Content-Disposition' => 'attachment; filename="' . $this->title . '.mobi"', 274 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.mobi"',
264 'Content-Transfer-Encoding' => 'binary', 275 'Content-Transfer-Encoding' => 'binary',
265 ] 276 ]
266 ); 277 );
@@ -285,14 +296,6 @@ class EntriesExport
285 $pdf->SetKeywords('wallabag'); 296 $pdf->SetKeywords('wallabag');
286 297
287 /* 298 /*
288 * Front page
289 */
290 $pdf->AddPage();
291 $intro = '<h1>' . $this->title . '</h1>' . $this->getExportInformation('tcpdf');
292
293 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
294
295 /*
296 * Adding actual entries 299 * Adding actual entries
297 */ 300 */
298 foreach ($this->entries as $entry) { 301 foreach ($this->entries as $entry) {
@@ -300,6 +303,22 @@ class EntriesExport
300 $pdf->SetKeywords($tag->getLabel()); 303 $pdf->SetKeywords($tag->getLabel());
301 } 304 }
302 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
303 $pdf->AddPage(); 322 $pdf->AddPage();
304 $html = '<h1>' . $entry->getTitle() . '</h1>'; 323 $html = '<h1>' . $entry->getTitle() . '</h1>';
305 $html .= $entry->getContent(); 324 $html .= $entry->getContent();
@@ -307,6 +326,14 @@ class EntriesExport
307 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true); 326 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
308 } 327 }
309 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
310 // set image scale factor 337 // set image scale factor
311 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 338 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
312 339
@@ -316,7 +343,7 @@ class EntriesExport
316 [ 343 [
317 'Content-Description' => 'File Transfer', 344 'Content-Description' => 'File Transfer',
318 'Content-type' => 'application/pdf', 345 'Content-type' => 'application/pdf',
319 'Content-Disposition' => 'attachment; filename="' . $this->title . '.pdf"', 346 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.pdf"',
320 'Content-Transfer-Encoding' => 'binary', 347 'Content-Transfer-Encoding' => 'binary',
321 ] 348 ]
322 ); 349 );
@@ -362,7 +389,7 @@ class EntriesExport
362 200, 389 200,
363 [ 390 [
364 'Content-type' => 'application/csv', 391 'Content-type' => 'application/csv',
365 'Content-Disposition' => 'attachment; filename="' . $this->title . '.csv"', 392 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.csv"',
366 'Content-Transfer-Encoding' => 'UTF-8', 393 'Content-Transfer-Encoding' => 'UTF-8',
367 ] 394 ]
368 ); 395 );
@@ -380,7 +407,7 @@ class EntriesExport
380 200, 407 200,
381 [ 408 [
382 'Content-type' => 'application/json', 409 'Content-type' => 'application/json',
383 'Content-Disposition' => 'attachment; filename="' . $this->title . '.json"', 410 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.json"',
384 'Content-Transfer-Encoding' => 'UTF-8', 411 'Content-Transfer-Encoding' => 'UTF-8',
385 ] 412 ]
386 ); 413 );
@@ -398,7 +425,7 @@ class EntriesExport
398 200, 425 200,
399 [ 426 [
400 'Content-type' => 'application/xml', 427 'Content-type' => 'application/xml',
401 'Content-Disposition' => 'attachment; filename="' . $this->title . '.xml"', 428 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.xml"',
402 'Content-Transfer-Encoding' => 'UTF-8', 429 'Content-Transfer-Encoding' => 'UTF-8',
403 ] 430 ]
404 ); 431 );
@@ -424,7 +451,7 @@ class EntriesExport
424 200, 451 200,
425 [ 452 [
426 'Content-type' => 'text/plain', 453 'Content-type' => 'text/plain',
427 'Content-Disposition' => 'attachment; filename="' . $this->title . '.txt"', 454 'Content-Disposition' => 'attachment; filename="' . $this->getSanitizedFilename() . '.txt"',
428 'Content-Transfer-Encoding' => 'UTF-8', 455 'Content-Transfer-Encoding' => 'UTF-8',
429 ] 456 ]
430 ); 457 );
@@ -467,4 +494,15 @@ class EntriesExport
467 494
468 return str_replace('%IMAGE%', '', $info); 495 return str_replace('%IMAGE%', '', $info);
469 } 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 }
470} 508}