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