FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ObligationsGetter.php
1 <?php
2 /*
3  Copyright (C) 2017,2020, Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 namespace Fossology\Lib\Report;
20 
24 
30 {
34  private $licenseDao;
35 
39  private $clearingDao;
40 
44  private $uploadDao;
45 
46  public function __construct()
47  {
48  global $container;
49  $this->licenseDao = $container->get('dao.license');
50  $this->clearingDao = $container->get('dao.clearing');
51  $this->uploadDao = $container->get('dao.upload');
52  }
53 
63  function getObligations($licenseStatements, $mainLicenseStatements, $uploadId, $groupId)
64  {
65  $licenseIds = $this->contentOnly($licenseStatements) ?: array();
66  $mainLicenseIds = $this->contentOnly($mainLicenseStatements);
67 
68  if (! empty($mainLicenseIds)) {
69  $allLicenseIds = array_unique(array_merge($licenseIds, $mainLicenseIds));
70  } else {
71  $allLicenseIds = array_unique($licenseIds);
72  }
73 
74  $bulkAddIds = $this->getBulkAddLicenseList($uploadId, $groupId);
75  $obligationRef = $this->licenseDao->getLicenseObligations($allLicenseIds, 'obligation_map') ?: array();
76  $obligationCandidate = $this->licenseDao->getLicenseObligations($allLicenseIds, 'obligation_candidate_map') ?: array();
77  $obligations = array_merge($obligationRef, $obligationCandidate);
78  $onlyLicenseIdsWithObligation = array_column($obligations, 'rf_fk');
79  if (!empty($bulkAddIds)) {
80  $onlyLicenseIdsWithObligation = array_unique(array_merge($onlyLicenseIdsWithObligation, $bulkAddIds));
81  }
82  $licenseWithoutObligations = array_diff($allLicenseIds, $onlyLicenseIdsWithObligation) ?: array();
83  foreach ($licenseWithoutObligations as $licenseWithoutObligation) {
84  $license = $this->licenseDao->getLicenseById($licenseWithoutObligation);
85  if (!empty($license)) {
86  $whiteLists[] = $license->getShortName();
87  }
88  }
89  $newobligations = $this->groupObligations($obligations, $uploadId);
90  return array($newobligations, $whiteLists);
91  }
92 
99  function getBulkAddLicenseList($uploadId, $groupId)
100  {
101  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
102  $parentTreeBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTableName);
103  $bulkHistory = $this->clearingDao->getBulkHistory($parentTreeBounds, $groupId, false);
104  if (!empty($bulkHistory)) {
105  $licenseLists = array_column($bulkHistory, 'addedLicenses');
106  $allLicenses = array();
107  foreach ($licenseLists as $licenseList) {
108  $allLicenses = array_unique(array_merge($allLicenses, $licenseList));
109  }
110  foreach ($allLicenses as $allLicense) {
111  $license = $this->licenseDao->getLicenseByShortName($allLicense);
112  if (!empty($license)) {
113  $licenseId[] = $license->getId();
114  }
115  }
116  }
117  return $licenseId;
118  }
119 
125  function groupObligations($obligations, $uploadId)
126  {
127  $groupedOb = array();
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 = $obligation['rf_shortname'];
134  $groupBy = $obText;
135  if (!in_array($licenseName,(array) $excludedObligations[$obTopic])) {
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  foreach ($licenseStatements as $licenseStatement) {
162  $licenseId[] = $licenseStatement["licenseId"];
163  }
164  return $licenseId;
165  }
166 }
getObligations($licenseStatements, $mainLicenseStatements, $uploadId, $groupId)
For given list of license statements, return obligations and white lists.
contentOnly($licenseStatements)
From a list of license statements, return only license id.
getBulkAddLicenseList($uploadId, $groupId)
Get list of licenses added by Monk bulk.
groupObligations($obligations, $uploadId)
Group obligations based on $groupBy.