FOSSology  4.7.1
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 
13 
19 {
23  private $licenseDao;
24 
28  private $uploadDao;
29 
30  public function __construct()
31  {
32  global $container;
33  $this->licenseDao = $container->get('dao.license');
34  $this->uploadDao = $container->get('dao.upload');
35  }
36 
46  function getObligations($licenseStatements, $mainLicenseStatements, $uploadId, $groupId)
47  {
48  $whiteLists = array();
49  $licenseIds = $this->contentOnly($licenseStatements) ?: array();
50  $mainLicenseIds = $this->contentOnly($mainLicenseStatements);
51 
52  if (!empty($mainLicenseIds)) {
53  $allLicenseIds = array_unique(array_merge($licenseIds, $mainLicenseIds));
54  } else {
55  $allLicenseIds = array_unique($licenseIds);
56  }
57 
58  $obligationRef = $this->licenseDao->getLicenseObligations($allLicenseIds) ?: array();
59  $obligationCandidate = $this->licenseDao->getLicenseObligations($allLicenseIds, true) ?: array();
60  $obligations = array_merge($obligationRef, $obligationCandidate);
61  $onlyLicenseIdsWithObligation = array_column($obligations, 'rf_fk');
62  $licenseWithObligations = array_unique(array_intersect($onlyLicenseIdsWithObligation, $allLicenseIds));
63  $licenseWithoutObligations = array_diff($allLicenseIds, $licenseWithObligations) ?: array();
64  foreach ($licenseWithoutObligations as $licenseWithoutObligation) {
65  $license = $this->licenseDao->getLicenseById($licenseWithoutObligation);
66  if (!empty($license)) {
67  $whiteLists[] = $license->getSpdxId();
68  }
69  }
70  list($newobligations, $newWhiteList) = $this->groupObligations($obligations, $uploadId);
71  return array($newobligations, array_unique(array_merge($whiteLists, $newWhiteList)));
72  }
73 
79  function groupObligations($obligations, $uploadId)
80  {
81  $groupedOb = array();
82  $whiteList = [];
83  $row = $this->uploadDao->getReportInfo($uploadId);
84  $excludedObligations = (array) json_decode($row['ri_excluded_obligations'], true);
85  foreach ($obligations as $obligation ) {
86  $obTopic = $obligation['ob_topic'];
87  $obText = $obligation['ob_text'];
88  $licenseName = LicenseRef::convertToSpdxId($obligation['rf_shortname'],
89  $obligation['rf_spdx_id']);
90  $groupBy = $obText;
91  if (!empty($excludedObligations) && array_key_exists($obTopic, $excludedObligations)) {
92  $obligationLicenseNames = $excludedObligations[$obTopic];
93  } else {
94  $obligationLicenseNames = array();
95  }
96  if (!in_array($licenseName, $obligationLicenseNames)) {
97  if (array_key_exists($groupBy, $groupedOb)) {
98  $currentLics = &$groupedOb[$groupBy]['license'];
99  if (!in_array($licenseName, $currentLics)) {
100  $currentLics[] = $licenseName;
101  }
102  } else {
103  $singleOb = array(
104  "topic" => $obTopic,
105  "text" => $obText,
106  "license" => array($licenseName)
107  );
108  $groupedOb[$groupBy] = $singleOb;
109  }
110  } else {
111  if (!in_array($licenseName, $whiteList)) {
112  $whiteList[] = $licenseName;
113  }
114  }
115  }
116 
117  // Make sure whitelist contains only licenses which are not in any
118  // obligations
119  foreach ($groupedOb as $obli) {
120  $whiteList = array_diff($whiteList, $obli['license']);
121  }
122  return [$groupedOb, $whiteList];
123  }
124 
130  function contentOnly($licenseStatements)
131  {
132  $licenseId = [];
133  foreach ($licenseStatements as $licenseStatement) {
134  $licenseId[] = $licenseStatement["licenseId"];
135  }
136  return $licenseId;
137  }
138 }
static convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
Definition: LicenseRef.php:106
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