1<?php
2
3namespace dokuwiki\Action;
4
5use dokuwiki\Action\Exception\ActionAbort;
6use dokuwiki\Extension\Event;
7
8/**
9 * Class Export
10 *
11 * Handle exporting by calling the appropriate renderer
12 *
13 * @package dokuwiki\Action
14 */
15class Export extends AbstractAction
16{
17    /** @inheritdoc */
18    public function minimumPermission()
19    {
20        return AUTH_READ;
21    }
22
23    /**
24     * Export a wiki page for various formats
25     *
26     * Triggers ACTION_EXPORT_POSTPROCESS
27     *
28     *  Event data:
29     *    data['id']      -- page id
30     *    data['mode']    -- requested export mode
31     *    data['headers'] -- export headers
32     *    data['output']  -- export output
33     *
34     * @author Andreas Gohr <andi@splitbrain.org>
35     * @author Michael Klier <chi@chimeric.de>
36     * @inheritdoc
37     */
38    public function preProcess()
39    {
40        global $ID;
41        global $REV;
42        global $conf;
43        global $lang;
44
45        $pre = '';
46        $post = '';
47        $headers = [];
48
49        // search engines: never cache exported docs! (Google only currently)
50        $headers['X-Robots-Tag'] = 'noindex';
51
52        $mode = substr($this->actionname, 7);
53        switch ($mode) {
54            case 'raw':
55                $headers['Content-Type'] = 'text/plain; charset=utf-8';
56                $headers['Content-Disposition'] = 'attachment; filename=' . noNS($ID) . '.txt';
57                $output = rawWiki($ID, $REV);
58                break;
59            case 'xhtml':
60                $pre .= '<!DOCTYPE html>' . DOKU_LF;
61                $pre .= '<html lang="' . $conf['lang'] . '" dir="' . $lang['direction'] . '">' . DOKU_LF;
62                $pre .= '<head>' . DOKU_LF;
63                $pre .= '  <meta charset="utf-8" />' . DOKU_LF; // FIXME improve wrapper
64                $pre .= '  <title>' . $ID . '</title>' . DOKU_LF;
65
66                // get metaheaders
67                ob_start();
68                tpl_metaheaders();
69                $pre .= ob_get_clean();
70
71                $pre .= '</head>' . DOKU_LF;
72                $pre .= '<body>' . DOKU_LF;
73                $pre .= '<div class="dokuwiki export">' . DOKU_LF;
74
75                // get toc
76                $pre .= tpl_toc(true);
77
78                $headers['Content-Type'] = 'text/html; charset=utf-8';
79                $output = p_wiki_xhtml($ID, $REV, false);
80
81                $post .= '</div>' . DOKU_LF;
82                $post .= '</body>' . DOKU_LF;
83                $post .= '</html>' . DOKU_LF;
84                break;
85            case 'xhtmlbody':
86                $headers['Content-Type'] = 'text/html; charset=utf-8';
87                $output = p_wiki_xhtml($ID, $REV, false);
88                break;
89            default:
90                $output = p_cached_output(wikiFN($ID, $REV), $mode, $ID);
91                $headers = p_get_metadata($ID, "format $mode");
92                break;
93        }
94
95        // prepare event data
96        $data = [];
97        $data['id'] = $ID;
98        $data['mode'] = $mode;
99        $data['headers'] = $headers;
100        $data['output'] =& $output;
101
102        Event::createAndTrigger('ACTION_EXPORT_POSTPROCESS', $data);
103
104        if (!empty($data['output'])) {
105            if (is_array($data['headers'])) foreach ($data['headers'] as $key => $val) {
106                header("$key: $val");
107            }
108            echo $pre . $data['output'] . $post;
109            exit;
110        }
111
112        throw new ActionAbort();
113    }
114}
115