FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2018 Siemens AG
4  Author: Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
20 use Monolog\Logger;
21 
23 {
24  const NO_LICENSE_FOUND = 'No_license_found';
25  const VOID_LICENSE = 'Void';
26 
28  private $dbManager;
30  private $logger;
32  private $candidatePrefix = '*';
33 
34  function __construct(DbManager $dbManager)
35  {
36  $this->dbManager = $dbManager;
37  $this->logger = new Logger(self::class);
38  }
39 
47  function getAgentFileLicenseMatches(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
48  {
49  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
50  $statementName = __METHOD__ . ".$uploadTreeTableName.$usageId";
51  $params = array($itemTreeBounds->getUploadId(), $itemTreeBounds->getLeft(), $itemTreeBounds->getRight());
52  if ($usageId==LicenseMap::TRIVIAL) {
53  $licenseJoin = "license_ref mlr ON license_file.rf_fk = mlr.rf_pk";
54  } else {
55  $params[] = $usageId;
56  $licenseMapCte = LicenseMap::getMappedLicenseRefView('$4');
57  $licenseJoin = "($licenseMapCte) AS mlr ON license_file.rf_fk = mlr.rf_origin";
58  }
59 
60  $this->dbManager->prepare($statementName,
61  "SELECT LFR.rf_shortname AS license_shortname,
62  LFR.rf_spdx_id AS spdx_id,
63  LFR.rf_fullname AS license_fullname,
64  LFR.rf_pk AS license_id,
65  LFR.fl_pk AS license_file_id,
66  LFR.pfile_fk as file_id,
67  LFR.rf_match_pct AS percent_match,
68  AG.agent_name AS agent_name,
69  AG.agent_pk AS agent_id,
70  AG.agent_rev AS agent_revision
71  FROM ( SELECT mlr.rf_fullname, mlr.rf_shortname, mlr.rf_spdx_id, mlr.rf_pk, license_file.fl_pk, license_file.agent_fk, license_file.pfile_fk, license_file.rf_match_pct
72  FROM license_file JOIN $licenseJoin) as LFR
73  INNER JOIN $uploadTreeTableName as UT ON UT.pfile_fk = LFR.pfile_fk
74  INNER JOIN agent as AG ON AG.agent_pk = LFR.agent_fk
75  WHERE AG.agent_enabled='true' and
76  UT.upload_fk=$1 AND UT.lft BETWEEN $2 and $3
77  ORDER BY license_shortname ASC, percent_match DESC");
78  $result = $this->dbManager->execute($statementName, $params);
79  $matches = array();
80  while ($row = $this->dbManager->fetchArray($result)) {
81  $licenseRef = new LicenseRef(intval($row['license_id']), $row['license_shortname'], $row['license_fullname'], $row['spdx_id']);
82  $agentRef = new AgentRef(intval($row['agent_id']), $row['agent_name'], $row['agent_revision']);
83  $matches[] = new LicenseMatch(intval($row['file_id']), $licenseRef, $agentRef, intval($row['license_file_id']), intval($row['percent_match']));
84  }
85 
86  $this->dbManager->freeResult($result);
87  return $matches;
88  }
89 
90 
97  function getBulkFileLicenseMatches(ItemTreeBounds $itemTreeBounds)
98  {
99  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
100  $statementName = __METHOD__ . ".$uploadTreeTableName";
101 
102  $this->dbManager->prepare($statementName,
103  "SELECT LF.rf_shortname AS license_shortname,
104  LF.rf_spdx_id AS spdx_id,
105  LF.rf_fullname AS license_fullname,
106  LF.rf_pk AS license_id,
107  LFB.lrb_pk AS license_file_id,
108  LSB.removing AS removing,
109  UT.pfile_fk as file_id
110  FROM license_ref_bulk as LFB
111  INNER JOIN license_set_bulk AS LSB ON LFB.lrb_pk = LSB.lrb_fk
112  INNER JOIN license_ref as LF on LF.rf_pk = LSB.rf_fk
113  INNER JOIN $uploadTreeTableName as UT ON UT.uploadtree_pk = LFB.uploadtree_fk
114  WHERE UT.upload_fk=$1 AND UT.lft BETWEEN $2 and $3
115  ORDER BY license_file_id ASC");
116 
117  $result = $this->dbManager->execute($statementName,
118  array($itemTreeBounds->getUploadId(), $itemTreeBounds->getLeft(), $itemTreeBounds->getRight()));
119 
120  $matches = array();
121 
122  while ($row = $this->dbManager->fetchArray($result)) {
123  $licenseRef = new LicenseRef($row['license_id'], $row['license_shortname'], $row['license_fullname'], $row['spdx_id']);
124  if ($row['removing'] == 'f') {
125  $agentID = 1;
126  $agentName = "bulk addition";
127  } else {
128  $agentID = 2;
129  $agentName = "bulk removal";
130  }
131  $agentRef = new AgentRef($agentID, $agentName, "empty");
132  $matches[] = new LicenseMatch(intval($row['file_id']), $licenseRef, $agentRef, intval($row['license_file_id']));
133  }
134 
135  $this->dbManager->freeResult($result);
136  return $matches;
137  }
138 
142  public function getLicenseRefs($search = null, $orderAscending = true)
143  {
144  if (isset($_SESSION) && array_key_exists('GroupId', $_SESSION)) {
145  $rfTable = 'license_all';
146  $options = array('columns' => array('rf_pk', 'rf_shortname', 'rf_fullname'), 'candidatePrefix' => $this->candidatePrefix);
147  $licenseViewDao = new LicenseViewProxy($_SESSION['GroupId'], $options, $rfTable);
148  $withCte = $licenseViewDao->asCTE();
149  } else {
150  $withCte = '';
151  $rfTable = 'ONLY license_ref';
152  }
153 
154  $searchCondition = $search ? "WHERE rf_shortname ilike $1" : "";
155 
156  $order = $orderAscending ? "ASC" : "DESC";
157  $statementName = __METHOD__ . ($search ? ".search_" . $search : "") . ".order_$order";
158 
159  $this->dbManager->prepare($statementName,
160  $sql = $withCte . " select rf_pk,rf_shortname,rf_spdx_id,rf_fullname from $rfTable $searchCondition order by LOWER(rf_shortname) $order");
161  $result = $this->dbManager->execute($statementName, $search ? array('%' . strtolower($search) . '%') : array());
162  $licenseRefs = array();
163  while ($row = $this->dbManager->fetchArray($result)) {
164  $licenseRefs[] = new LicenseRef(intval($row['rf_pk']), $row['rf_shortname'], $row['rf_fullname'], $row['rf_spdx_id']);
165  }
166  $this->dbManager->freeResult($result);
167  return $licenseRefs;
168  }
169 
170 
174  public function getConclusionLicenseRefs($groupId, $search = null, $orderAscending = true, $exclude=array())
175  {
176  $rfTable = 'license_all';
177  $options = array('columns' => array('rf_pk', 'rf_shortname', 'rf_fullname', 'rf_active', 'rf_spdx_id'),
178  'candidatePrefix' => $this->candidatePrefix);
179  $licenseViewDao = new LicenseViewProxy($groupId, $options, $rfTable);
180  $order = $orderAscending ? "ASC" : "DESC";
181  $statementName = __METHOD__ . ".order_$order";
182  $param = array();
183  /* exclude license with parent, excluded child or selfexcluded */
184  $sql = $licenseViewDao->asCTE()." SELECT rf_pk,rf_shortname,rf_spdx_id,rf_fullname FROM $rfTable
185  WHERE rf_active = 'yes' AND NOT EXISTS (select * from license_map WHERE rf_pk=rf_fk AND rf_fk!=rf_parent)";
186  if ($search) {
187  $param[] = '%' . $search . '%';
188  $statementName .= '.search';
189  $sql .= " AND rf_shortname ilike $1";
190  }
191  if (count($exclude)>0) {
192  // $param[] = $exclude;
193  $tuple = implode(',', $exclude);
194  $statementName .= '.exclude'.$tuple;
195  $sql .= " AND NOT EXISTS (select * from license_map WHERE rf_pk=rf_parent AND rf_fk IN ($tuple))
196  AND rf_pk NOT IN($tuple)";
197  }
198  $this->dbManager->prepare($statementName, "$sql ORDER BY LOWER(rf_shortname) $order");
199  $result = $this->dbManager->execute($statementName, $param);
200  $licenseRefs = array();
201  while ($row = $this->dbManager->fetchArray($result)) {
202  $licenseRefs[] = new LicenseRef(intval($row['rf_pk']), $row['rf_shortname'], $row['rf_fullname'], $row['rf_spdx_id']);
203  }
204  $this->dbManager->freeResult($result);
205  return $licenseRefs;
206  }
207 
208 
212  public function getLicenseArray($groupId = null)
213  {
214  $statementName = __METHOD__;
215  $rfTable = 'license_all';
216  $options = array('columns' => array('rf_pk', 'rf_shortname', 'rf_fullname', 'rf_active'), 'candidatePrefix' => $this->candidatePrefix);
217  if ($groupId === null) {
218  $groupId = (isset($_SESSION) && array_key_exists('GroupId', $_SESSION)) ? $_SESSION['GroupId'] : 0;
219  }
220  $licenseViewDao = new LicenseViewProxy($groupId, $options, $rfTable);
221  $withCte = $licenseViewDao->asCTE();
222 
223  $this->dbManager->prepare($statementName,
224  $withCte . " select rf_pk id,rf_shortname shortname,rf_fullname fullname from $rfTable WHERE rf_active = 'yes' ORDER BY LOWER(rf_shortname)");
225  $result = $this->dbManager->execute($statementName);
226  $licenseRefs = $this->dbManager->fetchAll($result);
227  $this->dbManager->freeResult($result);
228  return $licenseRefs;
229  }
230 
231 
239  public function getLicenseIdPerPfileForAgentId(ItemTreeBounds $itemTreeBounds, $selectedAgentId, $includeSubfolders=true, $nameRange=array())
240  {
241  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
242  $statementName = __METHOD__ . '.' . $uploadTreeTableName;
243  $param = array($selectedAgentId);
244 
245  if ($includeSubfolders) {
246  $param[] = $itemTreeBounds->getLeft();
247  $param[] = $itemTreeBounds->getRight();
248  $condition = "lft BETWEEN $2 AND $3";
249  $statementName .= ".subfolders";
250  if (!empty($nameRange)) {
251  $condition .= " AND ufile_name BETWEEN $4 and $5";
252  $param[] = $nameRange[0];
253  $param[] = $nameRange[1];
254  $statementName .= ".nameRange";
255  }
256  } else {
257  $param[] = $itemTreeBounds->getItemId();
258  $condition = "realparent = $2";
259  }
260 
261  if ('uploadtree_a' == $uploadTreeTableName) {
262  $param[] = $itemTreeBounds->getUploadId();
263  $condition .= " AND utree.upload_fk=$".count($param);
264  }
265 
266  $sql = "SELECT utree.pfile_fk as pfile_id,
267  license_ref.rf_pk as license_id,
268  rf_match_pct as match_percentage,
269  CAST($1 AS INT) AS agent_id,
270  uploadtree_pk
271  FROM license_file, license_ref, $uploadTreeTableName utree
272  WHERE agent_fk = $1
273  AND license_file.rf_fk = license_ref.rf_pk
274  AND license_file.pfile_fk = utree.pfile_fk
275  AND $condition
276  ORDER BY match_percentage ASC";
277 
278  $this->dbManager->prepare($statementName, $sql);
279  $result = $this->dbManager->execute($statementName, $param);
280  $licensesPerFileId = array();
281  while ($row = $this->dbManager->fetchArray($result)) {
282  $licensesPerFileId[$row['pfile_id']][$row['license_id']] = $row;
283  }
284 
285  $this->dbManager->freeResult($result);
286  return $licensesPerFileId;
287  }
288 
298  public function getLicensesPerFileNameForAgentId(ItemTreeBounds $itemTreeBounds,
299  $selectedAgentIds=null,
300  $includeSubfolders=true,
301  $excluding='',
302  $ignore=false,
303  &$clearingDecisionsForLicList = array(),
304  $includeTreeId=false)
305  {
306  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
307  $statementName = __METHOD__ . '.' . $uploadTreeTableName;
308  $param = array();
309 
310  $condition = " (ufile_mode & (1<<28)) = 0";
311  if ($includeSubfolders) {
312  $param[] = $itemTreeBounds->getLeft();
313  $param[] = $itemTreeBounds->getRight();
314  $condition .= " AND lft BETWEEN $1 AND $2";
315  $statementName .= ".subfolders";
316  } else {
317  $param[] = $itemTreeBounds->getItemId();
318  $condition .= " AND realparent = $1";
319  }
320 
321  if ('uploadtree_a' == $uploadTreeTableName) {
322  $param[] = $itemTreeBounds->getUploadId();
323  $condition .= " AND upload_fk=$".count($param);
324  }
325 
326  $agentSelect = "";
327  if ($selectedAgentIds !== null) {
328  $statementName .= ".".count($selectedAgentIds)."agents";
329  $agentSelect = "WHERE agent_fk IS NULL";
330  foreach ($selectedAgentIds as $selectedAgentId) {
331  $param[] = $selectedAgentId;
332  $agentSelect .= " OR agent_fk = $".count($param);
333  }
334  }
335 
336  $sql = "
337 SELECT uploadtree_pk, ufile_name, lft, rgt, ufile_mode,
338  rf_shortname, agent_fk
339 FROM (SELECT
340  uploadtree_pk, ufile_name,
341  lft, rgt, ufile_mode, pfile_fk
342  FROM $uploadTreeTableName
343  WHERE $condition) AS subselect1
344 LEFT JOIN (SELECT rf_shortname,pfile_fk,agent_fk
345  FROM license_file, license_ref
346  WHERE rf_fk = rf_pk) AS subselect2
347  ON subselect1.pfile_fk = subselect2.pfile_fk
348 $agentSelect
349 ORDER BY lft asc
350 ";
351 
352  $this->dbManager->prepare($statementName, $sql);
353  $result = $this->dbManager->execute($statementName, $param);
354  $licensesPerFileName = array();
355 
356  $row = $this->dbManager->fetchArray($result);
357  $pathStack = array($row['ufile_name']);
358  $rgtStack = array($row['rgt']);
359  $lastLft = $row['lft'];
360  $path = implode('/', $pathStack);
361  $this->addToLicensesPerFileName($licensesPerFileName, $path, $row,
362  $ignore, $clearingDecisionsForLicList, $includeTreeId);
363  while ($row = $this->dbManager->fetchArray($result)) {
364  if (!empty($excluding) && false!==strpos("/$row[ufile_name]/", $excluding)) {
365  $lastLft = $row['rgt'] + 1;
366  continue;
367  }
368  if ($row['lft'] < $lastLft) {
369  continue;
370  }
371 
372  $this->updateStackState($pathStack, $rgtStack, $lastLft, $row);
373  $path = implode('/', $pathStack);
374  $this->addToLicensesPerFileName($licensesPerFileName, $path, $row,
375  $ignore, $clearingDecisionsForLicList, $includeTreeId);
376  }
377  $this->dbManager->freeResult($result);
378  return array_reverse($licensesPerFileName);
379  }
380 
381  private function updateStackState(&$pathStack, &$rgtStack, &$lastLft, $row)
382  {
383  if ($row['lft'] >= $lastLft) {
384  while (count($rgtStack) > 0 && $row['lft'] > $rgtStack[count($rgtStack)-1]) {
385  array_pop($pathStack);
386  array_pop($rgtStack);
387  }
388  if ($row['lft'] > $lastLft) {
389  $pathStack[] = $row['ufile_name'];
390  $rgtStack[] = $row['rgt'];
391  $lastLft = $row['lft'];
392  }
393  }
394  }
395 
396  private function addToLicensesPerFileName(&$licensesPerFileName, $path, $row,
397  $ignore,
398  &$clearingDecisionsForLicList = array(),
399  $includeTreeId=false)
400  {
401  if (($row['ufile_mode'] & (1 << 29)) == 0) {
402  if ($row['rf_shortname']) {
403  $licensesPerFileName[$path]['scanResults'][] = $row['rf_shortname'];
404  if (array_key_exists($row['uploadtree_pk'], $clearingDecisionsForLicList)) {
405  $licensesPerFileName[$path]['concludedResults'][] = $clearingDecisionsForLicList[$row['uploadtree_pk']];
406  }
407  }
408  } else if (!$ignore) {
409  $licensesPerFileName[$path] = false;
410  }
411  if ($includeTreeId) {
412  $licensesPerFileName[$path]['uploadtree_pk'][] = $row['uploadtree_pk'];
413  }
414  }
415 
421  public function getLicenseHistogram(ItemTreeBounds $itemTreeBounds, $agentId=null)
422  {
423  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
424  $agentText = $agentId ? (is_array($agentId) ? implode(',', $agentId) : $agentId) : '-';
425  $statementName = __METHOD__ . '.' . $uploadTreeTableName . ".$agentText";
426  $param = array($itemTreeBounds->getUploadId(), $itemTreeBounds->getLeft(), $itemTreeBounds->getRight());
427  $sql = "SELECT rf_shortname AS license_shortname, rf_spdx_id AS spdx_id, rf_pk, count(*) AS count, count(distinct pfile_ref.pfile_fk) as \"unique\"
428  FROM ( SELECT license_ref.rf_shortname, license_ref.rf_spdx_id, license_ref.rf_pk, license_file.fl_pk, license_file.agent_fk, license_file.pfile_fk
429  FROM license_file
430  JOIN license_ref ON license_file.rf_fk = license_ref.rf_pk) AS pfile_ref
431  RIGHT JOIN $uploadTreeTableName UT ON pfile_ref.pfile_fk = UT.pfile_fk";
432  if (is_array($agentId)) {
433  $sql .= ' AND agent_fk=ANY($4)';
434  $param[] = '{' . implode(',', $agentId) . '}';
435  } elseif (!empty($agentId)) {
436  $sql .= ' AND agent_fk=$4';
437  $param[] = $agentId;
438  }
439  $sql .= " WHERE (rf_shortname IS NULL OR rf_shortname NOT IN ('Void')) AND upload_fk=$1
440  AND (UT.lft BETWEEN $2 AND $3) AND UT.ufile_mode&(3<<28)=0
441  GROUP BY license_shortname, spdx_id, rf_pk";
442  $this->dbManager->prepare($statementName, $sql);
443  $result = $this->dbManager->execute($statementName, $param);
444  $assocLicenseHist = array();
445  while ($row = $this->dbManager->fetchArray($result)) {
446  $shortname = empty($row['rf_pk']) ? self::NO_LICENSE_FOUND : $row['license_shortname'];
447  $assocLicenseHist[$shortname] = array(
448  'count' => intval($row['count']),
449  'unique' => intval($row['unique']),
450  'rf_pk' => intval($row['rf_pk']),
451  'spdx_id' => LicenseRef::convertToSpdxId($shortname, $row['spdx_id'])
452  );
453  }
454  $this->dbManager->freeResult($result);
455  return $assocLicenseHist;
456  }
457 
458  public function getLicenseShortnamesContained(ItemTreeBounds $itemTreeBounds, $latestSuccessfulAgentIds=null, $filterLicenses = array('VOID')) //'No_license_found',
459  {
460  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
461 
462  $noLicenseFoundStmt = empty($filterLicenses) ? "" : " AND rf_shortname NOT IN ("
463  . implode(", ", array_map(function ($name)
464  {
465  return "'" . $name . "'";
466  }, $filterLicenses)) . ")";
467 
468  $statementName = __METHOD__ . '.' . $uploadTreeTableName;
469 
470  $agentFilter = '';
471  if (is_array($latestSuccessfulAgentIds)) {
472  $agentIdSet = "{" . implode(',', $latestSuccessfulAgentIds) . "}";
473  $statementName .= ".$agentIdSet";
474  $agentFilter = " AND agent_fk=ANY('$agentIdSet')";
475  }
476 
477  $this->dbManager->prepare($statementName,
478  "SELECT license_ref.rf_shortname
479  FROM license_file JOIN license_ref ON license_file.rf_fk = license_ref.rf_pk
480  INNER JOIN $uploadTreeTableName uploadTree ON uploadTree.pfile_fk=license_file.pfile_fk
481  WHERE upload_fk=$1
482  AND lft BETWEEN $2 AND $3
483  $noLicenseFoundStmt $agentFilter
484  GROUP BY rf_shortname
485  ORDER BY rf_shortname ASC");
486  $result = $this->dbManager->execute($statementName,
487  array($itemTreeBounds->getUploadId(), $itemTreeBounds->getLeft(), $itemTreeBounds->getRight()));
488 
489  $licenses = array();
490  while ($row = $this->dbManager->fetchArray($result)) {
491  $licenses[] = $row['rf_shortname'];
492  }
493  $this->dbManager->freeResult($result);
494 
495  return $licenses;
496  }
497 
504  private function getLicenseByCondition($condition, $param, $groupId=null)
505  {
506  $extraCondition = "";
507  $row = $this->dbManager->getSingleRow(
508  "SELECT rf_pk, rf_shortname, rf_spdx_id, rf_fullname, rf_text, rf_url, rf_risk, rf_detector_type FROM ONLY license_ref WHERE $condition",
509  $param, __METHOD__ . ".$condition.only");
510  if (false === $row && isset($groupId)) {
511  $userId = (isset($_SESSION) && array_key_exists('UserId', $_SESSION)) ? $_SESSION['UserId'] : 0;
512  $statementName = __METHOD__ . ".$condition";
513  if (!empty($userId)) {
514  $param[] = $userId;
515  $extraCondition = "AND group_fk IN (SELECT group_fk FROM group_user_member WHERE user_fk=$".count($param).")";
516  $statementName .= ".userId";
517  }
518  if (is_int($groupId) && empty($userId)) {
519  $param[] = $groupId;
520  $extraCondition = "AND group_fk=$".count($param);
521  $statementName .= ".groupId";
522  }
523  $row = $this->dbManager->getSingleRow(
524  "SELECT rf_pk, rf_shortname, rf_spdx_id, rf_fullname, rf_text, rf_url, rf_risk, rf_detector_type FROM license_candidate WHERE $condition $extraCondition",
525  $param, $statementName);
526  }
527  if (false === $row) {
528  return null;
529  }
530  return new License(intval($row['rf_pk']), $row['rf_shortname'],
531  $row['rf_fullname'], $row['rf_risk'], $row['rf_text'], $row['rf_url'],
532  $row['rf_detector_type'], $row['rf_spdx_id']);
533  }
534 
540  public function getLicenseById($licenseId, $groupId=null)
541  {
542  return $this->getLicenseByCondition('rf_pk=$1', array($licenseId), $groupId);
543  }
544 
550  public function getLicenseByShortName($licenseShortname, $groupId=null)
551  {
552  return $this->getLicenseByCondition('rf_shortname=$1', array($licenseShortname), $groupId);
553  }
554 
560  public function getLicenseBySpdxId($licenseSpdxId, $groupId=null)
561  {
562  return $this->getLicenseByCondition('rf_spdx_id=$1', array($licenseSpdxId), $groupId);
563  }
564 
577  public function insertBulkLicense($userId, $groupId, $uploadTreeId, $licenseRemovals, $refText, $ignoreIrrelevant=true, $delimiters=null, $scanFindingsOnly=false)
578  {
579  if (strcasecmp($delimiters, "DEFAULT") === 0) {
580  $delimiters = null;
581  } elseif ($delimiters !== null) {
582  $delimiters = StringOperation::replaceUnicodeControlChar($delimiters);
583  }
584  $licenseRefBulkIdResult = $this->dbManager->getSingleRow(
585  "INSERT INTO license_ref_bulk (user_fk, group_fk, uploadtree_fk, rf_text, ignore_irrelevant, bulk_delimiters, scan_findings)
586  VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING lrb_pk",
587  array($userId, $groupId, $uploadTreeId,
589  $this->dbManager->booleanToDb($ignoreIrrelevant),
590  $delimiters,
591  $this->dbManager->booleanToDb($scanFindingsOnly)),
592  __METHOD__ . '.getLrb'
593  );
594  if ($licenseRefBulkIdResult === false) {
595  return -1;
596  }
597  $bulkId = $licenseRefBulkIdResult['lrb_pk'];
598 
599  $stmt = __METHOD__ . '.insertAction';
600  $this->dbManager->prepare($stmt, "INSERT INTO license_set_bulk (lrb_fk, rf_fk, removing, comment, reportinfo, acknowledgement) VALUES ($1,$2,$3,$4,$5,$6)");
601  foreach ($licenseRemovals as $licenseId=>$removing) {
602  $this->dbManager->execute($stmt, array($bulkId, $licenseId,
603  $this->dbManager->booleanToDb($removing[0]),
607  }
608 
609  return $bulkId ;
610  }
611 
617  public function isNewLicense($newShortname, $groupId)
618  {
619  $licenceViewDao = new LicenseViewProxy($groupId, array('columns' => array('rf_shortname')));
620  $sql = 'SELECT count(*) cnt FROM (' . $licenceViewDao->getDbViewQuery() . ') AS license_all WHERE rf_shortname=$1';
621  $duplicatedRef = $this->dbManager->getSingleRow($sql, array($newShortname), __METHOD__.".$groupId" );
622  return $duplicatedRef['cnt'] == 0;
623  }
624 
631  public function insertLicense($shortname, $refText, $spdxId = null)
632  {
633  $row = $this->dbManager->getSingleRow(
634  "INSERT INTO license_ref (rf_shortname, rf_text, rf_detector_type, rf_spdx_id) VALUES ($1, $2, 2, $3) RETURNING rf_pk",
638  __METHOD__.".addLicense" );
639  return $row["rf_pk"];
640  }
641 
647  public function insertUploadLicense($newShortname, $refText, $groupId, $userId)
648  {
649  $sql = 'INSERT INTO license_candidate (group_fk,rf_shortname,rf_fullname,rf_text,rf_md5,rf_detector_type,rf_user_fk_created) VALUES ($1,$2,$2,$3,md5($3),1,$4) RETURNING rf_pk';
650  $refArray = $this->dbManager->getSingleRow($sql, array($groupId,
652  StringOperation::replaceUnicodeControlChar($refText), $userId), __METHOD__);
653  return $refArray['rf_pk'];
654  }
655 
660  public function getLicenseCount()
661  {
662  $licenseRefTable = $this->dbManager->getSingleRow("SELECT COUNT(*) cnt FROM license_ref WHERE rf_text!=$1", array("License by Nomos."));
663  return intval($licenseRefTable['cnt']);
664  }
665 
675  public function updateCandidate($rf_pk, $shortname, $fullname, $rfText, $url,
676  $rfNotes, $lastmodified, $userIdmodified,
677  $readyformerge, $riskLvl, $spdxId = null)
678  {
679  $marydone = $this->dbManager->booleanToDb($readyformerge);
680  $sql = 'UPDATE license_candidate SET ' .
681  'rf_shortname=$2, rf_fullname=$3, rf_text=$4, rf_url=$5, rf_notes=$6, ' .
682  'rf_lastmodified=$7, rf_user_fk_modified=$8, marydone=$9, rf_risk=$10';
683  $params = array($rf_pk, StringOperation::replaceUnicodeControlChar($shortname),
686  StringOperation::replaceUnicodeControlChar($rfNotes), $lastmodified,
687  $userIdmodified, $marydone, $riskLvl);
688  $statement = __METHOD__;
689  if ($spdxId != null) {
690  $params[] = StringOperation::replaceUnicodeControlChar($spdxId);
691  $sql .= ', rf_spdx_id=$' . count($params);
692  $statement .= ".spdxid";
693  }
694  $sql .= ' WHERE rf_pk=$1';
695  $this->dbManager->getSingleRow($sql, $params, $statement);
696  }
697 
702  public function getLicenseParentById($licenseId, $groupId=null)
703  {
704  return $this->getLicenseByCondition(" rf_pk=(SELECT rf_parent FROM license_map WHERE usage=$1 AND rf_fk=$2 AND rf_fk!=rf_parent)",
705  array(LicenseMap::CONCLUSION,$licenseId), $groupId);
706  }
707 
714  public function getLicenseObligations($licenseLists, $candidate = false)
715  {
716  if (!empty($licenseLists)) {
717  $sql = "";
718  $params = array();
719  $params[] = '{' . implode(',', $licenseLists) . '}';
720  if ($candidate) {
721  $tableName='obligation_candidate_map';
722  $sql = "SELECT ob_pk, ob_topic, ob_text, ob_active, rf_fk, " .
723  "ob_type, ob_classification, ob_comment, " .
724  "rf_shortname, rf_spdx_id FROM obligation_ref " .
725  "JOIN $tableName ON $tableName.ob_fk = obligation_ref.ob_pk " .
726  "JOIN license_ref ON $tableName.rf_fk = license_ref.rf_pk " .
727  "WHERE ob_active='t' AND rf_fk = ANY($1::int[]);";
728  } else {
729  $tableName='obligation_map';
730  $conclusionmapCte = LicenseMap::getMappedLicenseRefView('$2');
731  $sql = "WITH conclusionmap AS (" . $conclusionmapCte . ") " .
732  "SELECT ob_pk, ob_topic, ob_text, ob_active, rf_origin AS rf_fk, " .
733  "ob_type, ob_classification, ob_comment, " .
734  "lr.rf_shortname, lr.rf_spdx_id FROM obligation_ref " .
735  "JOIN $tableName ON $tableName.ob_fk = obligation_ref.ob_pk " .
736  "JOIN conclusionmap ON $tableName.rf_fk = conclusionmap.rf_pk " .
737  "INNER JOIN license_ref lr ON conclusionmap.rf_origin = lr.rf_pk " .
738  "WHERE ob_active='t' AND rf_origin = ANY($1::int[]);";
739  $params[] = LicenseMap::CONCLUSION;
740  }
741  $statementName = __METHOD__.$tableName;
742  $this->dbManager->prepare($statementName, $sql);
743  $result = $this->dbManager->execute($statementName, $params);
744  $ObligationRef = $this->dbManager->fetchAll($result);
745  $this->dbManager->freeResult($result);
746  return $ObligationRef;
747  }
748  }
749 }
Wrapper class for license map.
Definition: LicenseMap.php:19
static getMappedLicenseRefView($usageExpr=' $1')
Query to get license map view along with license ref.
Definition: LicenseMap.php:191
getLicenseByCondition($condition, $param, $groupId=null)
Definition: LicenseDao.php:504
getLicenseBySpdxId($licenseSpdxId, $groupId=null)
Definition: LicenseDao.php:560
insertLicense($shortname, $refText, $spdxId=null)
Definition: LicenseDao.php:631
getLicenseHistogram(ItemTreeBounds $itemTreeBounds, $agentId=null)
Definition: LicenseDao.php:421
getLicenseByShortName($licenseShortname, $groupId=null)
Definition: LicenseDao.php:550
getBulkFileLicenseMatches(ItemTreeBounds $itemTreeBounds)
get all the tried bulk recognitions for a single file or uploadtree (currently unused)
Definition: LicenseDao.php:97
getAgentFileLicenseMatches(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
get all the licenses for a single file or uploadtree
Definition: LicenseDao.php:47
getLicenseRefs($search=null, $orderAscending=true)
Definition: LicenseDao.php:142
insertUploadLicense($newShortname, $refText, $groupId, $userId)
Definition: LicenseDao.php:647
getLicenseParentById($licenseId, $groupId=null)
Definition: LicenseDao.php:702
getLicenseArray($groupId=null)
Definition: LicenseDao.php:212
insertBulkLicense($userId, $groupId, $uploadTreeId, $licenseRemovals, $refText, $ignoreIrrelevant=true, $delimiters=null, $scanFindingsOnly=false)
Definition: LicenseDao.php:577
getConclusionLicenseRefs($groupId, $search=null, $orderAscending=true, $exclude=array())
Definition: LicenseDao.php:174
getLicensesPerFileNameForAgentId(ItemTreeBounds $itemTreeBounds, $selectedAgentIds=null, $includeSubfolders=true, $excluding='', $ignore=false, &$clearingDecisionsForLicList=array(), $includeTreeId=false)
Definition: LicenseDao.php:298
getLicenseIdPerPfileForAgentId(ItemTreeBounds $itemTreeBounds, $selectedAgentId, $includeSubfolders=true, $nameRange=array())
Definition: LicenseDao.php:239
isNewLicense($newShortname, $groupId)
Definition: LicenseDao.php:617
getLicenseObligations($licenseLists, $candidate=false)
Definition: LicenseDao.php:714
getLicenseById($licenseId, $groupId=null)
Definition: LicenseDao.php:540
updateCandidate($rf_pk, $shortname, $fullname, $rfText, $url, $rfNotes, $lastmodified, $userIdmodified, $readyformerge, $riskLvl, $spdxId=null)
Definition: LicenseDao.php:675
static convertToSpdxId($shortname, $spdxId)
Given a license's shortname and spdx id, give out spdx id to use in reports.
Definition: LicenseRef.php:106
static replaceUnicodeControlChar($input, $replace="")
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16