xref: /plugin/dw2pdf/action.php (revision 979c9cf518cc0cb5fa209d5dd01d48cfc68df73c)
1ee19bac3SLuigi Micco<?php
2ee19bac3SLuigi Micco/**
3ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
4ee19bac3SLuigi Micco *
5ee19bac3SLuigi Micco * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee19bac3SLuigi Micco * @author     Luigi Micco <l.micco@tiscali.it>
75db42babSAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
8ee19bac3SLuigi Micco */
9ee19bac3SLuigi Micco
100639157eSGerrit Uitslag/**
110639157eSGerrit Uitslag * Class action_plugin_dw2pdf
120639157eSGerrit Uitslag *
132a127a1dSGerrit Uitslag * Export html content to pdf, for different url parameter configurations
140639157eSGerrit Uitslag * DokuPDF which extends mPDF is used for generating the pdf from html.
150639157eSGerrit Uitslag */
160833b7cdSStephan Bauer
170833b7cdSStephan Baueruse dokuwiki\Cache\Cache;
180833b7cdSStephan Bauer
191ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
2002f9a447SGerrit Uitslag    /**
2102f9a447SGerrit Uitslag     * Settings for current export, collected from url param, plugin config, global config
2202f9a447SGerrit Uitslag     *
2302f9a447SGerrit Uitslag     * @var array
2402f9a447SGerrit Uitslag     */
25213fdb75SGerrit Uitslag    protected $exportConfig = null;
262a127a1dSGerrit Uitslag    /** @var string template name, to use templates from dw2pdf/tpl/<template name> */
2760e59de7SGerrit Uitslag    protected $tpl;
282a127a1dSGerrit Uitslag    /** @var string title of exported pdf */
2903352761SKirsten Roschanski    protected $title;
306a7f9d6cSGerrit Uitslag    /** @var array list of pages included in exported pdf */
3160e59de7SGerrit Uitslag    protected $list = array();
322a127a1dSGerrit Uitslag    /** @var bool|string path to temporary cachefile */
339b288b2aSGerrit Uitslag    protected $onetimefile = false;
349c76f78dSVincent GIRARD    protected $currentBookChapter = 0;
351c14c879SAndreas Gohr
361c14c879SAndreas Gohr    /**
371c14c879SAndreas Gohr     * Constructor. Sets the correct template
381c14c879SAndreas Gohr     */
392a127a1dSGerrit Uitslag    public function __construct() {
4002f9a447SGerrit Uitslag        $this->tpl   = $this->getExportConfig('template');
411c14c879SAndreas Gohr    }
421c14c879SAndreas Gohr
43ee19bac3SLuigi Micco    /**
449b288b2aSGerrit Uitslag     * Delete cached files that were for one-time use
459b288b2aSGerrit Uitslag     */
469b288b2aSGerrit Uitslag    public function __destruct() {
479b288b2aSGerrit Uitslag        if($this->onetimefile) {
489b288b2aSGerrit Uitslag            unlink($this->onetimefile);
499b288b2aSGerrit Uitslag        }
509b288b2aSGerrit Uitslag    }
519b288b2aSGerrit Uitslag
529b288b2aSGerrit Uitslag    /**
539c76f78dSVincent GIRARD     * Return the value of currentBookChapter, which is the order of the file to be added in a book generation
549c76f78dSVincent GIRARD     */
559c76f78dSVincent GIRARD    public function getCurrentBookChapter()
569c76f78dSVincent GIRARD    {
579c76f78dSVincent GIRARD        return $this->currentBookChapter;
589c76f78dSVincent GIRARD    }
599c76f78dSVincent GIRARD
609c76f78dSVincent GIRARD    /**
61ee19bac3SLuigi Micco     * Register the events
62177a7d30SGerrit Uitslag     *
63177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
64ee19bac3SLuigi Micco     */
656be736bfSGerrit Uitslag    public function register(Doku_Event_Handler $controller) {
66ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
676be736bfSGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
68026b594bSAndreas Gohr        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array());
69ee19bac3SLuigi Micco    }
70ee19bac3SLuigi Micco
711c14c879SAndreas Gohr    /**
721c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
73737417c6SKlap-in     *
74737417c6SKlap-in     * @param Doku_Event $event
751c14c879SAndreas Gohr     */
7644e8e8fbSGerrit Uitslag    public function convert(Doku_Event $event) {
772a127a1dSGerrit Uitslag        global $REV, $DATE_AT;
782d9cd424SGerrit Uitslag        global $conf, $INPUT;
79ee19bac3SLuigi Micco
801ef68647SAndreas Gohr        // our event?
812a127a1dSGerrit Uitslag        $allowedEvents = ['export_pdfbook', 'export_pdf', 'export_pdfns'];
822a127a1dSGerrit Uitslag        if(!in_array($event->data, $allowedEvents)) return;
83ee19bac3SLuigi Micco
842a127a1dSGerrit Uitslag        try{
852a127a1dSGerrit Uitslag            //collect pages and check permissions
862a127a1dSGerrit Uitslag            list($this->title, $this->list) = $this->collectExportablePages($event);
87d63e7fe7SGerrit Uitslag
882d9cd424SGerrit Uitslag            if($event->data === 'export_pdf' && ($REV || $DATE_AT)) {
899b288b2aSGerrit Uitslag                $cachefile = tempnam($conf['tmpdir'] . '/dwpdf', 'dw2pdf_');
909b288b2aSGerrit Uitslag                $this->onetimefile = $cachefile;
91f00df45eSMichael Große                $generateNewPdf = true;
92f00df45eSMichael Große            } else {
93a58f45f0SGerrit Uitslag                // prepare cache and its dependencies
94a58f45f0SGerrit Uitslag                $depends = array();
9503352761SKirsten Roschanski                $cache = $this->prepareCache($depends);
969b288b2aSGerrit Uitslag                $cachefile = $cache->cache;
9727195d5bSMichael Große                $generateNewPdf = !$this->getConf('usecache')
9827195d5bSMichael Große                    || $this->getExportConfig('isDebug')
9927195d5bSMichael Große                    || !$cache->useCache($depends);
100f00df45eSMichael Große            }
101d63e7fe7SGerrit Uitslag
102bd977188SGerrit Uitslag            // hard work only when no cache available or needed for debugging
103f00df45eSMichael Große            if($generateNewPdf) {
104e5f6c2cbSMichael Große                // generating the pdf may take a long time for larger wikis / namespaces with many pages
105e5f6c2cbSMichael Große                set_time_limit(0);
1062a127a1dSGerrit Uitslag                //may throw Mpdf\MpdfException as well
1079b288b2aSGerrit Uitslag                $this->generatePDF($cachefile, $event);
1082a127a1dSGerrit Uitslag            }
1092a127a1dSGerrit Uitslag        } catch(Exception $e) {
1102d9cd424SGerrit Uitslag            if($INPUT->has('selection')) {
1112d9cd424SGerrit Uitslag                http_status(400);
1122d9cd424SGerrit Uitslag                print $e->getMessage();
1132d9cd424SGerrit Uitslag                exit();
1142d9cd424SGerrit Uitslag            } else {
1152a127a1dSGerrit Uitslag                //prevent Action/Export()
116b34cb34eSSzymon Olewniczak                msg($e->getMessage(), -1);
1172a127a1dSGerrit Uitslag                $event->data = 'redirect';
118d9c13ec7SGerrit Uitslag                return;
119b34cb34eSSzymon Olewniczak            }
120d63e7fe7SGerrit Uitslag        }
1212d9cd424SGerrit Uitslag        $event->preventDefault(); // after prevent, $event->data cannot be changed
122d63e7fe7SGerrit Uitslag
123d63e7fe7SGerrit Uitslag        // deliver the file
1249b288b2aSGerrit Uitslag        $this->sendPDFFile($cachefile);  //exits
125d63e7fe7SGerrit Uitslag    }
126d63e7fe7SGerrit Uitslag
127d63e7fe7SGerrit Uitslag    /**
1282a127a1dSGerrit Uitslag     * Obtain list of pages and title, for different methods of exporting the pdf.
1292a127a1dSGerrit Uitslag     *  - Return a title and selection, throw otherwise an exception
1302a127a1dSGerrit Uitslag     *  - Check permisions
131d63e7fe7SGerrit Uitslag     *
132d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
1337c79bc79SGerrit Uitslag     * @return array|false
1342a127a1dSGerrit Uitslag     * @throws Exception
135d63e7fe7SGerrit Uitslag     */
1362a127a1dSGerrit Uitslag    protected function collectExportablePages(Doku_Event $event) {
13736a7917dSGerrit Uitslag        global $ID, $REV;
138d63e7fe7SGerrit Uitslag        global $INPUT;
1392a127a1dSGerrit Uitslag        global $conf, $lang;
140d63e7fe7SGerrit Uitslag
141d63e7fe7SGerrit Uitslag        // list of one or multiple pages
142d63e7fe7SGerrit Uitslag        $list = array();
14328e636eaSGerrit Uitslag
1444b4cebc2SLarsDW223        if($event->data == 'export_pdf') {
1452a127a1dSGerrit Uitslag            if(auth_quickaclcheck($ID) < AUTH_READ) {  // set more specific denied message
1462a127a1dSGerrit Uitslag                throw new Exception($lang['accessdenied']);
1472a127a1dSGerrit Uitslag            }
148d63e7fe7SGerrit Uitslag            $list[0] = $ID;
1492a127a1dSGerrit Uitslag            $title = $INPUT->str('pdftitle'); //DEPRECATED
1502a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
1512a127a1dSGerrit Uitslag            if(empty($title)) {
1522a127a1dSGerrit Uitslag                $title = p_get_first_heading($ID);
15315923cb9SGerrit Uitslag            }
154ce8af5d0SHativ            // use page name if title is still empty
1552a127a1dSGerrit Uitslag            if(empty($title)) {
1562a127a1dSGerrit Uitslag                $title = noNS($ID);
157ce8af5d0SHativ            }
158ad18f4e1SGerrit Uitslag
15936a7917dSGerrit Uitslag            $filename = wikiFN($ID, $REV);
16036a7917dSGerrit Uitslag            if(!file_exists($filename)) {
1612a127a1dSGerrit Uitslag                throw new Exception($this->getLang('notexist'));
16236a7917dSGerrit Uitslag            }
16336a7917dSGerrit Uitslag
1644b4cebc2SLarsDW223        } elseif($event->data == 'export_pdfns') {
165ad18f4e1SGerrit Uitslag            //check input for title and ns
1662a127a1dSGerrit Uitslag            if(!$title = $INPUT->str('book_title')) {
1672a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
168ad18f4e1SGerrit Uitslag            }
169177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
170ad18f4e1SGerrit Uitslag            if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
1712a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needns'));
172ad18f4e1SGerrit Uitslag            }
173ad18f4e1SGerrit Uitslag
17426be4eceSGerrit Uitslag            //sort order
175177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
176ad18f4e1SGerrit Uitslag            $sortoptions = array('pagename', 'date', 'natural');
177ad18f4e1SGerrit Uitslag            if(!in_array($order, $sortoptions)) {
178ad18f4e1SGerrit Uitslag                $order = 'natural';
179ad18f4e1SGerrit Uitslag            }
180ad18f4e1SGerrit Uitslag
18126be4eceSGerrit Uitslag            //search depth
182177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
183ad18f4e1SGerrit Uitslag            if($depth < 0) {
184ad18f4e1SGerrit Uitslag                $depth = 0;
185ad18f4e1SGerrit Uitslag            }
18626be4eceSGerrit Uitslag
187ad18f4e1SGerrit Uitslag            //page search
188ad18f4e1SGerrit Uitslag            $result = array();
189ad18f4e1SGerrit Uitslag            $opts = array('depth' => $depth); //recursive all levels
190ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
191ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
192ad18f4e1SGerrit Uitslag
19364541781SAnna Dabrowska            // exclude ids
19464541781SAnna Dabrowska            $excludes = $INPUT->arr('excludes');
19564541781SAnna Dabrowska            if (!empty($excludes)) {
196d31b75d5SAnna Dabrowska                $result = array_filter($result, function ($item) use ($excludes) {
1978c25a9b9SGerrit Uitslag                    return !in_array($item['id'], $excludes);
1988c25a9b9SGerrit Uitslag                });
1998c25a9b9SGerrit Uitslag            }
2008c25a9b9SGerrit Uitslag            // exclude namespaces
2018c25a9b9SGerrit Uitslag            $excludesns = $INPUT->arr('excludesns');
2028c25a9b9SGerrit Uitslag            if (!empty($excludesns)) {
2038c25a9b9SGerrit Uitslag                $result = array_filter($result, function ($item) use ($excludesns) {
2048c25a9b9SGerrit Uitslag                    foreach ($excludesns as $ns) {
2058c25a9b9SGerrit Uitslag                        if (strpos($item['id'], $ns . ':') === 0) return false;
2068c25a9b9SGerrit Uitslag                    }
2078c25a9b9SGerrit Uitslag                    return true;
208d31b75d5SAnna Dabrowska                });
20964541781SAnna Dabrowska            }
21064541781SAnna Dabrowska
21126be4eceSGerrit Uitslag            //sorting
212ad18f4e1SGerrit Uitslag            if(count($result) > 0) {
213ad18f4e1SGerrit Uitslag                if($order == 'date') {
214ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_datesort'));
21541e5d4e2SAndreas Gohr                } elseif ($order == 'pagename' || $order == 'natural') {
216ad18f4e1SGerrit Uitslag                    usort($result, array($this, '_pagenamesort'));
217ad18f4e1SGerrit Uitslag                }
218ad18f4e1SGerrit Uitslag            }
219ad18f4e1SGerrit Uitslag
220ad18f4e1SGerrit Uitslag            foreach($result as $item) {
221d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
222ad18f4e1SGerrit Uitslag            }
223ad18f4e1SGerrit Uitslag
224baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
225baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
226baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) {
227baa31dc5SGerrit Uitslag                        array_unshift($list,rtrim($pdfnamespace,':'));
228baa31dc5SGerrit Uitslag                    }
229baa31dc5SGerrit Uitslag                }
230baa31dc5SGerrit Uitslag            }
231baa31dc5SGerrit Uitslag
232737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
233b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator*/
23426be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
2352a127a1dSGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
2362a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2372a127a1dSGerrit Uitslag            if(empty($title)) {
2382a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
23926be4eceSGerrit Uitslag            }
240ad18f4e1SGerrit Uitslag
2412a127a1dSGerrit Uitslag            $list = explode("|", $_COOKIE['list-pagelist']);
2422a127a1dSGerrit Uitslag
243b3eed6e3SGerrit Uitslag        } elseif($INPUT->has('selection')) {
244b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
245b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
246b3eed6e3SGerrit Uitslag//                http_status(403);
247b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
248b3eed6e3SGerrit Uitslag//                exit();
249b3eed6e3SGerrit Uitslag//            }
250b3eed6e3SGerrit Uitslag
2517c79bc79SGerrit Uitslag            $list = json_decode($INPUT->str('selection', '', true), true);
252b3eed6e3SGerrit Uitslag            if (!is_array($list) || empty($list)) {
2532a127a1dSGerrit Uitslag                throw new Exception($this->getLang('empty'));
254b3eed6e3SGerrit Uitslag            }
255b3eed6e3SGerrit Uitslag
2562a127a1dSGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
2572a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2582a127a1dSGerrit Uitslag            if (empty($title)) {
2592a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
2602a127a1dSGerrit Uitslag            }
2612a127a1dSGerrit Uitslag
2622a127a1dSGerrit Uitslag        } elseif($INPUT->has('savedselection')) {
2632a127a1dSGerrit Uitslag            //export a saved selection of the Bookcreator Plugin
2642a127a1dSGerrit Uitslag            if(plugin_isdisabled('bookcreator')) {
2652a127a1dSGerrit Uitslag                throw new Exception($this->getLang('missingbookcreator'));
2662a127a1dSGerrit Uitslag            }
2672a127a1dSGerrit Uitslag            /** @var action_plugin_bookcreator_handleselection $SelectionHandling */
2682a127a1dSGerrit Uitslag            $SelectionHandling = plugin_load('action', 'bookcreator_handleselection');
2692a127a1dSGerrit Uitslag            $savedselection = $SelectionHandling->loadSavedSelection($INPUT->str('savedselection'));
2702a127a1dSGerrit Uitslag            $title = $savedselection['title'];
2712a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2722a127a1dSGerrit Uitslag            $list = $savedselection['selection'];
2732a127a1dSGerrit Uitslag
2742a127a1dSGerrit Uitslag            if(empty($title)) {
2752a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
276b3eed6e3SGerrit Uitslag            }
277b3eed6e3SGerrit Uitslag
278737417c6SKlap-in        } else {
27926be4eceSGerrit Uitslag            //show empty bookcreator message
2802a127a1dSGerrit Uitslag            throw new Exception($this->getLang('empty'));
281737417c6SKlap-in        }
282737417c6SKlap-in
283719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
284c7138b3fSGerrit Uitslag
285c7138b3fSGerrit Uitslag        $skippedpages = array();
286c7138b3fSGerrit Uitslag        foreach($list as $index => $pageid) {
287c7138b3fSGerrit Uitslag            if(auth_quickaclcheck($pageid) < AUTH_READ) {
288c7138b3fSGerrit Uitslag                $skippedpages[] = $pageid;
289c7138b3fSGerrit Uitslag                unset($list[$index]);
290c7138b3fSGerrit Uitslag            }
291c7138b3fSGerrit Uitslag        }
2922a127a1dSGerrit Uitslag        $list = array_filter($list, 'strlen'); //use of strlen() callback prevents removal of pagename '0'
293c7138b3fSGerrit Uitslag
294c7138b3fSGerrit Uitslag        //if selection contains forbidden pages throw (overridable) warning
295c7138b3fSGerrit Uitslag        if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) {
296c7138b3fSGerrit Uitslag            $msg = hsc(join(', ', $skippedpages));
2972a127a1dSGerrit Uitslag            throw new Exception(sprintf($this->getLang('forbidden'), $msg));
298c7138b3fSGerrit Uitslag        }
299c7138b3fSGerrit Uitslag
3002a127a1dSGerrit Uitslag        return array($title, $list);
301d63e7fe7SGerrit Uitslag    }
302d63e7fe7SGerrit Uitslag
303a58f45f0SGerrit Uitslag    /**
304a58f45f0SGerrit Uitslag     * Prepare cache
305a58f45f0SGerrit Uitslag     *
306a58f45f0SGerrit Uitslag     * @param array  $depends (reference) array with dependencies
307a58f45f0SGerrit Uitslag     * @return cache
308a58f45f0SGerrit Uitslag     */
30903352761SKirsten Roschanski    protected function prepareCache(&$depends) {
310a58f45f0SGerrit Uitslag        global $REV;
311a58f45f0SGerrit Uitslag
312ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
313ee19bac3SLuigi Micco            . $REV
314ee19bac3SLuigi Micco            . $this->getExportConfig('template')
315ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
316ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
317d83760efSGerrit Uitslag            . $this->getExportConfig('font-size')
318ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
3199c76f78dSVincent GIRARD            . $this->getExportConfig('headernumber')
320ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
32103352761SKirsten Roschanski            . $this->title;
3220833b7cdSStephan Bauer        $cache = new Cache($cachekey, '.dw2.pdf');
323ee19bac3SLuigi Micco
324ee19bac3SLuigi Micco        $dependencies = array();
325ee19bac3SLuigi Micco        foreach($this->list as $pageid) {
326ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
327ee19bac3SLuigi Micco
328ee19bac3SLuigi Micco            if(is_array($relations)) {
329ee19bac3SLuigi Micco                if(array_key_exists('media', $relations) && is_array($relations['media'])) {
330ee19bac3SLuigi Micco                    foreach($relations['media'] as $mediaid => $exists) {
331ee19bac3SLuigi Micco                        if($exists) {
332ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
333ee19bac3SLuigi Micco                        }
334ee19bac3SLuigi Micco                    }
335ee19bac3SLuigi Micco                }
336ee19bac3SLuigi Micco
337ee19bac3SLuigi Micco                if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
338ee19bac3SLuigi Micco                    foreach($relations['haspart'] as $part_pageid => $exists) {
339ee19bac3SLuigi Micco                        if($exists) {
340ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
341ee19bac3SLuigi Micco                        }
342ee19bac3SLuigi Micco                    }
343ee19bac3SLuigi Micco                }
344ee19bac3SLuigi Micco            }
345ee19bac3SLuigi Micco
346ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
347ee19bac3SLuigi Micco        }
348ee19bac3SLuigi Micco
349ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
350ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
351ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
352ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
353ee19bac3SLuigi Micco        $depends['files'] = array_merge(
354ee19bac3SLuigi Micco            $depends['files'],
355ee19bac3SLuigi Micco            $dependencies,
356ee19bac3SLuigi Micco            getConfigFiles('main')
357ee19bac3SLuigi Micco        );
358a58f45f0SGerrit Uitslag        return $cache;
359ee19bac3SLuigi Micco    }
360ee19bac3SLuigi Micco
361d63e7fe7SGerrit Uitslag    /**
362e53f1ec0SSzymon Olewniczak     * Returns the parsed Wikitext in dw2pdf for the given id and revision
363e53f1ec0SSzymon Olewniczak     *
364e53f1ec0SSzymon Olewniczak     * @param string     $id  page id
365e53f1ec0SSzymon Olewniczak     * @param string|int $rev revision timestamp or empty string
366e53f1ec0SSzymon Olewniczak     * @param string     $date_at
367e53f1ec0SSzymon Olewniczak     * @return null|string
368e53f1ec0SSzymon Olewniczak     */
369e53f1ec0SSzymon Olewniczak    protected function p_wiki_dw2pdf($id, $rev = '', $date_at = '') {
370e53f1ec0SSzymon Olewniczak        $file = wikiFN($id, $rev);
371e53f1ec0SSzymon Olewniczak
372e53f1ec0SSzymon Olewniczak        if(!file_exists($file)) return '';
373e53f1ec0SSzymon Olewniczak
374e53f1ec0SSzymon Olewniczak        //ensure $id is in global $ID (needed for parsing)
375e53f1ec0SSzymon Olewniczak        global $ID;
376e53f1ec0SSzymon Olewniczak        $keep = $ID;
377e53f1ec0SSzymon Olewniczak        $ID   = $id;
378e53f1ec0SSzymon Olewniczak
379e53f1ec0SSzymon Olewniczak        if($rev || $date_at) {
380e53f1ec0SSzymon Olewniczak            $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); //no caching on old revisions
381e53f1ec0SSzymon Olewniczak        } else {
382e53f1ec0SSzymon Olewniczak            $ret = p_cached_output($file, 'dw2pdf', $id);
383e53f1ec0SSzymon Olewniczak        }
384e53f1ec0SSzymon Olewniczak
385e53f1ec0SSzymon Olewniczak        //restore ID (just in case)
386e53f1ec0SSzymon Olewniczak        $ID = $keep;
387e53f1ec0SSzymon Olewniczak
388e53f1ec0SSzymon Olewniczak        return $ret;
389e53f1ec0SSzymon Olewniczak    }
390e53f1ec0SSzymon Olewniczak
391e53f1ec0SSzymon Olewniczak    /**
392d63e7fe7SGerrit Uitslag     * Build a pdf from the html
393d63e7fe7SGerrit Uitslag     *
394d63e7fe7SGerrit Uitslag     * @param string $cachefile
395d9c13ec7SGerrit Uitslag     * @param Doku_Event $event
3967c79bc79SGerrit Uitslag     * @throws \Mpdf\MpdfException
397d63e7fe7SGerrit Uitslag     */
3982d9cd424SGerrit Uitslag    protected function generatePDF($cachefile, $event) {
3992d9cd424SGerrit Uitslag        global $REV, $INPUT, $DATE_AT;
400e53f1ec0SSzymon Olewniczak
4012d9cd424SGerrit Uitslag        if ($event->data == 'export_pdf') { //only one page is exported
402e53f1ec0SSzymon Olewniczak            $rev = $REV;
403e53f1ec0SSzymon Olewniczak            $date_at = $DATE_AT;
4042a127a1dSGerrit Uitslag        } else { //we are exporting entire namespace, ommit revisions
405e53f1ec0SSzymon Olewniczak            $rev = $date_at = '';
406e53f1ec0SSzymon Olewniczak        }
40787c86ddaSAndreas Gohr
40802f9a447SGerrit Uitslag        //some shortcuts to export settings
40902f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
41002f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
41102f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
412b80f709fSNicolas        $watermark = $this->getExportConfig('watermark');
4136ea88a05SAndreas Gohr
4141ef68647SAndreas Gohr        // initialize PDF library
415cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
4166ea88a05SAndreas Gohr
417*979c9cf5SAndreas Gohr        $mpdf = new DokuPDF(
418*979c9cf5SAndreas Gohr            $this->getExportConfig('pagesize'),
4194870b378SLarsDW223            $this->getExportConfig('orientation'),
420*979c9cf5SAndreas Gohr            $this->getExportConfig('font-size'),
421*979c9cf5SAndreas Gohr            $this->getDocumentLanguage($this->list[0]) //use language of first page
422*979c9cf5SAndreas Gohr        );
423ee19bac3SLuigi Micco
424d62df65bSAndreas Gohr        // let mpdf fix local links
425d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
426d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
42721a55743SAnna Dabrowska        if(!empty($self['port'])) {
42802f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
42902f9a447SGerrit Uitslag        }
430d9c13ec7SGerrit Uitslag        $mpdf->SetBasePath($url);
431d62df65bSAndreas Gohr
43256d13144SAndreas Gohr        // Set the title
43303352761SKirsten Roschanski        $mpdf->SetTitle($this->title);
43456d13144SAndreas Gohr
435d63e7fe7SGerrit Uitslag        // some default document settings
436d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
437213fdb75SGerrit Uitslag        //      single-side document has only odd pages
438213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
439daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
440daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
44102f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
44202f9a447SGerrit Uitslag        if($hasToC) {
44302f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC
44402f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
44502f9a447SGerrit Uitslag        } else {
44602f9a447SGerrit Uitslag            $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off');
44702f9a447SGerrit Uitslag        }
4482eedf77dSAndreas Gohr
449b80f709fSNicolas        // Watermarker
450b80f709fSNicolas        if($watermark) {
451b80f709fSNicolas            $mpdf->SetWatermarkText($watermark);
452b80f709fSNicolas            $mpdf->showWatermarkText = true;
453b80f709fSNicolas        }
454b80f709fSNicolas
45556d13144SAndreas Gohr        // load the template
45603352761SKirsten Roschanski        $template = $this->load_template();
457ee19bac3SLuigi Micco
4581ef68647SAndreas Gohr        // prepare HTML header styles
459a2c33768SGerrit Uitslag        $html = '';
46002f9a447SGerrit Uitslag        if($isDebug) {
461a2c33768SGerrit Uitslag            $html .= '<html><head>';
462737417c6SKlap-in            $html .= '<style type="text/css">';
463a2c33768SGerrit Uitslag        }
464db1aa1bfSKirsten Roschanski
465db1aa1bfSKirsten Roschanski        $styles = '@page { size:auto; ' . $template['page'] . '}';
466a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
467254467c4SGerrit Uitslag
468254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
469254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
470254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
471254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
472db1aa1bfSKirsten Roschanski        $styles .= $this->load_css();
473254467c4SGerrit Uitslag
474a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
475a2c33768SGerrit Uitslag
47602f9a447SGerrit Uitslag        if($isDebug) {
477a2c33768SGerrit Uitslag            $html .= $styles;
4781ef68647SAndreas Gohr            $html .= '</style>';
4791ef68647SAndreas Gohr            $html .= '</head><body>';
480a2c33768SGerrit Uitslag        }
481a2c33768SGerrit Uitslag
482a2c33768SGerrit Uitslag        $body_start = $template['html'];
483a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
4842eedf77dSAndreas Gohr
4851e45476bSmnapp        // insert the cover page
486a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
487a2c33768SGerrit Uitslag
488a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
48902f9a447SGerrit Uitslag        if($isDebug) {
490a2c33768SGerrit Uitslag            $html .= $body_start;
491a2c33768SGerrit Uitslag        }
49202f9a447SGerrit Uitslag        if($hasToC) {
49302f9a447SGerrit Uitslag            //Note: - for double-sided document the ToC is always on an even number of pages, so that the following content is on a correct odd/even page
49402f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
49502f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
49602f9a447SGerrit Uitslag            $mpdf->TOCpagebreakByArray(
49702f9a447SGerrit Uitslag                array(
498230b098dSGerrit Uitslag                    'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
499230b098dSGerrit Uitslag                    'toc-bookmarkText' => $this->getLang('tocheader'),
50002f9a447SGerrit Uitslag                    'links' => true,
50102f9a447SGerrit Uitslag                    'outdent' => '1em',
50202f9a447SGerrit Uitslag                    'resetpagenum' => true, //start pagenumbering after ToC
50302f9a447SGerrit Uitslag                    'pagenumstyle' => '1'
50402f9a447SGerrit Uitslag                )
50502f9a447SGerrit Uitslag            );
50602f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
50702f9a447SGerrit Uitslag        }
50802f9a447SGerrit Uitslag
5091ef68647SAndreas Gohr        // loop over all pages
510c7138b3fSGerrit Uitslag        $counter = 0;
511c7138b3fSGerrit Uitslag        $no_pages = count($this->list);
512c7138b3fSGerrit Uitslag        foreach($this->list as $page) {
5139c76f78dSVincent GIRARD            $this->currentBookChapter = $counter;
514c7138b3fSGerrit Uitslag            $counter++;
515b3eed6e3SGerrit Uitslag
51679be256fSMichael Große            $pagehtml = $this->p_wiki_dw2pdf($page, $rev, $date_at);
517e53f1ec0SSzymon Olewniczak            //file doesn't exists
518e53f1ec0SSzymon Olewniczak            if($pagehtml == '') {
519b3eed6e3SGerrit Uitslag                continue;
520b3eed6e3SGerrit Uitslag            }
521719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
522c7138b3fSGerrit Uitslag            if($counter < $no_pages) {
523a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
524a2c33768SGerrit Uitslag            }
525a2c33768SGerrit Uitslag
526a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
52702f9a447SGerrit Uitslag            if($isDebug) {
528a2c33768SGerrit Uitslag                $html .= $pagehtml;
5291ef68647SAndreas Gohr            }
530ee19bac3SLuigi Micco        }
531ee19bac3SLuigi Micco
53233c15297SGerrit Uitslag        // insert the back page
533a2c33768SGerrit Uitslag        $body_end = $template['back'];
53433c15297SGerrit Uitslag
535a2c33768SGerrit Uitslag        $body_end .= '</div>';
536a2c33768SGerrit Uitslag
537d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
53802f9a447SGerrit Uitslag        if($isDebug) {
539a2c33768SGerrit Uitslag            $html .= $body_end;
540eeb17e15SAndreas Gohr            $html .= '</body>';
541eeb17e15SAndreas Gohr            $html .= '</html>';
542a2c33768SGerrit Uitslag        }
543f765508eSGerrit Uitslag
544f765508eSGerrit Uitslag        //Return html for debugging
54502f9a447SGerrit Uitslag        if($isDebug) {
54602f9a447SGerrit Uitslag            if($INPUT->str('debughtml', 'text', true) == 'html') {
54726be4eceSGerrit Uitslag                echo $html;
548a2c33768SGerrit Uitslag            } else {
549a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
550a2c33768SGerrit Uitslag                echo $html;
551a2c33768SGerrit Uitslag            }
55226be4eceSGerrit Uitslag            exit();
5537c79bc79SGerrit Uitslag        }
554f765508eSGerrit Uitslag
55587c86ddaSAndreas Gohr        // write to cache file
556d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
55787c86ddaSAndreas Gohr    }
55887c86ddaSAndreas Gohr
559d63e7fe7SGerrit Uitslag    /**
560d63e7fe7SGerrit Uitslag     * @param string $cachefile
561d63e7fe7SGerrit Uitslag     */
56203352761SKirsten Roschanski    protected function sendPDFFile($cachefile) {
56387c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
564b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
56587c86ddaSAndreas Gohr        header('Pragma: public');
566d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
567c0956f69SMichael Große        global $INPUT;
568f7b24f48SMichael Große        $outputTarget = $INPUT->str('outputTarget', $this->getConf('output'));
56987c86ddaSAndreas Gohr
57003352761SKirsten Roschanski        $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', '    ')));
571c0956f69SMichael Große        if($outputTarget === 'file') {
5729a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
57387c86ddaSAndreas Gohr        } else {
5749a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
57587c86ddaSAndreas Gohr        }
576ee19bac3SLuigi Micco
577b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
578b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
579b3eed6e3SGerrit Uitslag
580e993da11SGerrit Uitslag        //try to send file, and exit if done
581d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
58287c86ddaSAndreas Gohr
583d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
58487c86ddaSAndreas Gohr        if($fp) {
585d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
58687c86ddaSAndreas Gohr        } else {
58787c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
58887c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
58987c86ddaSAndreas Gohr        }
5901ef68647SAndreas Gohr        exit();
5911ef68647SAndreas Gohr    }
5921ef68647SAndreas Gohr
5936be736bfSGerrit Uitslag    /**
5942eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
59544e8e8fbSGerrit Uitslag     *
59644e8e8fbSGerrit Uitslag     * @return array
5971ef68647SAndreas Gohr     */
59803352761SKirsten Roschanski    protected function load_template() {
5991ef68647SAndreas Gohr        global $ID;
6001ef68647SAndreas Gohr        global $conf;
6010a11dabaSChris75forumname        global $INFO;
6021ef68647SAndreas Gohr
6032eedf77dSAndreas Gohr        // this is what we'll return
60421a55743SAnna Dabrowska        $output = [
6051e45476bSmnapp            'cover' => '',
60621a55743SAnna Dabrowska            'back' => '',
6072eedf77dSAndreas Gohr            'html'  => '',
6082eedf77dSAndreas Gohr            'page'  => '',
6092eedf77dSAndreas Gohr            'first' => '',
6102eedf77dSAndreas Gohr            'cite'  => '',
61121a55743SAnna Dabrowska        ];
6122eedf77dSAndreas Gohr
6132eedf77dSAndreas Gohr        // prepare header/footer elements
6142eedf77dSAndreas Gohr        $html = '';
61533c15297SGerrit Uitslag        foreach(array('header', 'footer') as $section) {
61633c15297SGerrit Uitslag            foreach(array('', '_odd', '_even', '_first') as $order) {
61702f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
61833c15297SGerrit Uitslag                if(file_exists($file)) {
61933c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
62033c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
62133c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
6222eedf77dSAndreas Gohr
6232eedf77dSAndreas Gohr                    // register the needed pseudo CSS
62433c15297SGerrit Uitslag                    if($order == '_first') {
62533c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
62633c15297SGerrit Uitslag                    } elseif($order == '_even') {
62733c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
62833c15297SGerrit Uitslag                    } elseif($order == '_odd') {
62933c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
630daa70883SAndreas Gohr                    } else {
63133c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
6322eedf77dSAndreas Gohr                    }
6332eedf77dSAndreas Gohr                }
6342eedf77dSAndreas Gohr            }
6352eedf77dSAndreas Gohr        }
6362eedf77dSAndreas Gohr
6371ef68647SAndreas Gohr        // prepare replacements
6381ef68647SAndreas Gohr        $replace = array(
6391ef68647SAndreas Gohr            '@PAGE@'    => '{PAGENO}',
64002f9a447SGerrit Uitslag            '@PAGES@'   => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
64103352761SKirsten Roschanski            '@TITLE@'   => hsc($this->title),
6421ef68647SAndreas Gohr            '@WIKI@'    => $conf['title'],
6431ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
6441ef68647SAndreas Gohr            '@DATE@'    => dformat(time()),
6458b11a0e6SChris75forumname            '@USERNAME@'=> $INFO['userinfo']['name'] ?? '',
6465d6fbaeaSAndreas Gohr            '@BASE@'    => DOKU_BASE,
647893987a2SAndreas Gohr            '@INC@'     => DOKU_INC,
648893987a2SAndreas Gohr            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
649893987a2SAndreas Gohr            '@TPLINC@'  => DOKU_INC . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
6501ef68647SAndreas Gohr        );
6511ef68647SAndreas Gohr
6522eedf77dSAndreas Gohr        // set HTML element
653a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
654a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
655a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
6561ef68647SAndreas Gohr
6571e45476bSmnapp        // cover page
65802f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
65933c15297SGerrit Uitslag        if(file_exists($coverfile)) {
66033c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
6611e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
6629b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
6636e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
6641e45476bSmnapp        }
6651e45476bSmnapp
66633c15297SGerrit Uitslag        // cover page
66702f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
66833c15297SGerrit Uitslag        if(file_exists($backfile)) {
66933c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
67033c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
67133c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
6729b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
67333c15297SGerrit Uitslag        }
67433c15297SGerrit Uitslag
6752eedf77dSAndreas Gohr        // citation box
67602f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
67733c15297SGerrit Uitslag        if(file_exists($citationfile)) {
67833c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
6792eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
6802eedf77dSAndreas Gohr        }
6811ef68647SAndreas Gohr
6822eedf77dSAndreas Gohr        return $output;
6831ef68647SAndreas Gohr    }
6841ef68647SAndreas Gohr
6851ef68647SAndreas Gohr    /**
686a180c973SKlap-in     * @param string $raw code with placeholders
687a180c973SKlap-in     * @param string $id  pageid
688a180c973SKlap-in     * @return string
689a180c973SKlap-in     */
690a180c973SKlap-in    protected function page_depend_replacements($raw, $id) {
691e53f1ec0SSzymon Olewniczak        global $REV, $DATE_AT;
692a180c973SKlap-in
6939b2636c8SDiego Belmar        // generate qr code for this page using quickchart.io (Google infographics api was deprecated in March 14, 2019)
694a180c973SKlap-in        $qr_code = '';
695a180c973SKlap-in        if($this->getConf('qrcodesize')) {
696a180c973SKlap-in            $url = urlencode(wl($id, '', '&', true));
6979b2636c8SDiego Belmar            $qr_code = '<img src="https://quickchart.io/qr?size=' .
6989b2636c8SDiego Belmar                $this->getConf('qrcodesize') . '&text=' . $url . '&margin=1&ecLevel=Q" />';
699a180c973SKlap-in        }
700a180c973SKlap-in        // prepare replacements
701a180c973SKlap-in        $replace['@ID@']      = $id;
702a180c973SKlap-in        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
703e53f1ec0SSzymon Olewniczak
704e53f1ec0SSzymon Olewniczak        $params = array();
705e53f1ec0SSzymon Olewniczak        if($DATE_AT) {
706e53f1ec0SSzymon Olewniczak            $params['at'] = $DATE_AT;
707e53f1ec0SSzymon Olewniczak        } elseif($REV) {
708e53f1ec0SSzymon Olewniczak            $params['rev'] = $REV;
709e53f1ec0SSzymon Olewniczak        }
710e53f1ec0SSzymon Olewniczak        $replace['@PAGEURL@'] = wl($id, $params, true, "&");
711a180c973SKlap-in        $replace['@QRCODE@']  = $qr_code;
712a180c973SKlap-in
713d824d23dSAnna Dabrowska        $content = $raw;
714a12c41c3SAnna Dabrowska
715a12c41c3SAnna Dabrowska        // let other plugins define their own replacements
716d824d23dSAnna Dabrowska        $evdata = ['id' => $id, 'replace' => &$replace, 'content' => &$content];
717a12c41c3SAnna Dabrowska        $event = new Doku_Event('PLUGIN_DW2PDF_REPLACE', $evdata);
718d824d23dSAnna Dabrowska        if ($event->advise_before()) {
719e3d68265SGerrit Uitslag            $content = str_replace(array_keys($replace), array_values($replace), $raw);
720d824d23dSAnna Dabrowska        }
721e3d68265SGerrit Uitslag
722a12c41c3SAnna Dabrowska        // plugins may post-process HTML, e.g to clean up unused replacements
723a12c41c3SAnna Dabrowska        $event->advise_after();
724a12c41c3SAnna Dabrowska
725e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
726e3d68265SGerrit Uitslag        $content = preg_replace_callback(
727e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
728e3d68265SGerrit Uitslag            array($this, 'replacedate'),
729e3d68265SGerrit Uitslag            $content
730e3d68265SGerrit Uitslag        );
731e3d68265SGerrit Uitslag
732e3d68265SGerrit Uitslag        return $content;
733a180c973SKlap-in    }
734a180c973SKlap-in
735e3d68265SGerrit Uitslag
736e3d68265SGerrit Uitslag    /**
737e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
738e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
739e3d68265SGerrit Uitslag     *
740e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
741e3d68265SGerrit Uitslag     * @return string
742e3d68265SGerrit Uitslag     */
743e3d68265SGerrit Uitslag    function replacedate($match) {
744e3d68265SGerrit Uitslag        global $conf;
745e3d68265SGerrit Uitslag        //no 2nd argument for default date format
746e3d68265SGerrit Uitslag        if($match[2] == null) {
747e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
748e3d68265SGerrit Uitslag        }
749e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
750e3d68265SGerrit Uitslag    }
751e3d68265SGerrit Uitslag
752a180c973SKlap-in    /**
7531c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
7541ef68647SAndreas Gohr     */
7551c14c879SAndreas Gohr    protected function load_css() {
756737417c6SKlap-in        global $conf;
7577c79bc79SGerrit Uitslag        //reuse the CSS dispatcher functions without triggering the main function
7581c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
7591c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
760ee19bac3SLuigi Micco
7611c14c879SAndreas Gohr        // prepare CSS files
7621c14c879SAndreas Gohr        $files = array_merge(
7631c14c879SAndreas Gohr            array(
7641c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/screen.css'
7651c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
7661c14c879SAndreas Gohr                DOKU_INC . 'lib/styles/print.css'
7671c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/styles/',
7681c14c879SAndreas Gohr            ),
76958e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
7701c14c879SAndreas Gohr            array(
7711c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.css'
7721c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
7731c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css'
7741c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
7751c14c879SAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css'
7761c14c879SAndreas Gohr                    => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
7771c14c879SAndreas Gohr            )
7781c14c879SAndreas Gohr        );
7791c14c879SAndreas Gohr        $css = '';
7801c14c879SAndreas Gohr        foreach($files as $file => $location) {
78128e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
78228e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
7831c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
7841ef68647SAndreas Gohr        }
7851ef68647SAndreas Gohr
78628e636eaSGerrit Uitslag        if(function_exists('css_parseless')) {
7871c14c879SAndreas Gohr            // apply pattern replacements
7887c6ca3bcSMichael Große            if (function_exists('css_styleini')) {
7897c6ca3bcSMichael Große                // compatiblity layer for pre-Greebo releases of DokuWiki
79028e636eaSGerrit Uitslag                $styleini = css_styleini($conf['template']);
7917c6ca3bcSMichael Große            } else {
7927c6ca3bcSMichael Große                // Greebo functionality
7937c6ca3bcSMichael Große                $styleUtils = new \dokuwiki\StyleUtils();
794d0f0c534SGerrit Uitslag                $styleini = $styleUtils->cssStyleini($conf['template']); // older versions need still the template
7957c6ca3bcSMichael Große            }
79628e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
79728e636eaSGerrit Uitslag
79828e636eaSGerrit Uitslag            // parse less
79928e636eaSGerrit Uitslag            $css = css_parseless($css);
80028e636eaSGerrit Uitslag        } else {
80128e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
8021c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
80328e636eaSGerrit Uitslag        }
8041ef68647SAndreas Gohr
8051c14c879SAndreas Gohr        return $css;
806ee19bac3SLuigi Micco    }
8071c14c879SAndreas Gohr
80858e6409eSAndreas Gohr    /**
80958e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
81058e6409eSAndreas Gohr     *
81158e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
81258e6409eSAndreas Gohr     *
81358e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
81458e6409eSAndreas Gohr     */
8156be736bfSGerrit Uitslag    protected function css_pluginPDFstyles() {
81658e6409eSAndreas Gohr        $list = array();
81758e6409eSAndreas Gohr        $plugins = plugin_list();
818f54b51f7SAndreas Gohr
819f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
82058e6409eSAndreas Gohr        foreach($plugins as $p) {
821f54b51f7SAndreas Gohr            if(in_array($p, $usestyle)) {
822f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
8238003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/";
8248003b493SGerrit Uitslag
825f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
8268003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/";
827f54b51f7SAndreas Gohr            }
828f54b51f7SAndreas Gohr
8298003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/";
8308003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/";
8318003b493SGerrit Uitslag
832ab96d816SMichael Große            if(file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) {
83358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
8348003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/";
83558e6409eSAndreas Gohr            } else {
83658e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
8378003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/";
83858e6409eSAndreas Gohr            }
83958e6409eSAndreas Gohr        }
8408b8ac6ecSAndreas Gohr
8418b8ac6ecSAndreas Gohr        // template support
8428b8ac6ecSAndreas Gohr        foreach (['pdf.css', 'pdf.less', 'css/pdf.css', 'css/pdf.less', 'styles/pdf.css', 'styles/pdf.less'] as $file) {
8438b8ac6ecSAndreas Gohr            if (file_exists(tpl_incdir() . $file)) {
8448b8ac6ecSAndreas Gohr                $list[tpl_incdir() . $file] = tpl_basedir() . $file;
8458b8ac6ecSAndreas Gohr            }
8468b8ac6ecSAndreas Gohr        }
8478b8ac6ecSAndreas Gohr
84858e6409eSAndreas Gohr        return $list;
84958e6409eSAndreas Gohr    }
850ad18f4e1SGerrit Uitslag
851ad18f4e1SGerrit Uitslag    /**
85260e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
85360e59de7SGerrit Uitslag     *
85460e59de7SGerrit Uitslag     * @return array
85560e59de7SGerrit Uitslag     */
85660e59de7SGerrit Uitslag    public function getExportedPages() {
85760e59de7SGerrit Uitslag        return $this->list;
85860e59de7SGerrit Uitslag    }
85960e59de7SGerrit Uitslag
86060e59de7SGerrit Uitslag    /**
861ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
86244e8e8fbSGerrit Uitslag     *
86344e8e8fbSGerrit Uitslag     * @param array $a
86444e8e8fbSGerrit Uitslag     * @param array $b
86544e8e8fbSGerrit Uitslag     * @return int
866ad18f4e1SGerrit Uitslag     */
867ad18f4e1SGerrit Uitslag    public function _datesort($a, $b) {
868ad18f4e1SGerrit Uitslag        if($b['rev'] < $a['rev']) return -1;
869ad18f4e1SGerrit Uitslag        if($b['rev'] > $a['rev']) return 1;
870ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
871ad18f4e1SGerrit Uitslag    }
872ad18f4e1SGerrit Uitslag
873ad18f4e1SGerrit Uitslag    /**
874ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
87544e8e8fbSGerrit Uitslag     * @param array $a
87644e8e8fbSGerrit Uitslag     * @param array $b
87744e8e8fbSGerrit Uitslag     * @return int
878ad18f4e1SGerrit Uitslag     */
879ad18f4e1SGerrit Uitslag    public function _pagenamesort($a, $b) {
88020025b90SAndreas Gohr        global $conf;
88120025b90SAndreas Gohr
88220025b90SAndreas Gohr        $partsA = explode(':', $a['id']);
88320025b90SAndreas Gohr        $countA = count($partsA);
88420025b90SAndreas Gohr        $partsB = explode(':', $b['id']);
88520025b90SAndreas Gohr        $countB = count($partsB);
886e00f6071SAndreas Gohr        $max = max($countA, $countB);
88720025b90SAndreas Gohr
88820025b90SAndreas Gohr
88920025b90SAndreas Gohr        // compare namepsace by namespace
890e00f6071SAndreas Gohr        for ($i = 0; $i < $max; $i++) {
891e00f6071SAndreas Gohr            $partA = $partsA[$i] ?: null;
892e00f6071SAndreas Gohr            $partB = $partsB[$i] ?: null;
89320025b90SAndreas Gohr
89420025b90SAndreas Gohr            // have we reached the page level?
895e00f6071SAndreas Gohr            if ($i === ($countA - 1) || $i === ($countB - 1)) {
89620025b90SAndreas Gohr                // start page first
89720025b90SAndreas Gohr                if ($partA == $conf['start']) return -1;
89820025b90SAndreas Gohr                if ($partB == $conf['start']) return 1;
89920025b90SAndreas Gohr            }
90020025b90SAndreas Gohr
901e00f6071SAndreas Gohr            // prefer page over namespace
902e00f6071SAndreas Gohr            if($partA === $partB) {
903e00f6071SAndreas Gohr                if (!isset($partsA[$i + 1])) return -1;
904e00f6071SAndreas Gohr                if (!isset($partsB[$i + 1])) return 1;
905e00f6071SAndreas Gohr                continue;
906e00f6071SAndreas Gohr            }
907e00f6071SAndreas Gohr
908e00f6071SAndreas Gohr
90920025b90SAndreas Gohr            // simply compare
91041e5d4e2SAndreas Gohr            return strnatcmp($partA, $partB);
91120025b90SAndreas Gohr        }
91220025b90SAndreas Gohr
91341e5d4e2SAndreas Gohr        return strnatcmp($a['id'], $b['id']);
914ad18f4e1SGerrit Uitslag    }
91526be4eceSGerrit Uitslag
91626be4eceSGerrit Uitslag    /**
9177c79bc79SGerrit Uitslag     * Collects settings from:
91802f9a447SGerrit Uitslag     *   1. url parameters
91902f9a447SGerrit Uitslag     *   2. plugin config
92002f9a447SGerrit Uitslag     *   3. global config
92102f9a447SGerrit Uitslag     */
92202f9a447SGerrit Uitslag    protected function loadExportConfig() {
92302f9a447SGerrit Uitslag        global $INPUT;
92402f9a447SGerrit Uitslag        global $conf;
92502f9a447SGerrit Uitslag
92602f9a447SGerrit Uitslag        $this->exportConfig = array();
92702f9a447SGerrit Uitslag
92802f9a447SGerrit Uitslag        // decide on the paper setup from param or config
92902f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
93002f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
93102f9a447SGerrit Uitslag
9324870b378SLarsDW223        // decide on the font-size from param or config
9334870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
9344870b378SLarsDW223
935213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided'));
936213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
937213fdb75SGerrit Uitslag
938b80f709fSNicolas        $this->exportConfig['watermark'] = $INPUT->str('watermark', '');
939b80f709fSNicolas
940213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc'));
94102f9a447SGerrit Uitslag        $levels = array();
94202f9a447SGerrit Uitslag        if($hasToC) {
94302f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
94421a55743SAnna Dabrowska            list($top_input, $max_input) = array_pad(explode('-', $toclevels, 2), 2, '');
94521a55743SAnna Dabrowska            list($top_conf, $max_conf) = array_pad(explode('-', $this->getConf('toclevels'), 2), 2, '');
94602f9a447SGerrit Uitslag            $bounds_input = array(
94702f9a447SGerrit Uitslag                'top' => array(
94802f9a447SGerrit Uitslag                    (int) $top_input,
94902f9a447SGerrit Uitslag                    (int) $top_conf
95002f9a447SGerrit Uitslag                ),
95102f9a447SGerrit Uitslag                'max' => array(
95202f9a447SGerrit Uitslag                    (int) $max_input,
95302f9a447SGerrit Uitslag                    (int) $max_conf
95402f9a447SGerrit Uitslag                )
95502f9a447SGerrit Uitslag            );
95602f9a447SGerrit Uitslag            $bounds = array(
95702f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
95802f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
95902f9a447SGerrit Uitslag
96002f9a447SGerrit Uitslag            );
96102f9a447SGerrit Uitslag            foreach($bounds_input as $bound => $values) {
96202f9a447SGerrit Uitslag                foreach($values as $value) {
96302f9a447SGerrit Uitslag                    if($value > 0 && $value <= 5) {
96402f9a447SGerrit Uitslag                        //stop at valid value and store
96502f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
96602f9a447SGerrit Uitslag                        break;
96702f9a447SGerrit Uitslag                    }
96802f9a447SGerrit Uitslag                }
96902f9a447SGerrit Uitslag            }
97002f9a447SGerrit Uitslag
97102f9a447SGerrit Uitslag            if($bounds['max'] < $bounds['top']) {
97202f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
97302f9a447SGerrit Uitslag            }
97402f9a447SGerrit Uitslag
97502f9a447SGerrit Uitslag            for($level = $bounds['top']; $level <= $bounds['max']; $level++) {
97602f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
97702f9a447SGerrit Uitslag            }
97802f9a447SGerrit Uitslag        }
97902f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
98002f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
98102f9a447SGerrit Uitslag
98202f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
98302f9a447SGerrit Uitslag
98402f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
98505d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
98602f9a447SGerrit Uitslag        if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
98702f9a447SGerrit Uitslag            $tpl = $tplconf;
98802f9a447SGerrit Uitslag        }
98902f9a447SGerrit Uitslag        if(!$tpl){
99002f9a447SGerrit Uitslag            $tpl = 'default';
99102f9a447SGerrit Uitslag        }
99202f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
99302f9a447SGerrit Uitslag
99402f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
99502f9a447SGerrit Uitslag    }
99602f9a447SGerrit Uitslag
99702f9a447SGerrit Uitslag    /**
99802f9a447SGerrit Uitslag     * Returns requested config
99902f9a447SGerrit Uitslag     *
100002f9a447SGerrit Uitslag     * @param string $name
100102f9a447SGerrit Uitslag     * @param mixed  $notset
100202f9a447SGerrit Uitslag     * @return mixed|bool
100302f9a447SGerrit Uitslag     */
100402f9a447SGerrit Uitslag    public function getExportConfig($name, $notset = false) {
100502f9a447SGerrit Uitslag        if ($this->exportConfig === null){
100602f9a447SGerrit Uitslag            $this->loadExportConfig();
100702f9a447SGerrit Uitslag        }
100802f9a447SGerrit Uitslag
100902f9a447SGerrit Uitslag        if(isset($this->exportConfig[$name])){
101002f9a447SGerrit Uitslag            return $this->exportConfig[$name];
101102f9a447SGerrit Uitslag        }else{
101202f9a447SGerrit Uitslag            return $notset;
101302f9a447SGerrit Uitslag        }
101402f9a447SGerrit Uitslag    }
1015d63e7fe7SGerrit Uitslag
1016d63e7fe7SGerrit Uitslag    /**
1017d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
1018d63e7fe7SGerrit Uitslag     *
1019d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
1020d63e7fe7SGerrit Uitslag     */
102144e8e8fbSGerrit Uitslag    public function addbutton(Doku_Event $event) {
1022e53f1ec0SSzymon Olewniczak        global $ID, $REV, $DATE_AT;
1023d63e7fe7SGerrit Uitslag
1024d63e7fe7SGerrit Uitslag        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
1025d63e7fe7SGerrit Uitslag            $params = array('do' => 'export_pdf');
1026e53f1ec0SSzymon Olewniczak            if($DATE_AT) {
1027e53f1ec0SSzymon Olewniczak                $params['at'] = $DATE_AT;
1028e53f1ec0SSzymon Olewniczak            } elseif($REV) {
1029d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
1030d63e7fe7SGerrit Uitslag            }
1031d63e7fe7SGerrit Uitslag
1032d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
1033d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
1034d63e7fe7SGerrit Uitslag                array('export_pdf' =>
1035d63e7fe7SGerrit Uitslag                          '<li>'
103672cadc31SChristian Paul                          . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
1037d63e7fe7SGerrit Uitslag                          . '<span>' . $this->getLang('export_pdf_button') . '</span>'
1038d63e7fe7SGerrit Uitslag                          . '</a>'
1039d63e7fe7SGerrit Uitslag                          . '</li>'
1040d63e7fe7SGerrit Uitslag                ) +
1041d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
1042d63e7fe7SGerrit Uitslag        }
1043d63e7fe7SGerrit Uitslag    }
1044026b594bSAndreas Gohr
1045026b594bSAndreas Gohr    /**
1046026b594bSAndreas Gohr     * Add 'export pdf' button to page tools, new SVG based mechanism
1047026b594bSAndreas Gohr     *
1048026b594bSAndreas Gohr     * @param Doku_Event $event
1049026b594bSAndreas Gohr     */
1050026b594bSAndreas Gohr    public function addsvgbutton(Doku_Event $event) {
10514c493e44SGerrit Uitslag        global $INFO;
10524c493e44SGerrit Uitslag        if($event->data['view'] != 'page' || !$this->getConf('showexportbutton')) {
10534c493e44SGerrit Uitslag            return;
10544c493e44SGerrit Uitslag        }
10554c493e44SGerrit Uitslag
10564c493e44SGerrit Uitslag        if(!$INFO['exists']) {
10574c493e44SGerrit Uitslag            return;
10584c493e44SGerrit Uitslag        }
10594c493e44SGerrit Uitslag
1060026b594bSAndreas Gohr        array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\dw2pdf\MenuItem()]);
1061026b594bSAndreas Gohr    }
1062*979c9cf5SAndreas Gohr
1063*979c9cf5SAndreas Gohr    /**
1064*979c9cf5SAndreas Gohr     * Get the language of the current document
1065*979c9cf5SAndreas Gohr     *
1066*979c9cf5SAndreas Gohr     * Uses the translation plugin if available
1067*979c9cf5SAndreas Gohr     * @return string
1068*979c9cf5SAndreas Gohr     */
1069*979c9cf5SAndreas Gohr    protected function getDocumentLanguage($pageid)
1070*979c9cf5SAndreas Gohr    {
1071*979c9cf5SAndreas Gohr        global $conf;
1072*979c9cf5SAndreas Gohr
1073*979c9cf5SAndreas Gohr        $lang = $conf['lang'];
1074*979c9cf5SAndreas Gohr        /** @var helper_plugin_translation $trans */
1075*979c9cf5SAndreas Gohr        $trans = plugin_load('helper', 'translation');
1076*979c9cf5SAndreas Gohr        if ($trans) {
1077*979c9cf5SAndreas Gohr            $tr = $trans->getLangPart($pageid);
1078*979c9cf5SAndreas Gohr            if ($tr) $lang = $tr;
1079*979c9cf5SAndreas Gohr        }
1080*979c9cf5SAndreas Gohr
1081*979c9cf5SAndreas Gohr        return $lang;
1082*979c9cf5SAndreas Gohr    }
1083ee19bac3SLuigi Micco}
1084