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