xref: /plugin/dw2pdf/action.php (revision 17e88313615eb017f1f062722741e306db2f9ca1)
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;
18*17e88313SGerrit Uitslaguse dokuwiki\plugin\dw2pdf\MenuItem;
19*17e88313SGerrit Uitslaguse dokuwiki\StyleUtils;
20*17e88313SGerrit Uitslaguse Mpdf\MpdfException;
210833b7cdSStephan Bauer
22*17e88313SGerrit Uitslagclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin
23*17e88313SGerrit Uitslag{
2402f9a447SGerrit Uitslag    /**
2502f9a447SGerrit Uitslag     * Settings for current export, collected from url param, plugin config, global config
2602f9a447SGerrit Uitslag     *
2702f9a447SGerrit Uitslag     * @var array
2802f9a447SGerrit Uitslag     */
29213fdb75SGerrit Uitslag    protected $exportConfig = null;
302a127a1dSGerrit Uitslag    /** @var string template name, to use templates from dw2pdf/tpl/<template name> */
3160e59de7SGerrit Uitslag    protected $tpl;
322a127a1dSGerrit Uitslag    /** @var string title of exported pdf */
3303352761SKirsten Roschanski    protected $title;
346a7f9d6cSGerrit Uitslag    /** @var array list of pages included in exported pdf */
35*17e88313SGerrit Uitslag    protected $list = [];
362a127a1dSGerrit Uitslag    /** @var bool|string path to temporary cachefile */
379b288b2aSGerrit Uitslag    protected $onetimefile = false;
389c76f78dSVincent GIRARD    protected $currentBookChapter = 0;
391c14c879SAndreas Gohr
401c14c879SAndreas Gohr    /**
411c14c879SAndreas Gohr     * Constructor. Sets the correct template
421c14c879SAndreas Gohr     */
43*17e88313SGerrit Uitslag    public function __construct()
44*17e88313SGerrit Uitslag    {
4502f9a447SGerrit Uitslag        $this->tpl = $this->getExportConfig('template');
461c14c879SAndreas Gohr    }
471c14c879SAndreas Gohr
48ee19bac3SLuigi Micco    /**
499b288b2aSGerrit Uitslag     * Delete cached files that were for one-time use
509b288b2aSGerrit Uitslag     */
51*17e88313SGerrit Uitslag    public function __destruct()
52*17e88313SGerrit Uitslag    {
539b288b2aSGerrit Uitslag        if ($this->onetimefile) {
549b288b2aSGerrit Uitslag            unlink($this->onetimefile);
559b288b2aSGerrit Uitslag        }
569b288b2aSGerrit Uitslag    }
579b288b2aSGerrit Uitslag
589b288b2aSGerrit Uitslag    /**
599c76f78dSVincent GIRARD     * Return the value of currentBookChapter, which is the order of the file to be added in a book generation
609c76f78dSVincent GIRARD     */
619c76f78dSVincent GIRARD    public function getCurrentBookChapter()
629c76f78dSVincent GIRARD    {
639c76f78dSVincent GIRARD        return $this->currentBookChapter;
649c76f78dSVincent GIRARD    }
659c76f78dSVincent GIRARD
669c76f78dSVincent GIRARD    /**
67ee19bac3SLuigi Micco     * Register the events
68177a7d30SGerrit Uitslag     *
69177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
70ee19bac3SLuigi Micco     */
71*17e88313SGerrit Uitslag    public function register(Doku_Event_Handler $controller)
72*17e88313SGerrit Uitslag    {
73*17e88313SGerrit Uitslag        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert');
74*17e88313SGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton');
75*17e88313SGerrit Uitslag        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton');
76ee19bac3SLuigi Micco    }
77ee19bac3SLuigi Micco
781c14c879SAndreas Gohr    /**
791c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
80737417c6SKlap-in     *
81737417c6SKlap-in     * @param Doku_Event $event
821c14c879SAndreas Gohr     */
83*17e88313SGerrit Uitslag    public function convert(Doku_Event $event)
84*17e88313SGerrit Uitslag    {
852a127a1dSGerrit Uitslag        global $REV, $DATE_AT;
862d9cd424SGerrit Uitslag        global $conf, $INPUT;
87ee19bac3SLuigi Micco
881ef68647SAndreas Gohr        // our event?
892a127a1dSGerrit Uitslag        $allowedEvents = ['export_pdfbook', 'export_pdf', 'export_pdfns'];
902a127a1dSGerrit Uitslag        if (!in_array($event->data, $allowedEvents)) return;
91ee19bac3SLuigi Micco
922a127a1dSGerrit Uitslag        try {
932a127a1dSGerrit Uitslag            //collect pages and check permissions
942a127a1dSGerrit Uitslag            list($this->title, $this->list) = $this->collectExportablePages($event);
95d63e7fe7SGerrit Uitslag
962d9cd424SGerrit Uitslag            if ($event->data === 'export_pdf' && ($REV || $DATE_AT)) {
979b288b2aSGerrit Uitslag                $cachefile = tempnam($conf['tmpdir'] . '/dwpdf', 'dw2pdf_');
989b288b2aSGerrit Uitslag                $this->onetimefile = $cachefile;
99f00df45eSMichael Große                $generateNewPdf = true;
100f00df45eSMichael Große            } else {
101a58f45f0SGerrit Uitslag                // prepare cache and its dependencies
102*17e88313SGerrit Uitslag                $depends = [];
10303352761SKirsten Roschanski                $cache = $this->prepareCache($depends);
1049b288b2aSGerrit Uitslag                $cachefile = $cache->cache;
10527195d5bSMichael Große                $generateNewPdf = !$this->getConf('usecache')
10627195d5bSMichael Große                    || $this->getExportConfig('isDebug')
10727195d5bSMichael Große                    || !$cache->useCache($depends);
108f00df45eSMichael Große            }
109d63e7fe7SGerrit Uitslag
110bd977188SGerrit Uitslag            // hard work only when no cache available or needed for debugging
111f00df45eSMichael Große            if ($generateNewPdf) {
112e5f6c2cbSMichael Große                // generating the pdf may take a long time for larger wikis / namespaces with many pages
113e5f6c2cbSMichael Große                set_time_limit(0);
1142a127a1dSGerrit Uitslag                //may throw Mpdf\MpdfException as well
1159b288b2aSGerrit Uitslag                $this->generatePDF($cachefile, $event);
1162a127a1dSGerrit Uitslag            }
1172a127a1dSGerrit Uitslag        } catch (Exception $e) {
1182d9cd424SGerrit Uitslag            if ($INPUT->has('selection')) {
1192d9cd424SGerrit Uitslag                http_status(400);
1202d9cd424SGerrit Uitslag                print $e->getMessage();
1212d9cd424SGerrit Uitslag                exit();
1222d9cd424SGerrit Uitslag            } else {
1232a127a1dSGerrit Uitslag                //prevent Action/Export()
124b34cb34eSSzymon Olewniczak                msg($e->getMessage(), -1);
1252a127a1dSGerrit Uitslag                $event->data = 'redirect';
126d9c13ec7SGerrit Uitslag                return;
127b34cb34eSSzymon Olewniczak            }
128d63e7fe7SGerrit Uitslag        }
1292d9cd424SGerrit Uitslag        $event->preventDefault(); // after prevent, $event->data cannot be changed
130d63e7fe7SGerrit Uitslag
131d63e7fe7SGerrit Uitslag        // deliver the file
1329b288b2aSGerrit Uitslag        $this->sendPDFFile($cachefile);  //exits
133d63e7fe7SGerrit Uitslag    }
134d63e7fe7SGerrit Uitslag
135d63e7fe7SGerrit Uitslag    /**
1362a127a1dSGerrit Uitslag     * Obtain list of pages and title, for different methods of exporting the pdf.
1372a127a1dSGerrit Uitslag     *  - Return a title and selection, throw otherwise an exception
1382a127a1dSGerrit Uitslag     *  - Check permisions
139d63e7fe7SGerrit Uitslag     *
140d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
1418ff32d7bSGerrit Uitslag     * @return array
1422a127a1dSGerrit Uitslag     * @throws Exception
143d63e7fe7SGerrit Uitslag     */
144*17e88313SGerrit Uitslag    protected function collectExportablePages(Doku_Event $event)
145*17e88313SGerrit Uitslag    {
14636a7917dSGerrit Uitslag        global $ID, $REV;
147d63e7fe7SGerrit Uitslag        global $INPUT;
1482a127a1dSGerrit Uitslag        global $conf, $lang;
149d63e7fe7SGerrit Uitslag
150d63e7fe7SGerrit Uitslag        // list of one or multiple pages
151*17e88313SGerrit Uitslag        $list = [];
15228e636eaSGerrit Uitslag
1534b4cebc2SLarsDW223        if ($event->data == 'export_pdf') {
1542a127a1dSGerrit Uitslag            if (auth_quickaclcheck($ID) < AUTH_READ) {  // set more specific denied message
1552a127a1dSGerrit Uitslag                throw new Exception($lang['accessdenied']);
1562a127a1dSGerrit Uitslag            }
157d63e7fe7SGerrit Uitslag            $list[0] = $ID;
1582a127a1dSGerrit Uitslag            $title = $INPUT->str('pdftitle'); //DEPRECATED
1592a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
1602a127a1dSGerrit Uitslag            if (empty($title)) {
1612a127a1dSGerrit Uitslag                $title = p_get_first_heading($ID);
16215923cb9SGerrit Uitslag            }
163ce8af5d0SHativ            // use page name if title is still empty
1642a127a1dSGerrit Uitslag            if (empty($title)) {
1652a127a1dSGerrit Uitslag                $title = noNS($ID);
166ce8af5d0SHativ            }
167ad18f4e1SGerrit Uitslag
16836a7917dSGerrit Uitslag            $filename = wikiFN($ID, $REV);
16936a7917dSGerrit Uitslag            if (!file_exists($filename)) {
1702a127a1dSGerrit Uitslag                throw new Exception($this->getLang('notexist'));
17136a7917dSGerrit Uitslag            }
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);
185*17e88313SGerrit 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
197*17e88313SGerrit Uitslag            $result = [];
198*17e88313SGerrit 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') {
223*17e88313SGerrit Uitslag                    usort($result, [$this, '_datesort']);
22441e5d4e2SAndreas Gohr                } elseif ($order == 'pagename' || $order == 'natural') {
225*17e88313SGerrit Uitslag                    usort($result, [$this, '_pagenamesort']);
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            }
240baa31dc5SGerrit Uitslag
241*17e88313SGerrit Uitslag        } elseif (!empty($_COOKIE['list-pagelist'])) {
242b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator */
24326be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
2442a127a1dSGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
2452a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2462a127a1dSGerrit Uitslag            if (empty($title)) {
2472a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
24826be4eceSGerrit Uitslag            }
249ad18f4e1SGerrit Uitslag
2502a127a1dSGerrit Uitslag            $list = explode("|", $_COOKIE['list-pagelist']);
2512a127a1dSGerrit Uitslag
252b3eed6e3SGerrit Uitslag        } elseif ($INPUT->has('selection')) {
253b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
254b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
255b3eed6e3SGerrit Uitslag//                http_status(403);
256b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
257b3eed6e3SGerrit Uitslag//                exit();
258b3eed6e3SGerrit Uitslag//            }
259b3eed6e3SGerrit Uitslag
2607c79bc79SGerrit Uitslag            $list = json_decode($INPUT->str('selection', '', true), true);
261b3eed6e3SGerrit Uitslag            if (!is_array($list) || empty($list)) {
2622a127a1dSGerrit Uitslag                throw new Exception($this->getLang('empty'));
263b3eed6e3SGerrit Uitslag            }
264b3eed6e3SGerrit Uitslag
2652a127a1dSGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
2662a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2672a127a1dSGerrit Uitslag            if (empty($title)) {
2682a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
2692a127a1dSGerrit Uitslag            }
2702a127a1dSGerrit Uitslag
2712a127a1dSGerrit Uitslag        } elseif ($INPUT->has('savedselection')) {
2722a127a1dSGerrit Uitslag            //export a saved selection of the Bookcreator Plugin
2732a127a1dSGerrit Uitslag            if (plugin_isdisabled('bookcreator')) {
2742a127a1dSGerrit Uitslag                throw new Exception($this->getLang('missingbookcreator'));
2752a127a1dSGerrit Uitslag            }
2762a127a1dSGerrit Uitslag            /** @var action_plugin_bookcreator_handleselection $SelectionHandling */
2772a127a1dSGerrit Uitslag            $SelectionHandling = plugin_load('action', 'bookcreator_handleselection');
2782a127a1dSGerrit Uitslag            $savedselection = $SelectionHandling->loadSavedSelection($INPUT->str('savedselection'));
2792a127a1dSGerrit Uitslag            $title = $savedselection['title'];
2802a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2812a127a1dSGerrit Uitslag            $list = $savedselection['selection'];
2822a127a1dSGerrit Uitslag
2832a127a1dSGerrit Uitslag            if (empty($title)) {
2842a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
285b3eed6e3SGerrit Uitslag            }
286b3eed6e3SGerrit Uitslag
287737417c6SKlap-in        } else {
28826be4eceSGerrit Uitslag            //show empty bookcreator message
2892a127a1dSGerrit Uitslag            throw new Exception($this->getLang('empty'));
290737417c6SKlap-in        }
291737417c6SKlap-in
292719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
293c7138b3fSGerrit Uitslag
294*17e88313SGerrit Uitslag        $skippedpages = [];
295c7138b3fSGerrit Uitslag        foreach ($list as $index => $pageid) {
296c7138b3fSGerrit Uitslag            if (auth_quickaclcheck($pageid) < AUTH_READ) {
297c7138b3fSGerrit Uitslag                $skippedpages[] = $pageid;
298c7138b3fSGerrit Uitslag                unset($list[$index]);
299c7138b3fSGerrit Uitslag            }
300c7138b3fSGerrit Uitslag        }
3012a127a1dSGerrit Uitslag        $list = array_filter($list, 'strlen'); //use of strlen() callback prevents removal of pagename '0'
302c7138b3fSGerrit Uitslag
303c7138b3fSGerrit Uitslag        //if selection contains forbidden pages throw (overridable) warning
304c7138b3fSGerrit Uitslag        if (!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) {
305c7138b3fSGerrit Uitslag            $msg = hsc(join(', ', $skippedpages));
3062a127a1dSGerrit Uitslag            throw new Exception(sprintf($this->getLang('forbidden'), $msg));
307c7138b3fSGerrit Uitslag        }
308c7138b3fSGerrit Uitslag
309*17e88313SGerrit Uitslag        return [$title, $list];
310d63e7fe7SGerrit Uitslag    }
311d63e7fe7SGerrit Uitslag
312a58f45f0SGerrit Uitslag    /**
313a58f45f0SGerrit Uitslag     * Prepare cache
314a58f45f0SGerrit Uitslag     *
315a58f45f0SGerrit Uitslag     * @param array $depends (reference) array with dependencies
316a58f45f0SGerrit Uitslag     * @return cache
317a58f45f0SGerrit Uitslag     */
318*17e88313SGerrit Uitslag    protected function prepareCache(&$depends)
319*17e88313SGerrit Uitslag    {
320a58f45f0SGerrit Uitslag        global $REV;
321a58f45f0SGerrit Uitslag
322ee19bac3SLuigi Micco        $cachekey = join(',', $this->list)
323ee19bac3SLuigi Micco            . $REV
324ee19bac3SLuigi Micco            . $this->getExportConfig('template')
325ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
326ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
327d83760efSGerrit Uitslag            . $this->getExportConfig('font-size')
328ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
3299c76f78dSVincent GIRARD            . $this->getExportConfig('headernumber')
330ee19bac3SLuigi Micco            . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0')
33103352761SKirsten Roschanski            . $this->title;
3320833b7cdSStephan Bauer        $cache = new Cache($cachekey, '.dw2.pdf');
333ee19bac3SLuigi Micco
334*17e88313SGerrit Uitslag        $dependencies = [];
335ee19bac3SLuigi Micco        foreach ($this->list as $pageid) {
336ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
337ee19bac3SLuigi Micco
338ee19bac3SLuigi Micco            if (is_array($relations)) {
339ee19bac3SLuigi Micco                if (array_key_exists('media', $relations) && is_array($relations['media'])) {
340ee19bac3SLuigi Micco                    foreach ($relations['media'] as $mediaid => $exists) {
341ee19bac3SLuigi Micco                        if ($exists) {
342ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
343ee19bac3SLuigi Micco                        }
344ee19bac3SLuigi Micco                    }
345ee19bac3SLuigi Micco                }
346ee19bac3SLuigi Micco
347ee19bac3SLuigi Micco                if (array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
348ee19bac3SLuigi Micco                    foreach ($relations['haspart'] as $part_pageid => $exists) {
349ee19bac3SLuigi Micco                        if ($exists) {
350ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
351ee19bac3SLuigi Micco                        }
352ee19bac3SLuigi Micco                    }
353ee19bac3SLuigi Micco                }
354ee19bac3SLuigi Micco            }
355ee19bac3SLuigi Micco
356ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
357ee19bac3SLuigi Micco        }
358ee19bac3SLuigi Micco
359ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
360ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
361ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/renderer.php';
362ee19bac3SLuigi Micco        $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
363ee19bac3SLuigi Micco        $depends['files'] = array_merge(
364ee19bac3SLuigi Micco            $depends['files'],
365ee19bac3SLuigi Micco            $dependencies,
366ee19bac3SLuigi Micco            getConfigFiles('main')
367ee19bac3SLuigi Micco        );
368a58f45f0SGerrit Uitslag        return $cache;
369ee19bac3SLuigi Micco    }
370ee19bac3SLuigi Micco
371d63e7fe7SGerrit Uitslag    /**
372e53f1ec0SSzymon Olewniczak     * Returns the parsed Wikitext in dw2pdf for the given id and revision
373e53f1ec0SSzymon Olewniczak     *
374e53f1ec0SSzymon Olewniczak     * @param string $id page id
375e53f1ec0SSzymon Olewniczak     * @param string|int $rev revision timestamp or empty string
376e53f1ec0SSzymon Olewniczak     * @param string $date_at
377e53f1ec0SSzymon Olewniczak     * @return null|string
378e53f1ec0SSzymon Olewniczak     */
379*17e88313SGerrit Uitslag    protected function p_wiki_dw2pdf($id, $rev = '', $date_at = '')
380*17e88313SGerrit Uitslag    {
381e53f1ec0SSzymon Olewniczak        $file = wikiFN($id, $rev);
382e53f1ec0SSzymon Olewniczak
383e53f1ec0SSzymon Olewniczak        if (!file_exists($file)) return '';
384e53f1ec0SSzymon Olewniczak
385e53f1ec0SSzymon Olewniczak        //ensure $id is in global $ID (needed for parsing)
386e53f1ec0SSzymon Olewniczak        global $ID;
387e53f1ec0SSzymon Olewniczak        $keep = $ID;
388e53f1ec0SSzymon Olewniczak        $ID = $id;
389e53f1ec0SSzymon Olewniczak
390e53f1ec0SSzymon Olewniczak        if ($rev || $date_at) {
391e53f1ec0SSzymon Olewniczak            $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); //no caching on old revisions
392e53f1ec0SSzymon Olewniczak        } else {
393e53f1ec0SSzymon Olewniczak            $ret = p_cached_output($file, 'dw2pdf', $id);
394e53f1ec0SSzymon Olewniczak        }
395e53f1ec0SSzymon Olewniczak
396e53f1ec0SSzymon Olewniczak        //restore ID (just in case)
397e53f1ec0SSzymon Olewniczak        $ID = $keep;
398e53f1ec0SSzymon Olewniczak
399e53f1ec0SSzymon Olewniczak        return $ret;
400e53f1ec0SSzymon Olewniczak    }
401e53f1ec0SSzymon Olewniczak
402e53f1ec0SSzymon Olewniczak    /**
403d63e7fe7SGerrit Uitslag     * Build a pdf from the html
404d63e7fe7SGerrit Uitslag     *
405d63e7fe7SGerrit Uitslag     * @param string $cachefile
406d9c13ec7SGerrit Uitslag     * @param Doku_Event $event
407*17e88313SGerrit Uitslag     * @throws MpdfException
408d63e7fe7SGerrit Uitslag     */
409*17e88313SGerrit Uitslag    protected function generatePDF($cachefile, $event)
410*17e88313SGerrit Uitslag    {
4112d9cd424SGerrit Uitslag        global $REV, $INPUT, $DATE_AT;
412e53f1ec0SSzymon Olewniczak
4132d9cd424SGerrit Uitslag        if ($event->data == 'export_pdf') { //only one page is exported
414e53f1ec0SSzymon Olewniczak            $rev = $REV;
415e53f1ec0SSzymon Olewniczak            $date_at = $DATE_AT;
4162a127a1dSGerrit Uitslag        } else { //we are exporting entire namespace, ommit revisions
417e53f1ec0SSzymon Olewniczak            $rev = $date_at = '';
418e53f1ec0SSzymon Olewniczak        }
41987c86ddaSAndreas Gohr
42002f9a447SGerrit Uitslag        //some shortcuts to export settings
42102f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
42202f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
42302f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
424b80f709fSNicolas        $watermark = $this->getExportConfig('watermark');
4256ea88a05SAndreas Gohr
4261ef68647SAndreas Gohr        // initialize PDF library
427cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__) . "/DokuPDF.class.php");
4286ea88a05SAndreas Gohr
429979c9cf5SAndreas Gohr        $mpdf = new DokuPDF(
430979c9cf5SAndreas Gohr            $this->getExportConfig('pagesize'),
4314870b378SLarsDW223            $this->getExportConfig('orientation'),
432979c9cf5SAndreas Gohr            $this->getExportConfig('font-size'),
433979c9cf5SAndreas Gohr            $this->getDocumentLanguage($this->list[0]) //use language of first page
434979c9cf5SAndreas Gohr        );
435ee19bac3SLuigi Micco
436d62df65bSAndreas Gohr        // let mpdf fix local links
437d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
438d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
43921a55743SAnna Dabrowska        if (!empty($self['port'])) {
44002f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
44102f9a447SGerrit Uitslag        }
442d9c13ec7SGerrit Uitslag        $mpdf->SetBasePath($url);
443d62df65bSAndreas Gohr
44456d13144SAndreas Gohr        // Set the title
44503352761SKirsten Roschanski        $mpdf->SetTitle($this->title);
44656d13144SAndreas Gohr
447d63e7fe7SGerrit Uitslag        // some default document settings
448d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
449213fdb75SGerrit Uitslag        //      single-side document has only odd pages
450213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
451daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
452daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
45302f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
45402f9a447SGerrit Uitslag        if ($hasToC) {
45502f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
45602f9a447SGerrit Uitslag        }
457*17e88313SGerrit Uitslag        $mpdf->PageNumSubstitutions[] = ['from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'];
4582eedf77dSAndreas Gohr
459b80f709fSNicolas        // Watermarker
460b80f709fSNicolas        if ($watermark) {
461b80f709fSNicolas            $mpdf->SetWatermarkText($watermark);
462b80f709fSNicolas            $mpdf->showWatermarkText = true;
463b80f709fSNicolas        }
464b80f709fSNicolas
46556d13144SAndreas Gohr        // load the template
46603352761SKirsten Roschanski        $template = $this->load_template();
467ee19bac3SLuigi Micco
4681ef68647SAndreas Gohr        // prepare HTML header styles
469a2c33768SGerrit Uitslag        $html = '';
47002f9a447SGerrit Uitslag        if ($isDebug) {
471a2c33768SGerrit Uitslag            $html .= '<html><head>';
472*17e88313SGerrit Uitslag            $html .= '<style>';
473a2c33768SGerrit Uitslag        }
474db1aa1bfSKirsten Roschanski
475db1aa1bfSKirsten Roschanski        $styles = '@page { size:auto; ' . $template['page'] . '}';
476a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
477254467c4SGerrit Uitslag
478254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
479254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
480254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
481254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
482db1aa1bfSKirsten Roschanski        $styles .= $this->load_css();
483254467c4SGerrit Uitslag
484a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
485a2c33768SGerrit Uitslag
48602f9a447SGerrit Uitslag        if ($isDebug) {
487a2c33768SGerrit Uitslag            $html .= $styles;
4881ef68647SAndreas Gohr            $html .= '</style>';
4891ef68647SAndreas Gohr            $html .= '</head><body>';
490a2c33768SGerrit Uitslag        }
491a2c33768SGerrit Uitslag
492a2c33768SGerrit Uitslag        $body_start = $template['html'];
493a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
4942eedf77dSAndreas Gohr
4951e45476bSmnapp        // insert the cover page
496a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
497a2c33768SGerrit Uitslag
498a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
49902f9a447SGerrit Uitslag        if ($isDebug) {
500a2c33768SGerrit Uitslag            $html .= $body_start;
501a2c33768SGerrit Uitslag        }
50202f9a447SGerrit Uitslag        if ($hasToC) {
50302f9a447SGerrit 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
50402f9a447SGerrit Uitslag            //      - first page of ToC starts always at odd page (so eventually an additional blank page is included before)
50502f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
506*17e88313SGerrit Uitslag            $mpdf->TOCpagebreakByArray([
507230b098dSGerrit Uitslag                'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
508230b098dSGerrit Uitslag                'toc-bookmarkText' => $this->getLang('tocheader'),
50902f9a447SGerrit Uitslag                'links' => true,
51002f9a447SGerrit Uitslag                'outdent' => '1em',
51102f9a447SGerrit Uitslag                'pagenumstyle' => '1'
512*17e88313SGerrit Uitslag            ]);
51302f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
51402f9a447SGerrit Uitslag        }
51502f9a447SGerrit Uitslag
5161ef68647SAndreas Gohr        // loop over all pages
517c7138b3fSGerrit Uitslag        $counter = 0;
518c7138b3fSGerrit Uitslag        $no_pages = count($this->list);
519c7138b3fSGerrit Uitslag        foreach ($this->list as $page) {
5209c76f78dSVincent GIRARD            $this->currentBookChapter = $counter;
521c7138b3fSGerrit Uitslag            $counter++;
522b3eed6e3SGerrit Uitslag
52379be256fSMichael Große            $pagehtml = $this->p_wiki_dw2pdf($page, $rev, $date_at);
524e53f1ec0SSzymon Olewniczak            //file doesn't exists
525e53f1ec0SSzymon Olewniczak            if ($pagehtml == '') {
526b3eed6e3SGerrit Uitslag                continue;
527b3eed6e3SGerrit Uitslag            }
528719256adSGerrit Uitslag            $pagehtml .= $this->page_depend_replacements($template['cite'], $page);
529c7138b3fSGerrit Uitslag            if ($counter < $no_pages) {
530a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
531a2c33768SGerrit Uitslag            }
532a2c33768SGerrit Uitslag
533a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
53402f9a447SGerrit Uitslag            if ($isDebug) {
535a2c33768SGerrit Uitslag                $html .= $pagehtml;
5361ef68647SAndreas Gohr            }
537ee19bac3SLuigi Micco        }
538ee19bac3SLuigi Micco
53933c15297SGerrit Uitslag        // insert the back page
540a2c33768SGerrit Uitslag        $body_end = $template['back'];
54133c15297SGerrit Uitslag
542a2c33768SGerrit Uitslag        $body_end .= '</div>';
543a2c33768SGerrit Uitslag
544d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
54502f9a447SGerrit Uitslag        if ($isDebug) {
546a2c33768SGerrit Uitslag            $html .= $body_end;
547eeb17e15SAndreas Gohr            $html .= '</body>';
548eeb17e15SAndreas Gohr            $html .= '</html>';
549a2c33768SGerrit Uitslag        }
550f765508eSGerrit Uitslag
551f765508eSGerrit Uitslag        //Return html for debugging
55202f9a447SGerrit Uitslag        if ($isDebug) {
553*17e88313SGerrit Uitslag            if ($INPUT->str('debughtml', 'text', true) == 'text') {
554a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
555a2c33768SGerrit Uitslag            }
556*17e88313SGerrit Uitslag            echo $html;
55726be4eceSGerrit Uitslag            exit();
5587c79bc79SGerrit Uitslag        }
559f765508eSGerrit Uitslag
56087c86ddaSAndreas Gohr        // write to cache file
561d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
56287c86ddaSAndreas Gohr    }
56387c86ddaSAndreas Gohr
564d63e7fe7SGerrit Uitslag    /**
565d63e7fe7SGerrit Uitslag     * @param string $cachefile
566d63e7fe7SGerrit Uitslag     */
567*17e88313SGerrit Uitslag    protected function sendPDFFile($cachefile)
568*17e88313SGerrit Uitslag    {
56987c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
570b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
57187c86ddaSAndreas Gohr        header('Pragma: public');
572d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
573c0956f69SMichael Große        global $INPUT;
574f7b24f48SMichael Große        $outputTarget = $INPUT->str('outputTarget', $this->getConf('output'));
57587c86ddaSAndreas Gohr
57603352761SKirsten Roschanski        $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', '    ')));
577c0956f69SMichael Große        if ($outputTarget === 'file') {
5789a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
57987c86ddaSAndreas Gohr        } else {
5809a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
58187c86ddaSAndreas Gohr        }
582ee19bac3SLuigi Micco
583b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
584b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
585b3eed6e3SGerrit Uitslag
586e993da11SGerrit Uitslag        //try to send file, and exit if done
587d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
58887c86ddaSAndreas Gohr
589d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
59087c86ddaSAndreas Gohr        if ($fp) {
591d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
59287c86ddaSAndreas Gohr        } else {
59387c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
59487c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
59587c86ddaSAndreas Gohr        }
5961ef68647SAndreas Gohr        exit();
5971ef68647SAndreas Gohr    }
5981ef68647SAndreas Gohr
5996be736bfSGerrit Uitslag    /**
6002eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
60144e8e8fbSGerrit Uitslag     *
60244e8e8fbSGerrit Uitslag     * @return array
6031ef68647SAndreas Gohr     */
604*17e88313SGerrit Uitslag    protected function load_template()
605*17e88313SGerrit Uitslag    {
6061ef68647SAndreas Gohr        global $ID;
6071ef68647SAndreas Gohr        global $conf;
6080a11dabaSChris75forumname        global $INFO;
6091ef68647SAndreas Gohr
6102eedf77dSAndreas Gohr        // this is what we'll return
61121a55743SAnna Dabrowska        $output = [
6121e45476bSmnapp            'cover' => '',
61321a55743SAnna Dabrowska            'back' => '',
6142eedf77dSAndreas Gohr            'html' => '',
6152eedf77dSAndreas Gohr            'page' => '',
6162eedf77dSAndreas Gohr            'first' => '',
6172eedf77dSAndreas Gohr            'cite' => '',
61821a55743SAnna Dabrowska        ];
6192eedf77dSAndreas Gohr
6202eedf77dSAndreas Gohr        // prepare header/footer elements
6212eedf77dSAndreas Gohr        $html = '';
622*17e88313SGerrit Uitslag        foreach (['header', 'footer'] as $section) {
623*17e88313SGerrit Uitslag            foreach (['', '_odd', '_even', '_first'] as $order) {
62402f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
62533c15297SGerrit Uitslag                if (file_exists($file)) {
62633c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
62733c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
62833c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
6292eedf77dSAndreas Gohr
6302eedf77dSAndreas Gohr                    // register the needed pseudo CSS
63133c15297SGerrit Uitslag                    if ($order == '_first') {
63233c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
63333c15297SGerrit Uitslag                    } elseif ($order == '_even') {
63433c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
63533c15297SGerrit Uitslag                    } elseif ($order == '_odd') {
63633c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
637daa70883SAndreas Gohr                    } else {
63833c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
6392eedf77dSAndreas Gohr                    }
6402eedf77dSAndreas Gohr                }
6412eedf77dSAndreas Gohr            }
6422eedf77dSAndreas Gohr        }
6432eedf77dSAndreas Gohr
6441ef68647SAndreas Gohr        // prepare replacements
645*17e88313SGerrit Uitslag        $replace = [
6461ef68647SAndreas Gohr            '@PAGE@' => '{PAGENO}',
64702f9a447SGerrit Uitslag            '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
64803352761SKirsten Roschanski            '@TITLE@' => hsc($this->title),
6491ef68647SAndreas Gohr            '@WIKI@' => $conf['title'],
6501ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
6511ef68647SAndreas Gohr            '@DATE@' => dformat(time()),
6528b11a0e6SChris75forumname            '@USERNAME@' => $INFO['userinfo']['name'] ?? '',
6535d6fbaeaSAndreas Gohr            '@BASE@' => DOKU_BASE,
654893987a2SAndreas Gohr            '@INC@' => DOKU_INC,
655893987a2SAndreas Gohr            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
656893987a2SAndreas Gohr            '@TPLINC@' => DOKU_INC . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
657*17e88313SGerrit Uitslag        ];
6581ef68647SAndreas Gohr
6592eedf77dSAndreas Gohr        // set HTML element
660a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
661a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
662a180c973SKlap-in        $output['html'] = $this->page_depend_replacements($html, $ID);
6631ef68647SAndreas Gohr
6641e45476bSmnapp        // cover page
66502f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
66633c15297SGerrit Uitslag        if (file_exists($coverfile)) {
66733c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
6681e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
6699b071da5SMichael            $output['cover'] = $this->page_depend_replacements($output['cover'], $ID);
6706e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
6711e45476bSmnapp        }
6721e45476bSmnapp
67333c15297SGerrit Uitslag        // cover page
67402f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
67533c15297SGerrit Uitslag        if (file_exists($backfile)) {
67633c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
67733c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
67833c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
6799b071da5SMichael            $output['back'] = $this->page_depend_replacements($output['back'], $ID);
68033c15297SGerrit Uitslag        }
68133c15297SGerrit Uitslag
6822eedf77dSAndreas Gohr        // citation box
68302f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
68433c15297SGerrit Uitslag        if (file_exists($citationfile)) {
68533c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
6862eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
6872eedf77dSAndreas Gohr        }
6881ef68647SAndreas Gohr
6892eedf77dSAndreas Gohr        return $output;
6901ef68647SAndreas Gohr    }
6911ef68647SAndreas Gohr
6921ef68647SAndreas Gohr    /**
693a180c973SKlap-in     * @param string $raw code with placeholders
694a180c973SKlap-in     * @param string $id pageid
695a180c973SKlap-in     * @return string
696a180c973SKlap-in     */
697*17e88313SGerrit Uitslag    protected function page_depend_replacements($raw, $id)
698*17e88313SGerrit Uitslag    {
699e53f1ec0SSzymon Olewniczak        global $REV, $DATE_AT;
700a180c973SKlap-in
701fb347f35SAndreas Gohr        // generate qr code for this page
702a180c973SKlap-in        $qr_code = '';
703fb347f35SAndreas Gohr        if ($this->getConf('qrcodescale')) {
704fb347f35SAndreas Gohr            $url = hsc(wl($id, '', '&', true));
705fb347f35SAndreas Gohr            $size = floatval($this->getConf('qrcodescale'));
706fb347f35SAndreas Gohr            $qr_code = '<barcode type="QR" code="' . $url . '" error="Q" disableborder="1" class="qrcode" size="' . $size . '" />';
707a180c973SKlap-in        }
708a180c973SKlap-in        // prepare replacements
709a180c973SKlap-in        $replace['@ID@'] = $id;
710a180c973SKlap-in        $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV)));
711e53f1ec0SSzymon Olewniczak
712*17e88313SGerrit Uitslag        $params = [];
713e53f1ec0SSzymon Olewniczak        if ($DATE_AT) {
714e53f1ec0SSzymon Olewniczak            $params['at'] = $DATE_AT;
715e53f1ec0SSzymon Olewniczak        } elseif ($REV) {
716e53f1ec0SSzymon Olewniczak            $params['rev'] = $REV;
717e53f1ec0SSzymon Olewniczak        }
718e53f1ec0SSzymon Olewniczak        $replace['@PAGEURL@'] = wl($id, $params, true, "&");
719a180c973SKlap-in        $replace['@QRCODE@'] = $qr_code;
720a180c973SKlap-in
721d824d23dSAnna Dabrowska        $content = $raw;
722a12c41c3SAnna Dabrowska
723a12c41c3SAnna Dabrowska        // let other plugins define their own replacements
724d824d23dSAnna Dabrowska        $evdata = ['id' => $id, 'replace' => &$replace, 'content' => &$content];
725a12c41c3SAnna Dabrowska        $event = new Doku_Event('PLUGIN_DW2PDF_REPLACE', $evdata);
726d824d23dSAnna Dabrowska        if ($event->advise_before()) {
727e3d68265SGerrit Uitslag            $content = str_replace(array_keys($replace), array_values($replace), $raw);
728d824d23dSAnna Dabrowska        }
729e3d68265SGerrit Uitslag
730a12c41c3SAnna Dabrowska        // plugins may post-process HTML, e.g to clean up unused replacements
731a12c41c3SAnna Dabrowska        $event->advise_after();
732a12c41c3SAnna Dabrowska
733e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
734e3d68265SGerrit Uitslag        $content = preg_replace_callback(
735e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
736*17e88313SGerrit Uitslag            [$this, 'replacedate'],
737e3d68265SGerrit Uitslag            $content
738e3d68265SGerrit Uitslag        );
739e3d68265SGerrit Uitslag
740e3d68265SGerrit Uitslag        return $content;
741a180c973SKlap-in    }
742a180c973SKlap-in
743e3d68265SGerrit Uitslag
744e3d68265SGerrit Uitslag    /**
745e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
746e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
747e3d68265SGerrit Uitslag     *
748e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
749e3d68265SGerrit Uitslag     * @return string
750e3d68265SGerrit Uitslag     */
751*17e88313SGerrit Uitslag    function replacedate($match)
752*17e88313SGerrit Uitslag    {
753e3d68265SGerrit Uitslag        global $conf;
754e3d68265SGerrit Uitslag        //no 2nd argument for default date format
755e3d68265SGerrit Uitslag        if ($match[2] == null) {
756e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
757e3d68265SGerrit Uitslag        }
758e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
759e3d68265SGerrit Uitslag    }
760e3d68265SGerrit Uitslag
761a180c973SKlap-in    /**
7621c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
7631ef68647SAndreas Gohr     */
764*17e88313SGerrit Uitslag    protected function load_css()
765*17e88313SGerrit Uitslag    {
766737417c6SKlap-in        global $conf;
7677c79bc79SGerrit Uitslag        //reuse the CSS dispatcher functions without triggering the main function
7681c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
7691c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
770ee19bac3SLuigi Micco
7711c14c879SAndreas Gohr        // prepare CSS files
7721c14c879SAndreas Gohr        $files = array_merge(
773*17e88313SGerrit Uitslag            [
774*17e88313SGerrit Uitslag                DOKU_INC . 'lib/styles/screen.css' => DOKU_BASE . 'lib/styles/',
775*17e88313SGerrit Uitslag                DOKU_INC . 'lib/styles/print.css' => DOKU_BASE . 'lib/styles/',
776*17e88313SGerrit Uitslag            ],
77758e6409eSAndreas Gohr            $this->css_pluginPDFstyles(),
778*17e88313SGerrit Uitslag            [
779*17e88313SGerrit Uitslag                DOKU_PLUGIN . 'dw2pdf/conf/style.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
780*17e88313SGerrit Uitslag                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
781*17e88313SGerrit Uitslag                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
782*17e88313SGerrit Uitslag            ]
7831c14c879SAndreas Gohr        );
7841c14c879SAndreas Gohr        $css = '';
7851c14c879SAndreas Gohr        foreach ($files as $file => $location) {
78628e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
78728e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
7881c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
7891ef68647SAndreas Gohr        }
7901ef68647SAndreas Gohr
79128e636eaSGerrit Uitslag        if (function_exists('css_parseless')) {
7921c14c879SAndreas Gohr            // apply pattern replacements
7937c6ca3bcSMichael Große            if (function_exists('css_styleini')) {
7947c6ca3bcSMichael Große                // compatiblity layer for pre-Greebo releases of DokuWiki
79528e636eaSGerrit Uitslag                $styleini = css_styleini($conf['template']);
7967c6ca3bcSMichael Große            } else {
7977c6ca3bcSMichael Große                // Greebo functionality
798*17e88313SGerrit Uitslag                $styleUtils = new StyleUtils();
799d0f0c534SGerrit Uitslag                $styleini = $styleUtils->cssStyleini($conf['template']); // older versions need still the template
8007c6ca3bcSMichael Große            }
80128e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
80228e636eaSGerrit Uitslag
80328e636eaSGerrit Uitslag            // parse less
80428e636eaSGerrit Uitslag            $css = css_parseless($css);
80528e636eaSGerrit Uitslag        } else {
80628e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
8071c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
80828e636eaSGerrit Uitslag        }
8091ef68647SAndreas Gohr
8101c14c879SAndreas Gohr        return $css;
811ee19bac3SLuigi Micco    }
8121c14c879SAndreas Gohr
81358e6409eSAndreas Gohr    /**
81458e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
81558e6409eSAndreas Gohr     *
81658e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
81758e6409eSAndreas Gohr     *
81858e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
81958e6409eSAndreas Gohr     */
820*17e88313SGerrit Uitslag    protected function css_pluginPDFstyles()
821*17e88313SGerrit Uitslag    {
822*17e88313SGerrit Uitslag        $list = [];
82358e6409eSAndreas Gohr        $plugins = plugin_list();
824f54b51f7SAndreas Gohr
825f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
82658e6409eSAndreas Gohr        foreach ($plugins as $p) {
827f54b51f7SAndreas Gohr            if (in_array($p, $usestyle)) {
828f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
8298003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/";
8308003b493SGerrit Uitslag
831f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
8328003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/";
833f54b51f7SAndreas Gohr            }
834f54b51f7SAndreas Gohr
8358003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/";
8368003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/";
8378003b493SGerrit Uitslag
838ab96d816SMichael Große            if (file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) {
83958e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
8408003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/";
84158e6409eSAndreas Gohr            } else {
84258e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
8438003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/";
84458e6409eSAndreas Gohr            }
84558e6409eSAndreas Gohr        }
8468b8ac6ecSAndreas Gohr
8478b8ac6ecSAndreas Gohr        // template support
8488b8ac6ecSAndreas Gohr        foreach (['pdf.css', 'pdf.less', 'css/pdf.css', 'css/pdf.less', 'styles/pdf.css', 'styles/pdf.less'] as $file) {
8498b8ac6ecSAndreas Gohr            if (file_exists(tpl_incdir() . $file)) {
8508b8ac6ecSAndreas Gohr                $list[tpl_incdir() . $file] = tpl_basedir() . $file;
8518b8ac6ecSAndreas Gohr            }
8528b8ac6ecSAndreas Gohr        }
8538b8ac6ecSAndreas Gohr
85458e6409eSAndreas Gohr        return $list;
85558e6409eSAndreas Gohr    }
856ad18f4e1SGerrit Uitslag
857ad18f4e1SGerrit Uitslag    /**
85860e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
85960e59de7SGerrit Uitslag     *
86060e59de7SGerrit Uitslag     * @return array
86160e59de7SGerrit Uitslag     */
862*17e88313SGerrit Uitslag    public function getExportedPages()
863*17e88313SGerrit Uitslag    {
86460e59de7SGerrit Uitslag        return $this->list;
86560e59de7SGerrit Uitslag    }
86660e59de7SGerrit Uitslag
86760e59de7SGerrit Uitslag    /**
868ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
86944e8e8fbSGerrit Uitslag     *
87044e8e8fbSGerrit Uitslag     * @param array $a
87144e8e8fbSGerrit Uitslag     * @param array $b
87244e8e8fbSGerrit Uitslag     * @return int
873ad18f4e1SGerrit Uitslag     */
874*17e88313SGerrit Uitslag    public function _datesort($a, $b)
875*17e88313SGerrit Uitslag    {
876ad18f4e1SGerrit Uitslag        if ($b['rev'] < $a['rev']) return -1;
877ad18f4e1SGerrit Uitslag        if ($b['rev'] > $a['rev']) return 1;
878ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
879ad18f4e1SGerrit Uitslag    }
880ad18f4e1SGerrit Uitslag
881ad18f4e1SGerrit Uitslag    /**
882ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
88344e8e8fbSGerrit Uitslag     * @param array $a
88444e8e8fbSGerrit Uitslag     * @param array $b
88544e8e8fbSGerrit Uitslag     * @return int
886ad18f4e1SGerrit Uitslag     */
887*17e88313SGerrit Uitslag    public function _pagenamesort($a, $b)
888*17e88313SGerrit Uitslag    {
88920025b90SAndreas Gohr        global $conf;
89020025b90SAndreas Gohr
89120025b90SAndreas Gohr        $partsA = explode(':', $a['id']);
89220025b90SAndreas Gohr        $countA = count($partsA);
89320025b90SAndreas Gohr        $partsB = explode(':', $b['id']);
89420025b90SAndreas Gohr        $countB = count($partsB);
895e00f6071SAndreas Gohr        $max = max($countA, $countB);
89620025b90SAndreas Gohr
89720025b90SAndreas Gohr
89820025b90SAndreas Gohr        // compare namepsace by namespace
899e00f6071SAndreas Gohr        for ($i = 0; $i < $max; $i++) {
900e00f6071SAndreas Gohr            $partA = $partsA[$i] ?: null;
901e00f6071SAndreas Gohr            $partB = $partsB[$i] ?: null;
90220025b90SAndreas Gohr
90320025b90SAndreas Gohr            // have we reached the page level?
904e00f6071SAndreas Gohr            if ($i === ($countA - 1) || $i === ($countB - 1)) {
90520025b90SAndreas Gohr                // start page first
90620025b90SAndreas Gohr                if ($partA == $conf['start']) return -1;
90720025b90SAndreas Gohr                if ($partB == $conf['start']) return 1;
90820025b90SAndreas Gohr            }
90920025b90SAndreas Gohr
910e00f6071SAndreas Gohr            // prefer page over namespace
911e00f6071SAndreas Gohr            if ($partA === $partB) {
912e00f6071SAndreas Gohr                if (!isset($partsA[$i + 1])) return -1;
913e00f6071SAndreas Gohr                if (!isset($partsB[$i + 1])) return 1;
914e00f6071SAndreas Gohr                continue;
915e00f6071SAndreas Gohr            }
916e00f6071SAndreas Gohr
917e00f6071SAndreas Gohr
91820025b90SAndreas Gohr            // simply compare
91941e5d4e2SAndreas Gohr            return strnatcmp($partA, $partB);
92020025b90SAndreas Gohr        }
92120025b90SAndreas Gohr
92241e5d4e2SAndreas Gohr        return strnatcmp($a['id'], $b['id']);
923ad18f4e1SGerrit Uitslag    }
92426be4eceSGerrit Uitslag
92526be4eceSGerrit Uitslag    /**
9267c79bc79SGerrit Uitslag     * Collects settings from:
92702f9a447SGerrit Uitslag     *   1. url parameters
92802f9a447SGerrit Uitslag     *   2. plugin config
92902f9a447SGerrit Uitslag     *   3. global config
93002f9a447SGerrit Uitslag     */
931*17e88313SGerrit Uitslag    protected function loadExportConfig()
932*17e88313SGerrit Uitslag    {
93302f9a447SGerrit Uitslag        global $INPUT;
93402f9a447SGerrit Uitslag        global $conf;
93502f9a447SGerrit Uitslag
936*17e88313SGerrit Uitslag        $this->exportConfig = [];
93702f9a447SGerrit Uitslag
93802f9a447SGerrit Uitslag        // decide on the paper setup from param or config
93902f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
94002f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
94102f9a447SGerrit Uitslag
9424870b378SLarsDW223        // decide on the font-size from param or config
9434870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
9444870b378SLarsDW223
945213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool)$this->getConf('doublesided'));
946213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
947213fdb75SGerrit Uitslag
948b80f709fSNicolas        $this->exportConfig['watermark'] = $INPUT->str('watermark', '');
949b80f709fSNicolas
950213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool)$this->getConf('toc'));
951*17e88313SGerrit Uitslag        $levels = [];
95202f9a447SGerrit Uitslag        if ($hasToC) {
95302f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
95421a55743SAnna Dabrowska            list($top_input, $max_input) = array_pad(explode('-', $toclevels, 2), 2, '');
95521a55743SAnna Dabrowska            list($top_conf, $max_conf) = array_pad(explode('-', $this->getConf('toclevels'), 2), 2, '');
956*17e88313SGerrit Uitslag            $bounds_input = [
957*17e88313SGerrit Uitslag                'top' => [
95802f9a447SGerrit Uitslag                    (int)$top_input,
95902f9a447SGerrit Uitslag                    (int)$top_conf
960*17e88313SGerrit Uitslag                ],
961*17e88313SGerrit Uitslag                'max' => [
96202f9a447SGerrit Uitslag                    (int)$max_input,
96302f9a447SGerrit Uitslag                    (int)$max_conf
964*17e88313SGerrit Uitslag                ]
965*17e88313SGerrit Uitslag            ];
966*17e88313SGerrit Uitslag            $bounds = [
96702f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
96802f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
96902f9a447SGerrit Uitslag
970*17e88313SGerrit Uitslag            ];
97102f9a447SGerrit Uitslag            foreach ($bounds_input as $bound => $values) {
97202f9a447SGerrit Uitslag                foreach ($values as $value) {
97302f9a447SGerrit Uitslag                    if ($value > 0 && $value <= 5) {
97402f9a447SGerrit Uitslag                        //stop at valid value and store
97502f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
97602f9a447SGerrit Uitslag                        break;
97702f9a447SGerrit Uitslag                    }
97802f9a447SGerrit Uitslag                }
97902f9a447SGerrit Uitslag            }
98002f9a447SGerrit Uitslag
98102f9a447SGerrit Uitslag            if ($bounds['max'] < $bounds['top']) {
98202f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
98302f9a447SGerrit Uitslag            }
98402f9a447SGerrit Uitslag
98502f9a447SGerrit Uitslag            for ($level = $bounds['top']; $level <= $bounds['max']; $level++) {
98602f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
98702f9a447SGerrit Uitslag            }
98802f9a447SGerrit Uitslag        }
98902f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
99002f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
99102f9a447SGerrit Uitslag
99202f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
99302f9a447SGerrit Uitslag
99402f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
99505d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
99602f9a447SGerrit Uitslag        if (!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
99702f9a447SGerrit Uitslag            $tpl = $tplconf;
99802f9a447SGerrit Uitslag        }
99902f9a447SGerrit Uitslag        if (!$tpl) {
100002f9a447SGerrit Uitslag            $tpl = 'default';
100102f9a447SGerrit Uitslag        }
100202f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
100302f9a447SGerrit Uitslag
100402f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
100502f9a447SGerrit Uitslag    }
100602f9a447SGerrit Uitslag
100702f9a447SGerrit Uitslag    /**
100802f9a447SGerrit Uitslag     * Returns requested config
100902f9a447SGerrit Uitslag     *
101002f9a447SGerrit Uitslag     * @param string $name
101102f9a447SGerrit Uitslag     * @param mixed $notset
101202f9a447SGerrit Uitslag     * @return mixed|bool
101302f9a447SGerrit Uitslag     */
1014*17e88313SGerrit Uitslag    public function getExportConfig($name, $notset = false)
1015*17e88313SGerrit Uitslag    {
101602f9a447SGerrit Uitslag        if ($this->exportConfig === null) {
101702f9a447SGerrit Uitslag            $this->loadExportConfig();
101802f9a447SGerrit Uitslag        }
101902f9a447SGerrit Uitslag
1020*17e88313SGerrit Uitslag        return $this->exportConfig[$name] ?? $notset;
102102f9a447SGerrit Uitslag    }
1022d63e7fe7SGerrit Uitslag
1023d63e7fe7SGerrit Uitslag    /**
1024d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
1025d63e7fe7SGerrit Uitslag     *
1026d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
1027d63e7fe7SGerrit Uitslag     */
1028*17e88313SGerrit Uitslag    public function addbutton(Doku_Event $event)
1029*17e88313SGerrit Uitslag    {
1030e53f1ec0SSzymon Olewniczak        global $ID, $REV, $DATE_AT;
1031d63e7fe7SGerrit Uitslag
1032d63e7fe7SGerrit Uitslag        if ($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
1033*17e88313SGerrit Uitslag            $params = ['do' => 'export_pdf'];
1034e53f1ec0SSzymon Olewniczak            if ($DATE_AT) {
1035e53f1ec0SSzymon Olewniczak                $params['at'] = $DATE_AT;
1036e53f1ec0SSzymon Olewniczak            } elseif ($REV) {
1037d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
1038d63e7fe7SGerrit Uitslag            }
1039d63e7fe7SGerrit Uitslag
1040d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
1041d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
1042*17e88313SGerrit Uitslag                ['export_pdf' =>
1043d63e7fe7SGerrit Uitslag                    '<li>'
104472cadc31SChristian Paul                    . '<a href="' . wl($ID, $params) . '"  class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">'
1045d63e7fe7SGerrit Uitslag                    . '<span>' . $this->getLang('export_pdf_button') . '</span>'
1046d63e7fe7SGerrit Uitslag                    . '</a>'
1047d63e7fe7SGerrit Uitslag                    . '</li>'
1048*17e88313SGerrit Uitslag                ] +
1049d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
1050d63e7fe7SGerrit Uitslag        }
1051d63e7fe7SGerrit Uitslag    }
1052026b594bSAndreas Gohr
1053026b594bSAndreas Gohr    /**
1054026b594bSAndreas Gohr     * Add 'export pdf' button to page tools, new SVG based mechanism
1055026b594bSAndreas Gohr     *
1056026b594bSAndreas Gohr     * @param Doku_Event $event
1057026b594bSAndreas Gohr     */
1058*17e88313SGerrit Uitslag    public function addsvgbutton(Doku_Event $event)
1059*17e88313SGerrit Uitslag    {
10604c493e44SGerrit Uitslag        global $INFO;
10614c493e44SGerrit Uitslag        if ($event->data['view'] != 'page' || !$this->getConf('showexportbutton')) {
10624c493e44SGerrit Uitslag            return;
10634c493e44SGerrit Uitslag        }
10644c493e44SGerrit Uitslag
10654c493e44SGerrit Uitslag        if (!$INFO['exists']) {
10664c493e44SGerrit Uitslag            return;
10674c493e44SGerrit Uitslag        }
10684c493e44SGerrit Uitslag
1069*17e88313SGerrit Uitslag        array_splice($event->data['items'], -1, 0, [new MenuItem()]);
1070026b594bSAndreas Gohr    }
1071979c9cf5SAndreas Gohr
1072979c9cf5SAndreas Gohr    /**
1073979c9cf5SAndreas Gohr     * Get the language of the current document
1074979c9cf5SAndreas Gohr     *
1075979c9cf5SAndreas Gohr     * Uses the translation plugin if available
1076979c9cf5SAndreas Gohr     * @return string
1077979c9cf5SAndreas Gohr     */
1078979c9cf5SAndreas Gohr    protected function getDocumentLanguage($pageid)
1079979c9cf5SAndreas Gohr    {
1080979c9cf5SAndreas Gohr        global $conf;
1081979c9cf5SAndreas Gohr
1082979c9cf5SAndreas Gohr        $lang = $conf['lang'];
1083979c9cf5SAndreas Gohr        /** @var helper_plugin_translation $trans */
1084979c9cf5SAndreas Gohr        $trans = plugin_load('helper', 'translation');
1085979c9cf5SAndreas Gohr        if ($trans) {
1086979c9cf5SAndreas Gohr            $tr = $trans->getLangPart($pageid);
1087979c9cf5SAndreas Gohr            if ($tr) $lang = $tr;
1088979c9cf5SAndreas Gohr        }
1089979c9cf5SAndreas Gohr
1090979c9cf5SAndreas Gohr        return $lang;
1091979c9cf5SAndreas Gohr    }
1092ee19bac3SLuigi Micco}
1093