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