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