FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseMap.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
13 
19 {
20  const CONCLUSION = 1;
21  const TRIVIAL = 2;
22  const FAMILY = 3;
23  const REPORT = 4;
24  const MAX_CHAR_LIMIT = "32767";
25  const TEXT_MAX_CHAR_LIMIT = "The maximum number of characters per cell in a CSV file(32,767) exceeded, Please edit this license text in UI";
26 
28  private $dbManager;
30  private $usageId;
32  private $groupId;
34  private $map = array();
35 
43  public function __construct(DbManager $dbManager, $groupId, $usageId=null, $full=false)
44  {
45  $this->usageId = $usageId?:self::CONCLUSION;
46  $this->groupId = $groupId;
47  $this->dbManager = $dbManager;
48  if ($this->usageId == self::TRIVIAL && !$full) {
49  return;
50  }
51  $licenseView = new LicenseViewProxy($groupId);
52  if ($full) {
53  $query = $licenseView->asCTE()
54  .' SELECT distinct on(rf_pk) rf_pk rf_fk, rf_shortname parent_shortname,
55  rf_spdx_id AS parent_spdx_id, rf_parent, rf_fullname AS parent_fullname
56  FROM (
57  SELECT r1.rf_pk, r2.rf_shortname, r2.rf_spdx_id, usage,
58  rf_parent, r2.rf_fullname FROM '.$licenseView->getDbViewName()
59  .' r1 inner join license_map on usage=$1 and rf_fk=r1.rf_pk
60  left join license_ref r2 on rf_parent=r2.rf_pk
61  UNION
62  SELECT rf_pk, rf_shortname, rf_spdx_id, -1 usage, rf_pk rf_parent,
63  rf_fullname FROM '.$licenseView->getDbViewName()
64  .') full_map ORDER BY rf_pk,usage DESC';
65 
66  $stmt = __METHOD__.".$this->usageId,$groupId,full";
67  } else {
68  $query = $licenseView->asCTE()
69  .' SELECT rf_fk, rf_shortname AS parent_shortname, rf_spdx_id AS parent_spdx_id, '
70  .'rf_parent, rf_fullname AS parent_fullname FROM license_map, '.$licenseView->getDbViewName()
71  .' WHERE rf_pk=rf_parent AND rf_fk!=rf_parent AND usage=$1';
72  $stmt = __METHOD__.".$this->usageId,$groupId";
73  }
74  $dbManager->prepare($stmt,$query);
75  $res = $dbManager->execute($stmt,array($this->usageId));
76  while ($row = $dbManager->fetchArray($res)) {
77  $this->map[$row['rf_fk']] = $row;
78  }
79  $dbManager->freeResult($res);
80  }
81 
87  public function getProjectedId($licenseId)
88  {
89  if (array_key_exists($licenseId, $this->map)) {
90  return $this->map[$licenseId]['rf_parent'];
91  }
92  return $licenseId;
93  }
94 
103  public function getProjectedShortname($licenseId, $defaultName=null)
104  {
105  if (array_key_exists($licenseId, $this->map)) {
106  return $this->map[$licenseId]['parent_shortname'];
107  }
108  return $defaultName;
109  }
110 
118  public function getProjectedSpdxId($licenseId, $defaultID=null)
119  {
120  if (array_key_exists($licenseId, $this->map)) {
121  return LicenseRef::convertToSpdxId($this->map[$licenseId]['parent_shortname'],
122  $this->map[$licenseId]['parent_spdx_id']);
123  }
124  return $defaultID;
125  }
126 
134  public function getProjectedName($licenseId, $defaultName=null)
135  {
136  if (array_key_exists($licenseId, $this->map)) {
137  $licenseName = $this->map[$licenseId]['parent_fullname'];
138  if (empty($licenseName)) {
139  $licenseName = $this->getProjectedShortname($licenseId, $defaultName);
140  }
141  return $licenseName;
142  }
143  return $defaultName;
144  }
145 
150  public function getUsage()
151  {
152  return $this->usageId;
153  }
154 
159  public function getGroupId()
160  {
161  return $this->groupId;
162  }
163 
168  public function getTopLevelLicenseRefs()
169  {
170  $licenseView = new LicenseViewProxy($this->groupId,
171  array('columns'=>array('rf_pk','rf_shortname', 'rf_spdx_id', 'rf_fullname')),
172  'license_visible');
173  $query = $licenseView->asCTE()
174  .' SELECT rf_pk, rf_shortname, rf_spdx_id, rf_fullname FROM '.$licenseView->getDbViewName()
175  .' LEFT JOIN license_map ON rf_pk=rf_fk AND rf_fk!=rf_parent AND usage=$1'
176  .' WHERE license_map_pk IS NULL';
177  $stmt = __METHOD__.".$this->usageId,$this->groupId";
178  $this->dbManager->prepare($stmt,$query);
179  $res = $this->dbManager->execute($stmt,array($this->usageId));
180  $topLevel = array();
181  while ($row = $this->dbManager->fetchArray($res)) {
182  $topLevel[$row['rf_pk']] = new LicenseRef($row['rf_pk'],$row['rf_shortname'],$row['rf_fullname'],$row['rf_spdx_id']);
183  }
184  return $topLevel;
185  }
186 
191  public static function getMappedLicenseRefView($usageExpr='$1')
192  {
193  return "SELECT bot.rf_pk rf_origin, top.rf_pk, top.rf_shortname, top.rf_fullname, top.rf_spdx_id FROM license_ref bot "
194  ."LEFT JOIN license_map ON bot.rf_pk=rf_fk AND usage=$usageExpr "
195  ."INNER JOIN license_ref top ON rf_parent=top.rf_pk OR rf_parent IS NULL AND bot.rf_pk=top.rf_pk";
196  }
197 
204  public function getObligationsForLicenseRef($license_ref, $candidate = false)
205  {
206  $tableName = $candidate ? "obligation_candidate_map" : "obligation_map";
207  $sql = "SELECT distinct(ob_fk) FROM $tableName WHERE rf_fk = $1;";
208  $ob_fks = $this->dbManager->getRows($sql, [$license_ref],
209  __METHOD__ . $tableName);
210  $returnVal = [];
211  foreach ($ob_fks as $row) {
212  $returnVal[] = $row['ob_fk'];
213  }
214  return $returnVal;
215  }
216 }
Wrapper class for license map.
Definition: LicenseMap.php:19
static getMappedLicenseRefView($usageExpr=' $1')
Query to get license map view along with license ref.
Definition: LicenseMap.php:191
getProjectedSpdxId($licenseId, $defaultID=null)
For a given license id, get the projected SPDX ID (or shortname if ID does not exist).
Definition: LicenseMap.php:118
getProjectedShortname($licenseId, $defaultName=null)
For a given license id, get the projected shortname.
Definition: LicenseMap.php:103
getProjectedId($licenseId)
For a given license id, get the projected id.
Definition: LicenseMap.php:87
__construct(DbManager $dbManager, $groupId, $usageId=null, $full=false)
Definition: LicenseMap.php:43
getObligationsForLicenseRef($license_ref, $candidate=false)
Get all Obligations attached with given license ref.
Definition: LicenseMap.php:204
getProjectedName($licenseId, $defaultName=null)
For a given license id, get the projected fullname. If empty, get the shortname instead.
Definition: LicenseMap.php:134
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
Contains business rules for FOSSology.