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