FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseCsvExport.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015, 2021 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 
10 
14 
25 {
28  protected $dbManager;
31  protected $delimiter = ',';
34  protected $enclosure = '"';
35 
40  public function __construct(DbManager $dbManager)
41  {
42  $this->dbManager = $dbManager;
43  }
44 
49  public function setDelimiter($delimiter=',')
50  {
51  $this->delimiter = substr($delimiter,0,1);
52  }
53 
58  public function setEnclosure($enclosure='"')
59  {
60  $this->enclosure = substr($enclosure,0,1);
61  }
62 
68  public function createCsv($rf=0, $allCandidates=false)
69  {
70  $forAllCandidates = "WHERE marydone = true";
71  if ($allCandidates) {
72  $forAllCandidates = "";
73  }
74  $forGroupBy = " GROUP BY rf.rf_shortname, rf.rf_fullname, rf.rf_spdx_id, rf.rf_text, rc.rf_shortname, rr.rf_shortname, rf.rf_url, rf.rf_notes, rf.rf_source, rf.rf_risk, gp.group_name";
75  $sql = "WITH marydoneCand AS (
76  SELECT * FROM license_candidate
77  $forAllCandidates
78 ), allLicenses AS (
79 SELECT DISTINCT ON(rf_pk) * FROM
80  ONLY license_ref
81  NATURAL FULL JOIN marydoneCand)
82 SELECT
83  rf.rf_shortname, rf.rf_fullname, rf.rf_spdx_id, rf.rf_text,
84  rc.rf_shortname parent_shortname, rr.rf_shortname report_shortname, rf.rf_url,
85  rf.rf_notes, rf.rf_source, rf.rf_risk, gp.group_name,
86  string_agg(ob_topic, ', ') obligations
87 FROM allLicenses AS rf
88  LEFT OUTER JOIN obligation_map om ON om.rf_fk = rf.rf_pk
89  LEFT OUTER JOIN obligation_ref ON ob_fk = ob_pk
90  FULL JOIN groups AS gp ON gp.group_pk = rf.group_fk
91  LEFT JOIN license_map mc ON mc.rf_fk=rf.rf_pk AND mc.usage=$2
92  LEFT JOIN license_ref rc ON mc.rf_parent=rc.rf_pk
93  LEFT JOIN license_map mr ON mr.rf_fk=rf.rf_pk AND mr.usage=$3
94  LEFT JOIN license_ref rr ON mr.rf_parent=rr.rf_pk
95 WHERE rf.rf_detector_type=$1";
96 
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).$forGroupBy;
102  $row = $this->dbManager->getSingleRow($sql,$param,$stmt);
103  $vars = $row ? array( $row ) : array();
104  } else {
105  $stmt = __METHOD__;
106  $sql .= $forGroupBy . ', rf.rf_pk 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  $out = fopen('php://output', 'w');
113  ob_start();
114  $head = array(
115  'shortname', 'fullname', 'spdx_id', 'text', 'parent_shortname',
116  'report_shortname', 'url', 'notes', 'source', 'risk', 'group',
117  'obligations');
118  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
119  fputcsv($out, $head, $this->delimiter, $this->enclosure);
120  foreach ($vars as $row) {
121  $row['rf_spdx_id'] = LicenseRef::convertToSpdxId($row['rf_shortname'],
122  $row['rf_spdx_id']);
123  if (strlen($row['rf_text']) > LicenseMap::MAX_CHAR_LIMIT) {
124  $row['rf_text'] = LicenseMap::TEXT_MAX_CHAR_LIMIT;
125  }
126  fputcsv($out, $row, $this->delimiter, $this->enclosure);
127  }
128  $content = ob_get_contents();
129  ob_end_clean();
130  return $content;
131  }
132 }
Helper class to export license list as a CSV from the DB.
setDelimiter($delimiter=',')
Update the delimiter.
createCsv($rf=0, $allCandidates=false)
Create the CSV from the DB.
setEnclosure($enclosure='"')
Update the enclosure.
Wrapper class for license map.
Definition: LicenseMap.php:19
static convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
Definition: LicenseRef.php:106
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Utility functions for specific applications.