xref: /plugin/dw2pdf/action.php (revision 852931daed0aa7c73fc4da5d421d2c117decf509)
1ee19bac3SLuigi Micco<?php
2*852931daSAndreas Gohr
3*852931daSAndreas Gohruse dokuwiki\Cache\Cache;
4*852931daSAndreas Gohruse dokuwiki\Extension\ActionPlugin;
5*852931daSAndreas Gohruse dokuwiki\Extension\Event;
6*852931daSAndreas Gohruse dokuwiki\Extension\EventHandler;
7*852931daSAndreas Gohruse dokuwiki\plugin\dw2pdf\MenuItem;
8*852931daSAndreas Gohruse dokuwiki\StyleUtils;
9*852931daSAndreas Gohruse Mpdf\MpdfException;
10*852931daSAndreas Gohr
11ee19bac3SLuigi Micco/**
12ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
13ee19bac3SLuigi Micco *
14*852931daSAndreas Gohr * Export html content to pdf, for different url parameter configurations
15*852931daSAndreas Gohr * DokuPDF which extends mPDF is used for generating the pdf from html.
16*852931daSAndreas Gohr *
17ee19bac3SLuigi Micco * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
18ee19bac3SLuigi Micco * @author     Luigi Micco <l.micco@tiscali.it>
195db42babSAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
20ee19bac3SLuigi Micco */
21*852931daSAndreas Gohrclass action_plugin_dw2pdf extends ActionPlugin
2217e88313SGerrit Uitslag{
2302f9a447SGerrit Uitslag    /**
2402f9a447SGerrit Uitslag     * Settings for current export, collected from url param, plugin config, global config
2502f9a447SGerrit Uitslag     *
2602f9a447SGerrit Uitslag     * @var array
2702f9a447SGerrit Uitslag     */
28*852931daSAndreas Gohr    protected $exportConfig;
292a127a1dSGerrit Uitslag    /** @var string template name, to use templates from dw2pdf/tpl/<template name> */
3060e59de7SGerrit Uitslag    protected $tpl;
312a127a1dSGerrit Uitslag    /** @var string title of exported pdf */
3203352761SKirsten Roschanski    protected $title;
336a7f9d6cSGerrit Uitslag    /** @var array list of pages included in exported pdf */
3417e88313SGerrit Uitslag    protected $list = [];
352a127a1dSGerrit Uitslag    /** @var bool|string path to temporary cachefile */
369b288b2aSGerrit Uitslag    protected $onetimefile = false;
379c76f78dSVincent GIRARD    protected $currentBookChapter = 0;
381c14c879SAndreas Gohr
391c14c879SAndreas Gohr    /**
401c14c879SAndreas Gohr     * Constructor. Sets the correct template
411c14c879SAndreas Gohr     */
4217e88313SGerrit Uitslag    public function __construct()
4317e88313SGerrit Uitslag    {
4402f9a447SGerrit Uitslag        $this->tpl = $this->getExportConfig('template');
451c14c879SAndreas Gohr    }
461c14c879SAndreas Gohr
47ee19bac3SLuigi Micco    /**
489b288b2aSGerrit Uitslag     * Delete cached files that were for one-time use
499b288b2aSGerrit Uitslag     */
5017e88313SGerrit Uitslag    public function __destruct()
5117e88313SGerrit Uitslag    {
529b288b2aSGerrit Uitslag        if ($this->onetimefile) {
539b288b2aSGerrit Uitslag            unlink($this->onetimefile);
549b288b2aSGerrit Uitslag        }
559b288b2aSGerrit Uitslag    }
569b288b2aSGerrit Uitslag
579b288b2aSGerrit Uitslag    /**
589c76f78dSVincent GIRARD     * Return the value of currentBookChapter, which is the order of the file to be added in a book generation
599c76f78dSVincent GIRARD     */
609c76f78dSVincent GIRARD    public function getCurrentBookChapter()
619c76f78dSVincent GIRARD    {
629c76f78dSVincent GIRARD        return $this->currentBookChapter;
639c76f78dSVincent GIRARD    }
649c76f78dSVincent GIRARD
659c76f78dSVincent GIRARD    /**
66ee19bac3SLuigi Micco     * Register the events
67177a7d30SGerrit Uitslag     *
68177a7d30SGerrit Uitslag     * @param Doku_Event_Handler $controller
69ee19bac3SLuigi Micco     */
70*852931daSAndreas Gohr    public function register(EventHandler $controller)
7117e88313SGerrit Uitslag    {
7217e88313SGerrit Uitslag        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert');
7317e88313SGerrit Uitslag        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton');
7417e88313SGerrit Uitslag        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton');
75ee19bac3SLuigi Micco    }
76ee19bac3SLuigi Micco
771c14c879SAndreas Gohr    /**
781c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
79737417c6SKlap-in     *
80737417c6SKlap-in     * @param Doku_Event $event
811c14c879SAndreas Gohr     */
82*852931daSAndreas Gohr    public function convert(Event $event)
8317e88313SGerrit Uitslag    {
842a127a1dSGerrit Uitslag        global $REV, $DATE_AT;
852d9cd424SGerrit Uitslag        global $conf, $INPUT;
86ee19bac3SLuigi Micco
871ef68647SAndreas Gohr        // our event?
882a127a1dSGerrit Uitslag        $allowedEvents = ['export_pdfbook', 'export_pdf', 'export_pdfns'];
892a127a1dSGerrit Uitslag        if (!in_array($event->data, $allowedEvents)) return;
90ee19bac3SLuigi Micco
912a127a1dSGerrit Uitslag        try {
922a127a1dSGerrit Uitslag            //collect pages and check permissions
93*852931daSAndreas Gohr            [$this->title, $this->list] = $this->collectExportablePages($event);
94d63e7fe7SGerrit Uitslag
952d9cd424SGerrit Uitslag            if ($event->data === 'export_pdf' && ($REV || $DATE_AT)) {
969b288b2aSGerrit Uitslag                $cachefile = tempnam($conf['tmpdir'] . '/dwpdf', 'dw2pdf_');
979b288b2aSGerrit Uitslag                $this->onetimefile = $cachefile;
98f00df45eSMichael Große                $generateNewPdf = true;
99f00df45eSMichael Große            } else {
100a58f45f0SGerrit Uitslag                // prepare cache and its dependencies
10117e88313SGerrit Uitslag                $depends = [];
10203352761SKirsten Roschanski                $cache = $this->prepareCache($depends);
1039b288b2aSGerrit Uitslag                $cachefile = $cache->cache;
10427195d5bSMichael Große                $generateNewPdf = !$this->getConf('usecache')
10527195d5bSMichael Große                    || $this->getExportConfig('isDebug')
10627195d5bSMichael Große                    || !$cache->useCache($depends);
107f00df45eSMichael Große            }
108d63e7fe7SGerrit Uitslag
109bd977188SGerrit Uitslag            // hard work only when no cache available or needed for debugging
110f00df45eSMichael Große            if ($generateNewPdf) {
111e5f6c2cbSMichael Große                // generating the pdf may take a long time for larger wikis / namespaces with many pages
112e5f6c2cbSMichael Große                set_time_limit(0);
1132a127a1dSGerrit Uitslag                //may throw Mpdf\MpdfException as well
1149b288b2aSGerrit Uitslag                $this->generatePDF($cachefile, $event);
1152a127a1dSGerrit Uitslag            }
1162a127a1dSGerrit Uitslag        } catch (Exception $e) {
1172d9cd424SGerrit Uitslag            if ($INPUT->has('selection')) {
1182d9cd424SGerrit Uitslag                http_status(400);
119*852931daSAndreas Gohr                echo $e->getMessage();
1202d9cd424SGerrit Uitslag                exit();
1212d9cd424SGerrit Uitslag            } else {
1222a127a1dSGerrit Uitslag                //prevent Action/Export()
123b34cb34eSSzymon Olewniczak                msg($e->getMessage(), -1);
1242a127a1dSGerrit Uitslag                $event->data = 'redirect';
125d9c13ec7SGerrit Uitslag                return;
126b34cb34eSSzymon Olewniczak            }
127d63e7fe7SGerrit Uitslag        }
1282d9cd424SGerrit Uitslag        $event->preventDefault(); // after prevent, $event->data cannot be changed
129d63e7fe7SGerrit Uitslag
130d63e7fe7SGerrit Uitslag        // deliver the file
1319b288b2aSGerrit Uitslag        $this->sendPDFFile($cachefile);  //exits
132d63e7fe7SGerrit Uitslag    }
133d63e7fe7SGerrit Uitslag
134d63e7fe7SGerrit Uitslag    /**
1352a127a1dSGerrit Uitslag     * Obtain list of pages and title, for different methods of exporting the pdf.
1362a127a1dSGerrit Uitslag     *  - Return a title and selection, throw otherwise an exception
1372a127a1dSGerrit Uitslag     *  - Check permisions
138d63e7fe7SGerrit Uitslag     *
139d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
1408ff32d7bSGerrit Uitslag     * @return array
1412a127a1dSGerrit Uitslag     * @throws Exception
142d63e7fe7SGerrit Uitslag     */
143*852931daSAndreas Gohr    protected function collectExportablePages(Event $event)
14417e88313SGerrit Uitslag    {
14536a7917dSGerrit Uitslag        global $ID, $REV;
146d63e7fe7SGerrit Uitslag        global $INPUT;
1472a127a1dSGerrit Uitslag        global $conf, $lang;
148d63e7fe7SGerrit Uitslag
149d63e7fe7SGerrit Uitslag        // list of one or multiple pages
15017e88313SGerrit Uitslag        $list = [];
15128e636eaSGerrit Uitslag
1524b4cebc2SLarsDW223        if ($event->data == 'export_pdf') {
1532a127a1dSGerrit Uitslag            if (auth_quickaclcheck($ID) < AUTH_READ) {  // set more specific denied message
1542a127a1dSGerrit Uitslag                throw new Exception($lang['accessdenied']);
1552a127a1dSGerrit Uitslag            }
156d63e7fe7SGerrit Uitslag            $list[0] = $ID;
1572a127a1dSGerrit Uitslag            $title = $INPUT->str('pdftitle'); //DEPRECATED
1582a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
1592a127a1dSGerrit Uitslag            if (empty($title)) {
1602a127a1dSGerrit Uitslag                $title = p_get_first_heading($ID);
16115923cb9SGerrit Uitslag            }
162ce8af5d0SHativ            // use page name if title is still empty
1632a127a1dSGerrit Uitslag            if (empty($title)) {
1642a127a1dSGerrit Uitslag                $title = noNS($ID);
165ce8af5d0SHativ            }
166ad18f4e1SGerrit Uitslag
16736a7917dSGerrit Uitslag            $filename = wikiFN($ID, $REV);
16836a7917dSGerrit Uitslag            if (!file_exists($filename)) {
1692a127a1dSGerrit Uitslag                throw new Exception($this->getLang('notexist'));
17036a7917dSGerrit Uitslag            }
1714b4cebc2SLarsDW223        } elseif ($event->data == 'export_pdfns') {
172ad18f4e1SGerrit Uitslag            //check input for title and ns
1732a127a1dSGerrit Uitslag            if (!$title = $INPUT->str('book_title')) {
1742a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
175ad18f4e1SGerrit Uitslag            }
176177a7d30SGerrit Uitslag            $pdfnamespace = cleanID($INPUT->str('book_ns'));
177ad18f4e1SGerrit Uitslag            if (!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) {
1782a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needns'));
179ad18f4e1SGerrit Uitslag            }
180ad18f4e1SGerrit Uitslag
18126be4eceSGerrit Uitslag            //sort order
182177a7d30SGerrit Uitslag            $order = $INPUT->str('book_order', 'natural', true);
18317e88313SGerrit Uitslag            $sortoptions = ['pagename', 'date', 'natural'];
184ad18f4e1SGerrit Uitslag            if (!in_array($order, $sortoptions)) {
185ad18f4e1SGerrit Uitslag                $order = 'natural';
186ad18f4e1SGerrit Uitslag            }
187ad18f4e1SGerrit Uitslag
18826be4eceSGerrit Uitslag            //search depth
189177a7d30SGerrit Uitslag            $depth = $INPUT->int('book_nsdepth', 0);
190ad18f4e1SGerrit Uitslag            if ($depth < 0) {
191ad18f4e1SGerrit Uitslag                $depth = 0;
192ad18f4e1SGerrit Uitslag            }
19326be4eceSGerrit Uitslag
194ad18f4e1SGerrit Uitslag            //page search
19517e88313SGerrit Uitslag            $result = [];
19617e88313SGerrit Uitslag            $opts = ['depth' => $depth]; //recursive all levels
197ad18f4e1SGerrit Uitslag            $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace));
198ad18f4e1SGerrit Uitslag            search($result, $conf['datadir'], 'search_allpages', $opts, $dir);
199ad18f4e1SGerrit Uitslag
20064541781SAnna Dabrowska            // exclude ids
20164541781SAnna Dabrowska            $excludes = $INPUT->arr('excludes');
20264541781SAnna Dabrowska            if (!empty($excludes)) {
203d31b75d5SAnna Dabrowska                $result = array_filter($result, function ($item) use ($excludes) {
2048c25a9b9SGerrit Uitslag                    return !in_array($item['id'], $excludes);
2058c25a9b9SGerrit Uitslag                });
2068c25a9b9SGerrit Uitslag            }
2078c25a9b9SGerrit Uitslag            // exclude namespaces
2088c25a9b9SGerrit Uitslag            $excludesns = $INPUT->arr('excludesns');
2098c25a9b9SGerrit Uitslag            if (!empty($excludesns)) {
2108c25a9b9SGerrit Uitslag                $result = array_filter($result, function ($item) use ($excludesns) {
2118c25a9b9SGerrit Uitslag                    foreach ($excludesns as $ns) {
2128c25a9b9SGerrit Uitslag                        if (strpos($item['id'], $ns . ':') === 0) return false;
2138c25a9b9SGerrit Uitslag                    }
2148c25a9b9SGerrit Uitslag                    return true;
215d31b75d5SAnna Dabrowska                });
21664541781SAnna Dabrowska            }
21764541781SAnna Dabrowska
21826be4eceSGerrit Uitslag            //sorting
219ad18f4e1SGerrit Uitslag            if (count($result) > 0) {
220ad18f4e1SGerrit Uitslag                if ($order == 'date') {
221*852931daSAndreas Gohr                    usort($result, [$this, 'cbDateSort']);
22241e5d4e2SAndreas Gohr                } elseif ($order == 'pagename' || $order == 'natural') {
223*852931daSAndreas Gohr                    usort($result, [$this, 'cbPagenameSort']);
224ad18f4e1SGerrit Uitslag                }
225ad18f4e1SGerrit Uitslag            }
226ad18f4e1SGerrit Uitslag
227ad18f4e1SGerrit Uitslag            foreach ($result as $item) {
228d63e7fe7SGerrit Uitslag                $list[] = $item['id'];
229ad18f4e1SGerrit Uitslag            }
230ad18f4e1SGerrit Uitslag
231baa31dc5SGerrit Uitslag            if ($pdfnamespace !== '') {
232baa31dc5SGerrit Uitslag                if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) {
233baa31dc5SGerrit Uitslag                    if (file_exists(wikiFN(rtrim($pdfnamespace, ':')))) {
234baa31dc5SGerrit Uitslag                        array_unshift($list, rtrim($pdfnamespace, ':'));
235baa31dc5SGerrit Uitslag                    }
236baa31dc5SGerrit Uitslag                }
237baa31dc5SGerrit Uitslag            }
23817e88313SGerrit Uitslag        } elseif (!empty($_COOKIE['list-pagelist'])) {
239b3eed6e3SGerrit Uitslag            /** @deprecated  April 2016 replaced by localStorage version of Bookcreator */
24026be4eceSGerrit Uitslag            //is in Bookmanager of bookcreator plugin a title given?
2412a127a1dSGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
2422a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2432a127a1dSGerrit Uitslag            if (empty($title)) {
2442a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
24526be4eceSGerrit Uitslag            }
246ad18f4e1SGerrit Uitslag
2472a127a1dSGerrit Uitslag            $list = explode("|", $_COOKIE['list-pagelist']);
248b3eed6e3SGerrit Uitslag        } elseif ($INPUT->has('selection')) {
249b3eed6e3SGerrit Uitslag            //handle Bookcreator requests based at localStorage
250b3eed6e3SGerrit Uitslag//            if(!checkSecurityToken()) {
251b3eed6e3SGerrit Uitslag//                http_status(403);
252b3eed6e3SGerrit Uitslag//                print $this->getLang('empty');
253b3eed6e3SGerrit Uitslag//                exit();
254b3eed6e3SGerrit Uitslag//            }
255b3eed6e3SGerrit Uitslag
2567c79bc79SGerrit Uitslag            $list = json_decode($INPUT->str('selection', '', true), true);
257*852931daSAndreas Gohr            if (!is_array($list) || $list === []) {
2582a127a1dSGerrit Uitslag                throw new Exception($this->getLang('empty'));
259b3eed6e3SGerrit Uitslag            }
260b3eed6e3SGerrit Uitslag
2612a127a1dSGerrit Uitslag            $title = $INPUT->str('pdfbook_title'); //DEPRECATED
2622a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2632a127a1dSGerrit Uitslag            if (empty($title)) {
2642a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
2652a127a1dSGerrit Uitslag            }
2662a127a1dSGerrit Uitslag        } elseif ($INPUT->has('savedselection')) {
2672a127a1dSGerrit Uitslag            //export a saved selection of the Bookcreator Plugin
2682a127a1dSGerrit Uitslag            if (plugin_isdisabled('bookcreator')) {
2692a127a1dSGerrit Uitslag                throw new Exception($this->getLang('missingbookcreator'));
2702a127a1dSGerrit Uitslag            }
2712a127a1dSGerrit Uitslag            /** @var action_plugin_bookcreator_handleselection $SelectionHandling */
2722a127a1dSGerrit Uitslag            $SelectionHandling = plugin_load('action', 'bookcreator_handleselection');
2732a127a1dSGerrit Uitslag            $savedselection = $SelectionHandling->loadSavedSelection($INPUT->str('savedselection'));
2742a127a1dSGerrit Uitslag            $title = $savedselection['title'];
2752a127a1dSGerrit Uitslag            $title = $INPUT->str('book_title', $title, true);
2762a127a1dSGerrit Uitslag            $list = $savedselection['selection'];
2772a127a1dSGerrit Uitslag
2782a127a1dSGerrit Uitslag            if (empty($title)) {
2792a127a1dSGerrit Uitslag                throw new Exception($this->getLang('needtitle'));
280b3eed6e3SGerrit Uitslag            }
281737417c6SKlap-in        } else {
28226be4eceSGerrit Uitslag            //show empty bookcreator message
2832a127a1dSGerrit Uitslag            throw new Exception($this->getLang('empty'));
284737417c6SKlap-in        }
285737417c6SKlap-in
286719256adSGerrit Uitslag        $list = array_map('cleanID', $list);
287c7138b3fSGerrit Uitslag
28817e88313SGerrit Uitslag        $skippedpages = [];
289c7138b3fSGerrit Uitslag        foreach ($list as $index => $pageid) {
290c7138b3fSGerrit Uitslag            if (auth_quickaclcheck($pageid) < AUTH_READ) {
291c7138b3fSGerrit Uitslag                $skippedpages[] = $pageid;
292c7138b3fSGerrit Uitslag                unset($list[$index]);
293c7138b3fSGerrit Uitslag            }
294c7138b3fSGerrit Uitslag        }
2952a127a1dSGerrit Uitslag        $list = array_filter($list, 'strlen'); //use of strlen() callback prevents removal of pagename '0'
296c7138b3fSGerrit Uitslag
297c7138b3fSGerrit Uitslag        //if selection contains forbidden pages throw (overridable) warning
298*852931daSAndreas Gohr        if (!$INPUT->bool('book_skipforbiddenpages') && $skippedpages !== []) {
299*852931daSAndreas Gohr            $msg = hsc(implode(', ', $skippedpages));
3002a127a1dSGerrit Uitslag            throw new Exception(sprintf($this->getLang('forbidden'), $msg));
301c7138b3fSGerrit Uitslag        }
302c7138b3fSGerrit Uitslag
30317e88313SGerrit Uitslag        return [$title, $list];
304d63e7fe7SGerrit Uitslag    }
305d63e7fe7SGerrit Uitslag
306a58f45f0SGerrit Uitslag    /**
307a58f45f0SGerrit Uitslag     * Prepare cache
308a58f45f0SGerrit Uitslag     *
309a58f45f0SGerrit Uitslag     * @param array $depends (reference) array with dependencies
310a58f45f0SGerrit Uitslag     * @return cache
311a58f45f0SGerrit Uitslag     */
31217e88313SGerrit Uitslag    protected function prepareCache(&$depends)
31317e88313SGerrit Uitslag    {
314a58f45f0SGerrit Uitslag        global $REV;
315a58f45f0SGerrit Uitslag
316*852931daSAndreas Gohr        $cachekey = implode(',', $this->list)
317ee19bac3SLuigi Micco            . $REV
318ee19bac3SLuigi Micco            . $this->getExportConfig('template')
319ee19bac3SLuigi Micco            . $this->getExportConfig('pagesize')
320ee19bac3SLuigi Micco            . $this->getExportConfig('orientation')
321d83760efSGerrit Uitslag            . $this->getExportConfig('font-size')
322ee19bac3SLuigi Micco            . $this->getExportConfig('doublesided')
3239c76f78dSVincent GIRARD            . $this->getExportConfig('headernumber')
324*852931daSAndreas Gohr            . ($this->getExportConfig('hasToC') ? implode('-', $this->getExportConfig('levels')) : '0')
32503352761SKirsten Roschanski            . $this->title;
3260833b7cdSStephan Bauer        $cache = new Cache($cachekey, '.dw2.pdf');
327ee19bac3SLuigi Micco
32817e88313SGerrit Uitslag        $dependencies = [];
329ee19bac3SLuigi Micco        foreach ($this->list as $pageid) {
330ee19bac3SLuigi Micco            $relations = p_get_metadata($pageid, 'relation');
331ee19bac3SLuigi Micco
332ee19bac3SLuigi Micco            if (is_array($relations)) {
333ee19bac3SLuigi Micco                if (array_key_exists('media', $relations) && is_array($relations['media'])) {
334ee19bac3SLuigi Micco                    foreach ($relations['media'] as $mediaid => $exists) {
335ee19bac3SLuigi Micco                        if ($exists) {
336ee19bac3SLuigi Micco                            $dependencies[] = mediaFN($mediaid);
337ee19bac3SLuigi Micco                        }
338ee19bac3SLuigi Micco                    }
339ee19bac3SLuigi Micco                }
340ee19bac3SLuigi Micco
341ee19bac3SLuigi Micco                if (array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
342ee19bac3SLuigi Micco                    foreach ($relations['haspart'] as $part_pageid => $exists) {
343ee19bac3SLuigi Micco                        if ($exists) {
344ee19bac3SLuigi Micco                            $dependencies[] = wikiFN($part_pageid);
345ee19bac3SLuigi Micco                        }
346ee19bac3SLuigi Micco                    }
347ee19bac3SLuigi Micco                }
348ee19bac3SLuigi Micco            }
349ee19bac3SLuigi Micco
350ee19bac3SLuigi Micco            $dependencies[] = metaFN($pageid, '.meta');
351ee19bac3SLuigi Micco        }
352ee19bac3SLuigi Micco
353ee19bac3SLuigi Micco        $depends['files'] = array_map('wikiFN', $this->list);
354ee19bac3SLuigi Micco        $depends['files'][] = __FILE__;
355*852931daSAndreas Gohr        $depends['files'][] = __DIR__ . '/renderer.php';
356*852931daSAndreas Gohr        $depends['files'][] = __DIR__ . '/mpdf/mpdf.php';
357ee19bac3SLuigi Micco        $depends['files'] = array_merge(
358ee19bac3SLuigi Micco            $depends['files'],
359ee19bac3SLuigi Micco            $dependencies,
360ee19bac3SLuigi Micco            getConfigFiles('main')
361ee19bac3SLuigi Micco        );
362a58f45f0SGerrit Uitslag        return $cache;
363ee19bac3SLuigi Micco    }
364ee19bac3SLuigi Micco
365d63e7fe7SGerrit Uitslag    /**
366e53f1ec0SSzymon Olewniczak     * Returns the parsed Wikitext in dw2pdf for the given id and revision
367e53f1ec0SSzymon Olewniczak     *
368e53f1ec0SSzymon Olewniczak     * @param string $id page id
369e53f1ec0SSzymon Olewniczak     * @param string|int $rev revision timestamp or empty string
370e53f1ec0SSzymon Olewniczak     * @param string $date_at
371e53f1ec0SSzymon Olewniczak     * @return null|string
372e53f1ec0SSzymon Olewniczak     */
373*852931daSAndreas Gohr    protected function wikiToDW2PDF($id, $rev = '', $date_at = '')
37417e88313SGerrit Uitslag    {
375e53f1ec0SSzymon Olewniczak        $file = wikiFN($id, $rev);
376e53f1ec0SSzymon Olewniczak
377e53f1ec0SSzymon Olewniczak        if (!file_exists($file)) return '';
378e53f1ec0SSzymon Olewniczak
379e53f1ec0SSzymon Olewniczak        //ensure $id is in global $ID (needed for parsing)
380e53f1ec0SSzymon Olewniczak        global $ID;
381e53f1ec0SSzymon Olewniczak        $keep = $ID;
382e53f1ec0SSzymon Olewniczak        $ID = $id;
383e53f1ec0SSzymon Olewniczak
384e53f1ec0SSzymon Olewniczak        if ($rev || $date_at) {
385*852931daSAndreas Gohr            //no caching on old revisions
386*852931daSAndreas Gohr            $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at);
387e53f1ec0SSzymon Olewniczak        } else {
388e53f1ec0SSzymon Olewniczak            $ret = p_cached_output($file, 'dw2pdf', $id);
389e53f1ec0SSzymon Olewniczak        }
390e53f1ec0SSzymon Olewniczak
391e53f1ec0SSzymon Olewniczak        //restore ID (just in case)
392e53f1ec0SSzymon Olewniczak        $ID = $keep;
393e53f1ec0SSzymon Olewniczak
394e53f1ec0SSzymon Olewniczak        return $ret;
395e53f1ec0SSzymon Olewniczak    }
396e53f1ec0SSzymon Olewniczak
397e53f1ec0SSzymon Olewniczak    /**
398d63e7fe7SGerrit Uitslag     * Build a pdf from the html
399d63e7fe7SGerrit Uitslag     *
400d63e7fe7SGerrit Uitslag     * @param string $cachefile
401d9c13ec7SGerrit Uitslag     * @param Doku_Event $event
40217e88313SGerrit Uitslag     * @throws MpdfException
403d63e7fe7SGerrit Uitslag     */
40417e88313SGerrit Uitslag    protected function generatePDF($cachefile, $event)
40517e88313SGerrit Uitslag    {
4062d9cd424SGerrit Uitslag        global $REV, $INPUT, $DATE_AT;
407e53f1ec0SSzymon Olewniczak
4082d9cd424SGerrit Uitslag        if ($event->data == 'export_pdf') { //only one page is exported
409e53f1ec0SSzymon Olewniczak            $rev = $REV;
410e53f1ec0SSzymon Olewniczak            $date_at = $DATE_AT;
411*852931daSAndreas Gohr        } else {
412*852931daSAndreas Gohr            //we are exporting entire namespace, ommit revisions
413*852931daSAndreas Gohr            $rev = '';
414*852931daSAndreas Gohr            $date_at = '';
415e53f1ec0SSzymon Olewniczak        }
41687c86ddaSAndreas Gohr
41702f9a447SGerrit Uitslag        //some shortcuts to export settings
41802f9a447SGerrit Uitslag        $hasToC = $this->getExportConfig('hasToC');
41902f9a447SGerrit Uitslag        $levels = $this->getExportConfig('levels');
42002f9a447SGerrit Uitslag        $isDebug = $this->getExportConfig('isDebug');
421b80f709fSNicolas        $watermark = $this->getExportConfig('watermark');
4226ea88a05SAndreas Gohr
4231ef68647SAndreas Gohr        // initialize PDF library
424*852931daSAndreas Gohr        require_once(__DIR__ . "/DokuPDF.class.php");
4256ea88a05SAndreas Gohr
426979c9cf5SAndreas Gohr        $mpdf = new DokuPDF(
427979c9cf5SAndreas Gohr            $this->getExportConfig('pagesize'),
4284870b378SLarsDW223            $this->getExportConfig('orientation'),
429979c9cf5SAndreas Gohr            $this->getExportConfig('font-size'),
430979c9cf5SAndreas Gohr            $this->getDocumentLanguage($this->list[0]) //use language of first page
431979c9cf5SAndreas Gohr        );
432ee19bac3SLuigi Micco
433d62df65bSAndreas Gohr        // let mpdf fix local links
434d62df65bSAndreas Gohr        $self = parse_url(DOKU_URL);
435d62df65bSAndreas Gohr        $url = $self['scheme'] . '://' . $self['host'];
43621a55743SAnna Dabrowska        if (!empty($self['port'])) {
43702f9a447SGerrit Uitslag            $url .= ':' . $self['port'];
43802f9a447SGerrit Uitslag        }
439d9c13ec7SGerrit Uitslag        $mpdf->SetBasePath($url);
440d62df65bSAndreas Gohr
44156d13144SAndreas Gohr        // Set the title
44203352761SKirsten Roschanski        $mpdf->SetTitle($this->title);
44356d13144SAndreas Gohr
444d63e7fe7SGerrit Uitslag        // some default document settings
445d63e7fe7SGerrit Uitslag        //note: double-sided document, starts at an odd page (first page is a right-hand side page)
446213fdb75SGerrit Uitslag        //      single-side document has only odd pages
447213fdb75SGerrit Uitslag        $mpdf->mirrorMargins = $this->getExportConfig('doublesided');
448daa70883SAndreas Gohr        $mpdf->setAutoTopMargin = 'stretch';
449daa70883SAndreas Gohr        $mpdf->setAutoBottomMargin = 'stretch';
45002f9a447SGerrit Uitslag//            $mpdf->pagenumSuffix = '/'; //prefix for {nbpg}
45102f9a447SGerrit Uitslag        if ($hasToC) {
45202f9a447SGerrit Uitslag            $mpdf->h2toc = $levels;
45302f9a447SGerrit Uitslag        }
45417e88313SGerrit Uitslag        $mpdf->PageNumSubstitutions[] = ['from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'];
4552eedf77dSAndreas Gohr
456b80f709fSNicolas        // Watermarker
457b80f709fSNicolas        if ($watermark) {
458b80f709fSNicolas            $mpdf->SetWatermarkText($watermark);
459b80f709fSNicolas            $mpdf->showWatermarkText = true;
460b80f709fSNicolas        }
461b80f709fSNicolas
46256d13144SAndreas Gohr        // load the template
463*852931daSAndreas Gohr        $template = $this->loadTemplate();
464ee19bac3SLuigi Micco
4651ef68647SAndreas Gohr        // prepare HTML header styles
466a2c33768SGerrit Uitslag        $html = '';
46702f9a447SGerrit Uitslag        if ($isDebug) {
468a2c33768SGerrit Uitslag            $html .= '<html><head>';
46917e88313SGerrit Uitslag            $html .= '<style>';
470a2c33768SGerrit Uitslag        }
471db1aa1bfSKirsten Roschanski
472db1aa1bfSKirsten Roschanski        $styles = '@page { size:auto; ' . $template['page'] . '}';
473a2c33768SGerrit Uitslag        $styles .= '@page :first {' . $template['first'] . '}';
474254467c4SGerrit Uitslag
475254467c4SGerrit Uitslag        $styles .= '@page landscape-page { size:landscape }';
476254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-landscape { page:landscape-page }';
477254467c4SGerrit Uitslag        $styles .= '@page portrait-page { size:portrait }';
478254467c4SGerrit Uitslag        $styles .= 'div.dw2pdf-portrait { page:portrait-page }';
479*852931daSAndreas Gohr        $styles .= $this->loadCSS();
480254467c4SGerrit Uitslag
481a2c33768SGerrit Uitslag        $mpdf->WriteHTML($styles, 1);
482a2c33768SGerrit Uitslag
48302f9a447SGerrit Uitslag        if ($isDebug) {
484a2c33768SGerrit Uitslag            $html .= $styles;
4851ef68647SAndreas Gohr            $html .= '</style>';
4861ef68647SAndreas Gohr            $html .= '</head><body>';
487a2c33768SGerrit Uitslag        }
488a2c33768SGerrit Uitslag
489a2c33768SGerrit Uitslag        $body_start = $template['html'];
490a2c33768SGerrit Uitslag        $body_start .= '<div class="dokuwiki">';
4912eedf77dSAndreas Gohr
4921e45476bSmnapp        // insert the cover page
493a2c33768SGerrit Uitslag        $body_start .= $template['cover'];
494a2c33768SGerrit Uitslag
495a2c33768SGerrit Uitslag        $mpdf->WriteHTML($body_start, 2, true, false); //start body html
49602f9a447SGerrit Uitslag        if ($isDebug) {
497a2c33768SGerrit Uitslag            $html .= $body_start;
498a2c33768SGerrit Uitslag        }
49902f9a447SGerrit Uitslag        if ($hasToC) {
500*852931daSAndreas Gohr            //Note: - for double-sided document the ToC is always on an even number of pages, so that the
501*852931daSAndreas Gohr            //        following content is on a correct odd/even page
502*852931daSAndreas Gohr            //      - first page of ToC starts always at odd page (so eventually an additional blank page
503*852931daSAndreas Gohr            //        is included before)
50402f9a447SGerrit Uitslag            //      - there is no page numbering at the pages of the ToC
50517e88313SGerrit Uitslag            $mpdf->TOCpagebreakByArray([
506230b098dSGerrit Uitslag                'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>',
507230b098dSGerrit Uitslag                'toc-bookmarkText' => $this->getLang('tocheader'),
50802f9a447SGerrit Uitslag                'links' => true,
50902f9a447SGerrit Uitslag                'outdent' => '1em',
51002f9a447SGerrit Uitslag                'pagenumstyle' => '1'
51117e88313SGerrit Uitslag            ]);
51202f9a447SGerrit Uitslag            $html .= '<tocpagebreak>';
51302f9a447SGerrit Uitslag        }
51402f9a447SGerrit Uitslag
5151ef68647SAndreas Gohr        // loop over all pages
516c7138b3fSGerrit Uitslag        $counter = 0;
517c7138b3fSGerrit Uitslag        $no_pages = count($this->list);
518c7138b3fSGerrit Uitslag        foreach ($this->list as $page) {
5199c76f78dSVincent GIRARD            $this->currentBookChapter = $counter;
520c7138b3fSGerrit Uitslag            $counter++;
521b3eed6e3SGerrit Uitslag
522*852931daSAndreas Gohr            $pagehtml = $this->wikiToDW2PDF($page, $rev, $date_at);
523e53f1ec0SSzymon Olewniczak            //file doesn't exists
524e53f1ec0SSzymon Olewniczak            if ($pagehtml == '') {
525b3eed6e3SGerrit Uitslag                continue;
526b3eed6e3SGerrit Uitslag            }
527*852931daSAndreas Gohr            $pagehtml .= $this->pageDependReplacements($template['cite'], $page);
528c7138b3fSGerrit Uitslag            if ($counter < $no_pages) {
529a2c33768SGerrit Uitslag                $pagehtml .= '<pagebreak />';
530a2c33768SGerrit Uitslag            }
531a2c33768SGerrit Uitslag
532a2c33768SGerrit Uitslag            $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html
53302f9a447SGerrit Uitslag            if ($isDebug) {
534a2c33768SGerrit Uitslag                $html .= $pagehtml;
5351ef68647SAndreas Gohr            }
536ee19bac3SLuigi Micco        }
537ee19bac3SLuigi Micco
53833c15297SGerrit Uitslag        // insert the back page
539a2c33768SGerrit Uitslag        $body_end = $template['back'];
54033c15297SGerrit Uitslag
541a2c33768SGerrit Uitslag        $body_end .= '</div>';
542a2c33768SGerrit Uitslag
543d63e7fe7SGerrit Uitslag        $mpdf->WriteHTML($body_end, 2, false, true); // finish body html
54402f9a447SGerrit Uitslag        if ($isDebug) {
545a2c33768SGerrit Uitslag            $html .= $body_end;
546eeb17e15SAndreas Gohr            $html .= '</body>';
547eeb17e15SAndreas Gohr            $html .= '</html>';
548a2c33768SGerrit Uitslag        }
549f765508eSGerrit Uitslag
550f765508eSGerrit Uitslag        //Return html for debugging
55102f9a447SGerrit Uitslag        if ($isDebug) {
55217e88313SGerrit Uitslag            if ($INPUT->str('debughtml', 'text', true) == 'text') {
553a2c33768SGerrit Uitslag                header('Content-Type: text/plain; charset=utf-8');
554a2c33768SGerrit Uitslag            }
55517e88313SGerrit Uitslag            echo $html;
55626be4eceSGerrit Uitslag            exit();
5577c79bc79SGerrit Uitslag        }
558f765508eSGerrit Uitslag
55987c86ddaSAndreas Gohr        // write to cache file
560d63e7fe7SGerrit Uitslag        $mpdf->Output($cachefile, 'F');
56187c86ddaSAndreas Gohr    }
56287c86ddaSAndreas Gohr
563d63e7fe7SGerrit Uitslag    /**
564d63e7fe7SGerrit Uitslag     * @param string $cachefile
565d63e7fe7SGerrit Uitslag     */
56617e88313SGerrit Uitslag    protected function sendPDFFile($cachefile)
56717e88313SGerrit Uitslag    {
56887c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
569b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
57087c86ddaSAndreas Gohr        header('Pragma: public');
571d63e7fe7SGerrit Uitslag        http_conditionalRequest(filemtime($cachefile));
572c0956f69SMichael Große        global $INPUT;
573f7b24f48SMichael Große        $outputTarget = $INPUT->str('outputTarget', $this->getConf('output'));
57487c86ddaSAndreas Gohr
57503352761SKirsten Roschanski        $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', '    ')));
576c0956f69SMichael Große        if ($outputTarget === 'file') {
5779a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
57887c86ddaSAndreas Gohr        } else {
5799a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
58087c86ddaSAndreas Gohr        }
581ee19bac3SLuigi Micco
582b3eed6e3SGerrit Uitslag        //Bookcreator uses jQuery.fileDownload.js, which requires a cookie.
583b3eed6e3SGerrit Uitslag        header('Set-Cookie: fileDownload=true; path=/');
584b3eed6e3SGerrit Uitslag
585e993da11SGerrit Uitslag        //try to send file, and exit if done
586d63e7fe7SGerrit Uitslag        http_sendfile($cachefile);
58787c86ddaSAndreas Gohr
588d63e7fe7SGerrit Uitslag        $fp = @fopen($cachefile, "rb");
58987c86ddaSAndreas Gohr        if ($fp) {
590d63e7fe7SGerrit Uitslag            http_rangeRequest($fp, filesize($cachefile), 'application/pdf');
59187c86ddaSAndreas Gohr        } else {
59287c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
593*852931daSAndreas Gohr            echo "Could not read file - bad permissions?";
59487c86ddaSAndreas Gohr        }
5951ef68647SAndreas Gohr        exit();
5961ef68647SAndreas Gohr    }
5971ef68647SAndreas Gohr
5986be736bfSGerrit Uitslag    /**
5992eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
60044e8e8fbSGerrit Uitslag     *
60144e8e8fbSGerrit Uitslag     * @return array
6021ef68647SAndreas Gohr     */
603*852931daSAndreas Gohr    protected function loadTemplate()
60417e88313SGerrit Uitslag    {
6051ef68647SAndreas Gohr        global $ID;
6061ef68647SAndreas Gohr        global $conf;
6070a11dabaSChris75forumname        global $INFO;
6081ef68647SAndreas Gohr
6092eedf77dSAndreas Gohr        // this is what we'll return
61021a55743SAnna Dabrowska        $output = [
6111e45476bSmnapp            'cover' => '',
61221a55743SAnna Dabrowska            'back' => '',
6132eedf77dSAndreas Gohr            'html' => '',
6142eedf77dSAndreas Gohr            'page' => '',
6152eedf77dSAndreas Gohr            'first' => '',
6162eedf77dSAndreas Gohr            'cite' => '',
61721a55743SAnna Dabrowska        ];
6182eedf77dSAndreas Gohr
6192eedf77dSAndreas Gohr        // prepare header/footer elements
6202eedf77dSAndreas Gohr        $html = '';
62117e88313SGerrit Uitslag        foreach (['header', 'footer'] as $section) {
62217e88313SGerrit Uitslag            foreach (['', '_odd', '_even', '_first'] as $order) {
62302f9a447SGerrit Uitslag                $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html';
62433c15297SGerrit Uitslag                if (file_exists($file)) {
62533c15297SGerrit Uitslag                    $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF;
62633c15297SGerrit Uitslag                    $html .= file_get_contents($file) . DOKU_LF;
62733c15297SGerrit Uitslag                    $html .= '</htmlpage' . $section . '>' . DOKU_LF;
6282eedf77dSAndreas Gohr
6292eedf77dSAndreas Gohr                    // register the needed pseudo CSS
63033c15297SGerrit Uitslag                    if ($order == '_first') {
63133c15297SGerrit Uitslag                        $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
63233c15297SGerrit Uitslag                    } elseif ($order == '_even') {
63333c15297SGerrit Uitslag                        $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
63433c15297SGerrit Uitslag                    } elseif ($order == '_odd') {
63533c15297SGerrit Uitslag                        $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF;
636daa70883SAndreas Gohr                    } else {
63733c15297SGerrit Uitslag                        $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF;
6382eedf77dSAndreas Gohr                    }
6392eedf77dSAndreas Gohr                }
6402eedf77dSAndreas Gohr            }
6412eedf77dSAndreas Gohr        }
6422eedf77dSAndreas Gohr
6431ef68647SAndreas Gohr        // prepare replacements
64417e88313SGerrit Uitslag        $replace = [
6451ef68647SAndreas Gohr            '@PAGE@' => '{PAGENO}',
64602f9a447SGerrit Uitslag            '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / '
64703352761SKirsten Roschanski            '@TITLE@' => hsc($this->title),
6481ef68647SAndreas Gohr            '@WIKI@' => $conf['title'],
6491ef68647SAndreas Gohr            '@WIKIURL@' => DOKU_URL,
6501ef68647SAndreas Gohr            '@DATE@' => dformat(time()),
6518b11a0e6SChris75forumname            '@USERNAME@' => $INFO['userinfo']['name'] ?? '',
6525d6fbaeaSAndreas Gohr            '@BASE@' => DOKU_BASE,
653893987a2SAndreas Gohr            '@INC@' => DOKU_INC,
654893987a2SAndreas Gohr            '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
655893987a2SAndreas Gohr            '@TPLINC@' => DOKU_INC . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/'
65617e88313SGerrit Uitslag        ];
6571ef68647SAndreas Gohr
6582eedf77dSAndreas Gohr        // set HTML element
659a180c973SKlap-in        $html = str_replace(array_keys($replace), array_values($replace), $html);
660a180c973SKlap-in        //TODO For bookcreator $ID (= bookmanager page) makes no sense
661*852931daSAndreas Gohr        $output['html'] = $this->pageDependReplacements($html, $ID);
6621ef68647SAndreas Gohr
6631e45476bSmnapp        // cover page
66402f9a447SGerrit Uitslag        $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html';
66533c15297SGerrit Uitslag        if (file_exists($coverfile)) {
66633c15297SGerrit Uitslag            $output['cover'] = file_get_contents($coverfile);
6671e45476bSmnapp            $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']);
668*852931daSAndreas Gohr            $output['cover'] = $this->pageDependReplacements($output['cover'], $ID);
6696e2ec302SGerrit Uitslag            $output['cover'] .= '<pagebreak />';
6701e45476bSmnapp        }
6711e45476bSmnapp
67233c15297SGerrit Uitslag        // cover page
67302f9a447SGerrit Uitslag        $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html';
67433c15297SGerrit Uitslag        if (file_exists($backfile)) {
67533c15297SGerrit Uitslag            $output['back'] = '<pagebreak />';
67633c15297SGerrit Uitslag            $output['back'] .= file_get_contents($backfile);
67733c15297SGerrit Uitslag            $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']);
678*852931daSAndreas Gohr            $output['back'] = $this->pageDependReplacements($output['back'], $ID);
67933c15297SGerrit Uitslag        }
68033c15297SGerrit Uitslag
6812eedf77dSAndreas Gohr        // citation box
68202f9a447SGerrit Uitslag        $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html';
68333c15297SGerrit Uitslag        if (file_exists($citationfile)) {
68433c15297SGerrit Uitslag            $output['cite'] = file_get_contents($citationfile);
6852eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
6862eedf77dSAndreas Gohr        }
6871ef68647SAndreas Gohr
6882eedf77dSAndreas Gohr        return $output;
6891ef68647SAndreas Gohr    }
6901ef68647SAndreas Gohr
6911ef68647SAndreas Gohr    /**
692a180c973SKlap-in     * @param string $raw code with placeholders
693a180c973SKlap-in     * @param string $id pageid
694a180c973SKlap-in     * @return string
695a180c973SKlap-in     */
696*852931daSAndreas Gohr    protected function pageDependReplacements($raw, $id)
69717e88313SGerrit Uitslag    {
698e53f1ec0SSzymon Olewniczak        global $REV, $DATE_AT;
699a180c973SKlap-in
700fb347f35SAndreas Gohr        // generate qr code for this page
701a180c973SKlap-in        $qr_code = '';
702fb347f35SAndreas Gohr        if ($this->getConf('qrcodescale')) {
703fb347f35SAndreas Gohr            $url = hsc(wl($id, '', '&', true));
704*852931daSAndreas Gohr            $size = (float)$this->getConf('qrcodescale');
705*852931daSAndreas Gohr            $qr_code = sprintf(
706*852931daSAndreas Gohr                '<barcode type="QR" code="%s" error="Q" disableborder="1" class="qrcode" size="%s" />',
707*852931daSAndreas Gohr                $url,
708*852931daSAndreas Gohr                $size
709*852931daSAndreas Gohr            );
710a180c973SKlap-in        }
711a180c973SKlap-in        // prepare replacements
712a180c973SKlap-in        $replace['@ID@'] = $id;
713a180c973SKlap-in        $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV)));
714e53f1ec0SSzymon Olewniczak
71517e88313SGerrit Uitslag        $params = [];
716e53f1ec0SSzymon Olewniczak        if ($DATE_AT) {
717e53f1ec0SSzymon Olewniczak            $params['at'] = $DATE_AT;
718e53f1ec0SSzymon Olewniczak        } elseif ($REV) {
719e53f1ec0SSzymon Olewniczak            $params['rev'] = $REV;
720e53f1ec0SSzymon Olewniczak        }
721e53f1ec0SSzymon Olewniczak        $replace['@PAGEURL@'] = wl($id, $params, true, "&");
722a180c973SKlap-in        $replace['@QRCODE@'] = $qr_code;
723a180c973SKlap-in
724d824d23dSAnna Dabrowska        $content = $raw;
725a12c41c3SAnna Dabrowska
726a12c41c3SAnna Dabrowska        // let other plugins define their own replacements
727d824d23dSAnna Dabrowska        $evdata = ['id' => $id, 'replace' => &$replace, 'content' => &$content];
728*852931daSAndreas Gohr        $event = new Event('PLUGIN_DW2PDF_REPLACE', $evdata);
729d824d23dSAnna Dabrowska        if ($event->advise_before()) {
730e3d68265SGerrit Uitslag            $content = str_replace(array_keys($replace), array_values($replace), $raw);
731d824d23dSAnna Dabrowska        }
732e3d68265SGerrit Uitslag
733a12c41c3SAnna Dabrowska        // plugins may post-process HTML, e.g to clean up unused replacements
734a12c41c3SAnna Dabrowska        $event->advise_after();
735a12c41c3SAnna Dabrowska
736e3d68265SGerrit Uitslag        // @DATE(<date>[, <format>])@
737e3d68265SGerrit Uitslag        $content = preg_replace_callback(
738e3d68265SGerrit Uitslag            '/@DATE\((.*?)(?:,\s*(.*?))?\)@/',
739*852931daSAndreas Gohr            [$this, 'replaceDate'],
740e3d68265SGerrit Uitslag            $content
741e3d68265SGerrit Uitslag        );
742e3d68265SGerrit Uitslag
743e3d68265SGerrit Uitslag        return $content;
744a180c973SKlap-in    }
745a180c973SKlap-in
746e3d68265SGerrit Uitslag
747e3d68265SGerrit Uitslag    /**
748e3d68265SGerrit Uitslag     * (callback) Replace date by request datestring
749e3d68265SGerrit Uitslag     * e.g. '%m(30-11-1975)' is replaced by '11'
750e3d68265SGerrit Uitslag     *
751e3d68265SGerrit Uitslag     * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern
752e3d68265SGerrit Uitslag     * @return string
753e3d68265SGerrit Uitslag     */
754*852931daSAndreas Gohr    public function replaceDate($match)
75517e88313SGerrit Uitslag    {
756e3d68265SGerrit Uitslag        global $conf;
757e3d68265SGerrit Uitslag        //no 2nd argument for default date format
758e3d68265SGerrit Uitslag        if ($match[2] == null) {
759e3d68265SGerrit Uitslag            $match[2] = $conf['dformat'];
760e3d68265SGerrit Uitslag        }
761e3d68265SGerrit Uitslag        return strftime($match[2], strtotime($match[1]));
762e3d68265SGerrit Uitslag    }
763e3d68265SGerrit Uitslag
764a180c973SKlap-in    /**
7651c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
7661ef68647SAndreas Gohr     */
767*852931daSAndreas Gohr    protected function loadCSS()
76817e88313SGerrit Uitslag    {
769737417c6SKlap-in        global $conf;
7707c79bc79SGerrit Uitslag        //reuse the CSS dispatcher functions without triggering the main function
7711c14c879SAndreas Gohr        define('SIMPLE_TEST', 1);
7721c14c879SAndreas Gohr        require_once(DOKU_INC . 'lib/exe/css.php');
773ee19bac3SLuigi Micco
7741c14c879SAndreas Gohr        // prepare CSS files
7751c14c879SAndreas Gohr        $files = array_merge(
77617e88313SGerrit Uitslag            [
77717e88313SGerrit Uitslag                DOKU_INC . 'lib/styles/screen.css' => DOKU_BASE . 'lib/styles/',
77817e88313SGerrit Uitslag                DOKU_INC . 'lib/styles/print.css' => DOKU_BASE . 'lib/styles/',
77917e88313SGerrit Uitslag            ],
780*852931daSAndreas Gohr            $this->cssPluginPDFstyles(),
78117e88313SGerrit Uitslag            [
78217e88313SGerrit Uitslag                DOKU_PLUGIN . 'dw2pdf/conf/style.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
783*852931daSAndreas Gohr                DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' =>
784*852931daSAndreas Gohr                    DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/',
78517e88313SGerrit Uitslag                DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' => DOKU_BASE . 'lib/plugins/dw2pdf/conf/',
78617e88313SGerrit Uitslag            ]
7871c14c879SAndreas Gohr        );
7881c14c879SAndreas Gohr        $css = '';
7891c14c879SAndreas Gohr        foreach ($files as $file => $location) {
79028e636eaSGerrit Uitslag            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
79128e636eaSGerrit Uitslag            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
7921c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
7931ef68647SAndreas Gohr        }
7941ef68647SAndreas Gohr
79528e636eaSGerrit Uitslag        if (function_exists('css_parseless')) {
7961c14c879SAndreas Gohr            // apply pattern replacements
7977c6ca3bcSMichael Große            if (function_exists('css_styleini')) {
7987c6ca3bcSMichael Große                // compatiblity layer for pre-Greebo releases of DokuWiki
79928e636eaSGerrit Uitslag                $styleini = css_styleini($conf['template']);
8007c6ca3bcSMichael Große            } else {
8017c6ca3bcSMichael Große                // Greebo functionality
80217e88313SGerrit Uitslag                $styleUtils = new StyleUtils();
803d0f0c534SGerrit Uitslag                $styleini = $styleUtils->cssStyleini($conf['template']); // older versions need still the template
8047c6ca3bcSMichael Große            }
80528e636eaSGerrit Uitslag            $css = css_applystyle($css, $styleini['replacements']);
80628e636eaSGerrit Uitslag
80728e636eaSGerrit Uitslag            // parse less
80828e636eaSGerrit Uitslag            $css = css_parseless($css);
80928e636eaSGerrit Uitslag        } else {
81028e636eaSGerrit Uitslag            // @deprecated 2013-12-19: fix backward compatibility
8111c14c879SAndreas Gohr            $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/');
81228e636eaSGerrit Uitslag        }
8131ef68647SAndreas Gohr
8141c14c879SAndreas Gohr        return $css;
815ee19bac3SLuigi Micco    }
8161c14c879SAndreas Gohr
81758e6409eSAndreas Gohr    /**
81858e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
81958e6409eSAndreas Gohr     *
82058e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
82158e6409eSAndreas Gohr     *
82258e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
82358e6409eSAndreas Gohr     */
824*852931daSAndreas Gohr    protected function cssPluginPDFstyles()
82517e88313SGerrit Uitslag    {
82617e88313SGerrit Uitslag        $list = [];
82758e6409eSAndreas Gohr        $plugins = plugin_list();
828f54b51f7SAndreas Gohr
829f54b51f7SAndreas Gohr        $usestyle = explode(',', $this->getConf('usestyles'));
83058e6409eSAndreas Gohr        foreach ($plugins as $p) {
831f54b51f7SAndreas Gohr            if (in_array($p, $usestyle)) {
832f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/";
8338003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/";
8348003b493SGerrit Uitslag
835f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
8368003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/";
837f54b51f7SAndreas Gohr            }
838f54b51f7SAndreas Gohr
8398003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/";
8408003b493SGerrit Uitslag            $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/";
8418003b493SGerrit Uitslag
842ab96d816SMichael Große            if (file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) {
84358e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/";
8448003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/";
84558e6409eSAndreas Gohr            } else {
84658e6409eSAndreas Gohr                $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/";
8478003b493SGerrit Uitslag                $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/";
84858e6409eSAndreas Gohr            }
84958e6409eSAndreas Gohr        }
8508b8ac6ecSAndreas Gohr
8518b8ac6ecSAndreas Gohr        // template support
852*852931daSAndreas Gohr        foreach (
853*852931daSAndreas Gohr            [
854*852931daSAndreas Gohr                     'pdf.css',
855*852931daSAndreas Gohr                     'pdf.less',
856*852931daSAndreas Gohr                     'css/pdf.css',
857*852931daSAndreas Gohr                     'css/pdf.less',
858*852931daSAndreas Gohr                     'styles/pdf.css',
859*852931daSAndreas Gohr                     'styles/pdf.less'
860*852931daSAndreas Gohr                 ] as $file
861*852931daSAndreas Gohr        ) {
8628b8ac6ecSAndreas Gohr            if (file_exists(tpl_incdir() . $file)) {
8638b8ac6ecSAndreas Gohr                $list[tpl_incdir() . $file] = tpl_basedir() . $file;
8648b8ac6ecSAndreas Gohr            }
8658b8ac6ecSAndreas Gohr        }
8668b8ac6ecSAndreas Gohr
86758e6409eSAndreas Gohr        return $list;
86858e6409eSAndreas Gohr    }
869ad18f4e1SGerrit Uitslag
870ad18f4e1SGerrit Uitslag    /**
87160e59de7SGerrit Uitslag     * Returns array of pages which will be included in the exported pdf
87260e59de7SGerrit Uitslag     *
87360e59de7SGerrit Uitslag     * @return array
87460e59de7SGerrit Uitslag     */
87517e88313SGerrit Uitslag    public function getExportedPages()
87617e88313SGerrit Uitslag    {
87760e59de7SGerrit Uitslag        return $this->list;
87860e59de7SGerrit Uitslag    }
87960e59de7SGerrit Uitslag
88060e59de7SGerrit Uitslag    /**
881ad18f4e1SGerrit Uitslag     * usort callback to sort by file lastmodified time
88244e8e8fbSGerrit Uitslag     *
88344e8e8fbSGerrit Uitslag     * @param array $a
88444e8e8fbSGerrit Uitslag     * @param array $b
88544e8e8fbSGerrit Uitslag     * @return int
886ad18f4e1SGerrit Uitslag     */
887*852931daSAndreas Gohr    public function cbDateSort($a, $b)
88817e88313SGerrit Uitslag    {
889ad18f4e1SGerrit Uitslag        if ($b['rev'] < $a['rev']) return -1;
890ad18f4e1SGerrit Uitslag        if ($b['rev'] > $a['rev']) return 1;
891ad18f4e1SGerrit Uitslag        return strcmp($b['id'], $a['id']);
892ad18f4e1SGerrit Uitslag    }
893ad18f4e1SGerrit Uitslag
894ad18f4e1SGerrit Uitslag    /**
895ad18f4e1SGerrit Uitslag     * usort callback to sort by page id
89644e8e8fbSGerrit Uitslag     * @param array $a
89744e8e8fbSGerrit Uitslag     * @param array $b
89844e8e8fbSGerrit Uitslag     * @return int
899ad18f4e1SGerrit Uitslag     */
900*852931daSAndreas Gohr    public function cbPagenameSort($a, $b)
90117e88313SGerrit Uitslag    {
90220025b90SAndreas Gohr        global $conf;
90320025b90SAndreas Gohr
90420025b90SAndreas Gohr        $partsA = explode(':', $a['id']);
90520025b90SAndreas Gohr        $countA = count($partsA);
90620025b90SAndreas Gohr        $partsB = explode(':', $b['id']);
90720025b90SAndreas Gohr        $countB = count($partsB);
908e00f6071SAndreas Gohr        $max = max($countA, $countB);
90920025b90SAndreas Gohr
91020025b90SAndreas Gohr
91120025b90SAndreas Gohr        // compare namepsace by namespace
912e00f6071SAndreas Gohr        for ($i = 0; $i < $max; $i++) {
913e00f6071SAndreas Gohr            $partA = $partsA[$i] ?: null;
914e00f6071SAndreas Gohr            $partB = $partsB[$i] ?: null;
91520025b90SAndreas Gohr
91620025b90SAndreas Gohr            // have we reached the page level?
917e00f6071SAndreas Gohr            if ($i === ($countA - 1) || $i === ($countB - 1)) {
91820025b90SAndreas Gohr                // start page first
91920025b90SAndreas Gohr                if ($partA == $conf['start']) return -1;
92020025b90SAndreas Gohr                if ($partB == $conf['start']) return 1;
92120025b90SAndreas Gohr            }
92220025b90SAndreas Gohr
923e00f6071SAndreas Gohr            // prefer page over namespace
924e00f6071SAndreas Gohr            if ($partA === $partB) {
925e00f6071SAndreas Gohr                if (!isset($partsA[$i + 1])) return -1;
926e00f6071SAndreas Gohr                if (!isset($partsB[$i + 1])) return 1;
927e00f6071SAndreas Gohr                continue;
928e00f6071SAndreas Gohr            }
929e00f6071SAndreas Gohr
930e00f6071SAndreas Gohr
93120025b90SAndreas Gohr            // simply compare
93241e5d4e2SAndreas Gohr            return strnatcmp($partA, $partB);
93320025b90SAndreas Gohr        }
93420025b90SAndreas Gohr
93541e5d4e2SAndreas Gohr        return strnatcmp($a['id'], $b['id']);
936ad18f4e1SGerrit Uitslag    }
93726be4eceSGerrit Uitslag
93826be4eceSGerrit Uitslag    /**
9397c79bc79SGerrit Uitslag     * Collects settings from:
94002f9a447SGerrit Uitslag     *   1. url parameters
94102f9a447SGerrit Uitslag     *   2. plugin config
94202f9a447SGerrit Uitslag     *   3. global config
94302f9a447SGerrit Uitslag     */
94417e88313SGerrit Uitslag    protected function loadExportConfig()
94517e88313SGerrit Uitslag    {
94602f9a447SGerrit Uitslag        global $INPUT;
94702f9a447SGerrit Uitslag        global $conf;
94802f9a447SGerrit Uitslag
94917e88313SGerrit Uitslag        $this->exportConfig = [];
95002f9a447SGerrit Uitslag
95102f9a447SGerrit Uitslag        // decide on the paper setup from param or config
95202f9a447SGerrit Uitslag        $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
95302f9a447SGerrit Uitslag        $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true);
95402f9a447SGerrit Uitslag
9554870b378SLarsDW223        // decide on the font-size from param or config
9564870b378SLarsDW223        $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true);
9574870b378SLarsDW223
958213fdb75SGerrit Uitslag        $doublesided = $INPUT->bool('doublesided', (bool)$this->getConf('doublesided'));
959213fdb75SGerrit Uitslag        $this->exportConfig['doublesided'] = $doublesided ? '1' : '0';
960213fdb75SGerrit Uitslag
961b80f709fSNicolas        $this->exportConfig['watermark'] = $INPUT->str('watermark', '');
962b80f709fSNicolas
963213fdb75SGerrit Uitslag        $hasToC = $INPUT->bool('toc', (bool)$this->getConf('toc'));
96417e88313SGerrit Uitslag        $levels = [];
96502f9a447SGerrit Uitslag        if ($hasToC) {
96602f9a447SGerrit Uitslag            $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true);
967*852931daSAndreas Gohr            [$top_input, $max_input] = array_pad(explode('-', $toclevels, 2), 2, '');
968*852931daSAndreas Gohr            [$top_conf, $max_conf] = array_pad(explode('-', $this->getConf('toclevels'), 2), 2, '');
96917e88313SGerrit Uitslag            $bounds_input = [
97017e88313SGerrit Uitslag                'top' => [
97102f9a447SGerrit Uitslag                    (int)$top_input,
97202f9a447SGerrit Uitslag                    (int)$top_conf
97317e88313SGerrit Uitslag                ],
97417e88313SGerrit Uitslag                'max' => [
97502f9a447SGerrit Uitslag                    (int)$max_input,
97602f9a447SGerrit Uitslag                    (int)$max_conf
97717e88313SGerrit Uitslag                ]
97817e88313SGerrit Uitslag            ];
97917e88313SGerrit Uitslag            $bounds = [
98002f9a447SGerrit Uitslag                'top' => $conf['toptoclevel'],
98102f9a447SGerrit Uitslag                'max' => $conf['maxtoclevel']
98202f9a447SGerrit Uitslag
98317e88313SGerrit Uitslag            ];
98402f9a447SGerrit Uitslag            foreach ($bounds_input as $bound => $values) {
98502f9a447SGerrit Uitslag                foreach ($values as $value) {
98602f9a447SGerrit Uitslag                    if ($value > 0 && $value <= 5) {
98702f9a447SGerrit Uitslag                        //stop at valid value and store
98802f9a447SGerrit Uitslag                        $bounds[$bound] = $value;
98902f9a447SGerrit Uitslag                        break;
99002f9a447SGerrit Uitslag                    }
99102f9a447SGerrit Uitslag                }
99202f9a447SGerrit Uitslag            }
99302f9a447SGerrit Uitslag
99402f9a447SGerrit Uitslag            if ($bounds['max'] < $bounds['top']) {
99502f9a447SGerrit Uitslag                $bounds['max'] = $bounds['top'];
99602f9a447SGerrit Uitslag            }
99702f9a447SGerrit Uitslag
99802f9a447SGerrit Uitslag            for ($level = $bounds['top']; $level <= $bounds['max']; $level++) {
99902f9a447SGerrit Uitslag                $levels["H$level"] = $level - 1;
100002f9a447SGerrit Uitslag            }
100102f9a447SGerrit Uitslag        }
100202f9a447SGerrit Uitslag        $this->exportConfig['hasToC'] = $hasToC;
100302f9a447SGerrit Uitslag        $this->exportConfig['levels'] = $levels;
100402f9a447SGerrit Uitslag
100502f9a447SGerrit Uitslag        $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true);
100602f9a447SGerrit Uitslag
100702f9a447SGerrit Uitslag        $tplconf = $this->getConf('template');
100805d2b507SGerrit Uitslag        $tpl = $INPUT->str('tpl', $tplconf, true);
100902f9a447SGerrit Uitslag        if (!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) {
101002f9a447SGerrit Uitslag            $tpl = $tplconf;
101102f9a447SGerrit Uitslag        }
101202f9a447SGerrit Uitslag        if (!$tpl) {
101302f9a447SGerrit Uitslag            $tpl = 'default';
101402f9a447SGerrit Uitslag        }
101502f9a447SGerrit Uitslag        $this->exportConfig['template'] = $tpl;
101602f9a447SGerrit Uitslag
101702f9a447SGerrit Uitslag        $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml');
101802f9a447SGerrit Uitslag    }
101902f9a447SGerrit Uitslag
102002f9a447SGerrit Uitslag    /**
102102f9a447SGerrit Uitslag     * Returns requested config
102202f9a447SGerrit Uitslag     *
102302f9a447SGerrit Uitslag     * @param string $name
102402f9a447SGerrit Uitslag     * @param mixed $notset
102502f9a447SGerrit Uitslag     * @return mixed|bool
102602f9a447SGerrit Uitslag     */
102717e88313SGerrit Uitslag    public function getExportConfig($name, $notset = false)
102817e88313SGerrit Uitslag    {
102902f9a447SGerrit Uitslag        if ($this->exportConfig === null) {
103002f9a447SGerrit Uitslag            $this->loadExportConfig();
103102f9a447SGerrit Uitslag        }
103202f9a447SGerrit Uitslag
103317e88313SGerrit Uitslag        return $this->exportConfig[$name] ?? $notset;
103402f9a447SGerrit Uitslag    }
1035d63e7fe7SGerrit Uitslag
1036d63e7fe7SGerrit Uitslag    /**
1037d63e7fe7SGerrit Uitslag     * Add 'export pdf'-button to pagetools
1038d63e7fe7SGerrit Uitslag     *
1039d63e7fe7SGerrit Uitslag     * @param Doku_Event $event
1040d63e7fe7SGerrit Uitslag     */
1041*852931daSAndreas Gohr    public function addbutton(Event $event)
104217e88313SGerrit Uitslag    {
1043e53f1ec0SSzymon Olewniczak        global $ID, $REV, $DATE_AT;
1044d63e7fe7SGerrit Uitslag
1045d63e7fe7SGerrit Uitslag        if ($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
104617e88313SGerrit Uitslag            $params = ['do' => 'export_pdf'];
1047e53f1ec0SSzymon Olewniczak            if ($DATE_AT) {
1048e53f1ec0SSzymon Olewniczak                $params['at'] = $DATE_AT;
1049e53f1ec0SSzymon Olewniczak            } elseif ($REV) {
1050d63e7fe7SGerrit Uitslag                $params['rev'] = $REV;
1051d63e7fe7SGerrit Uitslag            }
1052d63e7fe7SGerrit Uitslag
1053d63e7fe7SGerrit Uitslag            // insert button at position before last (up to top)
1054d63e7fe7SGerrit Uitslag            $event->data['items'] = array_slice($event->data['items'], 0, -1, true) +
1055*852931daSAndreas Gohr                ['export_pdf' => sprintf(
1056*852931daSAndreas Gohr                    '<li><a href="%s" class="%s" rel="nofollow" title="%s"><span>%s</span></a></li>',
1057*852931daSAndreas Gohr                    wl($ID, $params),
1058*852931daSAndreas Gohr                    'action export_pdf',
1059*852931daSAndreas Gohr                    $this->getLang('export_pdf_button'),
1060*852931daSAndreas Gohr                    $this->getLang('export_pdf_button')
1061*852931daSAndreas Gohr                )] +
1062d63e7fe7SGerrit Uitslag                array_slice($event->data['items'], -1, 1, true);
1063d63e7fe7SGerrit Uitslag        }
1064d63e7fe7SGerrit Uitslag    }
1065026b594bSAndreas Gohr
1066026b594bSAndreas Gohr    /**
1067026b594bSAndreas Gohr     * Add 'export pdf' button to page tools, new SVG based mechanism
1068026b594bSAndreas Gohr     *
1069026b594bSAndreas Gohr     * @param Doku_Event $event
1070026b594bSAndreas Gohr     */
1071*852931daSAndreas Gohr    public function addsvgbutton(Event $event)
107217e88313SGerrit Uitslag    {
10734c493e44SGerrit Uitslag        global $INFO;
10744c493e44SGerrit Uitslag        if ($event->data['view'] != 'page' || !$this->getConf('showexportbutton')) {
10754c493e44SGerrit Uitslag            return;
10764c493e44SGerrit Uitslag        }
10774c493e44SGerrit Uitslag
10784c493e44SGerrit Uitslag        if (!$INFO['exists']) {
10794c493e44SGerrit Uitslag            return;
10804c493e44SGerrit Uitslag        }
10814c493e44SGerrit Uitslag
108217e88313SGerrit Uitslag        array_splice($event->data['items'], -1, 0, [new MenuItem()]);
1083026b594bSAndreas Gohr    }
1084979c9cf5SAndreas Gohr
1085979c9cf5SAndreas Gohr    /**
1086979c9cf5SAndreas Gohr     * Get the language of the current document
1087979c9cf5SAndreas Gohr     *
1088979c9cf5SAndreas Gohr     * Uses the translation plugin if available
1089979c9cf5SAndreas Gohr     * @return string
1090979c9cf5SAndreas Gohr     */
1091979c9cf5SAndreas Gohr    protected function getDocumentLanguage($pageid)
1092979c9cf5SAndreas Gohr    {
1093979c9cf5SAndreas Gohr        global $conf;
1094979c9cf5SAndreas Gohr
1095979c9cf5SAndreas Gohr        $lang = $conf['lang'];
1096979c9cf5SAndreas Gohr        /** @var helper_plugin_translation $trans */
1097979c9cf5SAndreas Gohr        $trans = plugin_load('helper', 'translation');
1098979c9cf5SAndreas Gohr        if ($trans) {
1099979c9cf5SAndreas Gohr            $tr = $trans->getLangPart($pageid);
1100979c9cf5SAndreas Gohr            if ($tr) $lang = $tr;
1101979c9cf5SAndreas Gohr        }
1102979c9cf5SAndreas Gohr
1103979c9cf5SAndreas Gohr        return $lang;
1104979c9cf5SAndreas Gohr    }
1105ee19bac3SLuigi Micco}
1106