FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseCsvExport.php
Go to the documentation of this file.
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
20 
23 
34 {
37  protected $dbManager;
40  protected $delimiter = ',';
43  protected $enclosure = '"';
44 
49  public function __construct(DbManager $dbManager)
50  {
51  $this->dbManager = $dbManager;
52  }
53 
58  public function setDelimiter($delimiter=',')
59  {
60  $this->delimiter = substr($delimiter,0,1);
61  }
62 
67  public function setEnclosure($enclosure='"')
68  {
69  $this->enclosure = substr($enclosure,0,1);
70  }
71 
77  public function createCsv($rf=0)
78  {
79  $sql = "WITH marydoneCand AS (
80  SELECT * FROM license_candidate
81  WHERE marydone = true
82 ), allLicenses AS (
83 SELECT DISTINCT ON(rf_pk) * FROM
84  ONLY license_ref
85  NATURAL FULL JOIN marydoneCand)
86 SELECT
87  rf.rf_shortname, rf.rf_fullname, rf.rf_text, rc.rf_shortname parent_shortname,
88  rr.rf_shortname report_shortname, rf.rf_url, rf.rf_notes, rf.rf_source,
89  rf.rf_risk, gp.group_name
90 FROM allLicenses AS rf
91  FULL JOIN groups AS gp ON gp.group_pk = rf.group_fk
92  LEFT JOIN license_map mc ON mc.rf_fk=rf.rf_pk AND mc.usage=$2
93  LEFT JOIN license_ref rc ON mc.rf_parent=rc.rf_pk
94  LEFT JOIN license_map mr ON mr.rf_fk=rf.rf_pk AND mr.usage=$3
95  LEFT JOIN license_ref rr ON mr.rf_parent=rr.rf_pk
96 WHERE rf.rf_detector_type=$1";
97  $param = array(1, LicenseMap::CONCLUSION, LicenseMap::REPORT);
98  if ($rf > 0) {
99  $stmt = __METHOD__ . '.rf';
100  $param[] = $rf;
101  $sql .= ' AND rf.rf_pk = $'.count($param);
102  $row = $this->dbManager->getSingleRow($sql,$param,$stmt);
103  $vars = $row ? array( $row ) : array();
104  } else {
105  $stmt = __METHOD__;
106  $sql .= ' ORDER BY rf.rf_pk';
107  $this->dbManager->prepare($stmt,$sql);
108  $res = $this->dbManager->execute($stmt,$param);
109  $vars = $this->dbManager->fetchAll( $res );
110  $this->dbManager->freeResult($res);
111  }
112 
113  $out = fopen('php://output', 'w');
114  ob_start();
115  $head = array(
116  'shortname', 'fullname', 'text', 'parent_shortname', 'report_shortname',
117  'url', 'notes', 'source', 'risk', 'group');
118  fputcsv($out, $head, $this->delimiter, $this->enclosure);
119  foreach ($vars as $row) {
120  fputcsv($out, $row, $this->delimiter, $this->enclosure);
121  }
122  $content = ob_get_contents();
123  ob_end_clean();
124  return $content;
125  }
126 }
createCsv($rf=0)
Create the CSV from the DB.
Helper class to export license list as a CSV from the DB.
Utility functions for specific applications.
setDelimiter($delimiter=',')
Update the delimiter.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
setEnclosure($enclosure='"')
Update the enclosure.