FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseClearedGetter.php
1 <?php
2 /*
3  Copyright (C) 2014-2018, Siemens AG
4  Author: Daniele Fognini
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 namespace Fossology\Lib\Report;
21 
30 
32 {
34  private $onlyComments = false;
36  private $onlyAcknowledgements = false;
38  private $clearingDao;
40  private $licenseDao;
42  private $agentDao;
44  private $licenseCache = array();
46  protected $agentNames = AgentRef::AGENT_LIST;
47 
48  public function __construct()
49  {
50  global $container;
51 
52  $this->clearingDao = $container->get('dao.clearing');
53  $this->licenseDao = $container->get('dao.license');
54  $this->agentDao = $container->get('dao.agent');
55 
56  parent::__construct($groupBy = 'text');
57  }
58 
59  protected function getStatements($uploadId, $uploadTreeTableName, $groupId = null)
60  {
61  $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId,$uploadTreeTableName);
62  $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, $groupId);
63  $dbManager = $GLOBALS['container']->get('db.manager');
64  $licenseMap = new LicenseMap($dbManager, $groupId, LicenseMap::REPORT);
65  $ungroupedStatements = array();
66  foreach ($clearingDecisions as $clearingDecision) {
67  if ($clearingDecision->getType() == DecisionTypes::IRRELEVANT) {
68  continue;
69  }
71  foreach ($clearingDecision->getClearingLicenses() as $clearingLicense) {
72  if ($clearingLicense->isRemoved()) {
73  continue;
74  }
75 
76  if ($this->onlyComments && !($comment = $clearingLicense->getComment())) {
77  continue;
78  }
79 
80  if ($this->onlyAcknowledgements && !($acknowledgement = $clearingLicense->getAcknowledgement())) {
81  continue;
82  }
83 
84  $originLicenseId = $clearingLicense->getLicenseId();
85  $licenseId = $licenseMap->getProjectedId($originLicenseId);
86 
87  if ($this->onlyAcknowledgements) {
88  $text = $acknowledgement;
89  $risk = "";
90  } else if ($this->onlyComments) {
91  $text = $comment;
92  $risk = "";
93  } else {
94  $reportInfo = $clearingLicense->getReportInfo();
95  $text = $reportInfo ? : $this->getCachedLicenseText($licenseId, "any");
96  $risk = $this->getCachedLicenseRisk($licenseId, $groupId);
97  }
98 
99  $ungroupedStatements[] = array(
100  'licenseId' => $licenseId,
101  'risk' => $risk,
102  'content' => $licenseMap->getProjectedShortname($originLicenseId, $clearingLicense->getShortName()),
103  'uploadtree_pk' => $clearingDecision->getUploadTreeId(),
104  'text' => $text
105  );
106  }
107  }
108 
109  return $ungroupedStatements;
110  }
111 
115  public function setOnlyComments($displayOnlyCommentedLicenseClearings)
116  {
117  $this->onlyAcknowledgements = false;
118  $this->onlyComments = $displayOnlyCommentedLicenseClearings;
119  }
120 
124  public function setOnlyAcknowledgements($displayOnlyAcknowledgements)
125  {
126  $this->onlyComments = false;
127  $this->onlyAcknowledgements = $displayOnlyAcknowledgements;
128  }
129 
134  protected function getCachedLicenseText($licenseId, $groupId)
135  {
136  if (!array_key_exists($licenseId, $this->licenseCache)) {
137  $this->licenseCache[$licenseId] = $this->licenseDao->getLicenseById($licenseId, $groupId);
138  }
139  return $this->licenseCache[$licenseId]->getText();
140  }
141 
146  protected function getCachedLicenseRisk($licenseId, $groupId)
147  {
148  if (!array_key_exists($licenseId, $this->licenseCache)) {
149  $this->licenseCache[$licenseId] = $this->licenseDao->getLicenseById($licenseId, $groupId);
150  }
151  return $this->licenseCache[$licenseId]->getRisk();
152  }
153 
158  protected function getHistogram($uploadId, $groupId)
159  {
160  $LicenseHistArray = array();
161  $scannerAgents = array_keys($this->agentNames);
162  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
163  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
164  $allAgentIds = $scanJobProxy->getLatestSuccessfulAgentIds();
165  $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId);
166  $scannerLicenseHistogram = $this->licenseDao->getLicenseHistogram($itemTreeBounds, $allAgentIds);
167  $editedLicensesHist = $this->clearingDao->getClearedLicenseIdAndMultiplicities($itemTreeBounds, $groupId);
168  $noScannerLicenseFoundCount = array_key_exists(LicenseDao::NO_LICENSE_FOUND, $scannerLicenseHistogram)
169  ? $scannerLicenseHistogram[LicenseDao::NO_LICENSE_FOUND]['count'] : 0;
170  $editedNoLicenseFoundCount = array_key_exists(LicenseDao::NO_LICENSE_FOUND, $editedLicensesHist)
171  ? $editedLicensesHist[LicenseDao::NO_LICENSE_FOUND]['count'] : 0;
172 
173  $totalLicenses = array_unique(array_merge(array_keys($scannerLicenseHistogram), array_keys($editedLicensesHist)));
174  foreach ($totalLicenses as $licenseShortName) {
175  $count = 0;
176  if (array_key_exists($licenseShortName, $scannerLicenseHistogram)) {
177  $count = $scannerLicenseHistogram[$licenseShortName]['unique'];
178  }
179  $editedCount = array_key_exists($licenseShortName, $editedLicensesHist) ? $editedLicensesHist[$licenseShortName]['count'] : 0;
180  if (strcmp($licenseShortName, LicenseDao::NO_LICENSE_FOUND) !== 0) {
181  $LicenseHistArray[] = array("scannerCount" => $count, "editedCount" => $editedCount, "licenseShortname" => $licenseShortName);
182  } else {
183  $LicenseHistArray[] = array("scannerCount" => $noScannerLicenseFoundCount, "editedCount" => $editedNoLicenseFoundCount, "licenseShortname" => $licenseShortName);
184  }
185  }
186  return $LicenseHistArray;
187  }
188 
195  function checkLicenseId($licenses1, $licenses2)
196  {
197  return strcmp($licenses1['licenseId'], $licenses2['licenseId']);
198  }
199 
206  function updateIdentifiedGlobalLicenses($licensesMain, $licenses)
207  {
208  $onlyMainLic = array_udiff($licensesMain, $licenses, array($this, "checkLicenseId"));
209  $mainLicensesInIdetifiedFiles = array_uintersect($licenses, $licensesMain, array($this, "checkLicenseId"));
210  $onlyLicense = array_udiff($licenses, $licensesMain, array($this, "checkLicenseId"));
211  return array(
212  array_values(array_merge($onlyMainLic, $mainLicensesInIdetifiedFiles)),
213  array_values($onlyLicense)
214  );
215  }
216 }
checkLicenseId($licenses1, $licenses2)
callback to compare licenses
updateIdentifiedGlobalLicenses($licensesMain, $licenses)
Copy identified global licenses.
Wrapper class for license map.
Definition: LicenseMap.php:29
setOnlyAcknowledgements($displayOnlyAcknowledgements)
setOnlyComments($displayOnlyCommentedLicenseClearings)
getStatements($uploadId, $uploadTreeTableName, $groupId=null)