aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/EntriesExport.php
blob: fad0bb977d8822597b0a181d8f7f9e86140ef1d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php

namespace Wallabag\CoreBundle\Helper;

use PHPePub\Core\EPub;
use PHPePub\Core\Structure\OPF\DublinCore;

class EntriesExport
{
    private $format;
    private $method;
    private $title;
    private $entries;
    private $authors = array('wallabag');
    private $language;
    private $tags;

    public function __construct($entries)
    {
        $this->entries = $entries;

        foreach ($entries as $entry) {
            $this->tags[] = $entry->getTags();
        }
        if (count($entries) === 1) {
            $this->language = $entries[0]->getLanguage();
        }
    }

    /**
     * Sets the category of which we want to get articles, or just one entry.
     *
     * @param string $method Method to get articles
     */
    public function setMethod($method)
    {
        $this->method = $method;

        switch ($this->method) {
            case 'All':
                $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;
        }
    }

    /**
     * Sets the output format.
     *
     * @param string $format
     */
    public function exportAs($format)
    {
        $this->format = $format;

        switch ($this->format) {
            case 'epub':
                $this->produceEpub();
                break;

            case 'mobi':
                $this->produceMobi();
                break;

            case 'pdf':
                $this->producePDF();
                break;

            case 'csv':
                $this->produceCSV();
                break;

            default:
                break;
        }
    }

    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);
        $book->setIdentifier($this->title, EPub::IDENTIFIER_URI); // 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->setDescription(_('Some articles saved on my wallabag'));

        foreach ($this->authors as $author) {
            $book->setAuthor($author, $author);
        }

        $book->setPublisher('wallabag', 'wallabag'); // I hope this is a non existant address :)
        $book->setDate(time()); // Strictly not needed as the book date defaults to time().
        $book->setSourceURL("http://$_SERVER[HTTP_HOST]");

        $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
        $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');

        /*
         * Front page
         */

        $book->setCoverImage('Cover.png', file_get_contents('themes/_global/img/appicon/apple-touch-icon-152.png'), 'image/png');

        $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();

        /*
         * Adding actual entries
         */

        foreach ($this->entries as $entry) { //set tags as subjects
                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);
        }
        $book->finalize();
        $book->sendBook($this->title);
    }

    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('<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>');
        $content->appendImage(imagecreatefrompng('themes/_global/img/appicon/apple-touch-icon-152.png'));
        $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);

        // we offer file to download
        $mobi->download($this->title.'.mobi');
    }

    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><div style="text-align:center;" >
        <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);

        /*
         * 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);

        $pdf->Output($this->title.'.pdf', 'D');
    }

    private function produceCSV()
    {
        header('Content-type: application/csv');
        header('Content-Disposition: attachment; filename="'.$this->title.'.csv"');
        header('Content-Transfer-Encoding: UTF-8');

        $output = fopen('php://output', 'a');

        fputcsv($output, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'));
        foreach ($this->entries as $entry) {
            fputcsv($output, array($entry->getTitle(),
                                   $entry->getURL(),
                                   $entry->getContent(),
                                   implode(', ', $entry->getTags()->toArray()),
                                   $entry->getMimetype(),
                                   $entry->getLanguage(), ));
        }
        fclose($output);
        exit();
    }
}