FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AllDecisionsDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Siemens AG
4  Author: Shaheem Azmal M MD <shaheem.azmal@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
13 use Monolog\Logger;
14 
20 {
22  private $dbManager;
24  private $agentDao;
26  private $uploadDao;
27 
32  function __construct(DbManager $dbManager, Logger $logger)
33  {
34  $this->dbManager = $dbManager;
35  $this->logger = $logger;
36  global $container;
37  $this->agentDao = $container->get('dao.agent');
38  $this->uploadDao = $container->get('dao.upload');
39  }
40 
45  public function getAllJobTypeForUpload($uploadId)
46  {
47  $extendedQuery = " AND jq_type LIKE 'nomos' OR jq_type LIKE 'monk'".
48  " OR jq_type LIKE 'ojo' OR jq_type LIKE 'copyright'".
49  " OR jq_type LIKE 'ecc' OR jq_type LIKE 'ipra'";
50  $sql = "SELECT DISTINCT(jq_type) FROM jobqueue INNER JOIN job ON jq_job_fk=job_pk " .
51  "WHERE jq_end_bits ='1'".$extendedQuery." AND job_upload_fk=$1;";
52  $statementName = __METHOD__ . ".getAllFinishedJobsForUploadId";
53  $rows = $this->dbManager->getRows($sql, array($uploadId), $statementName);
54 
55  return array_column($rows, 'jq_type');
56  }
57 
65  public function getAllAgentEntriesForPfile($uploadId, $groupId, $userId, $skip)
66  {
67  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
68  return new UploadTreeProxy($uploadId,
69  array(UploadTreeProxy::OPT_SKIP_THESE => $skip,
70  UploadTreeProxy::OPT_GROUP_ID => $groupId),
71  $uploadTreeTableName,
72  'no_license_uploadtree' . $uploadId);
73  }
74 
82  public function getSqlQueryDataPfile($uploadId, $groupId, $userId, $skip='noLicense')
83  {
84  $allLicEntries = $this->getAllAgentEntriesForPfile($uploadId, $groupId, $userId, $skip);
85  $sql = "WITH latestResultsPfile AS (".$allLicEntries->getDbViewQuery().")".
86  "SELECT pfile_fk, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfilehash FROM latestResultsPfile LSP".
87  " INNER JOIN pfile p ON LSP.pfile_fk = p.pfile_pk " ;
88  $statementName = __METHOD__ . ".allLicEntrieslatestResultsPfile." . $skip;
89  $rows = $this->dbManager->getRows($sql, array(), $statementName);
90  return array_column($rows,'pfilehash','pfile_fk');
91  }
92 
98  public function getAllAgentUploadTreeDataForUpload($uploadId, $tableName)
99  {
100  $sql = "SELECT DISTINCT ON(uploadtree_pk) " .
101  "uploadtree_pk, ut.pfile_fk, ut.lft, ut.rgt FROM uploadtree ut " .
102  "INNER JOIN ".$tableName." ON ".$tableName.".pfile_fk=ut.pfile_fk WHERE upload_fk=$1";
103  $statementName = __METHOD__ . ".allLicEntriesuploadtreeForUpload";
104  $rows = $this->dbManager->getRows($sql, array($uploadId), $statementName);
105  foreach ($rows as $index => $row) {
106  $rows[$index]["path"] = implode("/",
107  array_column(Dir2Path($row["uploadtree_pk"]), "ufile_name"));
108  }
109  return $rows;
110  }
111 
117  public function getAllClearingDecisionDataForUpload($uploadId, $tableName)
118  {
119  $columns = "clearing_decision_pk, cd.uploadtree_fk, cd.pfile_fk, decision_type, scope, date_added";
120  $sql = "SELECT ".$columns." FROM clearing_decision cd ".
121  " INNER JOIN ".$tableName." ON ".$tableName.".pfile_fk=cd.pfile_fk ".
122  " INNER JOIN uploadtree ut ON cd.uploadtree_fk=ut.uploadtree_pk WHERE ut.upload_fk=$1";
123  $statementName = __METHOD__ . ".allLicEntriesClearingDecisionForUpload";
124  return $this->dbManager->getRows($sql, array($uploadId), $statementName);
125  }
126 
132  public function getAllClearingEventDataForUpload($uploadId, $tableName)
133  {
134  $columns = "DISTINCT(clearing_event_pk), ce.uploadtree_fk, rf_fk, removed, ".
135  " lrb_pk, type_fk, comment, reportinfo, acknowledgement, date_added";
136  $sql = "SELECT ".$columns." FROM clearing_event ce ".
137  " INNER JOIN uploadtree ut ON ce.uploadtree_fk=ut.uploadtree_pk ".
138  " INNER JOIN ".$tableName." ON ".$tableName.".pfile_fk=ut.pfile_fk " .
139  " LEFT JOIN jobqueue jq ON jq.jq_job_fk=ce.job_fk AND jq.jq_type='monkbulk'" .
140  " LEFT JOIN license_ref_bulk lrb ON jq.jq_args::int=lrb_pk " .
141  "WHERE ut.upload_fk=$1";
142  $statementName = __METHOD__ . ".allLicEntriesClearingEventForUpload";
143  return $this->dbManager->getRows($sql, array($uploadId), $statementName);
144  }
145 
151  public function getAllClearingDecisionEventDataForUpload($uploadId, $tableName)
152  {
153  $columns = "clearing_decision_fk, clearing_event_fk";
154  $sql = "SELECT ".$columns." FROM clearing_decision_event cde ".
155  " INNER JOIN clearing_decision cd ON cde.clearing_decision_fk=cd.clearing_decision_pk ".
156  " INNER JOIN ".$tableName." ON ".$tableName.".pfile_fk=cd.pfile_fk".
157  " INNER JOIN uploadtree ut ON cd.uploadtree_fk=ut.uploadtree_pk WHERE ut.upload_fk=$1";
158  $statementName = __METHOD__ . ".allLicEntriesClearingDecisionEventForUpload";
159  return $this->dbManager->getRows($sql, array($uploadId), $statementName);
160  }
161 
166  public function getAllLicenseRefBulkDataForUpload($uploadId)
167  {
168  $columns = "lrb_pk, rf_text, uploadtree_fk, ignore_irrelevant, bulk_delimiters, scan_findings";
169  $sql = "SELECT ".$columns." FROM license_ref_bulk lrb ".
170  " INNER JOIN uploadtree ut ON lrb.uploadtree_fk=ut.uploadtree_pk WHERE ut.upload_fk=$1";
171  $statementName = __METHOD__ . ".allLicEntriesLicenseRefBulkForUpload";
172  return $this->dbManager->getRows($sql, array($uploadId), $statementName);
173  }
174 
179  public function getAllLicenseSetBulkDataForUpload($uploadId)
180  {
181  $columns = "rf_fk, removing, lrb_fk, comment, reportinfo, acknowledgement";
182  $sql = "SELECT ".$columns." FROM license_set_bulk lsb ".
183  " INNER JOIN license_ref_bulk lrb ON lsb.lrb_fk=lrb.lrb_pk ".
184  " INNER JOIN uploadtree ut ON lrb.uploadtree_fk=ut.uploadtree_pk WHERE ut.upload_fk=$1";
185  $statementName = __METHOD__ . ".allLicEntriesLicenseSetBulkForUpload";
186  return $this->dbManager->getRows($sql, array($uploadId), $statementName);
187  }
188 
193  public function getAllBulkHighlightDataForUpload($uploadId)
194  {
195  $columns = "clearing_event_fk, lrb_fk, start, len";
196  $sql = "SELECT $columns FROM highlight_bulk hb INNER JOIN license_ref_bulk lrb ON hb.lrb_fk=lrb.lrb_pk " .
197  "INNER JOIN uploadtree ut ON lrb.uploadtree_fk=ut.uploadtree_pk WHERE ut.upload_fk=$1";
198  $statementName = __METHOD__ . ".allBulkHighlightDataForUpload";
199  return $this->dbManager->getRows($sql, [$uploadId], $statementName);
200  }
201 
207  public function getAllDataForGivenTableUpload($pfileTableName, $givenTableName)
208  {
209  $columns = "DISTINCT(".$givenTableName."_pk), ".$givenTableName.".pfile_fk, content, hash, copy_startbyte, copy_endbyte";
210  $sql = "SELECT ".$columns." FROM ".$givenTableName." ".
211  " INNER JOIN ".$pfileTableName." ON ".$pfileTableName.".pfile_fk=".$givenTableName.".pfile_fk";
212  $statementName = __METHOD__ . ".allLicEntries".$givenTableName."ForUpload";
213  return $this->dbManager->getRows($sql, array(), $statementName);
214  }
215 
221  public function getAllDataForGivenDecisionTableUpload($pfileTableName, $givenTableName)
222  {
223  $columns = "DISTINCT(".$givenTableName."_pk), ".$givenTableName.".pfile_fk, ".
224  " clearing_decision_type_fk, description, textfinding, hash, comment";
225  $sql = "SELECT ".$columns." FROM ".$givenTableName." ".
226  " INNER JOIN ".$pfileTableName." ON ".$pfileTableName.".pfile_fk=".$givenTableName.".pfile_fk";
227  $statementName = __METHOD__ . ".allLicEntries".$givenTableName."ForUpload";
228  return $this->dbManager->getRows($sql, array(), $statementName);
229  }
230 
237  public function getAllDataForGivenEventTableUpload($uploadId, $givenTableName, $tableType)
238  {
239  $columns = "DISTINCT(".$givenTableName."_pk), ".$tableType."_fk, ".
240  "uploadtree_fk, content, hash, is_enabled, scope";
241  $sql = "SELECT ".$columns." FROM ".$givenTableName." ".
242  " INNER JOIN uploadtree ut ON ".$givenTableName.".uploadtree_fk=ut.uploadtree_pk".
243  " WHERE ".$givenTableName.".upload_fk=$1";
244  $statementName = __METHOD__ . ".allLicEntries".$givenTableName."ForUpload";
245  return $this->dbManager->getRows($sql, array($uploadId), $statementName);
246  }
247 
252  public function getAllLicenseDataForUpload($uploadId)
253  {
254  $columns = "rf_pk, rf_shortname, rf_fullname, rf_text, rf_url, rf_notes, rf_md5, rf_risk";
255  $sql = "WITH alllicense AS (" .
256  "SELECT $columns, false AS is_candidate FROM ONLY license_ref UNION " .
257  "SELECT $columns, true AS is_candidate FROM ONLY license_candidate) " .
258  "SELECT lf.* FROM alllicense AS lf " .
259  " INNER JOIN clearing_event ce ON ce.rf_fk=lf.rf_pk " .
260  " INNER JOIN uploadtree ut ON ce.uploadtree_fk=ut.uploadtree_pk WHERE ut.upload_fk=$1" .
261  " UNION DISTINCT " .
262  "SELECT lf.* FROM alllicense AS lf " .
263  " INNER JOIN upload_clearing_license ucl ON ucl.rf_fk = lf.rf_pk AND ucl.upload_fk=$1;";
264  $statementName = __METHOD__ . ".allLicEntriesLicenseForUpload";
265  return $this->dbManager->getRows($sql, array($uploadId), $statementName);
266  }
267 
274  public function getAllAgentPfileIdsForUpload($uploadId, $groupId, $userId)
275  {
276  $licensePfile = array();
277  $copyrightPfile = array();
278  $eccPfile = array();
279  $ipPfile = array();
280  $licenseAgentNames = array('nomos','monk','ojo');
281  $executedAgents = $this->getAllJobTypeForUpload($uploadId);
282  foreach ($executedAgents as $agent) {
283  if (in_array($agent,$licenseAgentNames)) {
284  $executedAgents = array_diff($executedAgents,$licenseAgentNames);
285  $licensePfile = $this->getSqlQueryDataPfile($uploadId, $groupId, $userId);
286  } else if ($agent == 'copyright') {
287  $executedAgents = array_diff($executedAgents,array('copyright'));
288  $copyrightPfile = $this->getSqlQueryDataPfile($uploadId, $groupId, $userId, 'noCopyright');
289  } else if ($agent == 'ecc') {
290  $executedAgents = array_diff($executedAgents,array('ecc'));
291  $eccPfile = $this->getSqlQueryDataPfile($uploadId, $groupId, $userId, 'noEcc');
292  } else if ($agent == 'ipra') {
293  $executedAgents = array_diff($executedAgents,array('ipra'));
294  $ipPfile = $this->getSqlQueryDataPfile($uploadId, $groupId, $userId, 'noIpra');
295  }
296  }
297  return $licensePfile + $copyrightPfile + $eccPfile + $ipPfile;
298  }
299 }
getAllAgentPfileIdsForUpload($uploadId, $groupId, $userId)
getAllAgentEntriesForPfile($uploadId, $groupId, $userId, $skip)
getAllClearingDecisionEventDataForUpload($uploadId, $tableName)
getAllClearingDecisionDataForUpload($uploadId, $tableName)
getSqlQueryDataPfile($uploadId, $groupId, $userId, $skip='noLicense')
getAllDataForGivenTableUpload($pfileTableName, $givenTableName)
getAllDataForGivenDecisionTableUpload($pfileTableName, $givenTableName)
getAllAgentUploadTreeDataForUpload($uploadId, $tableName)
getAllDataForGivenEventTableUpload($uploadId, $givenTableName, $tableType)
__construct(DbManager $dbManager, Logger $logger)
getAllClearingEventDataForUpload($uploadId, $tableName)
Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
Return the path (without artifacts) of an uploadtree_pk.
Definition: common-dir.php:222
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16