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  $newobligations = $this->groupObligations($obligations, $uploadId);
83  return array($newobligations, $whiteLists);
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  $licenseLists = array_column($bulkHistory, 'addedLicenses');
100  $allLicenses = array();
101  foreach ($licenseLists as $licenseList) {
102  $allLicenses = array_unique(array_merge($allLicenses, $licenseList));
103  }
104  foreach ($allLicenses as $allLicense) {
105  $license = $this->licenseDao->getLicenseByShortName($allLicense);
106  if (!empty($license)) {
107  $licenseId[] = $license->getId();
108  }
109  }
110  }
111  return $licenseId;
112  }
113 
119  function groupObligations($obligations, $uploadId)
120  {
121  $groupedOb = array();
122  $row = $this->uploadDao->getReportInfo($uploadId);
123  $excludedObligations = (array) json_decode($row['ri_excluded_obligations'], true);
124  foreach ($obligations as $obligation ) {
125  $obTopic = $obligation['ob_topic'];
126  $obText = $obligation['ob_text'];
127  $licenseName = LicenseRef::convertToSpdxId($obligation['rf_shortname'],
128  $obligation['rf_spdx_id']);
129  $groupBy = $obText;
130  if (!empty($excludedObligations) && array_key_exists($obTopic, $excludedObligations)) {
131  $obligationLicenseNames = $excludedObligations[$obTopic];
132  } else {
133  $obligationLicenseNames = array();
134  }
135  if (!in_array($licenseName, $obligationLicenseNames)) {
136  if (array_key_exists($groupBy, $groupedOb)) {
137  $currentLics = &$groupedOb[$groupBy]['license'];
138  if (!in_array($licenseName, $currentLics)) {
139  $currentLics[] = $licenseName;
140  }
141  } else {
142  $singleOb = array(
143  "topic" => $obTopic,
144  "text" => $obText,
145  "license" => array($licenseName)
146  );
147  $groupedOb[$groupBy] = $singleOb;
148  }
149  }
150  }
151  return $groupedOb;
152  }
153 
159  function contentOnly($licenseStatements)
160  {
161  $licenseId = [];
162  foreach ($licenseStatements as $licenseStatement) {
163  $licenseId[] = $licenseStatement["licenseId"];
164  }
165  return $licenseId;
166  }
167 }
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.