FOSSology  4.4.0
Open Source License Compliance by Open Source Software
PfileDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
14 use Monolog\Logger;
15 
16 class PfileDao
17 {
18 
23  private $dbManager;
28  private $logger;
29  public function __construct(DbManager $dbManager, Logger $logger)
30  {
31  $this->dbManager = $dbManager;
32  $this->logger = $logger;
33  }
34 
44  public function getPfile($sha1 = null, $md5 = null, $sha256 = null,
45  $size = null)
46  {
47  $statement = __METHOD__ . ".getPfileId";
48  $sql = "SELECT * FROM pfile WHERE ";
49  $params = [];
50  $conds = [];
51  if (! empty($sha1)) {
52  $statement .= ".sha1";
53  $params[] = strtoupper($sha1);
54  $conds[] = "pfile_sha1 = $" . count($params);
55  }
56  if (! empty($md5)) {
57  $statement .= ".md5";
58  $params[] = strtoupper($md5);
59  $conds[] = "pfile_md5 = $" . count($params);
60  }
61  if (! empty($sha256)) {
62  $statement .= ".sha256";
63  $params[] = strtoupper($sha256);
64  $conds[] = "pfile_sha256 = $" . count($params);
65  }
66  if (! empty($size)) {
67  $statement .= ".size";
68  $params[] = $size;
69  $conds[] = "pfile_size = $" . count($params);
70  }
71  $sql .= join(" AND ", $conds);
72  $row = $this->dbManager->getSingleRow($sql, $params, $statement);
73  return (! empty($row)) ? $row : null;
74  }
75 
81  public function getScannerFindings($pfileId)
82  {
83  $statement = __METHOD__ . ".getScannerFindings";
84  $sql = "SELECT DISTINCT ON(rf_pk) rf_shortname " .
85  "FROM license_file AS lf " .
86  "INNER JOIN ONLY license_ref AS lr ON lf.rf_fk = lr.rf_pk " .
87  "WHERE lf.pfile_fk = $1;";
88  $params = [$pfileId];
89  $rows = $this->dbManager->getRows($sql, $params, $statement);
90  if (! empty($rows) && array_key_exists('rf_shortname', $rows[0])) {
91  $licenses = array_column($rows, 'rf_shortname');
92  natcasesort($licenses);
93  return array_values($licenses);
94  } else {
95  return [];
96  }
97  }
98 
108  public function getConclusions($groupId, $pfileId)
109  {
110  if (! $this->haveConclusions($groupId, $pfileId)) {
111  return ["NOASSERTION"];
112  }
113  $statement = __METHOD__ . ".getScannerFindings";
114  $sql = "WITH allDecsPfile AS (
115  SELECT cd.pfile_fk, cd.clearing_decision_pk, cd.decision_type,
116  lr.rf_shortname, ce.removed
117  FROM clearing_decision AS cd
118  INNER JOIN clearing_decision_event AS cde
119  ON cde.clearing_decision_fk = cd.clearing_decision_pk
120  INNER JOIN clearing_event AS ce
121  ON cde.clearing_event_fk = ce.clearing_event_pk
122  INNER JOIN license_ref AS lr
123  ON ce.rf_fk = lr.rf_pk
124  WHERE cd.pfile_fk = $1 AND (cd.group_fk = $2 OR cd.scope = " .
125  DecisionScopes::REPO . ")
126  ORDER BY cd.clearing_decision_pk DESC
127 ),
128 rankedDecs AS (
129  SELECT *, rank() OVER (
130  PARTITION BY pfile_fk, rf_shortname ORDER BY clearing_decision_pk DESC
131  ) rnk FROM allDecsPfile
132 )
133 SELECT * FROM rankedDecs
134 WHERE rnk = 1 AND removed = false AND decision_type = " .
135  DecisionTypes::IDENTIFIED . ";";
136  $params = [$pfileId, $groupId];
137  $rows = $this->dbManager->getRows($sql, $params, $statement);
138  if (! empty($rows) && array_key_exists('rf_shortname', $rows[0])) {
139  $licenses = array_column($rows, 'rf_shortname');
140  natcasesort($licenses);
141  return array_values($licenses);
142  } else {
143  return ["NONE"];
144  }
145  }
146 
153  public function getUploadForPackage($pfileId)
154  {
155  $statement = __METHOD__ . ".getUploadForPackage";
156  $sql = "SELECT upload_pk FROM upload WHERE pfile_fk = $1;";
157  $params = [$pfileId];
158  $rows = $this->dbManager->getRows($sql, $params, $statement);
159  if (! empty($rows) && array_key_exists('upload_pk', $rows[0])) {
160  $uploads = array_column($rows, 'upload_pk');
161  return array_map(function ($upload) {
162  return intval($upload);
163  }, $uploads);
164  } else {
165  return null;
166  }
167  }
168 
176  public function haveConclusions($groupId, $pfileId)
177  {
178  $statement = __METHOD__ . ".pfileHaveConclusions";
179  $sql = "SELECT count(*) AS cnt FROM clearing_decision " .
180  "WHERE pfile_fk = $1 AND (group_fk = $2 OR scope = " .
181  DecisionScopes::REPO . ");";
182  $params = [$pfileId, $groupId];
183  $row = $this->dbManager->getSingleRow($sql, $params, $statement);
184  if (! empty($row['cnt']) && $row['cnt'] > 0) {
185  return true;
186  }
187  return false;
188  }
189 
195  public function getCopyright($pfileId)
196  {
197  $statement = __METHOD__ . ".getCopyright";
198  $sql = "SELECT content " .
199  "FROM copyright " .
200  "WHERE (pfile_fk = $1) AND (is_enabled = TRUE) " .
201  "UNION " .
202  "SELECT textfinding " .
203  "FROM copyright_decision " .
204  "WHERE (pfile_fk = $1) AND (is_enabled = TRUE);";
205  $params = [$pfileId];
206  $rows = $this->dbManager->getRows($sql, $params, $statement);
207  if (!empty($rows) && array_key_exists('content', $rows[0])) {
208  $copyright = array_column($rows, 'content');
209  natcasesort($copyright);
210  return array_values($copyright);
211  } else {
212  return [];
213  }
214  }
215 }
216 
getPfile($sha1=null, $md5=null, $sha256=null, $size=null)
Definition: PfileDao.php:44
getScannerFindings($pfileId)
Definition: PfileDao.php:81
getConclusions($groupId, $pfileId)
Definition: PfileDao.php:108
haveConclusions($groupId, $pfileId)
Definition: PfileDao.php:176
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.
Definition: utils.c:39