FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ObligationsGetter.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2017, 2020 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Report;
9 
14 
20 {
24  private $licenseDao;
25 
29  private $clearingDao;
30 
34  private $uploadDao;
35 
36  public function __construct()
37  {
38  global $container;
39  $this->licenseDao = $container->get('dao.license');
40  $this->clearingDao = $container->get('dao.clearing');
41  $this->uploadDao = $container->get('dao.upload');
42  }
43 
53  function getObligations($licenseStatements, $mainLicenseStatements, $uploadId, $groupId)
54  {
55  $whiteLists = array();
56  $licenseIds = $this->contentOnly($licenseStatements) ?: array();
57  $mainLicenseIds = $this->contentOnly($mainLicenseStatements);
58 
59  if (!empty($mainLicenseIds)) {
60  $allLicenseIds = array_unique(array_merge($licenseIds, $mainLicenseIds));
61  } else {
62  $allLicenseIds = array_unique($licenseIds);
63  }
64 
65  $bulkAddIds = $this->getBulkAddLicenseList($uploadId, $groupId);
66  if (!empty($bulkAddIds)) {
67  $allLicenseIds = array_unique(array_merge($licenseIds, $bulkAddIds));
68  }
69 
70  $obligationRef = $this->licenseDao->getLicenseObligations($allLicenseIds) ?: array();
71  $obligationCandidate = $this->licenseDao->getLicenseObligations($allLicenseIds, true) ?: array();
72  $obligations = array_merge($obligationRef, $obligationCandidate);
73  $onlyLicenseIdsWithObligation = array_column($obligations, 'rf_fk');
74  $licenseWithObligations = array_unique(array_intersect($onlyLicenseIdsWithObligation, $allLicenseIds));
75  $licenseWithoutObligations = array_diff($allLicenseIds, $licenseWithObligations) ?: array();
76  foreach ($licenseWithoutObligations as $licenseWithoutObligation) {
77  $license = $this->licenseDao->getLicenseById($licenseWithoutObligation);
78  if (!empty($license)) {
79  $whiteLists[] = $license->getSpdxId();
80  }
81  }
82  list($newobligations, $newWhiteList) = $this->groupObligations($obligations, $uploadId);
83  return array($newobligations, array_unique(array_merge($whiteLists, $newWhiteList)));
84  }
85 
92  function getBulkAddLicenseList($uploadId, $groupId)
93  {
94  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
95  $parentTreeBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTableName);
96  $bulkHistory = $this->clearingDao->getBulkHistory($parentTreeBounds, $groupId, false);
97  $licenseId = [];
98  if (!empty($bulkHistory)) {
99  foreach ($bulkHistory as $key => $value) {
100  if (empty($value['id'])) {
101  unset($bulkHistory[$key]);
102  }
103  }
104  $licenseLists = array_column($bulkHistory, 'addedLicenses');
105  $allLicenses = array();
106  foreach ($licenseLists as $licenseList) {
107  $allLicenses = array_unique(array_merge($allLicenses, $licenseList));
108  }
109  foreach ($allLicenses as $allLicense) {
110  $license = $this->licenseDao->getLicenseByShortName($allLicense);
111  if (!empty($license)) {
112  $licenseId[] = $license->getId();
113  }
114  }
115  }
116  return $licenseId;
117  }
118 
124  function groupObligations($obligations, $uploadId)
125  {
126  $groupedOb = array();
127  $whiteList = [];
128  $row = $this->uploadDao->getReportInfo($uploadId);
129  $excludedObligations = (array) json_decode($row['ri_excluded_obligations'], true);
130  foreach ($obligations as $obligation ) {
131  $obTopic = $obligation['ob_topic'];
132  $obText = $obligation['ob_text'];
133  $licenseName = LicenseRef::convertToSpdxId($obligation['rf_shortname'],
134  $obligation['rf_spdx_id']);
135  $groupBy = $obText;
136  if (!empty($excludedObligations) && array_key_exists($obTopic, $excludedObligations)) {
137  $obligationLicenseNames = $excludedObligations[$obTopic];
138  } else {
139  $obligationLicenseNames = array();
140  }
141  if (!in_array($licenseName, $obligationLicenseNames)) {
142  if (array_key_exists($groupBy, $groupedOb)) {
143  $currentLics = &$groupedOb[$groupBy]['license'];
144  if (!in_array($licenseName, $currentLics)) {
145  $currentLics[] = $licenseName;
146  }
147  } else {
148  $singleOb = array(
149  "topic" => $obTopic,
150  "text" => $obText,
151  "license" => array($licenseName)
152  );
153  $groupedOb[$groupBy] = $singleOb;
154  }
155  } else {
156  if (!in_array($licenseName, $whiteList)) {
157  $whiteList[] = $licenseName;
158  }
159  }
160  }
161 
162  // Make sure whitelist contains only licenses which are not in any
163  // obligations
164  foreach ($groupedOb as $obli) {
165  $whiteList = array_diff($whiteList, $obli['license']);
166  }
167  return [$groupedOb, $whiteList];
168  }
169 
175  function contentOnly($licenseStatements)
176  {
177  $licenseId = [];
178  foreach ($licenseStatements as $licenseStatement) {
179  $licenseId[] = $licenseStatement["licenseId"];
180  }
181  return $licenseId;
182  }
183 }
static convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
Definition: LicenseRef.php:106
getBulkAddLicenseList($uploadId, $groupId)
Get list of licenses added by Monk bulk.
groupObligations($obligations, $uploadId)
Group obligations based on $groupBy.
contentOnly($licenseStatements)
From a list of license statements, return only license id.
getObligations($licenseStatements, $mainLicenseStatements, $uploadId, $groupId)
For given list of license statements, return obligations and white lists.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308