xref: /plugin/struct/renderer/csv.php (revision ba55430d2e5d308a2368c464ad356bf445d14fb5)
1eafc109fSAndreas Gohr<?php
2eafc109fSAndreas Gohr
3eafc109fSAndreas Gohr/**
4b22abfe9SAndreas Gohr * CSV export of tabular data
5eafc109fSAndreas Gohr *
6b22abfe9SAndreas Gohr * @link https://tools.ietf.org/html/rfc4180
7b22abfe9SAndreas Gohr * @link http://csvlint.io/
8eafc109fSAndreas Gohr */
9eafc109fSAndreas Gohrclass renderer_plugin_struct_csv extends Doku_Renderer {
10eafc109fSAndreas Gohr
11eafc109fSAndreas Gohr    protected $first = false;
12eafc109fSAndreas Gohr
13eafc109fSAndreas Gohr    /**
14eafc109fSAndreas Gohr     * Determine if out put is wanted right now
15eafc109fSAndreas Gohr     *
16eafc109fSAndreas Gohr     * @return bool
17eafc109fSAndreas Gohr     */
18eafc109fSAndreas Gohr    function _doOutput() {
19eafc109fSAndreas Gohr        global $INPUT;
20eafc109fSAndreas Gohr
21eafc109fSAndreas Gohr        if(
22eafc109fSAndreas Gohr            !isset($this->info['struct_table_hash']) or
23eafc109fSAndreas Gohr            $this->info['struct_table_hash'] != $INPUT->str('hash')
24eafc109fSAndreas Gohr        ) {
25eafc109fSAndreas Gohr            return false;
26eafc109fSAndreas Gohr        }
27eafc109fSAndreas Gohr
28eafc109fSAndreas Gohr        if(!empty($this->info['struct_table_meta'])) {
29eafc109fSAndreas Gohr            return false;
30eafc109fSAndreas Gohr        }
31eafc109fSAndreas Gohr
32eafc109fSAndreas Gohr        return true;
33eafc109fSAndreas Gohr    }
34eafc109fSAndreas Gohr
35eafc109fSAndreas Gohr    /**
36eafc109fSAndreas Gohr     * Our own format
37eafc109fSAndreas Gohr     *
38eafc109fSAndreas Gohr     * @return string
39eafc109fSAndreas Gohr     */
40eafc109fSAndreas Gohr    function getFormat() {
41eafc109fSAndreas Gohr        return 'struct_csv';
42eafc109fSAndreas Gohr    }
43eafc109fSAndreas Gohr
44eafc109fSAndreas Gohr    /**
45eafc109fSAndreas Gohr     * Set proper headers
46eafc109fSAndreas Gohr     */
47eafc109fSAndreas Gohr    function document_start() {
48eafc109fSAndreas Gohr        global $ID;
49b22abfe9SAndreas Gohr        $filename = noNS($ID) . '.csv';
50eafc109fSAndreas Gohr        $headers = array(
51b22abfe9SAndreas Gohr            'Content-Type' => 'text/csv',
52eafc109fSAndreas Gohr            'Content-Disposition' => 'attachment; filename="' . $filename . '";'
53eafc109fSAndreas Gohr        );
541ba236caSAndreas Gohr        p_set_metadata($ID, array('format' => array('struct_csv' => $headers)));
55eafc109fSAndreas Gohr        // don't cache
56eafc109fSAndreas Gohr        $this->nocache();
57eafc109fSAndreas Gohr    }
58eafc109fSAndreas Gohr
59eafc109fSAndreas Gohr    /**
60eafc109fSAndreas Gohr     * Opening a table row prevents the separator for the first following cell
61eafc109fSAndreas Gohr     */
62eafc109fSAndreas Gohr    function tablerow_open() {
63eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
64eafc109fSAndreas Gohr        $this->first = true;
65eafc109fSAndreas Gohr    }
66eafc109fSAndreas Gohr
67eafc109fSAndreas Gohr    /**
68b22abfe9SAndreas Gohr     * Output the delimiter (unless it's the first cell of this row) and the text wrapper
69eafc109fSAndreas Gohr     *
70eafc109fSAndreas Gohr     * @param int $colspan ignored
71eafc109fSAndreas Gohr     * @param null $align ignored
72eafc109fSAndreas Gohr     * @param int $rowspan ignored
73eafc109fSAndreas Gohr     */
74eafc109fSAndreas Gohr    function tablecell_open($colspan = 1, $align = null, $rowspan = 1) {
75eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
76eafc109fSAndreas Gohr        if(!$this->first) {
77b22abfe9SAndreas Gohr            $this->doc .= ",";
78eafc109fSAndreas Gohr        }
79eafc109fSAndreas Gohr        $this->first = false;
80b22abfe9SAndreas Gohr
81b22abfe9SAndreas Gohr        $this->doc .= '"';
82b22abfe9SAndreas Gohr    }
83b22abfe9SAndreas Gohr
84b22abfe9SAndreas Gohr    /**
85b22abfe9SAndreas Gohr     * Close the text wrapper
86b22abfe9SAndreas Gohr     */
87b22abfe9SAndreas Gohr    function tablecell_close() {
883889beb2SAndreas Gohr        if(!$this->_doOutput()) return;
89b22abfe9SAndreas Gohr        $this->doc .= '"';
90eafc109fSAndreas Gohr    }
91eafc109fSAndreas Gohr
92eafc109fSAndreas Gohr    /**
93eafc109fSAndreas Gohr     * Alias for tablecell_open
94eafc109fSAndreas Gohr     *
95eafc109fSAndreas Gohr     * @param int $colspan ignored
96eafc109fSAndreas Gohr     * @param null $align ignored
97eafc109fSAndreas Gohr     * @param int $rowspan ignored
98eafc109fSAndreas Gohr     */
99eafc109fSAndreas Gohr    function tableheader_open($colspan = 1, $align = null, $rowspan = 1) {
100eafc109fSAndreas Gohr        $this->tablecell_open($colspan, $align, $rowspan);
101eafc109fSAndreas Gohr    }
102eafc109fSAndreas Gohr
103eafc109fSAndreas Gohr    /**
104b22abfe9SAndreas Gohr     * Alias for tablecell_close
105b22abfe9SAndreas Gohr     */
106b22abfe9SAndreas Gohr    function tableheader_close() {
107b22abfe9SAndreas Gohr        $this->tablecell_close();
108b22abfe9SAndreas Gohr    }
109b22abfe9SAndreas Gohr
110b22abfe9SAndreas Gohr    /**
111b22abfe9SAndreas Gohr     * Add CRLF newline at the end of one line
112eafc109fSAndreas Gohr     */
113eafc109fSAndreas Gohr    function tablerow_close() {
114eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
115b22abfe9SAndreas Gohr        $this->doc .= "\r\n";
116eafc109fSAndreas Gohr    }
117eafc109fSAndreas Gohr
118eafc109fSAndreas Gohr    /**
119eafc109fSAndreas Gohr     * Outputs cell content
120eafc109fSAndreas Gohr     *
121eafc109fSAndreas Gohr     * @param string $text
122eafc109fSAndreas Gohr     */
123eafc109fSAndreas Gohr    function cdata($text) {
124eafc109fSAndreas Gohr        if(!$this->_doOutput()) return;
125b22abfe9SAndreas Gohr        if($text === '') return;
126b22abfe9SAndreas Gohr
127b22abfe9SAndreas Gohr        $this->doc .= str_replace('"', '""', $text);
128eafc109fSAndreas Gohr    }
129eafc109fSAndreas Gohr
130*ba55430dSAndreas Gohr
131*ba55430dSAndreas Gohr    #region overrides using cdata for output
132*ba55430dSAndreas Gohr
133eafc109fSAndreas Gohr    function internallink($link, $title = null) {
134*ba55430dSAndreas Gohr        if(is_null($title) or is_array($title) or $title == '') {
135*ba55430dSAndreas Gohr            $title = $this->_simpleTitle($link);
136*ba55430dSAndreas Gohr        }
137eafc109fSAndreas Gohr        $this->cdata($title);
138eafc109fSAndreas Gohr    }
139eafc109fSAndreas Gohr
140*ba55430dSAndreas Gohr    function externallink($link, $title = null) {
141*ba55430dSAndreas Gohr        if(is_null($title) or is_array($title) or $title == '') {
142*ba55430dSAndreas Gohr            $title = $link;
143*ba55430dSAndreas Gohr        }
144*ba55430dSAndreas Gohr        $this->cdata($title);
145*ba55430dSAndreas Gohr    }
146*ba55430dSAndreas Gohr
14749fe301bSAndreas Gohr    function emaillink($address, $name = null) {
14849fe301bSAndreas Gohr        $this->cdata($address);
14949fe301bSAndreas Gohr    }
150*ba55430dSAndreas Gohr
151*ba55430dSAndreas Gohr    function plugin($name, $args, $state = '', $match = '') {
152*ba55430dSAndreas Gohr        $this->cdata($match);
153*ba55430dSAndreas Gohr    }
154*ba55430dSAndreas Gohr
155*ba55430dSAndreas Gohr    function acronym($acronym) {
156*ba55430dSAndreas Gohr        $this->cdata($acronym);
157*ba55430dSAndreas Gohr    }
158*ba55430dSAndreas Gohr
159*ba55430dSAndreas Gohr    function code($text, $lang = null, $file = null) {
160*ba55430dSAndreas Gohr        $this->cdata($text);
161*ba55430dSAndreas Gohr    }
162*ba55430dSAndreas Gohr
163*ba55430dSAndreas Gohr    function header($text, $level, $pos) {
164*ba55430dSAndreas Gohr        $this->cdata($text);
165*ba55430dSAndreas Gohr    }
166*ba55430dSAndreas Gohr
167*ba55430dSAndreas Gohr    function linebreak() {
168*ba55430dSAndreas Gohr        $this->cdata("\r\n");
169*ba55430dSAndreas Gohr    }
170*ba55430dSAndreas Gohr
171*ba55430dSAndreas Gohr    function unformatted($text) {
172*ba55430dSAndreas Gohr        $this->cdata($text);
173*ba55430dSAndreas Gohr    }
174*ba55430dSAndreas Gohr
175*ba55430dSAndreas Gohr    function php($text) {
176*ba55430dSAndreas Gohr        $this->cdata($text);
177*ba55430dSAndreas Gohr    }
178*ba55430dSAndreas Gohr
179*ba55430dSAndreas Gohr    function phpblock($text) {
180*ba55430dSAndreas Gohr        $this->cdata($text);
181*ba55430dSAndreas Gohr    }
182*ba55430dSAndreas Gohr
183*ba55430dSAndreas Gohr    function html($text) {
184*ba55430dSAndreas Gohr        $this->cdata($text);
185*ba55430dSAndreas Gohr    }
186*ba55430dSAndreas Gohr
187*ba55430dSAndreas Gohr    function htmlblock($text) {
188*ba55430dSAndreas Gohr        $this->cdata($text);
189*ba55430dSAndreas Gohr    }
190*ba55430dSAndreas Gohr
191*ba55430dSAndreas Gohr    function preformatted($text) {
192*ba55430dSAndreas Gohr        $this->cdata($text);
193*ba55430dSAndreas Gohr    }
194*ba55430dSAndreas Gohr
195*ba55430dSAndreas Gohr    function file($text, $lang = null, $file = null) {
196*ba55430dSAndreas Gohr        $this->cdata($text);
197*ba55430dSAndreas Gohr    }
198*ba55430dSAndreas Gohr
199*ba55430dSAndreas Gohr    function smiley($smiley) {
200*ba55430dSAndreas Gohr        $this->cdata($smiley);
201*ba55430dSAndreas Gohr    }
202*ba55430dSAndreas Gohr
203*ba55430dSAndreas Gohr    function entity($entity) {
204*ba55430dSAndreas Gohr        $this->cdata($entity);
205*ba55430dSAndreas Gohr    }
206*ba55430dSAndreas Gohr
207*ba55430dSAndreas Gohr    function multiplyentity($x, $y) {
208*ba55430dSAndreas Gohr        $this->cdata($x . 'x' . $y);
209*ba55430dSAndreas Gohr    }
210*ba55430dSAndreas Gohr
211*ba55430dSAndreas Gohr    function locallink($hash, $name = null) {
212*ba55430dSAndreas Gohr        if(is_null($name) or is_array($name) or $name == '') {
213*ba55430dSAndreas Gohr            $name = $hash;
214*ba55430dSAndreas Gohr        }
215*ba55430dSAndreas Gohr        $this->cdata($name);
216*ba55430dSAndreas Gohr    }
217*ba55430dSAndreas Gohr
218*ba55430dSAndreas Gohr    function interwikilink($link, $title = null, $wikiName, $wikiUri) {
219*ba55430dSAndreas Gohr        if(is_null($title) or is_array($title) or $title == '') {
220*ba55430dSAndreas Gohr            $title = $wikiName . '>' . $link;
221*ba55430dSAndreas Gohr        }
222*ba55430dSAndreas Gohr        $this->cdata($title);
223*ba55430dSAndreas Gohr    }
224*ba55430dSAndreas Gohr
225*ba55430dSAndreas Gohr    function filelink($link, $title = null) {
226*ba55430dSAndreas Gohr        if(is_null($title) or is_array($title) or $title == '') {
227*ba55430dSAndreas Gohr            $title = $link;
228*ba55430dSAndreas Gohr        }
229*ba55430dSAndreas Gohr        $this->cdata($title);
230*ba55430dSAndreas Gohr    }
231*ba55430dSAndreas Gohr
232*ba55430dSAndreas Gohr    function windowssharelink($link, $title = null) {
233*ba55430dSAndreas Gohr        if(is_null($title) or is_array($title) or $title == '') {
234*ba55430dSAndreas Gohr            $title = $link;
235*ba55430dSAndreas Gohr        }
236*ba55430dSAndreas Gohr        $this->cdata($title);
237*ba55430dSAndreas Gohr    }
238*ba55430dSAndreas Gohr
239*ba55430dSAndreas Gohr    function internalmedia($src, $title = null, $align = null, $width = null,
240*ba55430dSAndreas Gohr                           $height = null, $cache = null, $linking = null) {
241*ba55430dSAndreas Gohr        $this->cdata($src);
242*ba55430dSAndreas Gohr    }
243*ba55430dSAndreas Gohr
244*ba55430dSAndreas Gohr    function externalmedia($src, $title = null, $align = null, $width = null,
245*ba55430dSAndreas Gohr                           $height = null, $cache = null, $linking = null) {
246*ba55430dSAndreas Gohr        $this->cdata($src);
247*ba55430dSAndreas Gohr    }
248*ba55430dSAndreas Gohr
249*ba55430dSAndreas Gohr    function internalmedialink($src, $title = null, $align = null,
250*ba55430dSAndreas Gohr                               $width = null, $height = null, $cache = null) {
251*ba55430dSAndreas Gohr        $this->cdata($src);
252*ba55430dSAndreas Gohr    }
253*ba55430dSAndreas Gohr
254*ba55430dSAndreas Gohr    function externalmedialink($src, $title = null, $align = null,
255*ba55430dSAndreas Gohr                               $width = null, $height = null, $cache = null) {
256*ba55430dSAndreas Gohr        $this->cdata($src);
257*ba55430dSAndreas Gohr    }
258*ba55430dSAndreas Gohr
259*ba55430dSAndreas Gohr    #endregion
260eafc109fSAndreas Gohr}
261