xref: /plugin/dw2pdf/action.php (revision d97a751ef5f1360cd4d5e5570eccc32548e668b8)
1ee19bac3SLuigi Micco<?php
2852931daSAndreas Gohr
3852931daSAndreas Gohruse dokuwiki\Cache\Cache;
4852931daSAndreas Gohruse dokuwiki\Extension\ActionPlugin;
5852931daSAndreas Gohruse dokuwiki\Extension\Event;
6852931daSAndreas Gohruse dokuwiki\Extension\EventHandler;
7852931daSAndreas Gohruse dokuwiki\plugin\dw2pdf\MenuItem;
8852931daSAndreas Gohruse dokuwiki\StyleUtils;
9852931daSAndreas Gohruse Mpdf\MpdfException;
10852931daSAndreas Gohr
11ee19bac3SLuigi Micco/**
12ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
13ee19bac3SLuigi Micco *
14852931daSAndreas Gohr * Export html content to pdf, for different url parameter configurations
15852931daSAndreas Gohr * DokuPDF which extends mPDF is used for generating the pdf from html.
16852931daSAndreas Gohr *
17ee19bac3SLuigi Micco * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
18ee19bac3SLuigi Micco * @author     Luigi Micco <l.micco@tiscali.it>
195db42babSAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
20ee19bac3SLuigi Micco */
21852931daSAndreas Gohrclass action_plugin_dw2pdf extends ActionPlugin
2217e88313SGerrit Uitslag{
2302f9a447SGerrit Uitslag    /**
2402f9a447SGerrit Uitslag     * Settings for current export, collected from url param, plugin config, global config
2502f9a447SGerrit Uitslag     *
2602f9a447SGerrit Uitslag     * @var array
2702f9a447SGerrit Uitslag     */
28852931daSAndreas Gohr    protected $exportConfig;
292a127a1dSGerrit Uitslag    /** @var string template name, to use templates from dw2pdf/tpl/<template name> */
3060e59de7SGerrit Uitslag    protected $tpl;
312a127a1dSGerrit Uitslag    /** @var string title of exported pdf */
3203352761SKirsten Roschanski    protected $title;
336a7f9d6cSGerrit Uitslag    /** @var array list of pages included in exported pdf */
3417e88313SGerrit Uitslag    protected $list = [];
352a127a1dSGerrit Uitslag    /** @var bool|string path to temporary cachefile */
369b288b2aSGerrit Uitslag    protected $onetimefile = false;
379c76f78dSVincent GIRARD    protected $currentBookChapter = 0;
381c14c879SAndreas Gohr
391c14c879SAndreas Gohr    /**
401c14c879SAndreas Gohr     * Constructor. Sets the correct template
411c14c879SAndreas Gohr     */
4217e88313SGerrit Uitslag    public function __construct()
4317e88313SGerrit Uitslag    {
44*d97a751eSGerrit Uitslag        require_once __DIR__ . '/vendor/autoload.php';
45*d97a751eSGerrit Uitslag
4602f9a447SGerrit Uitslag        $this->tpl = $this->getExportConfig('template');
471c14c879SAndreas Gohr    }
481c14c879SAndreas Gohr
49ee19bac3SLuigi Micco    /**
509b288b2aSGerrit Uitslag     * Delete cached files that were for one-time use
519b288b2aSGerrit Uitslag     */
5217e88313SGerrit Uitslag    public function __destruct()
5317e88313SGerrit Uitslag    {
549b288b2aSGerrit Uitslag        if ($this->onetimefile) {
559b288b2aSGerrit Uitslag            unlink($this->onetimefile);
569b288b2aSGerrit Uitslag        }
579b288b2aSGerrit Uitslag    }
589b288b2aSGerrit Uitslag
599b288b2aSGerrit Uitslag    /**
609c76f78dSVincent GIRARD     * Return the value of currentBookChapter, which is the order of the file to be added in a book generation
619c76f78dSVincent GIRARD     */
629c76f78dSVincent GIRARD    public function getCurrentBookChapter()
639c76f78dSVincent GIRARD    {
649c76f78dSVincent GIRARD        return $this->currentBookChapter;
659c76f78dSVincent GIRARD    }
669c76f78dSVincent GIRARD
679c76f78dSVincent GIRARD    /**
68ee19bac3SLuigi Micco     * Register the events
69177a7d30SGerrit Uitslag     *
70177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
71ee19bac3SLuigi Micco     */
72852931daSAndreas Gohr    public function register(EventHandler $controller)
7317e88313SGerrit Uitslag    {
7417e88313SGerrit Uitslag        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert');
7517e88313SGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton');
7617e88313SGerrit Uitslag        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton');
77ee19bac3SLuigi Micco    }
78ee19bac3SLuigi Micco
791c14c879SAndreas Gohr    /**
801c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
81737417c6SKlap-in     *
82737417c6SKlap-in     * @param Doku_Event $event
831c14c879SAndreas Gohr     */
84852931daSAndreas Gohr    public function convert(Event $event)
8517e88313SGerrit Uitslag    {
862a127a1dSGerrit Uitslag        global $REV, $DATE_AT;
872d9cd424SGerrit Uitslag        global $conf, $INPUT;
88ee19bac3SLuigi Micco
891ef68647SAndreas Gohr        // our event?
902a127a1dSGerrit Uitslag        $allowedEvents = ['export_pdfbook', 'export_pdf', 'export_pdfns'];
912a127a1dSGerrit Uitslag        if (!in_array($event->data, $allowedEvents)) return;
92ee19bac3SLuigi Micco
932a127a1dSGerrit Uitslag        try {
942a127a1dSGerrit Uitslag            //collect pages and check permissions
95852931daSAndreas Gohr            [$this->title, $this->list] = $this->collectExportablePages($event);
96d63e7fe7SGerrit Uitslag
972d9cd424SGerrit Uitslag            if ($event->data === 'export_pdf' && ($REV || $DATE_AT)) {
989b288b2aSGerrit Uitslag                $cachefile = tempnam($conf['tmpdir'] . '/dwpdf', 'dw2pdf_');
999b288b2aSGerrit Uitslag                $this->onetimefile = $cachefile;
100f00df45eSMichael Große                $generateNewPdf = true;
101f00df45eSMichael Große            } else {
102a58f45f0SGerrit Uitslag                // prepare cache and its dependencies
10317e88313SGerrit Uitslag                $depends = [];
10403352761SKirsten Roschanski                $cache = $this->prepareCache($depends);
1059b288b2aSGerrit Uitslag                $cachefile = $cache->cache;
10627195d5bSMichael Große                $generateNewPdf = !$this->getConf('usecache')
10727195d5bSMichael Große                    || $this->getExportConfig('isDebug')
10827195d5bSMichael Große                    || !$cache->useCache($depends);
109f00df45eSMichael Große            }
110d63e7fe7SGerrit Uitslag
111bd977188SGerrit Uitslag            // hard work only when no cache available or needed for debugging
112f00df45eSMichael Große            if ($generateNewPdf) {
113e5f6c2cbSMichael Große                // generating the pdf may take a long time for larger wikis / namespaces with many pages
114e5f6c2cbSMichael Große                set_time_limit(0);
1152a127a1dSGerrit Uitslag                //may throw Mpdf\MpdfException as well
1169b288b2aSGerrit Uitslag                $this->generatePDF($cachefile, $event);
1172a127a1dSGerrit Uitslag            }
1182a127a1dSGerrit Uitslag        } catch (Exception $e) {
1192d9cd424SGerrit Uitslag            if ($INPUT->has('selection')) {
1202d9cd424SGerrit Uitslag                http_status(400);
121852931daSAndreas Gohr                echo $e->getMessage();
1222d9cd424SGerrit Uitslag                exit();
1232d9cd424SGerrit Uitslag            } else {
1242a127a1dSGerrit Uitslag                //prevent Action/Export()
125b34cb34eSSzymon Olewniczak                msg($e->getMessage(), -1);
1262a127a1dSGerrit Uitslag                $event->data = 'redirect';
127d9c13ec7SGerrit Uitslag                return;
128b34cb34eSSzymon Olewniczak            }
129d63e7fe7SGerrit Uitslag        }
1302d9cd424SGerrit Uitslag        $event->preventDefault(); // after prevent, $event->data cannot be changed
131d63e7fe7SGerrit Uitslag
132d63e7fe7SGerrit Uitslag        // deliver the file
1339b288b2aSGerrit Uitslag        $this->sendPDFFile($cachefile);  //exits
134d63e7fe7SGerrit Uitslag    }
135d63e7fe7SGerrit Uitslag
136d63e7fe7SGerrit Uitslag    /**
1372a127a1dSGerrit Uitslag     * Obtain list of pages and title, for different methods of exporting the pdf.
1382a127a1dSGerrit Uitslag     *  - Return a title and selection, throw otherwise an exception
1392a127a1dSGerrit Uitslag     *  - Check permisions
140d63e7fe7SGerrit Uitslag     *
141d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
1428ff32d7bSGerrit Uitslag     * @return array
1432a127a1dSGerrit Uitslag     * @throws Exception
144d63e7fe7SGerrit Uitslag     */
145852931daSAndreas Gohr    protected function collectExportablePages(Event $event)
14617e88313SGerrit Uitslag    {
14736a7917dSGerrit Uitslag        global $ID, $REV;
148d63e7fe7SGerrit Uitslag        global $INPUT;
1492a127a1dSGerrit Uitslag        global $conf, $lang;
150d63e7fe7SGerrit Uitslag
151d63e7fe7SGerrit Uitslag        // list of one or multiple pages
15217e88313SGerrit Uitslag        $list = [];
15328e636eaSGerrit Uitslag
1544b4cebc2SLarsDW223        if ($event->data == 'export_pdf') {
1552a127a1dSGerrit Uitslag            if (auth_quickaclcheck($ID) < AUTH_READ) {  // set more specific denied message
1562a127a1dSGerrit Uitslag                throw new Exception($lang['accessdenied']);
1572a127a1dSGerrit Uitslag            }
158d63e7fe7SGerrit Uitslag            $list[0] = $ID;
1592a127a1dSGerrit Uitslag            $title = $INPUT->str('pdftitle'); //DEPRECATED
1602a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
1612a127a1dSGerrit Uitslag            if (empty($title)) {
1622a127a1dSGerrit Uitslag                $title = p_get_first_heading($ID);
16315923cb9SGerrit Uitslag            }
164ce8af5d0SHativ            // use page name if title is still empty
1652a127a1dSGerrit Uitslag            if (empty($title)) {
1662a127a1dSGerrit Uitslag                $title = noNS($ID);
167ce8af5d0SHativ            }
168ad18f4e1SGerrit Uitslag
16936a7917dSGerrit Uitslag            $filename = wikiFN($ID, $REV);
17036a7917dSGerrit Uitslag            if (!file_exists($filename)) {
1712a127a1dSGerrit Uitslag                throw new Exception($this->getLang('notexist'));
17236a7917dSGerrit Uitslag            }
1734b4cebc2SLarsDW223        } elseif ($event->data == 'export_pdfns') {
174ad18f4e1SGerrit Uitslag            //check input for title and ns
1752a127a1dSGerrit Uitslag            if (!$title = $INPUT->str('book_title')) {
1762a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
177ad18f4e1SGerrit Uitslag            }
178177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
179ad18f4e1SGerrit Uitslag            if (!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
1802a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needns'));
181ad18f4e1SGerrit Uitslag            }
182ad18f4e1SGerrit Uitslag
18326be4eceSGerrit Uitslag            //sort order
184177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
18517e88313SGerrit Uitslag            $sortoptions = ['pagename', 'date', 'natural'];
186ad18f4e1SGerrit Uitslag            if (!in_array($order, $sortoptions)) {
187ad18f4e1SGerrit Uitslag                $order = 'natural';
188ad18f4e1SGerrit Uitslag            }
189ad18f4e1SGerrit Uitslag
19026be4eceSGerrit Uitslag            //search depth
191177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
192ad18f4e1SGerrit Uitslag            if ($depth < 0) {
193ad18f4e1SGerrit Uitslag                $depth = 0;
194ad18f4e1SGerrit Uitslag            }
19526be4eceSGerrit Uitslag
196ad18f4e1SGerrit Uitslag            //page search
19717e88313SGerrit Uitslag            $result = [];
19817e88313SGerrit Uitslag            $opts = ['depth' => $depth]; //recursive all levels
199ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
200ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
201ad18f4e1SGerrit Uitslag
20264541781SAnna Dabrowska            // exclude ids
20364541781SAnna Dabrowska            $excludes = $INPUT->arr('excludes');
20464541781SAnna Dabrowska            if (!empty($excludes)) {
205d31b75d5SAnna Dabrowska                $result = array_filter($result, function ($item) use ($excludes) {
2068c25a9b9SGerrit Uitslag                    return !in_array($item['id'], $excludes);
2078c25a9b9SGerrit Uitslag                });
2088c25a9b9SGerrit Uitslag            }
2098c25a9b9SGerrit Uitslag            // exclude namespaces
2108c25a9b9SGerrit Uitslag            $excludesns = $INPUT->arr('excludesns');
2118c25a9b9SGerrit Uitslag            if (!empty($excludesns)) {
2128c25a9b9SGerrit Uitslag                $result = array_filter($result, function ($item) use ($excludesns) {
2138c25a9b9SGerrit Uitslag                    foreach ($excludesns as $ns) {
2148c25a9b9SGerrit Uitslag                        if (strpos($item['id'], $ns . ':') === 0) return false;
2158c25a9b9SGerrit Uitslag                    }
2168c25a9b9SGerrit Uitslag                    return true;
217d31b75d5SAnna Dabrowska                });
21864541781SAnna Dabrowska            }
21964541781SAnna Dabrowska
22026be4eceSGerrit Uitslag            //sorting
221ad18f4e1SGerrit Uitslag            if (count($result) > 0) {
222ad18f4e1SGerrit Uitslag                if ($order == 'date') {
223852931daSAndreas Gohr                    usort($result, [$this, 'cbDateSort']);
22441e5d4e2SAndreas Gohr                } elseif ($order == 'pagename' || $order == 'natural') {
225852931daSAndreas Gohr                    usort($result, [$this, 'cbPagenameSort']);
226ad18f4e1SGerrit Uitslag                }
227ad18f4e1SGerrit Uitslag            }
228ad18f4e1SGerrit Uitslag
229ad18f4e1SGerrit Uitslag            foreach ($result as $item) {
230d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
231ad18f4e1SGerrit Uitslag            }
232ad18f4e1SGerrit Uitslag
233baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
234baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
235baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace, ':')))) {
236baa31dc5SGerrit Uitslag                        array_unshift($list, rtrim($pdfnamespace, ':'));
237baa31dc5SGerrit Uitslag                    }
238baa31dc5SGerrit Uitslag                }
239baa31dc5SGerrit Uitslag            }
24017e88313SGerrit Uitslag        } elseif (!empty($_COOKIE['list-pagelist'])) {
241b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator */
24226be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
2432a127a1dSGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
2442a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2452a127a1dSGerrit Uitslag            if (empty($title)) {
2462a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
24726be4eceSGerrit Uitslag            }
248ad18f4e1SGerrit Uitslag
2492a127a1dSGerrit Uitslag            $list = explode("|", $_COOKIE['list-pagelist']);
250b3eed6e3SGerrit Uitslag        } elseif ($INPUT->has('selection')) {
251b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
252b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
253b3eed6e3SGerrit Uitslag//                http_status(403);
254b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
255b3eed6e3SGerrit Uitslag//                exit();
256b3eed6e3SGerrit Uitslag//            }
257b3eed6e3SGerrit Uitslag
2587c79bc79SGerrit Uitslag            $list = json_decode($INPUT->str('selection', '', true), true);
259852931daSAndreas Gohr            if (!is_array($list) || $list === []) {
2602a127a1dSGerrit Uitslag                throw new Exception($this->getLang('empty'));
261b3eed6e3SGerrit Uitslag            }
262b3eed6e3SGerrit Uitslag
2632a127a1dSGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
2642a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2652a127a1dSGerrit Uitslag            if (empty($title)) {
2662a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
2672a127a1dSGerrit Uitslag            }
2682a127a1dSGerrit Uitslag        } elseif ($INPUT->has('savedselection')) {
2692a127a1dSGerrit Uitslag            //export a saved selection of the Bookcreator Plugin
2702a127a1dSGerrit Uitslag            if (plugin_isdisabled('bookcreator')) {
2712a127a1dSGerrit Uitslag                throw new Exception($this->getLang('missingbookcreator'));
2722a127a1dSGerrit Uitslag            }
2732a127a1dSGerrit Uitslag            /** @var action_plugin_bookcreator_handleselection $SelectionHandling */
2742a127a1dSGerrit Uitslag            $SelectionHandling = plugin_load('action', 'bookcreator_handleselection');
2752a127a1dSGerrit Uitslag            $savedselection = $SelectionHandling->loadSavedSelection($INPUT->str('savedselection'));
2762a127a1dSGerrit Uitslag            $title = $savedselection['title'];
2772a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2782a127a1dSGerrit Uitslag            $list = $savedselection['selection'];
2792a127a1dSGerrit Uitslag
2802a127a1dSGerrit Uitslag            if (empty($title)) {
2812a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
282b3eed6e3SGerrit Uitslag            }
283737417c6SKlap-in        } else {
28426be4eceSGerrit Uitslag            //show empty bookcreator message
2852a127a1dSGerrit Uitslag            throw new Exception($this->getLang('empty'));
286737417c6SKlap-in        }
287737417c6SKlap-in
288719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
289c7138b3fSGerrit Uitslag
29017e88313SGerrit Uitslag        $skippedpages = [];
291c7138b3fSGerrit Uitslag        foreach ($list as $index => $pageid) {
292c7138b3fSGerrit Uitslag            if (auth_quickaclcheck($pageid) < AUTH_READ) {
293c7138b3fSGerrit Uitslag                $skippedpages[] = $pageid;
294c7138b3fSGerrit Uitslag                unset($list[$index]);
295c7138b3fSGerrit Uitslag            }
296c7138b3fSGerrit Uitslag        }
2972a127a1dSGerrit Uitslag        $list = array_filter($list, 'strlen'); //use of strlen() callback prevents removal of pagename '0'
298c7138b3fSGerrit Uitslag
299c7138b3fSGerrit Uitslag        //if selection contains forbidden pages throw (overridable) warning
300852931daSAndreas Gohr        if (!$INPUT->bool('book_skipforbiddenpages') && $skippedpages !== []) {
301852931daSAndreas Gohr            $msg = hsc(implode(', ', $skippedpages));
3022a127a1dSGerrit Uitslag            throw new Exception(sprintf($this->getLang('forbidden'), $msg));
303c7138b3fSGerrit Uitslag        }
304c7138b3fSGerrit Uitslag
30517e88313SGerrit Uitslag        return [$title, $list];
306d63e7fe7SGerrit Uitslag    }
307d63e7fe7SGerrit Uitslag
308a58f45f0SGerrit Uitslag    /**
309a58f45f0SGerrit Uitslag     * Prepare cache
310a58f45f0SGerrit Uitslag     *
311a58f45f0SGerrit Uitslag     * @param array $depends (reference) array with dependencies
312a58f45f0SGerrit Uitslag     * @return cache
313a58f45f0SGerrit Uitslag     */
31417e88313SGerrit Uitslag    protected function prepareCache(&$depends)
31517e88313SGerrit Uitslag    {
316a58f45f0SGerrit Uitslag        global $REV;
317a58f45f0SGerrit Uitslag
318852931daSAndreas Gohr        $cachekey = implode(',', $this->list)
319ee19bac3SLuigi Micco            . $REV
320ee19bac3SLuigi Micco            . $this->getExportConfig('template')
321ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
322ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
323d83760efSGerrit Uitslag            . $this->getExportConfig('font-size')
324ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
3259c76f78dSVincent GIRARD            . $this->getExportConfig('headernumber')
326852931daSAndreas Gohr            . ($this->getExportConfig('hasToC') ? implode('-', $this->getExportConfig('levels')) : '0')
32703352761SKirsten Roschanski            . $this->title;
3280833b7cdSStephan Bauer        $cache = new Cache($cachekey, '.dw2.pdf');
329ee19bac3SLuigi Micco
33017e88313SGerrit Uitslag        $dependencies = [];
331ee19bac3SLuigi Micco        foreach ($this->list as $pageid) {
332ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
333ee19bac3SLuigi Micco
334ee19bac3SLuigi Micco            if (is_array($relations)) {
335ee19bac3SLuigi Micco                if (array_key_exists('media', $relations) && is_array($relations['media'])) {
336ee19bac3SLuigi Micco                    foreach ($relations['media'] as $mediaid => $exists) {
337ee19bac3SLuigi Micco                        if ($exists) {
338ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
339ee19bac3SLuigi Micco                        }
340ee19bac3SLuigi Micco                    }
341ee19bac3SLuigi Micco                }
342ee19bac3SLuigi Micco
343ee19bac3SLuigi Micco                if (array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
344ee19bac3SLuigi Micco                    foreach ($relations['haspart'] as $part_pageid => $exists) {
345ee19bac3SLuigi Micco                        if ($exists) {
346ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
347ee19bac3SLuigi Micco                        }
348ee19bac3SLuigi Micco                    }
349ee19bac3SLuigi Micco                }
350ee19bac3SLuigi Micco            }
351ee19bac3SLuigi Micco
352ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
353ee19bac3SLuigi Micco        }
354ee19bac3SLuigi Micco
355ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
356ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
357852931daSAndreas Gohr        $depends['files'][] = __DIR__ . '/renderer.php';
358852931daSAndreas Gohr        $depends['files'][] = __DIR__ . '/mpdf/mpdf.php';
359ee19bac3SLuigi Micco        $depends['files'] = array_merge(
360ee19bac3SLuigi Micco            $depends['files'],
361ee19bac3SLuigi Micco            $dependencies,
362ee19bac3SLuigi Micco            getConfigFiles('main')
363ee19bac3SLuigi Micco        );
364a58f45f0SGerrit Uitslag        return $cache;
365ee19bac3SLuigi Micco    }
366ee19bac3SLuigi Micco
367d63e7fe7SGerrit Uitslag    /**
368e53f1ec0SSzymon Olewniczak     * Returns the parsed Wikitext in dw2pdf for the given id and revision
369e53f1ec0SSzymon Olewniczak     *
370e53f1ec0SSzymon Olewniczak     * @param string $id page id
371e53f1ec0SSzymon Olewniczak     * @param string|int $rev revision timestamp or empty string
372e53f1ec0SSzymon Olewniczak     * @param string $date_at
373e53f1ec0SSzymon Olewniczak     * @return null|string
374e53f1ec0SSzymon Olewniczak     */
375852931daSAndreas Gohr    protected function wikiToDW2PDF($id, $rev = '', $date_at = '')
37617e88313SGerrit Uitslag    {
377e53f1ec0SSzymon Olewniczak        $file = wikiFN($id, $rev);
378e53f1ec0SSzymon Olewniczak
379e53f1ec0SSzymon Olewniczak        if (!file_exists($file)) return '';
380e53f1ec0SSzymon Olewniczak
381e53f1ec0SSzymon Olewniczak        //ensure $id is in global $ID (needed for parsing)
382e53f1ec0SSzymon Olewniczak        global $ID;
383e53f1ec0SSzymon Olewniczak        $keep = $ID;
384e53f1ec0SSzymon Olewniczak        $ID = $id;
385e53f1ec0SSzymon Olewniczak
386e53f1ec0SSzymon Olewniczak        if ($rev || $date_at) {
387852931daSAndreas Gohr            //no caching on old revisions
388852931daSAndreas Gohr            $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at);
389e53f1ec0SSzymon Olewniczak        } else {
390e53f1ec0SSzymon Olewniczak            $ret = p_cached_output($file, 'dw2pdf', $id);
391e53f1ec0SSzymon Olewniczak        }
392e53f1ec0SSzymon Olewniczak
393e53f1ec0SSzymon Olewniczak        //restore ID (just in case)
394e53f1ec0SSzymon Olewniczak        $ID = $keep;
395e53f1ec0SSzymon Olewniczak
396e53f1ec0SSzymon Olewniczak        return $ret;
397e53f1ec0SSzymon Olewniczak    }
398e53f1ec0SSzymon Olewniczak
399e53f1ec0SSzymon Olewniczak    /**
400d63e7fe7SGerrit Uitslag     * Build a pdf from the html
401d63e7fe7SGerrit Uitslag     *
402d63e7fe7SGerrit Uitslag     * @param string $cachefile
403d9c13ec7SGerrit Uitslag     * @param Doku_Event $event
40417e88313SGerrit Uitslag     * @throws MpdfException
405d63e7fe7SGerrit Uitslag     */
40617e88313SGerrit Uitslag    protected function generatePDF($cachefile, $event)
40717e88313SGerrit Uitslag    {
4082d9cd424SGerrit Uitslag        global $REV, $INPUT, $DATE_AT;
409e53f1ec0SSzymon Olewniczak
4102d9cd424SGerrit Uitslag        if ($event->data == 'export_pdf') { //only one page is exported
411e53f1ec0SSzymon Olewniczak            $rev = $REV;
412e53f1ec0SSzymon Olewniczak            $date_at = $DATE_AT;
413852931daSAndreas Gohr        } else {
414852931daSAndreas Gohr            //we are exporting entire namespace, ommit revisions
415852931daSAndreas Gohr            $rev = '';
416852931daSAndreas Gohr            $date_at = '';
417e53f1ec0SSzymon Olewniczak        }
41887c86ddaSAndreas Gohr
41902f9a447SGerrit Uitslag        //some shortcuts to export settings
42002f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
42102f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
42202f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
423b80f709fSNicolas        $watermark = $this->getExportConfig('watermark');
4246ea88a05SAndreas Gohr
4251ef68647SAndreas Gohr        // initialize PDF library
426852931daSAndreas Gohr        require_once(__DIR__ . "/DokuPDF.class.php");
4276ea88a05SAndreas Gohr
428979c9cf5SAndreas Gohr        $mpdf = new DokuPDF(
429979c9cf5SAndreas Gohr            $this->getExportConfig('pagesize'),
4304870b378SLarsDW223            $this->getExportConfig('orientation'),
431979c9cf5SAndreas Gohr            $this->getExportConfig('font-size'),
432979c9cf5SAndreas Gohr            $this->getDocumentLanguage($this->list[0]) //use language of first page
433979c9cf5SAndreas Gohr        );
434ee19bac3SLuigi Micco
435d62df65bSAndreas Gohr        // let mpdf fix local links
436d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
437d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
43821a55743SAnna Dabrowska        if (!empty($self['port'])) {
43902f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
44002f9a447SGerrit Uitslag        }
441d9c13ec7SGerrit Uitslag        $mpdf->SetBasePath($url);
442d62df65bSAndreas Gohr
44356d13144SAndreas Gohr        // Set the title
44403352761SKirsten Roschanski        $mpdf->SetTitle($this->title);
44556d13144SAndreas Gohr
446d63e7fe7SGerrit Uitslag        // some default document settings
447d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
448213fdb75SGerrit Uitslag        //      single-side document has only odd pages
449213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
450daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
451daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
45202f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
45302f9a447SGerrit Uitslag        if ($hasToC) {
45402f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
45502f9a447SGerrit Uitslag        }
45617e88313SGerrit Uitslag        $mpdf->PageNumSubstitutions[] = ['from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'];
4572eedf77dSAndreas Gohr
458b80f709fSNicolas        // Watermarker
459b80f709fSNicolas        if ($watermark) {
460b80f709fSNicolas            $mpdf->SetWatermarkText($watermark);
461b80f709fSNicolas            $mpdf->showWatermarkText = true;
462b80f709fSNicolas        }
463b80f709fSNicolas
46456d13144SAndreas Gohr        // load the template
465852931daSAndreas Gohr        $template = $this->loadTemplate();
466ee19bac3SLuigi Micco
4671ef68647SAndreas Gohr        // prepare HTML header styles
468a2c33768SGerrit Uitslag        $html = '';
46902f9a447SGerrit Uitslag        if ($isDebug) {
470a2c33768SGerrit Uitslag            $html .= '<html><head>';
47117e88313SGerrit Uitslag            $html .= '<style>';
472a2c33768SGerrit Uitslag        }
473db1aa1bfSKirsten Roschanski
474db1aa1bfSKirsten Roschanski        $styles = '@page { size:auto; ' . $template['page'] . '}';
475a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
476254467c4SGerrit Uitslag
477254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
478254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
479254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
480254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
481852931daSAndreas Gohr        $styles .= $this->loadCSS();
482254467c4SGerrit Uitslag
483a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
484a2c33768SGerrit Uitslag
48502f9a447SGerrit Uitslag        if ($isDebug) {
486a2c33768SGerrit Uitslag            $html .= $styles;
4871ef68647SAndreas Gohr            $html .= '</style>';
4881ef68647SAndreas Gohr            $html .= '</head><body>';
489a2c33768SGerrit Uitslag        }
490a2c33768SGerrit Uitslag
491a2c33768SGerrit Uitslag        $body_start = $template['html'];
492a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
4932eedf77dSAndreas Gohr
4941e45476bSmnapp        // insert the cover page
495a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
496a2c33768SGerrit Uitslag
497a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
49802f9a447SGerrit Uitslag        if ($isDebug) {
499a2c33768SGerrit Uitslag            $html .= $body_start;
500a2c33768SGerrit Uitslag        }
50102f9a447SGerrit Uitslag        if ($hasToC) {
502852931daSAndreas Gohr            //Note: - for double-sided document the ToC is always on an even number of pages, so that the
503852931daSAndreas Gohr            //        following content is on a correct odd/even page
504852931daSAndreas Gohr            //      - first page of ToC starts always at odd page (so eventually an additional blank page
505852931daSAndreas Gohr            //        is included before)
50602f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
50717e88313SGerrit Uitslag            $mpdf->TOCpagebreakByArray([
508230b098dSGerrit Uitslag                'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
509230b098dSGerrit Uitslag                'toc-bookmarkText' => $this->getLang('tocheader'),
51002f9a447SGerrit Uitslag                'links' => true,
51102f9a447SGerrit Uitslag                'outdent' => '1em',
51202f9a447SGerrit Uitslag                'pagenumstyle' => '1'
51317e88313SGerrit Uitslag            ]);
51402f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
51502f9a447SGerrit Uitslag        }
51602f9a447SGerrit Uitslag
5171ef68647SAndreas Gohr        // loop over all pages
518c7138b3fSGerrit Uitslag        $counter = 0;
519c7138b3fSGerrit Uitslag        $no_pages = count($this->list);
520c7138b3fSGerrit Uitslag        foreach ($this->list as $page) {
5219c76f78dSVincent GIRARD            $this->currentBookChapter = $counter;
522c7138b3fSGerrit Uitslag            $counter++;
523b3eed6e3SGerrit Uitslag
524852931daSAndreas Gohr            $pagehtml = $this->wikiToDW2PDF($page, $rev, $date_at);
525e53f1ec0SSzymon Olewniczak            //file doesn't exists
526e53f1ec0SSzymon Olewniczak            if ($pagehtml == '') {
527b3eed6e3SGerrit Uitslag                continue;
528b3eed6e3SGerrit Uitslag            }
529852931daSAndreas Gohr            $pagehtml .= $this->pageDependReplacements($template['cite'], $page);
530c7138b3fSGerrit Uitslag            if ($counter < $no_pages) {
531a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
532a2c33768SGerrit Uitslag            }
533a2c33768SGerrit Uitslag
534a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
53502f9a447SGerrit Uitslag            if ($isDebug) {
536a2c33768SGerrit Uitslag                $html .= $pagehtml;
5371ef68647SAndreas Gohr            }
538ee19bac3SLuigi Micco        }
539ee19bac3SLuigi Micco
54033c15297SGerrit Uitslag        // insert the back page
541a2c33768SGerrit Uitslag        $body_end = $template['back'];
54233c15297SGerrit Uitslag
543a2c33768SGerrit Uitslag        $body_end .= '</div>';
544a2c33768SGerrit Uitslag
545d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
54602f9a447SGerrit Uitslag        if ($isDebug) {
547a2c33768SGerrit Uitslag            $html .= $body_end;
548eeb17e15SAndreas Gohr            $html .= '</body>';
549eeb17e15SAndreas Gohr            $html .= '</html>';
550a2c33768SGerrit Uitslag        }
551f765508eSGerrit Uitslag
552f765508eSGerrit Uitslag        //Return html for debugging
55302f9a447SGerrit Uitslag        if ($isDebug) {
55417e88313SGerrit Uitslag            if ($INPUT->str('debughtml', 'text', true) == 'text') {
555a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
556a2c33768SGerrit Uitslag            }
55717e88313SGerrit Uitslag            echo $html;
55826be4eceSGerrit Uitslag            exit();
5597c79bc79SGerrit Uitslag        }
560f765508eSGerrit Uitslag
56187c86ddaSAndreas Gohr        // write to cache file
562d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
56387c86ddaSAndreas Gohr    }
56487c86ddaSAndreas Gohr
565d63e7fe7SGerrit Uitslag    /**
566d63e7fe7SGerrit Uitslag     * @param string $cachefile
567d63e7fe7SGerrit Uitslag     */
56817e88313SGerrit Uitslag    protected function sendPDFFile($cachefile)
56917e88313SGerrit Uitslag    {
57087c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
571b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
57287c86ddaSAndreas Gohr        header('Pragma: public');
573d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
574c0956f69SMichael Große        global $INPUT;
575f7b24f48SMichael Große        $outputTarget = $INPUT->str('outputTarget', $this->getConf('output'));
57687c86ddaSAndreas Gohr
57703352761SKirsten Roschanski        $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', '    ')));
578c0956f69SMichael Große        if ($outputTarget === 'file') {
5799a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
58087c86ddaSAndreas Gohr        } else {
5819a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
58287c86ddaSAndreas Gohr        }
583ee19bac3SLuigi Micco
584b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
585b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
586b3eed6e3SGerrit Uitslag
587e993da11SGerrit Uitslag        //try to send file, and exit if done
588d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
58987c86ddaSAndreas Gohr
590d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
59187c86ddaSAndreas Gohr        if ($fp) {
592d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
59387c86ddaSAndreas Gohr        } else {
59487c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
595852931daSAndreas Gohr            echo "Could not read file - bad permissions?";
59687c86ddaSAndreas Gohr        }
5971ef68647SAndreas Gohr        exit();
5981ef68647SAndreas Gohr    }
5991ef68647SAndreas Gohr
6006be736bfSGerrit Uitslag    /**
6012eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
60244e8e8fbSGerrit Uitslag     *
60344e8e8fbSGerrit Uitslag     * @return array
6041ef68647SAndreas Gohr     */
605852931daSAndreas Gohr    protected function loadTemplate()
60617e88313SGerrit Uitslag    {
6071ef68647SAndreas Gohr        global $ID;
6081ef68647SAndreas Gohr        global $conf;
6090a11dabaSChris75forumname        global $INFO;
6101ef68647SAndreas Gohr
6112eedf77dSAndreas Gohr        // this is what we'll return
61221a55743SAnna Dabrowska        $output = [
6131e45476bSmnapp            'cover' => '',
61421a55743SAnna Dabrowska            'back' => '',
6152eedf77dSAndreas Gohr            'html' => '',
6162eedf77dSAndreas Gohr            'page' => '',
6172eedf77dSAndreas Gohr            'first' => '',
6182eedf77dSAndreas Gohr            'cite' => '',
61921a55743SAnna Dabrowska        ];
6202eedf77dSAndreas Gohr
6212eedf77dSAndreas Gohr        // prepare header/footer elements
6222eedf77dSAndreas Gohr        $html = '';
62317e88313SGerrit Uitslag        foreach (['header', 'footer'] as $section) {
62417e88313SGerrit Uitslag            foreach (['', '_odd', '_even', '_first'] as $order) {
62502f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
62633c15297SGerrit Uitslag                if (file_exists($file)) {
62733c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
62833c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
62933c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
6302eedf77dSAndreas Gohr
6312eedf77dSAndreas Gohr                    // register the needed pseudo CSS
63233c15297SGerrit Uitslag                    if ($order == '_first') {
63333c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
63433c15297SGerrit Uitslag                    } elseif ($order == '_even') {
63533c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
63633c15297SGerrit Uitslag                    } elseif ($order == '_odd') {
63733c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
638daa70883SAndreas Gohr                    } else {
63933c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
6402eedf77dSAndreas Gohr                    }
6412eedf77dSAndreas Gohr                }
6422eedf77dSAndreas Gohr            }
6432eedf77dSAndreas Gohr        }
6442eedf77dSAndreas Gohr
6451ef68647SAndreas Gohr        // prepare replacements
64617e88313SGerrit Uitslag        $replace = [
6471ef68647SAndreas Gohr            '@PAGE@' => '{PAGENO}',
64802f9a447SGerrit Uitslag            '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
64903352761SKirsten Roschanski            '@TITLE@' => hsc($this->title),
6501ef68647SAndreas Gohr            '@WIKI@' => $conf['title'],
6511ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
6521ef68647SAndreas Gohr            '@DATE@' => dformat(time()),
6538b11a0e6SChris75forumname            '@USERNAME@' => $INFO['userinfo']['name'] ?? '',
6545d6fbaeaSAndreas Gohr            '@BASE@' => DOKU_BASE,
655893987a2SAndreas Gohr            '@INC@' => DOKU_INC,
656893987a2SAndreas Gohr            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
657893987a2SAndreas Gohr            '@TPLINC@' => DOKU_INC . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
65817e88313SGerrit Uitslag        ];
6591ef68647SAndreas Gohr
6602eedf77dSAndreas Gohr        // set HTML element
661a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
662a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
663852931daSAndreas Gohr        $output['html'] = $this->pageDependReplacements($html, $ID);
6641ef68647SAndreas Gohr
6651e45476bSmnapp        // cover page
66602f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
66733c15297SGerrit Uitslag        if (file_exists($coverfile)) {
66833c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
6691e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
670852931daSAndreas Gohr            $output['cover'] = $this->pageDependReplacements($output['cover'], $ID);
6716e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
6721e45476bSmnapp        }
6731e45476bSmnapp
67433c15297SGerrit Uitslag        // cover page
67502f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
67633c15297SGerrit Uitslag        if (file_exists($backfile)) {
67733c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
67833c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
67933c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
680852931daSAndreas Gohr            $output['back'] = $this->pageDependReplacements($output['back'], $ID);
68133c15297SGerrit Uitslag        }
68233c15297SGerrit Uitslag
6832eedf77dSAndreas Gohr        // citation box
68402f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
68533c15297SGerrit Uitslag        if (file_exists($citationfile)) {
68633c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
6872eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
6882eedf77dSAndreas Gohr        }
6891ef68647SAndreas Gohr
6902eedf77dSAndreas Gohr        return $output;
6911ef68647SAndreas Gohr    }
6921ef68647SAndreas Gohr
6931ef68647SAndreas Gohr    /**
694a180c973SKlap-in     * @param string $raw code with placeholders
695a180c973SKlap-in     * @param string $id pageid
696a180c973SKlap-in     * @return string
697a180c973SKlap-in     */
698852931daSAndreas Gohr    protected function pageDependReplacements($raw, $id)
69917e88313SGerrit Uitslag    {
700e53f1ec0SSzymon Olewniczak        global $REV, $DATE_AT;
701a180c973SKlap-in
702fb347f35SAndreas Gohr        // generate qr code for this page
703a180c973SKlap-in        $qr_code = '';
704fb347f35SAndreas Gohr        if ($this->getConf('qrcodescale')) {
705fb347f35SAndreas Gohr            $url = hsc(wl($id, '', '&', true));
706852931daSAndreas Gohr            $size = (float)$this->getConf('qrcodescale');
707852931daSAndreas Gohr            $qr_code = sprintf(
708852931daSAndreas Gohr                '<barcode type="QR" code="%s" error="Q" disableborder="1" class="qrcode" size="%s" />',
709852931daSAndreas Gohr                $url,
710852931daSAndreas Gohr                $size
711852931daSAndreas Gohr            );
712a180c973SKlap-in        }
713a180c973SKlap-in        // prepare replacements
714a180c973SKlap-in        $replace['@ID@'] = $id;
715a180c973SKlap-in        $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV)));
716e53f1ec0SSzymon Olewniczak
71717e88313SGerrit Uitslag        $params = [];
718e53f1ec0SSzymon Olewniczak        if ($DATE_AT) {
719e53f1ec0SSzymon Olewniczak            $params['at'] = $DATE_AT;
720e53f1ec0SSzymon Olewniczak        } elseif ($REV) {
721e53f1ec0SSzymon Olewniczak            $params['rev'] = $REV;
722e53f1ec0SSzymon Olewniczak        }
723e53f1ec0SSzymon Olewniczak        $replace['@PAGEURL@'] = wl($id, $params, true, "&");
724a180c973SKlap-in        $replace['@QRCODE@'] = $qr_code;
725a180c973SKlap-in
726d824d23dSAnna Dabrowska        $content = $raw;
727a12c41c3SAnna Dabrowska
728a12c41c3SAnna Dabrowska        // let other plugins define their own replacements
729d824d23dSAnna Dabrowska        $evdata = ['id' => $id, 'replace' => &$replace, 'content' => &$content];
730852931daSAndreas Gohr        $event = new Event('PLUGIN_DW2PDF_REPLACE', $evdata);
731d824d23dSAnna Dabrowska        if ($event->advise_before()) {
732e3d68265SGerrit Uitslag            $content = str_replace(array_keys($replace), array_values($replace), $raw);
733d824d23dSAnna Dabrowska        }
734e3d68265SGerrit Uitslag
735a12c41c3SAnna Dabrowska        // plugins may post-process HTML, e.g to clean up unused replacements
736a12c41c3SAnna Dabrowska        $event->advise_after();
737a12c41c3SAnna Dabrowska
738e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
739e3d68265SGerrit Uitslag        $content = preg_replace_callback(
740e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
741852931daSAndreas Gohr            [$this, 'replaceDate'],
742e3d68265SGerrit Uitslag            $content
743e3d68265SGerrit Uitslag        );
744e3d68265SGerrit Uitslag
745e3d68265SGerrit Uitslag        return $content;
746a180c973SKlap-in    }
747a180c973SKlap-in
748e3d68265SGerrit Uitslag
749e3d68265SGerrit Uitslag    /**
750e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
751e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
752e3d68265SGerrit Uitslag     *
753e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
754e3d68265SGerrit Uitslag     * @return string
755e3d68265SGerrit Uitslag     */
756852931daSAndreas Gohr    public function replaceDate($match)
75717e88313SGerrit Uitslag    {
758e3d68265SGerrit Uitslag        global $conf;
759e3d68265SGerrit Uitslag        //no 2nd argument for default date format
760e3d68265SGerrit Uitslag        if ($match[2] == null) {
761e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
762e3d68265SGerrit Uitslag        }
763e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
764e3d68265SGerrit Uitslag    }
765e3d68265SGerrit Uitslag
766a180c973SKlap-in    /**
7671c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
7681ef68647SAndreas Gohr     */
769852931daSAndreas Gohr    protected function loadCSS()
77017e88313SGerrit Uitslag    {
771737417c6SKlap-in        global $conf;
7727c79bc79SGerrit Uitslag        //reuse the CSS dispatcher functions without triggering the main function
7731c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
7741c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
775ee19bac3SLuigi Micco
7761c14c879SAndreas Gohr        // prepare CSS files
7771c14c879SAndreas Gohr        $files = array_merge(
77817e88313SGerrit Uitslag            [
77917e88313SGerrit Uitslag                DOKU_INC . 'lib/styles/screen.css' => DOKU_BASE . 'lib/styles/',
78017e88313SGerrit Uitslag                DOKU_INC . 'lib/styles/print.css' => DOKU_BASE . 'lib/styles/',
78117e88313SGerrit Uitslag            ],
782852931daSAndreas Gohr            $this->cssPluginPDFstyles(),
78317e88313SGerrit Uitslag            [
78417e88313SGerrit Uitslag                DOKU_PLUGIN . 'dw2pdf/conf/style.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
785852931daSAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' =>
786852931daSAndreas Gohr                    DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
78717e88313SGerrit Uitslag                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
78817e88313SGerrit Uitslag            ]
7891c14c879SAndreas Gohr        );
7901c14c879SAndreas Gohr        $css = '';
7911c14c879SAndreas Gohr        foreach ($files as $file => $location) {
79228e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
79328e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
7941c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
7951ef68647SAndreas Gohr        }
7961ef68647SAndreas Gohr
79728e636eaSGerrit Uitslag        if (function_exists('css_parseless')) {
7981c14c879SAndreas Gohr            // apply pattern replacements
7997c6ca3bcSMichael Große            if (function_exists('css_styleini')) {
8007c6ca3bcSMichael Große                // compatiblity layer for pre-Greebo releases of DokuWiki
80128e636eaSGerrit Uitslag                $styleini = css_styleini($conf['template']);
8027c6ca3bcSMichael Große            } else {
8037c6ca3bcSMichael Große                // Greebo functionality
80417e88313SGerrit Uitslag                $styleUtils = new StyleUtils();
805d0f0c534SGerrit Uitslag                $styleini = $styleUtils->cssStyleini($conf['template']); // older versions need still the template
8067c6ca3bcSMichael Große            }
80728e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
80828e636eaSGerrit Uitslag
80928e636eaSGerrit Uitslag            // parse less
81028e636eaSGerrit Uitslag            $css = css_parseless($css);
81128e636eaSGerrit Uitslag        } else {
81228e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
8131c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
81428e636eaSGerrit Uitslag        }
8151ef68647SAndreas Gohr
8161c14c879SAndreas Gohr        return $css;
817ee19bac3SLuigi Micco    }
8181c14c879SAndreas Gohr
81958e6409eSAndreas Gohr    /**
82058e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
82158e6409eSAndreas Gohr     *
82258e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
82358e6409eSAndreas Gohr     *
82458e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
82558e6409eSAndreas Gohr     */
826852931daSAndreas Gohr    protected function cssPluginPDFstyles()
82717e88313SGerrit Uitslag    {
82817e88313SGerrit Uitslag        $list = [];
82958e6409eSAndreas Gohr        $plugins = plugin_list();
830f54b51f7SAndreas Gohr
831f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
83258e6409eSAndreas Gohr        foreach ($plugins as $p) {
833f54b51f7SAndreas Gohr            if (in_array($p, $usestyle)) {
834f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
8358003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/";
8368003b493SGerrit Uitslag
837f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
8388003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/";
839f54b51f7SAndreas Gohr            }
840f54b51f7SAndreas Gohr
8418003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/";
8428003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/";
8438003b493SGerrit Uitslag
844ab96d816SMichael Große            if (file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) {
84558e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
8468003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/";
84758e6409eSAndreas Gohr            } else {
84858e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
8498003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/";
85058e6409eSAndreas Gohr            }
85158e6409eSAndreas Gohr        }
8528b8ac6ecSAndreas Gohr
8538b8ac6ecSAndreas Gohr        // template support
854852931daSAndreas Gohr        foreach (
855852931daSAndreas Gohr            [
856852931daSAndreas Gohr                     'pdf.css',
857852931daSAndreas Gohr                     'pdf.less',
858852931daSAndreas Gohr                     'css/pdf.css',
859852931daSAndreas Gohr                     'css/pdf.less',
860852931daSAndreas Gohr                     'styles/pdf.css',
861852931daSAndreas Gohr                     'styles/pdf.less'
862852931daSAndreas Gohr                 ] as $file
863852931daSAndreas Gohr        ) {
8648b8ac6ecSAndreas Gohr            if (file_exists(tpl_incdir() . $file)) {
8658b8ac6ecSAndreas Gohr                $list[tpl_incdir() . $file] = tpl_basedir() . $file;
8668b8ac6ecSAndreas Gohr            }
8678b8ac6ecSAndreas Gohr        }
8688b8ac6ecSAndreas Gohr
86958e6409eSAndreas Gohr        return $list;
87058e6409eSAndreas Gohr    }
871ad18f4e1SGerrit Uitslag
872ad18f4e1SGerrit Uitslag    /**
87360e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
87460e59de7SGerrit Uitslag     *
87560e59de7SGerrit Uitslag     * @return array
87660e59de7SGerrit Uitslag     */
87717e88313SGerrit Uitslag    public function getExportedPages()
87817e88313SGerrit Uitslag    {
87960e59de7SGerrit Uitslag        return $this->list;
88060e59de7SGerrit Uitslag    }
88160e59de7SGerrit Uitslag
88260e59de7SGerrit Uitslag    /**
883ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
88444e8e8fbSGerrit Uitslag     *
88544e8e8fbSGerrit Uitslag     * @param array $a
88644e8e8fbSGerrit Uitslag     * @param array $b
88744e8e8fbSGerrit Uitslag     * @return int
888ad18f4e1SGerrit Uitslag     */
889852931daSAndreas Gohr    public function cbDateSort($a, $b)
89017e88313SGerrit Uitslag    {
891ad18f4e1SGerrit Uitslag        if ($b['rev'] < $a['rev']) return -1;
892ad18f4e1SGerrit Uitslag        if ($b['rev'] > $a['rev']) return 1;
893ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
894ad18f4e1SGerrit Uitslag    }
895ad18f4e1SGerrit Uitslag
896ad18f4e1SGerrit Uitslag    /**
897ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
89844e8e8fbSGerrit Uitslag     * @param array $a
89944e8e8fbSGerrit Uitslag     * @param array $b
90044e8e8fbSGerrit Uitslag     * @return int
901ad18f4e1SGerrit Uitslag     */
902852931daSAndreas Gohr    public function cbPagenameSort($a, $b)
90317e88313SGerrit Uitslag    {
90420025b90SAndreas Gohr        global $conf;
90520025b90SAndreas Gohr
90620025b90SAndreas Gohr        $partsA = explode(':', $a['id']);
90720025b90SAndreas Gohr        $countA = count($partsA);
90820025b90SAndreas Gohr        $partsB = explode(':', $b['id']);
90920025b90SAndreas Gohr        $countB = count($partsB);
910e00f6071SAndreas Gohr        $max = max($countA, $countB);
91120025b90SAndreas Gohr
91220025b90SAndreas Gohr
91320025b90SAndreas Gohr        // compare namepsace by namespace
914e00f6071SAndreas Gohr        for ($i = 0; $i < $max; $i++) {
915e00f6071SAndreas Gohr            $partA = $partsA[$i] ?: null;
916e00f6071SAndreas Gohr            $partB = $partsB[$i] ?: null;
91720025b90SAndreas Gohr
91820025b90SAndreas Gohr            // have we reached the page level?
919e00f6071SAndreas Gohr            if ($i === ($countA - 1) || $i === ($countB - 1)) {
92020025b90SAndreas Gohr                // start page first
92120025b90SAndreas Gohr                if ($partA == $conf['start']) return -1;
92220025b90SAndreas Gohr                if ($partB == $conf['start']) return 1;
92320025b90SAndreas Gohr            }
92420025b90SAndreas Gohr
925e00f6071SAndreas Gohr            // prefer page over namespace
926e00f6071SAndreas Gohr            if ($partA === $partB) {
927e00f6071SAndreas Gohr                if (!isset($partsA[$i + 1])) return -1;
928e00f6071SAndreas Gohr                if (!isset($partsB[$i + 1])) return 1;
929e00f6071SAndreas Gohr                continue;
930e00f6071SAndreas Gohr            }
931e00f6071SAndreas Gohr
932e00f6071SAndreas Gohr
93320025b90SAndreas Gohr            // simply compare
93441e5d4e2SAndreas Gohr            return strnatcmp($partA, $partB);
93520025b90SAndreas Gohr        }
93620025b90SAndreas Gohr
93741e5d4e2SAndreas Gohr        return strnatcmp($a['id'], $b['id']);
938ad18f4e1SGerrit Uitslag    }
93926be4eceSGerrit Uitslag
94026be4eceSGerrit Uitslag    /**
9417c79bc79SGerrit Uitslag     * Collects settings from:
94202f9a447SGerrit Uitslag     *   1. url parameters
94302f9a447SGerrit Uitslag     *   2. plugin config
94402f9a447SGerrit Uitslag     *   3. global config
94502f9a447SGerrit Uitslag     */
94617e88313SGerrit Uitslag    protected function loadExportConfig()
94717e88313SGerrit Uitslag    {
94802f9a447SGerrit Uitslag        global $INPUT;
94902f9a447SGerrit Uitslag        global $conf;
95002f9a447SGerrit Uitslag
95117e88313SGerrit Uitslag        $this->exportConfig = [];
95202f9a447SGerrit Uitslag
95302f9a447SGerrit Uitslag        // decide on the paper setup from param or config
95402f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
95502f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
95602f9a447SGerrit Uitslag
9574870b378SLarsDW223        // decide on the font-size from param or config
9584870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
9594870b378SLarsDW223
960213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool)$this->getConf('doublesided'));
961213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
962213fdb75SGerrit Uitslag
963b80f709fSNicolas        $this->exportConfig['watermark'] = $INPUT->str('watermark', '');
964b80f709fSNicolas
965213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool)$this->getConf('toc'));
96617e88313SGerrit Uitslag        $levels = [];
96702f9a447SGerrit Uitslag        if ($hasToC) {
96802f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
969852931daSAndreas Gohr            [$top_input, $max_input] = array_pad(explode('-', $toclevels, 2), 2, '');
970852931daSAndreas Gohr            [$top_conf, $max_conf] = array_pad(explode('-', $this->getConf('toclevels'), 2), 2, '');
97117e88313SGerrit Uitslag            $bounds_input = [
97217e88313SGerrit Uitslag                'top' => [
97302f9a447SGerrit Uitslag                    (int)$top_input,
97402f9a447SGerrit Uitslag                    (int)$top_conf
97517e88313SGerrit Uitslag                ],
97617e88313SGerrit Uitslag                'max' => [
97702f9a447SGerrit Uitslag                    (int)$max_input,
97802f9a447SGerrit Uitslag                    (int)$max_conf
97917e88313SGerrit Uitslag                ]
98017e88313SGerrit Uitslag            ];
98117e88313SGerrit Uitslag            $bounds = [
98202f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
98302f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
98402f9a447SGerrit Uitslag
98517e88313SGerrit Uitslag            ];
98602f9a447SGerrit Uitslag            foreach ($bounds_input as $bound => $values) {
98702f9a447SGerrit Uitslag                foreach ($values as $value) {
98802f9a447SGerrit Uitslag                    if ($value > 0 && $value <= 5) {
98902f9a447SGerrit Uitslag                        //stop at valid value and store
99002f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
99102f9a447SGerrit Uitslag                        break;
99202f9a447SGerrit Uitslag                    }
99302f9a447SGerrit Uitslag                }
99402f9a447SGerrit Uitslag            }
99502f9a447SGerrit Uitslag
99602f9a447SGerrit Uitslag            if ($bounds['max'] < $bounds['top']) {
99702f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
99802f9a447SGerrit Uitslag            }
99902f9a447SGerrit Uitslag
100002f9a447SGerrit Uitslag            for ($level = $bounds['top']; $level <= $bounds['max']; $level++) {
100102f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
100202f9a447SGerrit Uitslag            }
100302f9a447SGerrit Uitslag        }
100402f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
100502f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
100602f9a447SGerrit Uitslag
100702f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
100802f9a447SGerrit Uitslag
100902f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
101005d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
101102f9a447SGerrit Uitslag        if (!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
101202f9a447SGerrit Uitslag            $tpl = $tplconf;
101302f9a447SGerrit Uitslag        }
101402f9a447SGerrit Uitslag        if (!$tpl) {
101502f9a447SGerrit Uitslag            $tpl = 'default';
101602f9a447SGerrit Uitslag        }
101702f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
101802f9a447SGerrit Uitslag
101902f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
102002f9a447SGerrit Uitslag    }
102102f9a447SGerrit Uitslag
102202f9a447SGerrit Uitslag    /**
102302f9a447SGerrit Uitslag     * Returns requested config
102402f9a447SGerrit Uitslag     *
102502f9a447SGerrit Uitslag     * @param string $name
102602f9a447SGerrit Uitslag     * @param mixed $notset
102702f9a447SGerrit Uitslag     * @return mixed|bool
102802f9a447SGerrit Uitslag     */
102917e88313SGerrit Uitslag    public function getExportConfig($name, $notset = false)
103017e88313SGerrit Uitslag    {
103102f9a447SGerrit Uitslag        if ($this->exportConfig === null) {
103202f9a447SGerrit Uitslag            $this->loadExportConfig();
103302f9a447SGerrit Uitslag        }
103402f9a447SGerrit Uitslag
103517e88313SGerrit Uitslag        return $this->exportConfig[$name] ?? $notset;
103602f9a447SGerrit Uitslag    }
1037d63e7fe7SGerrit Uitslag
1038d63e7fe7SGerrit Uitslag    /**
1039d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
1040d63e7fe7SGerrit Uitslag     *
1041d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
1042d63e7fe7SGerrit Uitslag     */
1043852931daSAndreas Gohr    public function addbutton(Event $event)
104417e88313SGerrit Uitslag    {
1045e53f1ec0SSzymon Olewniczak        global $ID, $REV, $DATE_AT;
1046d63e7fe7SGerrit Uitslag
1047d63e7fe7SGerrit Uitslag        if ($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
104817e88313SGerrit Uitslag            $params = ['do' => 'export_pdf'];
1049e53f1ec0SSzymon Olewniczak            if ($DATE_AT) {
1050e53f1ec0SSzymon Olewniczak                $params['at'] = $DATE_AT;
1051e53f1ec0SSzymon Olewniczak            } elseif ($REV) {
1052d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
1053d63e7fe7SGerrit Uitslag            }
1054d63e7fe7SGerrit Uitslag
1055d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
1056d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
1057852931daSAndreas Gohr                ['export_pdf' => sprintf(
1058852931daSAndreas Gohr                    '<li><a href="%s" class="%s" rel="nofollow" title="%s"><span>%s</span></a></li>',
1059852931daSAndreas Gohr                    wl($ID, $params),
1060852931daSAndreas Gohr                    'action export_pdf',
1061852931daSAndreas Gohr                    $this->getLang('export_pdf_button'),
1062852931daSAndreas Gohr                    $this->getLang('export_pdf_button')
1063852931daSAndreas Gohr                )] +
1064d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
1065d63e7fe7SGerrit Uitslag        }
1066d63e7fe7SGerrit Uitslag    }
1067026b594bSAndreas Gohr
1068026b594bSAndreas Gohr    /**
1069026b594bSAndreas Gohr     * Add 'export pdf' button to page tools, new SVG based mechanism
1070026b594bSAndreas Gohr     *
1071026b594bSAndreas Gohr     * @param Doku_Event $event
1072026b594bSAndreas Gohr     */
1073852931daSAndreas Gohr    public function addsvgbutton(Event $event)
107417e88313SGerrit Uitslag    {
10754c493e44SGerrit Uitslag        global $INFO;
10764c493e44SGerrit Uitslag        if ($event->data['view'] != 'page' || !$this->getConf('showexportbutton')) {
10774c493e44SGerrit Uitslag            return;
10784c493e44SGerrit Uitslag        }
10794c493e44SGerrit Uitslag
10804c493e44SGerrit Uitslag        if (!$INFO['exists']) {
10814c493e44SGerrit Uitslag            return;
10824c493e44SGerrit Uitslag        }
10834c493e44SGerrit Uitslag
108417e88313SGerrit Uitslag        array_splice($event->data['items'], -1, 0, [new MenuItem()]);
1085026b594bSAndreas Gohr    }
1086979c9cf5SAndreas Gohr
1087979c9cf5SAndreas Gohr    /**
1088979c9cf5SAndreas Gohr     * Get the language of the current document
1089979c9cf5SAndreas Gohr     *
1090979c9cf5SAndreas Gohr     * Uses the translation plugin if available
1091979c9cf5SAndreas Gohr     * @return string
1092979c9cf5SAndreas Gohr     */
1093979c9cf5SAndreas Gohr    protected function getDocumentLanguage($pageid)
1094979c9cf5SAndreas Gohr    {
1095979c9cf5SAndreas Gohr        global $conf;
1096979c9cf5SAndreas Gohr
1097979c9cf5SAndreas Gohr        $lang = $conf['lang'];
1098979c9cf5SAndreas Gohr        /** @var helper_plugin_translation $trans */
1099979c9cf5SAndreas Gohr        $trans = plugin_load('helper', 'translation');
1100979c9cf5SAndreas Gohr        if ($trans) {
1101979c9cf5SAndreas Gohr            $tr = $trans->getLangPart($pageid);
1102979c9cf5SAndreas Gohr            if ($tr) $lang = $tr;
1103979c9cf5SAndreas Gohr        }
1104979c9cf5SAndreas Gohr
1105979c9cf5SAndreas Gohr        return $lang;
1106979c9cf5SAndreas Gohr    }
1107ee19bac3SLuigi Micco}
1108