mirror of
https://github.com/wallabag/wallabag.git
synced 2024-06-02 13:29:22 +00:00
Rework on export
- all export now return a `HttpFoundation\Response` - return a 404 on unsupported format - add tests
This commit is contained in:
parent
03690d1387
commit
add597bad9
|
@ -51,6 +51,7 @@ parameters:
|
||||||
export_epub: true
|
export_epub: true
|
||||||
export_mobi: true
|
export_mobi: true
|
||||||
export_pdf: true
|
export_pdf: true
|
||||||
|
wallabag_url: http://v2.wallabag.org
|
||||||
|
|
||||||
# default user config
|
# default user config
|
||||||
items_on_page: 12
|
items_on_page: 12
|
||||||
|
|
|
@ -51,6 +51,7 @@ parameters:
|
||||||
export_epub: true
|
export_epub: true
|
||||||
export_mobi: true
|
export_mobi: true
|
||||||
export_pdf: true
|
export_pdf: true
|
||||||
|
wallabag_url: http://v2.wallabag.org
|
||||||
|
|
||||||
# default user config
|
# default user config
|
||||||
items_on_page: 12
|
items_on_page: 12
|
||||||
|
|
|
@ -51,6 +51,7 @@ parameters:
|
||||||
export_epub: true
|
export_epub: true
|
||||||
export_mobi: true
|
export_mobi: true
|
||||||
export_pdf: true
|
export_pdf: true
|
||||||
|
wallabag_url: http://v2.wallabag.org
|
||||||
|
|
||||||
# default user config
|
# default user config
|
||||||
items_on_page: 12
|
items_on_page: 12
|
||||||
|
|
|
@ -51,6 +51,7 @@ parameters:
|
||||||
export_epub: true
|
export_epub: true
|
||||||
export_mobi: true
|
export_mobi: true
|
||||||
export_pdf: true
|
export_pdf: true
|
||||||
|
wallabag_url: http://v2.wallabag.org
|
||||||
|
|
||||||
# default user config
|
# default user config
|
||||||
items_on_page: 12
|
items_on_page: 12
|
||||||
|
|
|
@ -57,7 +57,7 @@
|
||||||
"friendsofsymfony/oauth-server-bundle": "^1.4@dev",
|
"friendsofsymfony/oauth-server-bundle": "^1.4@dev",
|
||||||
"scheb/two-factor-bundle": "~1.4",
|
"scheb/two-factor-bundle": "~1.4",
|
||||||
"grandt/phpepub": "~4.0",
|
"grandt/phpepub": "~4.0",
|
||||||
"wallabag/phpMobi": "~1.0.0"
|
"wallabag/php-mobi": "~1.0.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"doctrine/doctrine-fixtures-bundle": "~2.2.0",
|
"doctrine/doctrine-fixtures-bundle": "~2.2.0",
|
||||||
|
|
27
composer.lock
generated
27
composer.lock
generated
|
@ -3796,41 +3796,52 @@
|
||||||
"time": "2015-11-05 12:49:06"
|
"time": "2015-11-05 12:49:06"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "wallabag/phpMobi",
|
"name": "wallabag/php-mobi",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/wallabag/phpMobi.git",
|
"url": "https://github.com/wallabag/phpMobi.git",
|
||||||
"reference": "5137696542f08f8e6a0603c01970c6d3eca9873d"
|
"reference": "1cd7d022fe6be838535d6bba917d19cc48dcf487"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/wallabag/phpMobi/zipball/5137696542f08f8e6a0603c01970c6d3eca9873d",
|
"url": "https://api.github.com/repos/wallabag/phpMobi/zipball/1cd7d022fe6be838535d6bba917d19cc48dcf487",
|
||||||
"reference": "5137696542f08f8e6a0603c01970c6d3eca9873d",
|
"reference": "1cd7d022fe6be838535d6bba917d19cc48dcf487",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.0"
|
"php": ">=5.3.0"
|
||||||
},
|
},
|
||||||
|
"replace": {
|
||||||
|
"wallabag/phpmobi": "*"
|
||||||
|
},
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"files": [
|
"files": [
|
||||||
"MOBIClass/MOBI.php"
|
"MOBIClass/MOBI.php"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"license": [
|
||||||
|
"Apache-2.0"
|
||||||
|
],
|
||||||
"authors": [
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Sander Kromwijk",
|
||||||
|
"email": "s.kromwijk@gmail.co",
|
||||||
|
"role": "Original developer"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "Nicolas Lœuillet",
|
"name": "Nicolas Lœuillet",
|
||||||
"email": "nicolas@loeuillet.org",
|
"email": "nicolas@loeuillet.org",
|
||||||
"homepage": "http://www.cdetc.fr"
|
"homepage": "http://www.cdetc.fr"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"description": "An experimental Mobipocket file creator in PHP.",
|
"description": "A Mobipocket file (.mobi) creator in PHP.",
|
||||||
"homepage": "https://github.com/wallabag/phpMobi",
|
"homepage": "https://github.com/wallabag/phpMobi",
|
||||||
"support": {
|
"support": {
|
||||||
"source": "https://github.com/wallabag/phpMobi/tree/1.0.0"
|
"source": "https://github.com/wallabag/phpMobi/tree/1.0.1"
|
||||||
},
|
},
|
||||||
"time": "2015-01-19 12:43:17"
|
"time": "2015-10-16 08:42:42"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "willdurand/hateoas",
|
"name": "willdurand/hateoas",
|
||||||
|
|
|
@ -4,62 +4,55 @@ namespace Wallabag\CoreBundle\Controller;
|
||||||
|
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Wallabag\CoreBundle\Entity\Entry;
|
use Wallabag\CoreBundle\Entity\Entry;
|
||||||
use Wallabag\CoreBundle\Helper\EntriesExport;
|
|
||||||
|
|
||||||
class ExportController extends Controller
|
class ExportController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Gets all entries for current user.
|
|
||||||
*
|
|
||||||
* @Route("/export/{category}.{format}", name="ebook", requirements={
|
|
||||||
* "_format": "epub|mobi|pdf|json|xml|txt|csv"
|
|
||||||
* })
|
|
||||||
*/
|
|
||||||
public function getEntriesAction($format, $category)
|
|
||||||
{
|
|
||||||
$repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
|
|
||||||
switch ($category) {
|
|
||||||
case 'all':
|
|
||||||
$method = 'All';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'unread':
|
|
||||||
$method = 'Unread';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'starred':
|
|
||||||
$method = 'Starred';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'archive':
|
|
||||||
$method = 'Archive';
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
$methodBuilder = 'getBuilderFor'.$method.'ByUser';
|
|
||||||
$qb = $repository->$methodBuilder($this->getUser()->getId());
|
|
||||||
$entries = $qb->getQuery()->getResult();
|
|
||||||
|
|
||||||
$export = new EntriesExport($entries);
|
|
||||||
$export->setMethod($method);
|
|
||||||
$export->exportAs($format);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets one entry content.
|
* Gets one entry content.
|
||||||
*
|
*
|
||||||
* @param Entry $entry
|
* @param Entry $entry
|
||||||
*
|
*
|
||||||
* @Route("/export/id/{id}.{format}", requirements={"id" = "\d+"}, name="ebook_entry")
|
* @Route("/export/{id}.{format}", requirements={"id" = "\d+"}, name="export_entry")
|
||||||
*/
|
*/
|
||||||
public function getEntryAction(Entry $entry, $format)
|
public function downloadEntryAction(Entry $entry, $format)
|
||||||
{
|
{
|
||||||
$export = new EntriesExport(array($entry));
|
try {
|
||||||
$export->setMethod('entry');
|
return $this->get('wallabag_core.helper.entries_export')
|
||||||
$export->exportAs($format);
|
->setEntries($entry)
|
||||||
|
->updateTitle('entry')
|
||||||
|
->exportAs($format);
|
||||||
|
} catch (\InvalidArgumentException $e) {
|
||||||
|
throw new NotFoundHttpException($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export all entries for current user.
|
||||||
|
*
|
||||||
|
* @Route("/export/{category}.{format}", name="export_entries", requirements={
|
||||||
|
* "_format": "epub|mobi|pdf|json|xml|txt|csv",
|
||||||
|
* "category": "all|unread|starred|archive"
|
||||||
|
* })
|
||||||
|
*/
|
||||||
|
public function downloadEntriesAction($format, $category)
|
||||||
|
{
|
||||||
|
$method = ucfirst($category);
|
||||||
|
$methodBuilder = 'getBuilderFor'.$method.'ByUser';
|
||||||
|
$entries = $this->getDoctrine()
|
||||||
|
->getRepository('WallabagCoreBundle:Entry')
|
||||||
|
->$methodBuilder($this->getUser()->getId())
|
||||||
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
|
|
||||||
|
try {
|
||||||
|
return $this->get('wallabag_core.helper.entries_export')
|
||||||
|
->setEntries($entries)
|
||||||
|
->updateTitle($method)
|
||||||
|
->exportAs($format);
|
||||||
|
} catch (\InvalidArgumentException $e) {
|
||||||
|
throw new NotFoundHttpException($e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,27 +4,51 @@ namespace Wallabag\CoreBundle\Helper;
|
||||||
|
|
||||||
use PHPePub\Core\EPub;
|
use PHPePub\Core\EPub;
|
||||||
use PHPePub\Core\Structure\OPF\DublinCore;
|
use PHPePub\Core\Structure\OPF\DublinCore;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
class EntriesExport
|
class EntriesExport
|
||||||
{
|
{
|
||||||
private $format;
|
private $wallabagUrl;
|
||||||
private $method;
|
private $logoPath;
|
||||||
private $title;
|
private $title = '';
|
||||||
private $entries;
|
private $entries = array();
|
||||||
private $authors = array('wallabag');
|
private $authors = array('wallabag');
|
||||||
private $language;
|
private $language = '';
|
||||||
private $tags;
|
private $tags = array();
|
||||||
|
private $footerTemplate = '<div style="text-align:center;">
|
||||||
|
<p>Produced by wallabag with %EXPORT_METHOD%</p>
|
||||||
|
<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>
|
||||||
|
</div';
|
||||||
|
|
||||||
public function __construct($entries)
|
/**
|
||||||
|
* @param string $wallabagUrl Wallabag instance url
|
||||||
|
* @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
|
||||||
|
*/
|
||||||
|
public function __construct($wallabagUrl, $logoPath)
|
||||||
{
|
{
|
||||||
|
$this->wallabagUrl = $wallabagUrl;
|
||||||
|
$this->logoPath = $logoPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define entries.
|
||||||
|
*
|
||||||
|
* @param array|Entry $entries An array of entries or one entry
|
||||||
|
*/
|
||||||
|
public function setEntries($entries)
|
||||||
|
{
|
||||||
|
if (!is_array($entries)) {
|
||||||
|
$this->language = $entries->getLanguage();
|
||||||
|
$entries = array($entries);
|
||||||
|
}
|
||||||
|
|
||||||
$this->entries = $entries;
|
$this->entries = $entries;
|
||||||
|
|
||||||
foreach ($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
$this->tags[] = $entry->getTags();
|
$this->tags[] = $entry->getTags();
|
||||||
}
|
}
|
||||||
if (count($entries) === 1) {
|
|
||||||
$this->language = $entries[0]->getLanguage();
|
return $this;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,29 +56,15 @@ class EntriesExport
|
||||||
*
|
*
|
||||||
* @param string $method Method to get articles
|
* @param string $method Method to get articles
|
||||||
*/
|
*/
|
||||||
public function setMethod($method)
|
public function updateTitle($method)
|
||||||
{
|
{
|
||||||
$this->method = $method;
|
$this->title = $method.' articles';
|
||||||
|
|
||||||
switch ($this->method) {
|
if ('entry' === $method) {
|
||||||
case 'All':
|
$this->title = $this->entries[0]->getTitle();
|
||||||
$this->title = 'All Articles';
|
|
||||||
break;
|
|
||||||
case 'Unread':
|
|
||||||
$this->title = 'Unread articles';
|
|
||||||
break;
|
|
||||||
case 'Starred':
|
|
||||||
$this->title = 'Starred articles';
|
|
||||||
break;
|
|
||||||
case 'Archive':
|
|
||||||
$this->title = 'Archived articles';
|
|
||||||
break;
|
|
||||||
case 'entry':
|
|
||||||
$this->title = $this->entries[0]->getTitle();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,30 +74,26 @@ class EntriesExport
|
||||||
*/
|
*/
|
||||||
public function exportAs($format)
|
public function exportAs($format)
|
||||||
{
|
{
|
||||||
$this->format = $format;
|
switch ($format) {
|
||||||
|
|
||||||
switch ($this->format) {
|
|
||||||
case 'epub':
|
case 'epub':
|
||||||
$this->produceEpub();
|
return $this->produceEpub();
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mobi':
|
case 'mobi':
|
||||||
$this->produceMobi();
|
return $this->produceMobi();
|
||||||
break;
|
|
||||||
|
|
||||||
case 'pdf':
|
case 'pdf':
|
||||||
$this->producePDF();
|
return $this->producePDF();
|
||||||
break;
|
|
||||||
|
|
||||||
case 'csv':
|
case 'csv':
|
||||||
$this->produceCSV();
|
return $this->produceCSV();
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use PHPePub to dump a .epub file.
|
||||||
|
*/
|
||||||
private function produceEpub()
|
private function produceEpub()
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
@ -98,7 +104,7 @@ class EntriesExport
|
||||||
."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
|
."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
|
||||||
.'<head>'
|
.'<head>'
|
||||||
."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
|
."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
|
||||||
.'<title>'._('wallabag articles book')."</title>\n"
|
."<title>wallabag articles book</title>\n"
|
||||||
."</head>\n"
|
."</head>\n"
|
||||||
."<body>\n";
|
."<body>\n";
|
||||||
|
|
||||||
|
@ -111,17 +117,21 @@ class EntriesExport
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$book->setTitle($this->title);
|
$book->setTitle($this->title);
|
||||||
$book->setIdentifier($this->title, EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID.
|
// Could also be the ISBN number, prefered for published books, or a UUID.
|
||||||
$book->setLanguage($this->language); // 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.
|
$book->setIdentifier($this->title, EPub::IDENTIFIER_URI);
|
||||||
$book->setDescription(_('Some articles saved on my wallabag'));
|
// 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.
|
||||||
|
$book->setLanguage($this->language);
|
||||||
|
$book->setDescription('Some articles saved on my wallabag');
|
||||||
|
|
||||||
foreach ($this->authors as $author) {
|
foreach ($this->authors as $author) {
|
||||||
$book->setAuthor($author, $author);
|
$book->setAuthor($author, $author);
|
||||||
}
|
}
|
||||||
|
|
||||||
$book->setPublisher('wallabag', 'wallabag'); // I hope this is a non existant address :)
|
// I hope this is a non existant address :)
|
||||||
$book->setDate(time()); // Strictly not needed as the book date defaults to time().
|
$book->setPublisher('wallabag', 'wallabag');
|
||||||
$book->setSourceURL("http://$_SERVER[HTTP_HOST]");
|
// Strictly not needed as the book date defaults to time().
|
||||||
|
$book->setDate(time());
|
||||||
|
$book->setSourceURL($this->wallabagUrl);
|
||||||
|
|
||||||
$book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
|
$book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
|
||||||
$book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');
|
$book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');
|
||||||
|
@ -129,12 +139,11 @@ class EntriesExport
|
||||||
/*
|
/*
|
||||||
* Front page
|
* Front page
|
||||||
*/
|
*/
|
||||||
|
if (file_exists($this->logoPath)) {
|
||||||
|
$book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png');
|
||||||
|
}
|
||||||
|
|
||||||
$book->setCoverImage('Cover.png', file_get_contents('themes/_global/img/appicon/apple-touch-icon-152.png'), 'image/png');
|
$book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
|
||||||
|
|
||||||
$cover = $content_start.'<div style="text-align:center;"><p>'._('Produced by wallabag with PHPePub').'</p><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></div>'.$bookEnd;
|
|
||||||
|
|
||||||
$book->addChapter('Notices', 'Cover2.html', $cover);
|
|
||||||
|
|
||||||
$book->buildTOC();
|
$book->buildTOC();
|
||||||
|
|
||||||
|
@ -142,18 +151,31 @@ class EntriesExport
|
||||||
* Adding actual entries
|
* Adding actual entries
|
||||||
*/
|
*/
|
||||||
|
|
||||||
foreach ($this->entries as $entry) { //set tags as subjects
|
// set tags as subjects
|
||||||
foreach ($this->tags as $tag) {
|
foreach ($this->entries as $entry) {
|
||||||
$book->setSubject($tag['value']);
|
foreach ($this->tags as $tag) {
|
||||||
}
|
$book->setSubject($tag['value']);
|
||||||
|
}
|
||||||
|
|
||||||
$chapter = $content_start.$entry->getContent().$bookEnd;
|
$chapter = $content_start.$entry->getContent().$bookEnd;
|
||||||
$book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
|
$book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
|
||||||
}
|
}
|
||||||
$book->finalize();
|
|
||||||
$book->sendBook($this->title);
|
return Response::create(
|
||||||
|
$book->getBook(),
|
||||||
|
200,
|
||||||
|
array(
|
||||||
|
'Content-Description' => 'File Transfer',
|
||||||
|
'Content-type' => 'application/epub+zip',
|
||||||
|
'Content-Disposition' => 'attachment; filename="'.$this->title.'.epub"',
|
||||||
|
'Content-Transfer-Encoding' => 'binary',
|
||||||
|
)
|
||||||
|
)->send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use PHPMobi to dump a .mobi file.
|
||||||
|
*/
|
||||||
private function produceMobi()
|
private function produceMobi()
|
||||||
{
|
{
|
||||||
$mobi = new \MOBI();
|
$mobi = new \MOBI();
|
||||||
|
@ -162,7 +184,6 @@ class EntriesExport
|
||||||
/*
|
/*
|
||||||
* Book metadata
|
* Book metadata
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$content->set('title', $this->title);
|
$content->set('title', $this->title);
|
||||||
$content->set('author', implode($this->authors));
|
$content->set('author', implode($this->authors));
|
||||||
$content->set('subject', $this->title);
|
$content->set('subject', $this->title);
|
||||||
|
@ -170,15 +191,15 @@ class EntriesExport
|
||||||
/*
|
/*
|
||||||
* Front page
|
* Front page
|
||||||
*/
|
*/
|
||||||
|
$content->appendParagraph($this->getExportInformation('PHPMobi'));
|
||||||
$content->appendParagraph('<div style="text-align:center;" ><p>'._('Produced by wallabag with PHPMobi').'</p><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></div>');
|
if (file_exists($this->logoPath)) {
|
||||||
$content->appendImage(imagecreatefrompng('themes/_global/img/appicon/apple-touch-icon-152.png'));
|
$content->appendImage(imagecreatefrompng($this->logoPath));
|
||||||
|
}
|
||||||
$content->appendPageBreak();
|
$content->appendPageBreak();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Adding actual entries
|
* Adding actual entries
|
||||||
*/
|
*/
|
||||||
|
|
||||||
foreach ($this->entries as $entry) {
|
foreach ($this->entries as $entry) {
|
||||||
$content->appendChapterTitle($entry->getTitle());
|
$content->appendChapterTitle($entry->getTitle());
|
||||||
$content->appendParagraph($entry->getContent());
|
$content->appendParagraph($entry->getContent());
|
||||||
|
@ -189,10 +210,22 @@ class EntriesExport
|
||||||
// the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
|
// the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
|
||||||
$this->title = preg_replace('/[^A-Za-z0-9\-]/', '', $this->title);
|
$this->title = preg_replace('/[^A-Za-z0-9\-]/', '', $this->title);
|
||||||
|
|
||||||
// we offer file to download
|
return Response::create(
|
||||||
$mobi->download($this->title.'.mobi');
|
$mobi->toString(),
|
||||||
|
200,
|
||||||
|
array(
|
||||||
|
'Accept-Ranges' => 'bytes',
|
||||||
|
'Content-Description' => 'File Transfer',
|
||||||
|
'Content-type' => 'application/x-mobipocket-ebook',
|
||||||
|
'Content-Disposition' => 'attachment; filename="'.$this->title.'.mobi"',
|
||||||
|
'Content-Transfer-Encoding' => 'binary',
|
||||||
|
)
|
||||||
|
)->send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use TCPDF to dump a .pdf file.
|
||||||
|
*/
|
||||||
private function producePDF()
|
private function producePDF()
|
||||||
{
|
{
|
||||||
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
|
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
|
||||||
|
@ -200,7 +233,6 @@ class EntriesExport
|
||||||
/*
|
/*
|
||||||
* Book metadata
|
* Book metadata
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$pdf->SetCreator(PDF_CREATOR);
|
$pdf->SetCreator(PDF_CREATOR);
|
||||||
$pdf->SetAuthor('wallabag');
|
$pdf->SetAuthor('wallabag');
|
||||||
$pdf->SetTitle($this->title);
|
$pdf->SetTitle($this->title);
|
||||||
|
@ -210,19 +242,14 @@ class EntriesExport
|
||||||
/*
|
/*
|
||||||
* Front page
|
* Front page
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$pdf->AddPage();
|
$pdf->AddPage();
|
||||||
$intro = '<h1>'.$this->title.'</h1><div style="text-align:center;" >
|
$intro = '<h1>'.$this->title.'</h1>'.$this->getExportInformation('tcpdf');
|
||||||
<p>'._('Produced by wallabag with tcpdf').'</p>
|
|
||||||
<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>
|
|
||||||
<img src="themes/_global/img/appicon/apple-touch-icon-152.png" /></div>';
|
|
||||||
|
|
||||||
$pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
|
$pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Adding actual entries
|
* Adding actual entries
|
||||||
*/
|
*/
|
||||||
|
|
||||||
foreach ($this->entries as $entry) {
|
foreach ($this->entries as $entry) {
|
||||||
foreach ($this->tags as $tag) {
|
foreach ($this->tags as $tag) {
|
||||||
$pdf->SetKeywords($tag['value']);
|
$pdf->SetKeywords($tag['value']);
|
||||||
|
@ -231,33 +258,82 @@ class EntriesExport
|
||||||
$pdf->AddPage();
|
$pdf->AddPage();
|
||||||
$html = '<h1>'.$entry->getTitle().'</h1>';
|
$html = '<h1>'.$entry->getTitle().'</h1>';
|
||||||
$html .= $entry->getContent();
|
$html .= $entry->getContent();
|
||||||
|
|
||||||
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
|
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set image scale factor
|
// set image scale factor
|
||||||
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
|
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
|
||||||
|
|
||||||
$pdf->Output($this->title.'.pdf', 'D');
|
return Response::create(
|
||||||
|
$pdf->Output('', 'S'),
|
||||||
|
200,
|
||||||
|
array(
|
||||||
|
'Content-Description' => 'File Transfer',
|
||||||
|
'Content-type' => 'application/pdf',
|
||||||
|
'Content-Disposition' => 'attachment; filename="'.$this->title.'.pdf"',
|
||||||
|
'Content-Transfer-Encoding' => 'binary',
|
||||||
|
)
|
||||||
|
)->send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspired from CsvFileDumper.
|
||||||
|
*/
|
||||||
private function produceCSV()
|
private function produceCSV()
|
||||||
{
|
{
|
||||||
header('Content-type: application/csv');
|
$delimiter = ';';
|
||||||
header('Content-Disposition: attachment; filename="'.$this->title.'.csv"');
|
$enclosure = '"';
|
||||||
header('Content-Transfer-Encoding: UTF-8');
|
$handle = fopen('php://memory', 'rb+');
|
||||||
|
|
||||||
$output = fopen('php://output', 'a');
|
fputcsv($handle, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'), $delimiter, $enclosure);
|
||||||
|
|
||||||
fputcsv($output, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'));
|
|
||||||
foreach ($this->entries as $entry) {
|
foreach ($this->entries as $entry) {
|
||||||
fputcsv($output, array($entry->getTitle(),
|
fputcsv(
|
||||||
$entry->getURL(),
|
$handle,
|
||||||
$entry->getContent(),
|
array(
|
||||||
implode(', ', $entry->getTags()->toArray()),
|
$entry->getTitle(),
|
||||||
$entry->getMimetype(),
|
$entry->getURL(),
|
||||||
$entry->getLanguage(), ));
|
$entry->getContent(),
|
||||||
|
implode(', ', $entry->getTags()->toArray()),
|
||||||
|
$entry->getMimetype(),
|
||||||
|
$entry->getLanguage(),
|
||||||
|
),
|
||||||
|
$delimiter,
|
||||||
|
$enclosure
|
||||||
|
);
|
||||||
}
|
}
|
||||||
fclose($output);
|
|
||||||
exit();
|
rewind($handle);
|
||||||
|
$output = stream_get_contents($handle);
|
||||||
|
fclose($handle);
|
||||||
|
|
||||||
|
return Response::create(
|
||||||
|
$output,
|
||||||
|
200,
|
||||||
|
array(
|
||||||
|
'Content-type' => 'application/csv',
|
||||||
|
'Content-Disposition' => 'attachment; filename="'.$this->title.'.csv"',
|
||||||
|
'Content-Transfer-Encoding' => 'UTF-8',
|
||||||
|
)
|
||||||
|
)->send();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a kind of footer / information for the epub.
|
||||||
|
*
|
||||||
|
* @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getExportInformation($type)
|
||||||
|
{
|
||||||
|
$info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate);
|
||||||
|
|
||||||
|
if ('tcpdf' === $type) {
|
||||||
|
return str_replace('%IMAGE%', '<img src="'.$this->logoPath.'" />', $info);
|
||||||
|
}
|
||||||
|
|
||||||
|
return str_replace('%IMAGE%', '', $info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,3 +64,9 @@ services:
|
||||||
- %language%
|
- %language%
|
||||||
tags:
|
tags:
|
||||||
- { name: kernel.event_subscriber }
|
- { name: kernel.event_subscriber }
|
||||||
|
|
||||||
|
wallabag_core.helper.entries_export:
|
||||||
|
class: Wallabag\CoreBundle\Helper\EntriesExport
|
||||||
|
arguments:
|
||||||
|
- %wallabag_url%
|
||||||
|
- src/Wallabag/CoreBundle/Resources/views/themes/_global/public/img/appicon/apple-touch-icon-152.png
|
||||||
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Wallabag\CoreBundle\Tests\Controller;
|
||||||
|
|
||||||
|
use Wallabag\CoreBundle\Tests\WallabagCoreTestCase;
|
||||||
|
|
||||||
|
class ExportControllerTest extends WallabagCoreTestCase
|
||||||
|
{
|
||||||
|
public function testLogin()
|
||||||
|
{
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
$client->request('GET', '/export/unread.csv');
|
||||||
|
|
||||||
|
$this->assertEquals(302, $client->getResponse()->getStatusCode());
|
||||||
|
$this->assertContains('login', $client->getResponse()->headers->get('location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnknownCategoryExport()
|
||||||
|
{
|
||||||
|
$this->logInAs('admin');
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
$crawler = $client->request('GET', '/export/awesomeness.epub');
|
||||||
|
|
||||||
|
$this->assertEquals(404, $client->getResponse()->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnknownFormatExport()
|
||||||
|
{
|
||||||
|
$this->logInAs('admin');
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
$crawler = $client->request('GET', '/export/unread.xslx');
|
||||||
|
|
||||||
|
$this->assertEquals(404, $client->getResponse()->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEpubExport()
|
||||||
|
{
|
||||||
|
$this->logInAs('admin');
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
$crawler = $client->request('GET', '/export/archive.epub');
|
||||||
|
ob_end_clean();
|
||||||
|
|
||||||
|
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||||
|
|
||||||
|
$headers = $client->getResponse()->headers;
|
||||||
|
$this->assertEquals('application/epub+zip', $headers->get('content-type'));
|
||||||
|
$this->assertEquals('attachment; filename="Archive articles.epub"', $headers->get('content-disposition'));
|
||||||
|
$this->assertEquals('binary', $headers->get('content-transfer-encoding'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMobiExport()
|
||||||
|
{
|
||||||
|
$this->logInAs('admin');
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
$content = $client->getContainer()
|
||||||
|
->get('doctrine.orm.entity_manager')
|
||||||
|
->getRepository('WallabagCoreBundle:Entry')
|
||||||
|
->findOneByUsernameAndNotArchived('admin');
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
$crawler = $client->request('GET', '/export/'.$content->getId().'.mobi');
|
||||||
|
ob_end_clean();
|
||||||
|
|
||||||
|
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||||
|
|
||||||
|
$headers = $client->getResponse()->headers;
|
||||||
|
$this->assertEquals('application/x-mobipocket-ebook', $headers->get('content-type'));
|
||||||
|
$this->assertEquals('attachment; filename="testtitleentry1.mobi"', $headers->get('content-disposition'));
|
||||||
|
$this->assertEquals('binary', $headers->get('content-transfer-encoding'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPdfExport()
|
||||||
|
{
|
||||||
|
$this->logInAs('admin');
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
$crawler = $client->request('GET', '/export/all.pdf');
|
||||||
|
ob_end_clean();
|
||||||
|
|
||||||
|
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||||
|
|
||||||
|
$headers = $client->getResponse()->headers;
|
||||||
|
$this->assertEquals('application/pdf', $headers->get('content-type'));
|
||||||
|
$this->assertEquals('attachment; filename="All articles.pdf"', $headers->get('content-disposition'));
|
||||||
|
$this->assertEquals('binary', $headers->get('content-transfer-encoding'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCsvExport()
|
||||||
|
{
|
||||||
|
$this->logInAs('admin');
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
$crawler = $client->request('GET', '/export/unread.csv');
|
||||||
|
ob_end_clean();
|
||||||
|
|
||||||
|
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||||
|
|
||||||
|
$headers = $client->getResponse()->headers;
|
||||||
|
$this->assertEquals('application/csv', $headers->get('content-type'));
|
||||||
|
$this->assertEquals('attachment; filename="Unread articles.csv"', $headers->get('content-disposition'));
|
||||||
|
$this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
|
||||||
|
|
||||||
|
$csv = str_getcsv($client->getResponse()->getContent(), "\n");
|
||||||
|
|
||||||
|
$this->assertGreaterThan(1, $csv);
|
||||||
|
$this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue