aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2015-10-15 20:06:59 +0200
committerNicolas LÅ“uillet <nicolas.loeuillet@smile.fr>2015-11-09 16:32:48 +0100
commit03690d138792dde6405e3d2eb3c53f6572eb3c43 (patch)
treea2700157311b8e83dac9c3bdbe26a8719a4a3ad2 /src/Wallabag/CoreBundle/Helper
parentf1eccfd63f214dcc730ab0d18a694a5465f425db (diff)
downloadwallabag-03690d138792dde6405e3d2eb3c53f6572eb3c43.tar.gz
wallabag-03690d138792dde6405e3d2eb3c53f6572eb3c43.tar.zst
wallabag-03690d138792dde6405e3d2eb3c53f6572eb3c43.zip
Start work on export
For now: - ebook - mobi - pdf - csv
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper')
-rw-r--r--src/Wallabag/CoreBundle/Helper/EntriesExport.php263
1 files changed, 263 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php
new file mode 100644
index 00000000..fad0bb97
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php
@@ -0,0 +1,263 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use PHPePub\Core\EPub;
6use PHPePub\Core\Structure\OPF\DublinCore;
7
8class EntriesExport
9{
10 private $format;
11 private $method;
12 private $title;
13 private $entries;
14 private $authors = array('wallabag');
15 private $language;
16 private $tags;
17
18 public function __construct($entries)
19 {
20 $this->entries = $entries;
21
22 foreach ($entries as $entry) {
23 $this->tags[] = $entry->getTags();
24 }
25 if (count($entries) === 1) {
26 $this->language = $entries[0]->getLanguage();
27 }
28 }
29
30 /**
31 * Sets the category of which we want to get articles, or just one entry.
32 *
33 * @param string $method Method to get articles
34 */
35 public function setMethod($method)
36 {
37 $this->method = $method;
38
39 switch ($this->method) {
40 case 'All':
41 $this->title = 'All Articles';
42 break;
43 case 'Unread':
44 $this->title = 'Unread articles';
45 break;
46 case 'Starred':
47 $this->title = 'Starred articles';
48 break;
49 case 'Archive':
50 $this->title = 'Archived articles';
51 break;
52 case 'entry':
53 $this->title = $this->entries[0]->getTitle();
54 break;
55 default:
56 break;
57 }
58 }
59
60 /**
61 * Sets the output format.
62 *
63 * @param string $format
64 */
65 public function exportAs($format)
66 {
67 $this->format = $format;
68
69 switch ($this->format) {
70 case 'epub':
71 $this->produceEpub();
72 break;
73
74 case 'mobi':
75 $this->produceMobi();
76 break;
77
78 case 'pdf':
79 $this->producePDF();
80 break;
81
82 case 'csv':
83 $this->produceCSV();
84 break;
85
86 default:
87 break;
88 }
89 }
90
91 private function produceEpub()
92 {
93 /*
94 * Start and End of the book
95 */
96 $content_start =
97 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
98 ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
99 .'<head>'
100 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
101 .'<title>'._('wallabag articles book')."</title>\n"
102 ."</head>\n"
103 ."<body>\n";
104
105 $bookEnd = "</body>\n</html>\n";
106
107 $book = new EPub(EPub::BOOK_VERSION_EPUB3);
108
109 /*
110 * Book metadata
111 */
112
113 $book->setTitle($this->title);
114 $book->setIdentifier($this->title, EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID.
115 $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.
116 $book->setDescription(_('Some articles saved on my wallabag'));
117
118 foreach ($this->authors as $author) {
119 $book->setAuthor($author, $author);
120 }
121
122 $book->setPublisher('wallabag', 'wallabag'); // I hope this is a non existant address :)
123 $book->setDate(time()); // Strictly not needed as the book date defaults to time().
124 $book->setSourceURL("http://$_SERVER[HTTP_HOST]");
125
126 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
127 $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');
128
129 /*
130 * Front page
131 */
132
133 $book->setCoverImage('Cover.png', file_get_contents('themes/_global/img/appicon/apple-touch-icon-152.png'), 'image/png');
134
135 $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;
136
137 $book->addChapter('Notices', 'Cover2.html', $cover);
138
139 $book->buildTOC();
140
141 /*
142 * Adding actual entries
143 */
144
145 foreach ($this->entries as $entry) { //set tags as subjects
146 foreach ($this->tags as $tag) {
147 $book->setSubject($tag['value']);
148 }
149
150 $chapter = $content_start.$entry->getContent().$bookEnd;
151 $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
152 }
153 $book->finalize();
154 $book->sendBook($this->title);
155 }
156
157 private function produceMobi()
158 {
159 $mobi = new \MOBI();
160 $content = new \MOBIFile();
161
162 /*
163 * Book metadata
164 */
165
166 $content->set('title', $this->title);
167 $content->set('author', implode($this->authors));
168 $content->set('subject', $this->title);
169
170 /*
171 * Front page
172 */
173
174 $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>');
175 $content->appendImage(imagecreatefrompng('themes/_global/img/appicon/apple-touch-icon-152.png'));
176 $content->appendPageBreak();
177
178 /*
179 * Adding actual entries
180 */
181
182 foreach ($this->entries as $entry) {
183 $content->appendChapterTitle($entry->getTitle());
184 $content->appendParagraph($entry->getContent());
185 $content->appendPageBreak();
186 }
187 $mobi->setContentProvider($content);
188
189 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
190 $this->title = preg_replace('/[^A-Za-z0-9\-]/', '', $this->title);
191
192 // we offer file to download
193 $mobi->download($this->title.'.mobi');
194 }
195
196 private function producePDF()
197 {
198 $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
199
200 /*
201 * Book metadata
202 */
203
204 $pdf->SetCreator(PDF_CREATOR);
205 $pdf->SetAuthor('wallabag');
206 $pdf->SetTitle($this->title);
207 $pdf->SetSubject('Articles via wallabag');
208 $pdf->SetKeywords('wallabag');
209
210 /*
211 * Front page
212 */
213
214 $pdf->AddPage();
215 $intro = '<h1>'.$this->title.'</h1><div style="text-align:center;" >
216 <p>'._('Produced by wallabag with tcpdf').'</p>
217 <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>
218 <img src="themes/_global/img/appicon/apple-touch-icon-152.png" /></div>';
219
220 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
221
222 /*
223 * Adding actual entries
224 */
225
226 foreach ($this->entries as $entry) {
227 foreach ($this->tags as $tag) {
228 $pdf->SetKeywords($tag['value']);
229 }
230
231 $pdf->AddPage();
232 $html = '<h1>'.$entry->getTitle().'</h1>';
233 $html .= $entry->getContent();
234 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
235 }
236
237 // set image scale factor
238 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
239
240 $pdf->Output($this->title.'.pdf', 'D');
241 }
242
243 private function produceCSV()
244 {
245 header('Content-type: application/csv');
246 header('Content-Disposition: attachment; filename="'.$this->title.'.csv"');
247 header('Content-Transfer-Encoding: UTF-8');
248
249 $output = fopen('php://output', 'a');
250
251 fputcsv($output, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'));
252 foreach ($this->entries as $entry) {
253 fputcsv($output, array($entry->getTitle(),
254 $entry->getURL(),
255 $entry->getContent(),
256 implode(', ', $entry->getTags()->toArray()),
257 $entry->getMimetype(),
258 $entry->getLanguage(), ));
259 }
260 fclose($output);
261 exit();
262 }
263}