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 $this->cdata($match); 153 } 154 155 function acronym($acronym) { 156 $this->cdata($acronym); 157 } 158 159 function code($text, $lang = null, $file = null) { 160 $this->cdata($text); 161 } 162 163 function header($text, $level, $pos) { 164 $this->cdata($text); 165 } 166 167 function linebreak() { 168 $this->cdata("\r\n"); 169 } 170 171 function unformatted($text) { 172 $this->cdata($text); 173 } 174 175 function php($text) { 176 $this->cdata($text); 177 } 178 179 function phpblock($text) { 180 $this->cdata($text); 181 } 182 183 function html($text) { 184 $this->cdata($text); 185 } 186 187 function htmlblock($text) { 188 $this->cdata($text); 189 } 190 191 function preformatted($text) { 192 $this->cdata($text); 193 } 194 195 function file($text, $lang = null, $file = null) { 196 $this->cdata($text); 197 } 198 199 function smiley($smiley) { 200 $this->cdata($smiley); 201 } 202 203 function entity($entity) { 204 $this->cdata($entity); 205 } 206 207 function multiplyentity($x, $y) { 208 $this->cdata($x . 'x' . $y); 209 } 210 211 function locallink($hash, $name = null) { 212 if(is_null($name) or is_array($name) or $name == '') { 213 $name = $hash; 214 } 215 $this->cdata($name); 216 } 217 218 function interwikilink($link, $title = null, $wikiName, $wikiUri) { 219 if(is_null($title) or is_array($title) or $title == '') { 220 $title = $wikiName . '>' . $link; 221 } 222 $this->cdata($title); 223 } 224 225 function filelink($link, $title = null) { 226 if(is_null($title) or is_array($title) or $title == '') { 227 $title = $link; 228 } 229 $this->cdata($title); 230 } 231 232 function windowssharelink($link, $title = null) { 233 if(is_null($title) or is_array($title) or $title == '') { 234 $title = $link; 235 } 236 $this->cdata($title); 237 } 238 239 function internalmedia($src, $title = null, $align = null, $width = null, 240 $height = null, $cache = null, $linking = null) { 241 $this->cdata($src); 242 } 243 244 function externalmedia($src, $title = null, $align = null, $width = null, 245 $height = null, $cache = null, $linking = null) { 246 $this->cdata($src); 247 } 248 249 function internalmedialink($src, $title = null, $align = null, 250 $width = null, $height = null, $cache = null) { 251 $this->cdata($src); 252 } 253 254 function externalmedialink($src, $title = null, $align = null, 255 $width = null, $height = null, $cache = null) { 256 $this->cdata($src); 257 } 258 259 #endregion 260} 261