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, $generateJson=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_licensetype, 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 AS shortname, rf.rf_fullname AS fullname, rf.rf_spdx_id AS spdx_id, rf.rf_licensetype as license_type, rf.rf_text AS text,
84  rc.rf_shortname parent_shortname, rr.rf_shortname report_shortname, rf.rf_url AS url,
85  rf.rf_notes AS notes, rf.rf_source AS source, rf.rf_risk AS risk, gp.group_name AS group,
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  if ($generateJson) {
113  return json_encode($vars, JSON_PRETTY_PRINT);
114  } else {
115  $out = fopen('php://output', 'w');
116  ob_start();
117  $head = array(
118  'shortname', 'fullname', 'spdx_id', 'licensetype', 'text', 'parent_shortname',
119  'report_shortname', 'url', 'notes', 'source', 'risk', 'group',
120  'obligations');
121  fputs($out, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
122  fputcsv($out, $head, $this->delimiter, $this->enclosure);
123  foreach ($vars as $row) {
124  $row['spdx_id'] = LicenseRef::convertToSpdxId($row['shortname'],
125  $row['spdx_id']);
126  if (strlen($row['text']) > LicenseMap::MAX_CHAR_LIMIT) {
127  $row['text'] = LicenseMap::TEXT_MAX_CHAR_LIMIT;
128  }
129  fputcsv($out, $row, $this->delimiter, $this->enclosure);
130  }
131  $content = ob_get_contents();
132  ob_end_clean();
133  return $content;
134  }
135  }
136 }
Helper class to export license list as a CSV from the DB.
setDelimiter($delimiter=',')
Update the delimiter.
createCsv($rf=0, $allCandidates=false, $generateJson=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.