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