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