1*f36cc634SAndreas Gohr<?php 2*f36cc634SAndreas Gohr 3*f36cc634SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 4*f36cc634SAndreas Gohr 5*f36cc634SAndreas Gohr/** 6*f36cc634SAndreas Gohr * Class CSVExporter 7*f36cc634SAndreas Gohr * 8*f36cc634SAndreas Gohr * exports raw schema data to CSV. For lookup schemas this data can be reimported again through 9*f36cc634SAndreas Gohr * CSVImporter 10*f36cc634SAndreas Gohr * 11*f36cc634SAndreas Gohr * Note this is different from syntax/csv.php 12*f36cc634SAndreas Gohr * 13*f36cc634SAndreas Gohr * @package dokuwiki\plugin\struct\meta 14*f36cc634SAndreas Gohr */ 15*f36cc634SAndreas Gohrclass CSVExporter { 16*f36cc634SAndreas Gohr 17*f36cc634SAndreas Gohr protected $islookup = false; 18*f36cc634SAndreas Gohr 19*f36cc634SAndreas Gohr /** 20*f36cc634SAndreas Gohr * CSVImporter constructor. 21*f36cc634SAndreas Gohr * 22*f36cc634SAndreas Gohr * @throws StructException 23*f36cc634SAndreas Gohr * @param string $table 24*f36cc634SAndreas Gohr */ 25*f36cc634SAndreas Gohr public function __construct($table) { 26*f36cc634SAndreas Gohr 27*f36cc634SAndreas Gohr $search = new Search(); 28*f36cc634SAndreas Gohr $search->addSchema($table); 29*f36cc634SAndreas Gohr $search->addColumn('*'); 30*f36cc634SAndreas Gohr $result = $search->execute(); 31*f36cc634SAndreas Gohr 32*f36cc634SAndreas Gohr $this->islookup = $search->getSchemas()[0]->isLookup(); 33*f36cc634SAndreas Gohr if(!$this->islookup) { 34*f36cc634SAndreas Gohr $pids = $search->getPids(); 35*f36cc634SAndreas Gohr } else { 36*f36cc634SAndreas Gohr $pids = array(); 37*f36cc634SAndreas Gohr } 38*f36cc634SAndreas Gohr 39*f36cc634SAndreas Gohr echo $this->header($search->getColumns()); 40*f36cc634SAndreas Gohr foreach($result as $i => $row) { 41*f36cc634SAndreas Gohr if(!$this->islookup) { 42*f36cc634SAndreas Gohr $pid = $pids[$i]; 43*f36cc634SAndreas Gohr } else { 44*f36cc634SAndreas Gohr $pid = 0; 45*f36cc634SAndreas Gohr } 46*f36cc634SAndreas Gohr 47*f36cc634SAndreas Gohr echo $this->row($row, $pid); 48*f36cc634SAndreas Gohr } 49*f36cc634SAndreas Gohr } 50*f36cc634SAndreas Gohr 51*f36cc634SAndreas Gohr /** 52*f36cc634SAndreas Gohr * Create the header 53*f36cc634SAndreas Gohr * 54*f36cc634SAndreas Gohr * @param Column[] $columns 55*f36cc634SAndreas Gohr * @return string 56*f36cc634SAndreas Gohr */ 57*f36cc634SAndreas Gohr protected function header($columns) { 58*f36cc634SAndreas Gohr $row = ''; 59*f36cc634SAndreas Gohr 60*f36cc634SAndreas Gohr if(!$this->islookup) { 61*f36cc634SAndreas Gohr $row .= $this->escape('pid'); 62*f36cc634SAndreas Gohr $row .= ','; 63*f36cc634SAndreas Gohr } 64*f36cc634SAndreas Gohr 65*f36cc634SAndreas Gohr foreach($columns as $i => $col) { 66*f36cc634SAndreas Gohr $row .= $this->escape($col->getLabel()); 67*f36cc634SAndreas Gohr $row .= ','; 68*f36cc634SAndreas Gohr } 69*f36cc634SAndreas Gohr return rtrim($row, ',') . "\r\n"; 70*f36cc634SAndreas Gohr } 71*f36cc634SAndreas Gohr 72*f36cc634SAndreas Gohr /** 73*f36cc634SAndreas Gohr * Create one row of data 74*f36cc634SAndreas Gohr * 75*f36cc634SAndreas Gohr * @param Value[] $values 76*f36cc634SAndreas Gohr * @param string $pid pid of this row 77*f36cc634SAndreas Gohr * @return string 78*f36cc634SAndreas Gohr */ 79*f36cc634SAndreas Gohr protected function row($values, $pid) { 80*f36cc634SAndreas Gohr $row = ''; 81*f36cc634SAndreas Gohr 82*f36cc634SAndreas Gohr if(!$this->islookup) { 83*f36cc634SAndreas Gohr $row .= $this->escape($pid); 84*f36cc634SAndreas Gohr $row .= ','; 85*f36cc634SAndreas Gohr } 86*f36cc634SAndreas Gohr 87*f36cc634SAndreas Gohr foreach($values as $value) { 88*f36cc634SAndreas Gohr /** @var Value $value */ 89*f36cc634SAndreas Gohr $val = $value->getRawValue(); 90*f36cc634SAndreas Gohr if(is_array($val)) $val = join(',', $val); 91*f36cc634SAndreas Gohr 92*f36cc634SAndreas Gohr $row .= $this->escape($val); 93*f36cc634SAndreas Gohr $row .= ','; 94*f36cc634SAndreas Gohr } 95*f36cc634SAndreas Gohr 96*f36cc634SAndreas Gohr return rtrim($row, ',') . "\r\n"; 97*f36cc634SAndreas Gohr } 98*f36cc634SAndreas Gohr 99*f36cc634SAndreas Gohr /** 100*f36cc634SAndreas Gohr * Escapes and wraps the given string 101*f36cc634SAndreas Gohr * 102*f36cc634SAndreas Gohr * Uses doubled quotes for escaping which seems to be the standard escaping method for CSV 103*f36cc634SAndreas Gohr * 104*f36cc634SAndreas Gohr * @param string $str 105*f36cc634SAndreas Gohr * @return string 106*f36cc634SAndreas Gohr */ 107*f36cc634SAndreas Gohr protected function escape($str) { 108*f36cc634SAndreas Gohr return '"' . str_replace('"', '""', $str) . '"'; 109*f36cc634SAndreas Gohr } 110*f36cc634SAndreas Gohr 111*f36cc634SAndreas Gohr} 112