Merge pull request #1422 from wallabag/v2-ebook

V2 – Export entries
This commit is contained in:
Nicolas Lœuillet 2015-11-09 16:45:48 +01:00
commit 0a0c600887
16 changed files with 1138 additions and 8 deletions

View file

@ -51,6 +51,7 @@ parameters:
export_epub: true
export_mobi: true
export_pdf: true
wallabag_url: http://v2.wallabag.org
# default user config
items_on_page: 12

View file

@ -51,6 +51,7 @@ parameters:
export_epub: true
export_mobi: true
export_pdf: true
wallabag_url: http://v2.wallabag.org
# default user config
items_on_page: 12

View file

@ -51,6 +51,7 @@ parameters:
export_epub: true
export_mobi: true
export_pdf: true
wallabag_url: http://v2.wallabag.org
# default user config
items_on_page: 12

View file

@ -51,6 +51,7 @@ parameters:
export_epub: true
export_mobi: true
export_pdf: true
wallabag_url: http://v2.wallabag.org
# default user config
items_on_page: 12

View file

@ -55,7 +55,9 @@
"j0k3r/graby": "~1.0",
"friendsofsymfony/user-bundle": "dev-master",
"friendsofsymfony/oauth-server-bundle": "^1.4@dev",
"scheb/two-factor-bundle": "~1.4"
"scheb/two-factor-bundle": "~1.4",
"grandt/phpepub": "~4.0",
"wallabag/php-mobi": "~1.0.0"
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "~2.2.0",
@ -63,6 +65,12 @@
"phpunit/phpunit": "~4.4",
"symfony/phpunit-bridge": "~2.7.0"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/wallabag/phpMobi"
}
],
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",

353
composer.lock generated
View file

@ -4,8 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "6bd09434f83c7e6b5e1c75fddbd7608b",
"content-hash": "d07d54c4cc6f4f4947c652bd659af02e",
"hash": "a9ec461e17166dcda1563dd55f6ff861",
"packages": [
{
"name": "doctrine/annotations",
@ -1088,6 +1087,244 @@
],
"time": "2015-11-03 10:24:23"
},
{
"name": "grandt/binstring",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/Grandt/PHPBinString.git",
"reference": "825fe2ac8a68190f651fc2dbc07b6edde18bc431"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Grandt/PHPBinString/zipball/825fe2ac8a68190f651fc2dbc07b6edde18bc431",
"reference": "825fe2ac8a68190f651fc2dbc07b6edde18bc431",
"shasum": ""
},
"require": {
"php": ">=5.0"
},
"type": "library",
"autoload": {
"classmap": [
"BinString.php",
"BinStringStatic.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-2.1"
],
"authors": [
{
"name": "A. Grandt",
"email": "php@grandt.com",
"role": "Developer"
}
],
"description": "A class for working around the use of mbstring.func_override",
"homepage": "https://github.com/Grandt/PHPBinString",
"keywords": [
"binary strings",
"mbstring"
],
"time": "2015-08-13 06:14:41"
},
{
"name": "grandt/phpepub",
"version": "4.0.3",
"source": {
"type": "git",
"url": "https://github.com/Grandt/PHPePub.git",
"reference": "dee0c5549a8d2c6bf6a1ad5b4ee21d245b711fca"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Grandt/PHPePub/zipball/dee0c5549a8d2c6bf6a1ad5b4ee21d245b711fca",
"reference": "dee0c5549a8d2c6bf6a1ad5b4ee21d245b711fca",
"shasum": ""
},
"require": {
"grandt/phpresizegif": ">=1.0.3",
"grandt/relativepath": ">=1.0.1",
"php": ">=5.3.0",
"phpzip/phpzip": ">=2.0.7"
},
"type": "library",
"autoload": {
"psr-4": {
"PHPePub\\": "src/PHPePub"
},
"classmap": [
"src/lib.uuid.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-2.1"
],
"authors": [
{
"name": "A. Grandt",
"email": "php@grandt.com",
"homepage": "http://grandt.com",
"role": "Developer"
}
],
"description": "Package to create and stream e-books in the ePub 2.0 and 3.0 formats.",
"homepage": "https://github.com/Grandt/PHPZip",
"keywords": [
"e-book",
"epub"
],
"time": "2015-09-15 08:47:09"
},
{
"name": "grandt/phpresizegif",
"version": "1.0.3",
"source": {
"type": "git",
"url": "https://github.com/Grandt/PHPResizeGif.git",
"reference": "775f6810fcda2fd1d8ca881d44a80c8d310ae7fe"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Grandt/PHPResizeGif/zipball/775f6810fcda2fd1d8ca881d44a80c8d310ae7fe",
"reference": "775f6810fcda2fd1d8ca881d44a80c8d310ae7fe",
"shasum": ""
},
"require": {
"grandt/binstring": ">=0.2.0",
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"grandt\\ResizeGif\\": "src/ResizeGif",
"grandt\\ResizeGif\\Files\\": "src/ResizeGif/Files",
"grandt\\ResizeGif\\Structure\\": "src/ResizeGif/Structure",
"grandt\\ResizeGif\\Debug\\": "src/ResizeGif/Debug"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-2.1"
],
"authors": [
{
"name": "A. Grandt",
"email": "php@grandt.com",
"homepage": "http://grandt.com",
"role": "Developer"
}
],
"description": "GIF89a compliant Gif resizer, including transparency and optimized gifs with sub sized elements.",
"homepage": "https://github.com/Grandt/PHPResizeGif",
"keywords": [
"GIF89a",
"animated gif",
"gif",
"resize"
],
"time": "2015-05-10 10:52:24"
},
{
"name": "grandt/phpzipmerge",
"version": "1.0.4",
"source": {
"type": "git",
"url": "https://github.com/Grandt/PHPZipMerge.git",
"reference": "0b1273d3c2dbfe244904158b1dbd65a663264fb9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Grandt/PHPZipMerge/zipball/0b1273d3c2dbfe244904158b1dbd65a663264fb9",
"reference": "0b1273d3c2dbfe244904158b1dbd65a663264fb9",
"shasum": ""
},
"require": {
"grandt/binstring": ">=1.0.0",
"grandt/relativepath": ">=1.0.1",
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"ZipMerge\\": "src/ZipMerge"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-2.1"
],
"authors": [
{
"name": "A. Grandt",
"email": "php@grandt.com",
"homepage": "http://grandt.com",
"role": "Developer"
},
{
"name": "Greg Kappatos",
"homepage": "http://websiteconnect.com.au",
"role": "Developer"
}
],
"description": "Merge and stream multiple Zip files on the fly.",
"homepage": "https://github.com/Grandt/PHPZipMerge",
"keywords": [
"archive",
"compressed",
"compression",
"merge",
"phpzip",
"pkzip",
"stream",
"zip"
],
"time": "2015-08-18 13:49:33"
},
{
"name": "grandt/relativepath",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/Grandt/PHPRelativePath.git",
"reference": "19541133c24143b6295688472c54dd6ed15a5462"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Grandt/PHPRelativePath/zipball/19541133c24143b6295688472c54dd6ed15a5462",
"reference": "19541133c24143b6295688472c54dd6ed15a5462",
"shasum": ""
},
"require": {
"php": ">=5.0"
},
"type": "library",
"autoload": {
"classmap": [
"RelativePath.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-2.1"
],
"authors": [
{
"name": "A. Grandt",
"email": "php@grandt.com",
"role": "Developer"
}
],
"description": "A class for cleaning up/collapsing relative paths. Like real_path, but without the need for the path to exist on the filesystem.",
"homepage": "https://github.com/Grandt/PHPRelativePath",
"keywords": [
"file path"
],
"time": "2015-05-14 08:18:23"
},
{
"name": "guzzlehttp/guzzle",
"version": "5.3.0",
@ -2523,6 +2760,67 @@
],
"time": "2015-07-25 16:39:46"
},
{
"name": "phpzip/phpzip",
"version": "2.0.7",
"source": {
"type": "git",
"url": "https://github.com/Grandt/PHPZip.git",
"reference": "a43a7ce8b2f21050f8b143876c5c1661b0d65306"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Grandt/PHPZip/zipball/a43a7ce8b2f21050f8b143876c5c1661b0d65306",
"reference": "a43a7ce8b2f21050f8b143876c5c1661b0d65306",
"shasum": ""
},
"require": {
"grandt/binstring": ">=0.2.0",
"grandt/phpzipmerge": ">=1.0.3",
"grandt/relativepath": ">=1.0.1",
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"PHPZip\\Zip\\": "src/Zip"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-2.1"
],
"authors": [
{
"name": "Adam Schmalhofer",
"email": "Adam.Schmalhofer@gmx.de",
"role": "Developer"
},
{
"name": "A. Grandt",
"email": "php@grandt.com",
"homepage": "http://grandt.com",
"role": "Developer"
},
{
"name": "Greg Kappatos",
"homepage": "http://websiteconnect.com.au",
"role": "Developer"
}
],
"description": "Package to create and stream archives of compressed files in ZIP format with PHP 5.3+",
"homepage": "https://github.com/Grandt/PHPZip",
"keywords": [
"archive",
"compressed",
"compression",
"phpzip",
"pkzip",
"stream",
"zip"
],
"time": "2015-04-30 06:45:53"
},
{
"name": "psr/log",
"version": "1.0.0",
@ -3496,6 +3794,55 @@
],
"time": "2015-11-05 12:49:06"
},
{
"name": "wallabag/php-mobi",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/wallabag/php-mobi.git",
"reference": "1cd7d022fe6be838535d6bba917d19cc48dcf487"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/wallabag/php-mobi/zipball/1cd7d022fe6be838535d6bba917d19cc48dcf487",
"reference": "1cd7d022fe6be838535d6bba917d19cc48dcf487",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"replace": {
"wallabag/phpmobi": "*"
},
"type": "library",
"autoload": {
"files": [
"MOBIClass/MOBI.php"
]
},
"license": [
"Apache-2.0"
],
"authors": [
{
"name": "Sander Kromwijk",
"email": "s.kromwijk@gmail.co",
"role": "Original developer"
},
{
"name": "Nicolas Lœuillet",
"email": "nicolas@loeuillet.org",
"homepage": "http://www.cdetc.fr"
}
],
"description": "A Mobipocket file (.mobi) creator in PHP.",
"homepage": "https://github.com/wallabag/phpMobi",
"support": {
"source": "https://github.com/wallabag/php-mobi/tree/1.0.1",
"issues": "https://github.com/wallabag/php-mobi/issues"
},
"time": "2015-10-16 08:42:42"
},
{
"name": "willdurand/hateoas",
"version": "v2.6.0",
@ -3602,7 +3949,7 @@
],
"authors": [
{
"name": "William Durand",
"name": "William DURAND",
"email": "william.durand1@gmail.com"
}
],

View file

@ -0,0 +1,65 @@
<?php
namespace Wallabag\CoreBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Wallabag\CoreBundle\Entity\Entry;
/**
* The try/catch can be removed once all formats will be implemented.
* Still need implementation: txt.
*/
class ExportController extends Controller
{
/**
* Gets one entry content.
*
* @param Entry $entry
*
* @Route("/export/{id}.{format}", name="export_entry", requirements={
* "format": "epub|mobi|pdf|json|xml|txt|csv",
* "id": "\d+"
* })
*/
public function downloadEntryAction(Entry $entry, $format)
{
try {
return $this->get('wallabag_core.helper.entries_export')
->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());
}
}
}

View file

@ -19,6 +19,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
$entry1->setUrl('http://0.0.0.0');
$entry1->setReadingTime(11);
$entry1->setDomainName('domain.io');
$entry1->setMimetype('text/html');
$entry1->setTitle('test title entry1');
$entry1->setContent('This is my content /o/');
$entry1->setLanguage('en');
@ -31,6 +32,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
$entry2->setUrl('http://0.0.0.0');
$entry2->setReadingTime(1);
$entry2->setDomainName('domain.io');
$entry2->setMimetype('text/html');
$entry2->setTitle('test title entry2');
$entry2->setContent('This is my content /o/');
$entry2->setLanguage('fr');
@ -43,6 +45,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
$entry3->setUrl('http://0.0.0.0');
$entry3->setReadingTime(1);
$entry3->setDomainName('domain.io');
$entry3->setMimetype('text/html');
$entry3->setTitle('test title entry3');
$entry3->setContent('This is my content /o/');
$entry3->setLanguage('en');
@ -63,6 +66,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
$entry4->setUrl('http://0.0.0.0');
$entry4->setReadingTime(12);
$entry4->setDomainName('domain.io');
$entry4->setMimetype('text/html');
$entry4->setTitle('test title entry4');
$entry4->setContent('This is my content /o/');
$entry4->setLanguage('en');
@ -83,6 +87,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
$entry5->setUrl('http://0.0.0.0');
$entry5->setReadingTime(12);
$entry5->setDomainName('domain.io');
$entry5->setMimetype('text/html');
$entry5->setTitle('test title entry5');
$entry5->setContent('This is my content /o/');
$entry5->setStarred(true);
@ -97,6 +102,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
$entry6->setUrl('http://0.0.0.0');
$entry6->setReadingTime(12);
$entry6->setDomainName('domain.io');
$entry6->setMimetype('text/html');
$entry6->setTitle('test title entry6');
$entry6->setContent('This is my content /o/');
$entry6->setArchived(true);

View file

@ -6,6 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Hateoas\Configuration\Annotation as Hateoas;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\XmlRoot;
use Wallabag\UserBundle\Entity\User;
@ -27,6 +28,8 @@ class Entry
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Groups({"entries_for_user", "export_all"})
*/
private $id;
@ -34,6 +37,8 @@ class Entry
* @var string
*
* @ORM\Column(name="title", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $title;
@ -42,6 +47,8 @@ class Entry
*
* @Assert\NotBlank()
* @ORM\Column(name="url", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $url;
@ -49,6 +56,8 @@ class Entry
* @var bool
*
* @ORM\Column(name="is_archived", type="boolean")
*
* @Groups({"entries_for_user", "export_all"})
*/
private $isArchived = false;
@ -56,6 +65,8 @@ class Entry
* @var bool
*
* @ORM\Column(name="is_starred", type="boolean")
*
* @Groups({"entries_for_user", "export_all"})
*/
private $isStarred = false;
@ -63,6 +74,8 @@ class Entry
* @var string
*
* @ORM\Column(name="content", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $content;
@ -70,6 +83,8 @@ class Entry
* @var date
*
* @ORM\Column(name="created_at", type="datetime")
*
* @Groups({"export_all"})
*/
private $createdAt;
@ -77,6 +92,8 @@ class Entry
* @var date
*
* @ORM\Column(name="updated_at", type="datetime")
*
* @Groups({"export_all"})
*/
private $updatedAt;
@ -84,6 +101,8 @@ class Entry
* @var string
*
* @ORM\Column(name="comments", type="text", nullable=true)
*
* @Groups({"export_all"})
*/
private $comments;
@ -91,6 +110,8 @@ class Entry
* @var string
*
* @ORM\Column(name="mimetype", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $mimetype;
@ -98,6 +119,8 @@ class Entry
* @var string
*
* @ORM\Column(name="language", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $language;
@ -105,6 +128,8 @@ class Entry
* @var int
*
* @ORM\Column(name="reading_time", type="integer", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $readingTime;
@ -112,6 +137,8 @@ class Entry
* @var string
*
* @ORM\Column(name="domain_name", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $domainName;
@ -119,6 +146,8 @@ class Entry
* @var string
*
* @ORM\Column(name="preview_picture", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $previewPicture;
@ -126,17 +155,23 @@ class Entry
* @var bool
*
* @ORM\Column(name="is_public", type="boolean", nullable=true, options={"default" = false})
*
* @Groups({"export_all"})
*/
private $isPublic;
/**
* @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
*
* @Groups({"export_all"})
*/
private $user;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
* @ORM\JoinTable
*
* @Groups({"entries_for_user", "export_all"})
*/
private $tags;

View file

@ -0,0 +1,394 @@
<?php
namespace Wallabag\CoreBundle\Helper;
use PHPePub\Core\EPub;
use PHPePub\Core\Structure\OPF\DublinCore;
use Symfony\Component\HttpFoundation\Response;
use JMS\Serializer;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializationContext;
/**
* This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest.
*/
class EntriesExport
{
private $wallabagUrl;
private $logoPath;
private $title = '';
private $entries = array();
private $authors = array('wallabag');
private $language = '';
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';
/**
* @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;
foreach ($entries as $entry) {
$this->tags[] = $entry->getTags();
}
return $this;
}
/**
* Sets the category of which we want to get articles, or just one entry.
*
* @param string $method Method to get articles
*/
public function updateTitle($method)
{
$this->title = $method.' articles';
if ('entry' === $method) {
$this->title = $this->entries[0]->getTitle();
}
return $this;
}
/**
* Sets the output format.
*
* @param string $format
*/
public function exportAs($format)
{
switch ($format) {
case 'epub':
return $this->produceEpub();
case 'mobi':
return $this->produceMobi();
case 'pdf':
return $this->producePDF();
case 'csv':
return $this->produceCSV();
case 'json':
return $this->produceJSON();
case 'xml':
return $this->produceXML();
}
throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
}
/**
* Use PHPePub to dump a .epub file.
*/
private function produceEpub()
{
/*
* Start and End of the book
*/
$content_start =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
.'<head>'
."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
."<title>wallabag articles book</title>\n"
."</head>\n"
."<body>\n";
$bookEnd = "</body>\n</html>\n";
$book = new EPub(EPub::BOOK_VERSION_EPUB3);
/*
* Book metadata
*/
$book->setTitle($this->title);
// Could also be the ISBN number, prefered for published books, or a UUID.
$book->setIdentifier($this->title, EPub::IDENTIFIER_URI);
// 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) {
$book->setAuthor($author, $author);
}
// I hope this is a non existant address :)
$book->setPublisher('wallabag', 'wallabag');
// 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, 'wallabag');
/*
* Front page
*/
if (file_exists($this->logoPath)) {
$book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png');
}
$book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
$book->buildTOC();
/*
* Adding actual entries
*/
// set tags as subjects
foreach ($this->entries as $entry) {
foreach ($this->tags as $tag) {
$book->setSubject($tag['value']);
}
$chapter = $content_start.$entry->getContent().$bookEnd;
$book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
}
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()
{
$mobi = new \MOBI();
$content = new \MOBIFile();
/*
* Book metadata
*/
$content->set('title', $this->title);
$content->set('author', implode($this->authors));
$content->set('subject', $this->title);
/*
* Front page
*/
$content->appendParagraph($this->getExportInformation('PHPMobi'));
if (file_exists($this->logoPath)) {
$content->appendImage(imagecreatefrompng($this->logoPath));
}
$content->appendPageBreak();
/*
* Adding actual entries
*/
foreach ($this->entries as $entry) {
$content->appendChapterTitle($entry->getTitle());
$content->appendParagraph($entry->getContent());
$content->appendPageBreak();
}
$mobi->setContentProvider($content);
// 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);
return Response::create(
$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()
{
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
/*
* Book metadata
*/
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('wallabag');
$pdf->SetTitle($this->title);
$pdf->SetSubject('Articles via wallabag');
$pdf->SetKeywords('wallabag');
/*
* Front page
*/
$pdf->AddPage();
$intro = '<h1>'.$this->title.'</h1>'.$this->getExportInformation('tcpdf');
$pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
/*
* Adding actual entries
*/
foreach ($this->entries as $entry) {
foreach ($this->tags as $tag) {
$pdf->SetKeywords($tag['value']);
}
$pdf->AddPage();
$html = '<h1>'.$entry->getTitle().'</h1>';
$html .= $entry->getContent();
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
}
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
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()
{
$delimiter = ';';
$enclosure = '"';
$handle = fopen('php://memory', 'rb+');
fputcsv($handle, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'), $delimiter, $enclosure);
foreach ($this->entries as $entry) {
fputcsv(
$handle,
array(
$entry->getTitle(),
$entry->getURL(),
// remove new line to avoid crazy results
str_replace(array("\r\n", "\r", "\n"), '', $entry->getContent()),
implode(', ', $entry->getTags()->toArray()),
$entry->getMimetype(),
$entry->getLanguage(),
),
$delimiter,
$enclosure
);
}
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();
}
private function produceJSON()
{
return Response::create(
$this->prepareSerializingContent('json'),
200,
array(
'Content-type' => 'application/json',
'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"',
'Content-Transfer-Encoding' => 'UTF-8',
)
)->send();
}
private function produceXML()
{
return Response::create(
$this->prepareSerializingContent('xml'),
200,
array(
'Content-type' => 'application/xml',
'Content-Disposition' => 'attachment; filename="'.$this->title.'.xml"',
'Content-Transfer-Encoding' => 'UTF-8',
)
)->send();
}
/**
* Return a Serializer object for producing processes that need it (JSON & XML).
*
* @return Serializer
*/
private function prepareSerializingContent($format)
{
$serializer = SerializerBuilder::create()->build();
return $serializer->serialize(
$this->entries,
$format,
SerializationContext::create()->setGroups(array('entries_for_user'))
);
}
/**
* 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);
}
}

View file

@ -64,3 +64,9 @@ services:
- %language%
tags:
- { 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

View file

@ -91,6 +91,24 @@
{% endfor %}
</ul>
<!-- Export -->
<div id="export" class="side-nav fixed right-aligned">
{% set currentRoute = app.request.attributes.get('_route') %}
{% if currentRoute == 'homepage' %}
{% set currentRoute = 'unread' %}
{% endif %}
<h4 class="center">{% trans %}Export{% endtrans %}</h4>
<ul>
<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'epub' }) }}">{% trans %}EPUB{% endtrans %}</a></li>
<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'mobi' }) }}">{% trans %}MOBI{% endtrans %}</a></li>
<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'pdf' }) }}">{% trans %}PDF{% endtrans %}</a></li>
<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'xml' }) }}">{% trans %}XML{% endtrans %}</a></li>
<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'json' }) }}">{% trans %}JSON{% endtrans %}</a></li>
<li class="bold"><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'csv' }) }}">{% trans %}CSV{% endtrans %}</a></li>
<li class="bold"><del><a class="waves-effect" href="{{ path('export_entries', { 'category': currentRoute, 'format': 'txt' }) }}">{% trans %}TXT{% endtrans %}</a></del></li>
</ul>
</div>
<!-- Filters -->
<div id="filters" class="side-nav fixed right-aligned">
<form action="{{ path('all') }}">

View file

@ -102,13 +102,16 @@
<li class="bold">
<a class="waves-effect collapsible-header">
<i class="mdi-file-file-download small"></i>
<span><del>{% trans %}Download{% endtrans %}</del></span>
<span>{% trans %}Download{% endtrans %}</span>
</a>
<div class="collapsible-body">
<ul>
{% if export_epub %}<li><del><a href="?epub&amp;method=id&amp;value={{ entry.id }}" title="Generate ePub file">EPUB</a></del></li>{% endif %}
{% if export_mobi %}<li><del><a href="?mobi&amp;method=id&amp;value={{ entry.id }}" title="Generate Mobi file">MOBI</a></del></li>{% endif %}
{% if export_pdf %}<li><del><a href="?pdf&amp;method=id&amp;value={{ entry.id }}" title="Generate PDF file">PDF</a></del> </li>{% endif %}
{% if export_epub %}<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'epub' }) }}" title="Generate ePub file">EPUB</a></li>{% endif %}
{% if export_mobi %}<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'mobi' }) }}" title="Generate Mobi file">MOBI</a></li>{% endif %}
{% if export_pdf %}<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'pdf' }) }}" title="Generate PDF file">PDF</a></li>{% endif %}
<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'csv' }) }}" title="Generate CSV file">CSV</a></li>
<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'json' }) }}" title="Generate JSON file">JSON</a></li>
<li><a href="{{ path('export_entry', { 'id': entry.id, 'format': 'xml' }) }}" title="Generate XML file">XML</a></li>
</ul>
</div>
</li>

View file

@ -59,6 +59,7 @@
<li class="bold"><a title="{% trans %}Add a new entry{% endtrans %}" class="waves-effect" href="{{ path('new') }}" id="nav-btn-add"><i class="mdi-content-add"></i></a></li>
<li><a title="{% trans %}Search{% endtrans %}" class="waves-effect" href="javascript: void(null);" id="nav-btn-search"><i class="mdi-action-search"></i></a>
<li id="button_filters"><a title="{% trans %}Filter entries{% endtrans %}" href="#" data-activates="filters" class="nav-panel-menu button-collapse-right"><i class="mdi-content-filter-list"></i></a></li>
<li id="button_export"><a title="{% trans %}Export{% endtrans %}" class="nav-panel-menu button-collapse-right" href="#" data-activates="export" class="nav-panel-menu button-collapse-right"><i class="mdi-file-file-download"></i></a></li>
</ul>
</div>
<form method="get" action="index.php">

View file

@ -11,6 +11,14 @@ function init_filters() {
}
}
function init_export() {
// no display if export not aviable
if ($("div").is("#export")) {
$('#button_export').show();
$('.button-collapse-right').sideNav({ edge: 'right' });
}
}
$(document).ready(function(){
// sideNav
$('.button-collapse').sideNav();
@ -26,6 +34,7 @@ $(document).ready(function(){
format: 'dd/mm/yyyy',
});
init_filters();
init_export();
$('#nav-btn-add-tag').on('click', function(){
$(".nav-panel-add-tag").toggle(100);

View file

@ -0,0 +1,234 @@
<?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();
$client->request('GET', '/export/awesomeness.epub');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
public function testUnknownFormatExport()
{
$this->logInAs('admin');
$client = $this->getClient();
$client->request('GET', '/export/unread.xslx');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
public function testUnsupportedFormatExport()
{
$this->logInAs('admin');
$client = $this->getClient();
$client->request('GET', '/export/unread.txt');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
$content = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findOneByUsernameAndNotArchived('admin');
$client->request('GET', '/export/'.$content->getId().'.txt');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
public function testBadEntryId()
{
$this->logInAs('admin');
$client = $this->getClient();
$client->request('GET', '/export/0.mobi');
$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="'.preg_replace('/[^A-Za-z0-9\-]/', '', $content->getTitle()).'.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();
// to be sure results are the same
$contentInDB = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->createQueryBuilder('e')
->leftJoin('e.user', 'u')
->where('u.username = :username')->setParameter('username', 'admin')
->andWhere('e.isArchived = true')
->getQuery()
->getArrayResult();
ob_start();
$crawler = $client->request('GET', '/export/archive.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="Archive 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);
// +1 for title line
$this->assertEquals(count($contentInDB)+1, count($csv));
$this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]);
}
public function testJsonExport()
{
$this->logInAs('admin');
$client = $this->getClient();
// to be sure results are the same
$contentInDB = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->createQueryBuilder('e')
->leftJoin('e.user', 'u')
->where('u.username = :username')->setParameter('username', 'admin')
->getQuery()
->getArrayResult();
ob_start();
$crawler = $client->request('GET', '/export/all.json');
ob_end_clean();
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
$this->assertEquals('application/json', $headers->get('content-type'));
$this->assertEquals('attachment; filename="All articles.json"', $headers->get('content-disposition'));
$this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals(count($contentInDB), count($content));
$this->assertArrayHasKey('id', $content[0]);
$this->assertArrayHasKey('title', $content[0]);
$this->assertArrayHasKey('url', $content[0]);
$this->assertArrayHasKey('is_archived', $content[0]);
$this->assertArrayHasKey('is_starred', $content[0]);
$this->assertArrayHasKey('content', $content[0]);
$this->assertArrayHasKey('mimetype', $content[0]);
$this->assertArrayHasKey('language', $content[0]);
$this->assertArrayHasKey('reading_time', $content[0]);
$this->assertArrayHasKey('domain_name', $content[0]);
$this->assertArrayHasKey('tags', $content[0]);
}
public function testXmlExport()
{
$this->logInAs('admin');
$client = $this->getClient();
// to be sure results are the same
$contentInDB = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->createQueryBuilder('e')
->leftJoin('e.user', 'u')
->where('u.username = :username')->setParameter('username', 'admin')
->andWhere('e.isArchived = false')
->getQuery()
->getArrayResult();
ob_start();
$crawler = $client->request('GET', '/export/unread.xml');
ob_end_clean();
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
$this->assertEquals('application/xml', $headers->get('content-type'));
$this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition'));
$this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
$content = new \SimpleXMLElement($client->getResponse()->getContent());
$this->assertGreaterThan(0, $content->count());
$this->assertEquals(count($contentInDB), $content->count());
$this->assertNotEmpty('id', (string) $content->entry[0]->id);
$this->assertNotEmpty('title', (string) $content->entry[0]->title);
$this->assertNotEmpty('url', (string) $content->entry[0]->url);
$this->assertNotEmpty('content', (string) $content->entry[0]->content);
$this->assertNotEmpty('domain_name', (string) $content->entry[0]->domain_name);
}
}