1<?php 2 3/** 4 * TSV export of tabular data 5 * 6 * @link http://www.iana.org/assignments/media-types/text/tab-separated-values 7 */ 8class renderer_plugin_struct_csv extends Doku_Renderer { 9 10 protected $first = false; 11 12 /** 13 * Determine if out put is wanted right now 14 * 15 * @return bool 16 */ 17 function _doOutput() { 18 global $INPUT; 19 20 if( 21 !isset($this->info['struct_table_hash']) or 22 $this->info['struct_table_hash'] != $INPUT->str('hash') 23 ) { 24 return false; 25 } 26 27 if(!empty($this->info['struct_table_meta'])) { 28 return false; 29 } 30 31 return true; 32 } 33 34 /** 35 * Our own format 36 * 37 * @return string 38 */ 39 function getFormat() { 40 return 'struct_csv'; 41 } 42 43 /** 44 * Set proper headers 45 */ 46 function document_start() { 47 global $ID; 48 $filename = noNS($ID) . '.tsv'; 49 $headers = array( 50 'Content-Type' => 'text/tab-separated-values', 51 'Content-Disposition' => 'attachment; filename="' . $filename . '";' 52 ); 53 p_set_metadata($ID, array('format' => array('struct' => $headers))); 54 // don't cache 55 $this->nocache(); 56 } 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 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 .= "\t"; 78 } 79 $this->first = false; 80 } 81 82 /** 83 * Alias for tablecell_open 84 * 85 * @param int $colspan ignored 86 * @param null $align ignored 87 * @param int $rowspan ignored 88 */ 89 function tableheader_open($colspan = 1, $align = null, $rowspan = 1) { 90 if(!$this->_doOutput()) return; 91 $this->tablecell_open($colspan, $align, $rowspan); 92 } 93 94 /** 95 * Add newline at the end of one line 96 */ 97 function tablerow_close() { 98 if(!$this->_doOutput()) return; 99 $this->doc .= "\n"; 100 } 101 102 /** 103 * Outputs cell content 104 * 105 * @param string $text 106 */ 107 function cdata($text) { 108 if(!$this->_doOutput()) return; 109 // FIXME how to handle newlines in TSV?? 110 $this->doc .= str_replace("\t", ' ', $text); // TSV does not allow tabs in fields 111 } 112 113 function internallink($link, $title = null) { 114 $this->cdata($title); 115 } 116 117} 118