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