FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ReportUtils.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2023 Sushant Kumar(sushantmishra02102002@gmail.com)
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 namespace Fossology\Lib\Report;
8 
26 use Symfony\Component\DependencyInjection\ContainerBuilder;
27 
28 
30 {
34  protected $agentNames = AgentRef::AGENT_LIST;
38  protected $container;
42  private $licenseMap;
46  protected $dbManager;
50  private $uploadDao;
54  private $licenseDao;
59  private $clearingDao;
60 
61  function __construct()
62  {
63  global $container;
64  $this->container = $container;
65 
66  $this->dbManager = $this->container->get('db.manager');
67  $this->uploadDao = $this->container->get('dao.upload');
68  $this->licenseDao = $this->container->get('dao.license');
69  $this->clearingDao = $this->container->get('dao.clearing');
70  $this->licenseMap = null;
71  }
72 
79  public function addClearingStatus(&$filesWithLicenses, ItemTreeBounds $itemTreeBounds, $groupId)
80  {
81  $alreadyClearedUploadTreeView = new UploadTreeProxy($itemTreeBounds->getUploadId(),
82  array(UploadTreeProxy::OPT_SKIP_THESE => UploadTreeProxy::OPT_SKIP_ALREADY_CLEARED,
83  UploadTreeProxy::OPT_ITEM_FILTER => "AND (lft BETWEEN ".$itemTreeBounds->getLeft()." AND ".$itemTreeBounds->getRight().")",
84  UploadTreeProxy::OPT_GROUP_ID => $groupId),
85  $itemTreeBounds->getUploadTreeTableName(),
86  'already_cleared_uploadtree' . $itemTreeBounds->getUploadId());
87 
88  $alreadyClearedUploadTreeView->materialize();
89  $filesThatShouldStillBeCleared = $alreadyClearedUploadTreeView->getNonArtifactDescendants($itemTreeBounds);
90  $alreadyClearedUploadTreeView->unmaterialize();
91 
92  $uploadTreeIds = array_keys($filesWithLicenses);
93  foreach ($uploadTreeIds as $uploadTreeId) {
94  if (!array_key_exists($uploadTreeId, $filesWithLicenses)) {
95  $filesWithLicenses[$uploadTreeId] = new FileNode();
96  }
97  $filesWithLicenses[$uploadTreeId]->setIsCleared(false === array_key_exists($uploadTreeId, $filesThatShouldStillBeCleared));
98  }
99  }
100 
109  public function addScannerResults(&$filesWithLicenses, ItemTreeBounds $itemTreeBounds, $groupId, &$licensesInDocument)
110  {
111  if ($this->licenseMap === null) {
112  $this->licenseMap = new LicenseMap($this->dbManager, $groupId, LicenseMap::REPORT, true);
113  }
114  $uploadId = $itemTreeBounds->getUploadId();
115  $scannerAgents = array_keys($this->agentNames);
116  $scanJobProxy = new ScanJobProxy($this->container->get('dao.agent'), $uploadId);
117  $scanJobProxy->createAgentStatus($scannerAgents);
118  $scannerIds = $scanJobProxy->getLatestSuccessfulAgentIds();
119  if (empty($scannerIds)) {
120  return [];
121  }
122  $tableName = $itemTreeBounds->getUploadTreeTableName();
123  $stmt = __METHOD__ .'.scanner_findings';
124  $sql = "SELECT DISTINCT uploadtree_pk,rf_fk FROM $tableName ut, license_file
125  WHERE ut.pfile_fk=license_file.pfile_fk AND rf_fk IS NOT NULL AND agent_fk=any($1)";
126  $param = array('{'.implode(',',$scannerIds).'}');
127  if ($tableName == 'uploadtree_a') {
128  $param[] = $uploadId;
129  $sql .= " AND upload_fk=$".count($param);
130  $stmt .= $tableName;
131  }
132  $sql .= " GROUP BY uploadtree_pk,rf_fk";
133  $rows = $this->dbManager->getRows($sql, $param, $stmt);
134  foreach ($rows as $row) {
135  $reportedLicenseId = $this->licenseMap->getProjectedId($row['rf_fk']);
136  $foundLicense = $this->licenseDao->getLicenseById($reportedLicenseId);
137  if ($foundLicense !== null && $foundLicense->getShortName() != 'Void') {
138  $reportLicId = "$reportedLicenseId-" . md5($foundLicense->getText());
139  $listedLicense = !StringOperation::stringStartsWith(
140  $foundLicense->getSpdxId(), LicenseRef::SPDXREF_PREFIX);
141 
142  if (!array_key_exists($row['uploadtree_pk'], $filesWithLicenses)) {
143  $filesWithLicenses[$row['uploadtree_pk']] = new FileNode();
144  }
145  if ($foundLicense->getShortName() != 'No_license_found') {
146  $filesWithLicenses[$row['uploadtree_pk']]->addScanner($reportLicId);
147  } else {
148  $filesWithLicenses[$row['uploadtree_pk']]->addScanner("");
149  }
150  if (!array_key_exists($reportLicId, $licensesInDocument)) {
151  $licensesInDocument[$reportLicId] = (new SpdxLicenseInfo())
152  ->setLicenseObj($foundLicense)
153  ->setCustomText(false)
154  ->setListedLicense($listedLicense);
155  }
156  }
157  }
158  return $scannerIds;
159  }
160 
166  public function addCopyrightResults(&$filesWithLicenses, $uploadId)
167  {
168  $agentName = array('copyright', 'reso');
170  $copyrightDao = $this->container->get('dao.copyright');
172  $scanJobProxy = new ScanJobProxy($this->container->get('dao.agent'),
173  $uploadId);
174 
175  $scanJobProxy->createAgentStatus($agentName);
176  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
177  if (!array_key_exists($agentName[0], $selectedScanners)) {
178  return;
179  }
180  $latestAgentId[] = $selectedScanners[$agentName[0]];
181  if (array_key_exists($agentName[1], $selectedScanners)) {
182  $latestAgentId[] = $selectedScanners[$agentName[1]];
183  }
184  $ids = implode(',', $latestAgentId);
185  $extrawhere = ' agent_fk IN ('.$ids.')';
186 
187  $uploadtreeTable = $this->uploadDao->getUploadtreeTableName($uploadId);
188  $allScannerEntries = $copyrightDao->getScannerEntries('copyright', $uploadtreeTable, $uploadId, $type='statement', $extrawhere);
189  $allEditedEntries = $copyrightDao->getEditedEntries('copyright_decision', $uploadtreeTable, $uploadId, $decisionType=null);
190  foreach ($allScannerEntries as $finding) {
191  if (!array_key_exists($finding['uploadtree_pk'], $filesWithLicenses)) {
192  $filesWithLicenses[$finding['uploadtree_pk']] = new FileNode();
193  }
194  $filesWithLicenses[$finding['uploadtree_pk']]->addCopyright(\convertToUTF8($finding['content'],false));
195  }
196  foreach ($allEditedEntries as $finding) {
197  if (!array_key_exists($finding['uploadtree_pk'], $filesWithLicenses)) {
198  $filesWithLicenses[$finding['uploadtree_pk']] = new FileNode();
199  }
200  $filesWithLicenses[$finding['uploadtree_pk']]->addCopyright(\convertToUTF8($finding['textfinding'],false));
201  }
202  }
203 
213  ItemTreeBounds $itemTreeBounds, $groupId, $agentObj, &$licensesInDocument)
214  {
215  if ($this->licenseMap === null) {
216  $this->licenseMap = new LicenseMap($this->dbManager, $groupId, LicenseMap::REPORT, true);
217  }
218 
219  $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, $groupId);
220 
221  $filesWithLicenses = array();
222  $clearingsProceeded = 0;
223  foreach ($clearingDecisions as $clearingDecision) {
224  $clearingsProceeded += 1;
225  if (($clearingsProceeded&2047)==0) {
226  $agentObj->heartbeat(0);
227  }
228  if ($clearingDecision->getType() == DecisionTypes::IRRELEVANT) {
229  continue;
230  }
231 
232  foreach ($clearingDecision->getClearingEvents() as $clearingEvent) {
233  $clearingLicense = $clearingEvent->getClearingLicense();
234  if ($clearingLicense->isRemoved()) {
235  continue;
236  }
237 
238  if (!array_key_exists($clearingDecision->getUploadTreeId(),
239  $filesWithLicenses)) {
240  $filesWithLicenses[$clearingDecision->getUploadTreeId()] = new FileNode();
241  }
242 
243  /* ADD COMMENT */
244  $filesWithLicenses[$clearingDecision->getUploadTreeId()]
245  ->addComment($clearingLicense->getComment());
246  /* ADD Acknowledgement */
247  $filesWithLicenses[$clearingDecision->getUploadTreeId()]
248  ->addAcknowledgement($clearingLicense->getAcknowledgement());
249  $reportedLicenseId = $this->licenseMap->getProjectedId($clearingLicense->getLicenseId());
250  $concludedLicense = $this->licenseDao->getLicenseById($reportedLicenseId, $groupId);
251  if ($clearingEvent->getReportinfo()) {
252  $customLicenseText = $clearingEvent->getReportinfo();
253  $reportedLicenseShortname = $concludedLicense->getShortName() . '-' .
254  md5($customLicenseText);
255  $reportedLicenseShortname = LicenseRef::convertToSpdxId($reportedLicenseShortname, "");
256 
257  $reportLicId = "$reportedLicenseId-" . md5($customLicenseText);
258  $filesWithLicenses[$clearingDecision->getUploadTreeId()]
259  ->addConcludedLicense($reportLicId);
260  if (!array_key_exists($reportLicId, $licensesInDocument)) {
261  $licenseObj = new License($concludedLicense->getId(),
262  $reportedLicenseShortname, $concludedLicense->getFullName(),
263  $concludedLicense->getRisk(), $customLicenseText,
264  $concludedLicense->getUrl(), $concludedLicense->getDetectorType(),
265  $concludedLicense->getSpdxId());
266  $licensesInDocument[$reportLicId] = (new SpdxLicenseInfo())
267  ->setLicenseObj($licenseObj)
268  ->setCustomText(true)
269  ->setListedLicense(false);
270  }
271  } else {
272  $reportLicId = $concludedLicense->getId() . "-" .
273  md5($concludedLicense->getText());
274  $filesWithLicenses[$clearingDecision->getUploadTreeId()]
275  ->addConcludedLicense($reportLicId);
276  if (!array_key_exists($reportLicId, $licensesInDocument)) {
277  $licenseObj = $this->licenseDao->getLicenseById($reportedLicenseId, $groupId);
278  $listedLicense = !StringOperation::stringStartsWith(
279  $licenseObj->getSpdxId(), LicenseRef::SPDXREF_PREFIX);
280  $licensesInDocument[$reportLicId] = (new SpdxLicenseInfo())
281  ->setLicenseObj($licenseObj)
282  ->setCustomText(false)
283  ->setListedLicense($listedLicense);
284  }
285  }
286  }
287  }
288  return $filesWithLicenses;
289  }
290 
302  public function updateOrInsertReportgenEntry($upload_fk, $job_fk, $filepath)
303  {
304  $sqlCheck = "SELECT 1 FROM reportgen WHERE upload_fk = $1 AND filepath = $2";
305  $row = $this->dbManager->getSingleRow($sqlCheck, [$upload_fk, $filepath],
306  __METHOD__.'.checkReportgenEntry');
307 
308  if (!empty($row)) {
309  $sqlUpdate = "UPDATE reportgen SET job_fk = $1 WHERE upload_fk = $2 AND filepath = $3";
310  $this->dbManager->getSingleRow($sqlUpdate, [$job_fk, $upload_fk, $filepath],
311  __METHOD__.'.updateReportgen');
312  } else {
313  $this->dbManager->insertTableRow('reportgen',
314  ['upload_fk' => $upload_fk, 'job_fk' => $job_fk, 'filepath' => $filepath],
315  __METHOD__);
316  }
317  }
318 }
Structure of an Agent with all required parameters.
Definition: Agent.php:41
Wrapper class for license map.
Definition: LicenseMap.php:19
static convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
Definition: LicenseRef.php:106
addScannerResults(&$filesWithLicenses, ItemTreeBounds $itemTreeBounds, $groupId, &$licensesInDocument)
Attach finding agents to the files and return names of scanners.
updateOrInsertReportgenEntry($upload_fk, $job_fk, $filepath)
addClearingStatus(&$filesWithLicenses, ItemTreeBounds $itemTreeBounds, $groupId)
Add clearing status to the files.
Definition: ReportUtils.php:79
getFilesWithLicensesFromClearings(ItemTreeBounds $itemTreeBounds, $groupId, $agentObj, &$licensesInDocument)
Given an ItemTreeBounds, get the files with clearings.
static stringStartsWith($haystack, $needle)
if(! defined('ENT_SUBSTITUTE')) convertToUTF8($content, $toHTML=true)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16