xref: /dokuwiki/inc/Action/Export.php (revision b1a9a7addb625755af19900df6d0fba816dce1c3)
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    /** @inheritdoc */
18d868eb89SAndreas Gohr    public function minimumPermission()
19d868eb89SAndreas Gohr    {
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     */
38d868eb89SAndreas Gohr    public function preProcess()
39d868eb89SAndreas Gohr    {
40f21dad39SAndreas Gohr        global $ID;
41f21dad39SAndreas Gohr        global $REV;
42f21dad39SAndreas Gohr        global $conf;
43f21dad39SAndreas Gohr        global $lang;
44f21dad39SAndreas Gohr
45f21dad39SAndreas Gohr        $pre = '';
46f21dad39SAndreas Gohr        $post = '';
476723156fSAndreas Gohr        $headers = [];
48f21dad39SAndreas Gohr
49f21dad39SAndreas Gohr        // search engines: never cache exported docs! (Google only currently)
50f21dad39SAndreas Gohr        $headers['X-Robots-Tag'] = 'noindex';
51f21dad39SAndreas Gohr
5273522543SAndreas Gohr        $mode = substr($this->actionname, 7);
53f21dad39SAndreas Gohr        switch ($mode) {
54f21dad39SAndreas Gohr            case 'raw':
55f21dad39SAndreas Gohr                $headers['Content-Type'] = 'text/plain; charset=utf-8';
56f21dad39SAndreas Gohr                $headers['Content-Disposition'] = 'attachment; filename=' . noNS($ID) . '.txt';
57f21dad39SAndreas Gohr                $output = rawWiki($ID, $REV);
58f21dad39SAndreas Gohr                break;
59f21dad39SAndreas Gohr            case 'xhtml':
60f21dad39SAndreas Gohr                $pre .= '<!DOCTYPE html>' . DOKU_LF;
61f21dad39SAndreas Gohr                $pre .= '<html lang="' . $conf['lang'] . '" dir="' . $lang['direction'] . '">' . DOKU_LF;
62f21dad39SAndreas Gohr                $pre .= '<head>' . DOKU_LF;
63f21dad39SAndreas Gohr                $pre .= '  <meta charset="utf-8" />' . DOKU_LF; // FIXME improve wrapper
64f21dad39SAndreas Gohr                $pre .= '  <title>' . $ID . '</title>' . DOKU_LF;
65f21dad39SAndreas Gohr
66f21dad39SAndreas Gohr                // get metaheaders
67f21dad39SAndreas Gohr                ob_start();
68f21dad39SAndreas Gohr                tpl_metaheaders();
69f21dad39SAndreas Gohr                $pre .= ob_get_clean();
70f21dad39SAndreas Gohr
71f21dad39SAndreas Gohr                $pre .= '</head>' . DOKU_LF;
72f21dad39SAndreas Gohr                $pre .= '<body>' . DOKU_LF;
73f21dad39SAndreas Gohr                $pre .= '<div class="dokuwiki export">' . DOKU_LF;
74f21dad39SAndreas Gohr
75f21dad39SAndreas Gohr                // get toc
76f21dad39SAndreas Gohr                $pre .= tpl_toc(true);
77f21dad39SAndreas Gohr
78f21dad39SAndreas Gohr                $headers['Content-Type'] = 'text/html; charset=utf-8';
79f21dad39SAndreas Gohr                $output = p_wiki_xhtml($ID, $REV, false);
80f21dad39SAndreas Gohr
81f21dad39SAndreas Gohr                $post .= '</div>' . DOKU_LF;
82f21dad39SAndreas Gohr                $post .= '</body>' . DOKU_LF;
83f21dad39SAndreas Gohr                $post .= '</html>' . DOKU_LF;
84f21dad39SAndreas Gohr                break;
85f21dad39SAndreas Gohr            case 'xhtmlbody':
86f21dad39SAndreas Gohr                $headers['Content-Type'] = 'text/html; charset=utf-8';
87f21dad39SAndreas Gohr                $output = p_wiki_xhtml($ID, $REV, false);
88f21dad39SAndreas Gohr                break;
89*b1a9a7adSKazutaka Miyasaka            case 'metadata':
90*b1a9a7adSKazutaka Miyasaka                // metadata should not be exported
91*b1a9a7adSKazutaka Miyasaka                break;
92f21dad39SAndreas Gohr            default:
93f21dad39SAndreas Gohr                $output = p_cached_output(wikiFN($ID, $REV), $mode, $ID);
94f21dad39SAndreas Gohr                $headers = p_get_metadata($ID, "format $mode");
95f21dad39SAndreas Gohr                break;
96f21dad39SAndreas Gohr        }
97f21dad39SAndreas Gohr
98f21dad39SAndreas Gohr        // prepare event data
996723156fSAndreas Gohr        $data = [];
100f21dad39SAndreas Gohr        $data['id'] = $ID;
101f21dad39SAndreas Gohr        $data['mode'] = $mode;
102f21dad39SAndreas Gohr        $data['headers'] = $headers;
103f21dad39SAndreas Gohr        $data['output'] =& $output;
104f21dad39SAndreas Gohr
105cbb44eabSAndreas Gohr        Event::createAndTrigger('ACTION_EXPORT_POSTPROCESS', $data);
106f21dad39SAndreas Gohr
107f21dad39SAndreas Gohr        if (!empty($data['output'])) {
108f21dad39SAndreas Gohr            if (is_array($data['headers'])) foreach ($data['headers'] as $key => $val) {
109f21dad39SAndreas Gohr                header("$key: $val");
110f21dad39SAndreas Gohr            }
11126dfc232SAndreas Gohr            echo $pre . $data['output'] . $post;
112f21dad39SAndreas Gohr            exit;
113f21dad39SAndreas Gohr        }
114f21dad39SAndreas Gohr
115f21dad39SAndreas Gohr        throw new ActionAbort();
116f21dad39SAndreas Gohr    }
117f21dad39SAndreas Gohr}
118