FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseClearedGetter.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2018 Siemens AG
4  Author: Daniele Fognini
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Report;
9 
20 
22 {
24  private $onlyComments = false;
26  private $onlyAcknowledgements = false;
28  private $clearingDao;
30  private $licenseDao;
32  private $agentDao;
34  private $licenseCache = array();
36  protected $agentNames = AgentRef::AGENT_LIST;
37 
38  public function __construct()
39  {
40  global $container;
41 
42  $this->clearingDao = $container->get('dao.clearing');
43  $this->licenseDao = $container->get('dao.license');
44  $this->agentDao = $container->get('dao.agent');
45 
46  parent::__construct($groupBy = 'text');
47  }
48 
49  protected function getStatements($uploadId, $uploadTreeTableName, $groupId = null)
50  {
51  $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId,$uploadTreeTableName);
52  $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, $groupId);
53  $dbManager = $GLOBALS['container']->get('db.manager');
54  $licenseMap = new LicenseMap($dbManager, $groupId, LicenseMap::REPORT);
55  $ungroupedStatements = array();
56  foreach ($clearingDecisions as $clearingDecision) {
57  if ($clearingDecision->getType() == DecisionTypes::IRRELEVANT) {
58  continue;
59  }
61  foreach ($clearingDecision->getClearingLicenses() as $clearingLicense) {
62  if ($clearingLicense->isRemoved()) {
63  continue;
64  }
65 
66  $comment = $clearingLicense->getComment();
67  if ($this->onlyComments && !($comment)) {
68  continue;
69  }
70 
71  $acknowledgement = $clearingLicense->getAcknowledgement();
72  if ($this->onlyAcknowledgements && !($acknowledgement)) {
73  continue;
74  }
75 
76  $originLicenseId = $clearingLicense->getLicenseId();
77  $licenseId = $licenseMap->getProjectedId($originLicenseId);
78 
79  if ($this->onlyAcknowledgements) {
80  $text = $acknowledgement;
81  $risk = "";
82  } else if ($this->onlyComments) {
83  $text = $comment;
84  $risk = "";
85  } else {
86  $reportInfo = $clearingLicense->getReportInfo();
87  $text = $reportInfo ? : $this->getCachedLicenseText($licenseId, "any");
88  $risk = $this->getCachedLicenseRisk($licenseId, $groupId);
89  $acknowledgement = $clearingLicense->getAcknowledgement();
90  }
91 
92  $ungroupedStatements[] = array(
93  'licenseId' => $licenseId,
94  'risk' => $risk,
95  'content' => $licenseMap->getProjectedSpdxId(
96  $originLicenseId, $clearingLicense->getSpdxId()),
97  'uploadtree_pk' => $clearingDecision->getUploadTreeId(),
98  'text' => $text,
99  'acknowledgement' => $acknowledgement
100  );
101  }
102  }
103 
104  return $ungroupedStatements;
105  }
106 
112  public function getCleared($uploadId, $objectAgent, $groupId=null,
113  $extended=true, $agentCall=null, $isUnifiedReport=false)
114  {
115  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
116  $ungroupedStatements = $this->getStatements($uploadId, $uploadTreeTableName,
117  $groupId);
118  $this->changeTreeIdsToPaths($ungroupedStatements, $uploadTreeTableName,
119  $uploadId);
120  if ($this->onlyAcknowledgements || $this->onlyComments) {
121  return $this->groupStatementsSpecial($ungroupedStatements, $objectAgent);
122  }
123  return $this->groupStatements($ungroupedStatements, $extended, $agentCall,
124  $isUnifiedReport, $objectAgent);
125  }
126 
133  protected function groupStatementsSpecial($ungrupedStatements, $objectAgent)
134  {
135  $statements = array();
136  $countLoop = 0;
137  foreach ($ungrupedStatements as $statement) {
138  $licenseId = $statement['licenseId'];
139  $content = convertToUTF8($statement['content'], false);
140  $content = htmlspecialchars($content, ENT_DISALLOWED);
141  $text = convertToUTF8($statement['text'], false);
142  $text = htmlspecialchars($text, ENT_DISALLOWED);
143  $fileName = $statement['fileName'];
144 
145  $statementKey = md5("$content.$text");
146  if (!array_key_exists($statementKey, $statements)) {
147  $statements[$statementKey] = [
148  "licenseId" => $licenseId,
149  "content" => $content,
150  "text" => $text,
151  "files" => [$fileName]
152  ];
153  } else {
154  if (!in_array($fileName, $statements[$statementKey]["files"])) {
155  $statements[$statementKey]["files"][] = $fileName;
156  }
157  }
158 
159  //To keep the scheduler alive for large files
160  $countLoop += 1;
161  if ($countLoop % 500 == 0) {
162  $objectAgent->heartbeat(0);
163  }
164  }
165  $statements = array_values($statements);
166  usort($statements, function($a, $b) {
167  return strnatcmp($a["content"], $b["content"]);
168  });
169  if (!empty($objectAgent)) {
170  $objectAgent->heartbeat(count($statements));
171  }
172  return array("statements" => array_values($statements));
173  }
174 
178  public function setOnlyComments($displayOnlyCommentedLicenseClearings)
179  {
180  $this->onlyAcknowledgements = false;
181  $this->onlyComments = $displayOnlyCommentedLicenseClearings;
182  }
183 
187  public function setOnlyAcknowledgements($displayOnlyAcknowledgements)
188  {
189  $this->onlyComments = false;
190  $this->onlyAcknowledgements = $displayOnlyAcknowledgements;
191  }
192 
197  protected function getCachedLicenseText($licenseId, $groupId)
198  {
199  if (!array_key_exists($licenseId, $this->licenseCache)) {
200  $this->licenseCache[$licenseId] = $this->licenseDao->getLicenseById($licenseId, $groupId);
201  }
202  return $this->licenseCache[$licenseId]->getText();
203  }
204 
209  protected function getCachedLicenseRisk($licenseId, $groupId)
210  {
211  if (!array_key_exists($licenseId, $this->licenseCache)) {
212  $this->licenseCache[$licenseId] = $this->licenseDao->getLicenseById($licenseId, $groupId);
213  }
214  return $this->licenseCache[$licenseId]->getRisk();
215  }
216 
221  protected function getHistogram($uploadId, $groupId)
222  {
223  $LicenseHistArray = array();
224  $scannerAgents = array_keys($this->agentNames);
225  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
226  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
227  $allAgentIds = $scanJobProxy->getLatestSuccessfulAgentIds();
228  $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId);
229  $scannerLicenseHistogram = $this->licenseDao->getLicenseHistogram($itemTreeBounds, $allAgentIds);
230  $editedLicensesHist = $this->clearingDao->getClearedLicenseIdAndMultiplicities($itemTreeBounds, $groupId);
231  $noScannerLicenseFoundCount = array_key_exists(LicenseDao::NO_LICENSE_FOUND, $scannerLicenseHistogram)
232  ? $scannerLicenseHistogram[LicenseDao::NO_LICENSE_FOUND]['count'] : 0;
233  $editedNoLicenseFoundCount = array_key_exists(LicenseDao::NO_LICENSE_FOUND, $editedLicensesHist)
234  ? $editedLicensesHist[LicenseDao::NO_LICENSE_FOUND]['count'] : 0;
235 
236  $totalLicenses = array_unique(array_merge(array_keys($scannerLicenseHistogram), array_keys($editedLicensesHist)));
237  foreach ($totalLicenses as $licenseShortName) {
238  $count = 0;
239  if (array_key_exists($licenseShortName, $scannerLicenseHistogram)) {
240  $count = $scannerLicenseHistogram[$licenseShortName]['unique'];
241  }
242  $editedCount = array_key_exists($licenseShortName, $editedLicensesHist) ? $editedLicensesHist[$licenseShortName]['count'] : 0;
243 
244  if (array_key_exists($licenseShortName, $scannerLicenseHistogram)) {
245  $licenseReportId = $scannerLicenseHistogram[$licenseShortName]['spdx_id'];
246  } else {
247  $licenseReportId = $editedLicensesHist[$licenseShortName]['spdx_id'];
248  }
249 
250  if (strcmp($licenseShortName, LicenseDao::NO_LICENSE_FOUND) !== 0) {
251  $LicenseHistArray[] = array("scannerCount" => $count, "editedCount" => $editedCount, "licenseShortname" => $licenseReportId);
252  } else {
253  $LicenseHistArray[] = array("scannerCount" => $noScannerLicenseFoundCount, "editedCount" => $editedNoLicenseFoundCount, "licenseShortname" => $licenseReportId);
254  }
255  }
256  return $LicenseHistArray;
257  }
258 
265  function checkLicenseId($licenses1, $licenses2)
266  {
267  return strcmp($licenses1['licenseId'], $licenses2['licenseId']);
268  }
269 
276  function updateIdentifiedGlobalLicenses($licensesMain, $licenses)
277  {
278  $onlyMainLic = array_udiff($licensesMain, $licenses, array($this, "checkLicenseId"));
279  $mainLicensesInIdentifiedFiles = array_uintersect($licenses, $licensesMain, array($this, "checkLicenseId"));
280  $onlyLicense = array_udiff($licenses, $licensesMain, array($this, "checkLicenseId"));
281  return array(
282  array_values(array_merge($onlyMainLic, $mainLicensesInIdentifiedFiles)),
283  array_values($onlyLicense)
284  );
285  }
286 }
Structure of an Agent with all required parameters.
Definition: Agent.php:41
Wrapper class for license map.
Definition: LicenseMap.php:19
getStatements($uploadId, $uploadTreeTableName, $groupId=null)
setOnlyComments($displayOnlyCommentedLicenseClearings)
updateIdentifiedGlobalLicenses($licensesMain, $licenses)
Copy identified global licenses.
setOnlyAcknowledgements($displayOnlyAcknowledgements)
checkLicenseId($licenses1, $licenses2)
callback to compare licenses
getCleared($uploadId, $objectAgent, $groupId=null, $extended=true, $agentCall=null, $isUnifiedReport=false)
groupStatementsSpecial($ungrupedStatements, $objectAgent)
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)