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