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