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  $foundLicense->getShortName() != 'No_license_found') {
139  $reportLicId = "$reportedLicenseId-" . md5($foundLicense->getText());
140  $listedLicense = !StringOperation::stringStartsWith(
141  $foundLicense->getSpdxId(), LicenseRef::SPDXREF_PREFIX);
142 
143  if (!array_key_exists($row['uploadtree_pk'], $filesWithLicenses)) {
144  $filesWithLicenses[$row['uploadtree_pk']] = new FileNode();
145  }
146  $filesWithLicenses[$row['uploadtree_pk']]->addScanner($reportLicId);
147  if (!array_key_exists($reportLicId, $licensesInDocument)) {
148  $licensesInDocument[$reportLicId] = (new SpdxLicenseInfo())
149  ->setLicenseObj($foundLicense)
150  ->setCustomText(false)
151  ->setListedLicense($listedLicense);
152  }
153  }
154  }
155  return $scannerIds;
156  }
157 
163  public function addCopyrightResults(&$filesWithLicenses, $uploadId)
164  {
165  $agentName = array('copyright', 'reso');
167  $copyrightDao = $this->container->get('dao.copyright');
169  $scanJobProxy = new ScanJobProxy($this->container->get('dao.agent'),
170  $uploadId);
171 
172  $scanJobProxy->createAgentStatus($agentName);
173  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
174  if (!array_key_exists($agentName[0], $selectedScanners)) {
175  return;
176  }
177  $latestAgentId[] = $selectedScanners[$agentName[0]];
178  if (array_key_exists($agentName[1], $selectedScanners)) {
179  $latestAgentId[] = $selectedScanners[$agentName[1]];
180  }
181  $ids = implode(',', $latestAgentId);
182  $extrawhere = ' agent_fk IN ('.$ids.')';
183 
184  $uploadtreeTable = $this->uploadDao->getUploadtreeTableName($uploadId);
185  $allScannerEntries = $copyrightDao->getScannerEntries('copyright', $uploadtreeTable, $uploadId, $type='statement', $extrawhere);
186  $allEditedEntries = $copyrightDao->getEditedEntries('copyright_decision', $uploadtreeTable, $uploadId, $decisionType=null);
187  foreach ($allScannerEntries as $finding) {
188  if (!array_key_exists($finding['uploadtree_pk'], $filesWithLicenses)) {
189  $filesWithLicenses[$finding['uploadtree_pk']] = new FileNode();
190  }
191  $filesWithLicenses[$finding['uploadtree_pk']]->addCopyright(\convertToUTF8($finding['content'],false));
192  }
193  foreach ($allEditedEntries as $finding) {
194  if (!array_key_exists($finding['uploadtree_pk'], $filesWithLicenses)) {
195  $filesWithLicenses[$finding['uploadtree_pk']] = new FileNode();
196  }
197  $filesWithLicenses[$finding['uploadtree_pk']]->addCopyright(\convertToUTF8($finding['textfinding'],false));
198  }
199  }
200 
210  ItemTreeBounds $itemTreeBounds, $groupId, $agentObj, &$licensesInDocument)
211  {
212  if ($this->licenseMap === null) {
213  $this->licenseMap = new LicenseMap($this->dbManager, $groupId, LicenseMap::REPORT, true);
214  }
215 
216  $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, $groupId);
217 
218  $filesWithLicenses = array();
219  $clearingsProceeded = 0;
220  foreach ($clearingDecisions as $clearingDecision) {
221  $clearingsProceeded += 1;
222  if (($clearingsProceeded&2047)==0) {
223  $agentObj->heartbeat(0);
224  }
225  if ($clearingDecision->getType() == DecisionTypes::IRRELEVANT) {
226  continue;
227  }
228 
229  foreach ($clearingDecision->getClearingEvents() as $clearingEvent) {
230  $clearingLicense = $clearingEvent->getClearingLicense();
231  if ($clearingLicense->isRemoved()) {
232  continue;
233  }
234 
235  if (!array_key_exists($clearingDecision->getUploadTreeId(),
236  $filesWithLicenses)) {
237  $filesWithLicenses[$clearingDecision->getUploadTreeId()] = new FileNode();
238  }
239 
240  /* ADD COMMENT */
241  $filesWithLicenses[$clearingDecision->getUploadTreeId()]
242  ->addComment($clearingLicense->getComment());
243  /* ADD Acknowledgement */
244  $filesWithLicenses[$clearingDecision->getUploadTreeId()]
245  ->addAcknowledgement($clearingLicense->getAcknowledgement());
246  $reportedLicenseId = $this->licenseMap->getProjectedId($clearingLicense->getLicenseId());
247  $concludedLicense = $this->licenseDao->getLicenseById($reportedLicenseId, $groupId);
248  if ($clearingEvent->getReportinfo()) {
249  $customLicenseText = $clearingEvent->getReportinfo();
250  $reportedLicenseShortname = $concludedLicense->getShortName() . '-' .
251  md5($customLicenseText);
252  $reportedLicenseShortname = LicenseRef::convertToSpdxId($reportedLicenseShortname, "");
253 
254  $reportLicId = "$reportedLicenseId-" . md5($customLicenseText);
255  $filesWithLicenses[$clearingDecision->getUploadTreeId()]
256  ->addConcludedLicense($reportLicId);
257  if (!array_key_exists($reportLicId, $licensesInDocument)) {
258  $licenseObj = new License($concludedLicense->getId(),
259  $reportedLicenseShortname, $concludedLicense->getFullName(),
260  $concludedLicense->getRisk(), $customLicenseText,
261  $concludedLicense->getUrl(), $concludedLicense->getDetectorType(),
262  $concludedLicense->getSpdxId());
263  $licensesInDocument[$reportLicId] = (new SpdxLicenseInfo())
264  ->setLicenseObj($licenseObj)
265  ->setCustomText(true)
266  ->setListedLicense(false);
267  }
268  } else {
269  $reportLicId = $concludedLicense->getId() . "-" .
270  md5($concludedLicense->getText());
271  $filesWithLicenses[$clearingDecision->getUploadTreeId()]
272  ->addConcludedLicense($reportLicId);
273  if (!array_key_exists($reportLicId, $licensesInDocument)) {
274  $licenseObj = $this->licenseDao->getLicenseById($reportedLicenseId, $groupId);
275  $listedLicense = !StringOperation::stringStartsWith(
276  $licenseObj->getSpdxId(), LicenseRef::SPDXREF_PREFIX);
277  $licensesInDocument[$reportLicId] = (new SpdxLicenseInfo())
278  ->setLicenseObj($licenseObj)
279  ->setCustomText(false)
280  ->setListedLicense($listedLicense);
281  }
282  }
283  }
284  }
285  return $filesWithLicenses;
286  }
287 }
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.
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