FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseDao.php
1 <?php
2 /*
3 Copyright (C) 2014-2018, Siemens AG
4 Author: Andreas Würl
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\Dao;
21 
30 use Monolog\Logger;
31 
33 {
34  const NO_LICENSE_FOUND = 'No_license_found';
35 
37  private $dbManager;
39  private $logger;
41  private $candidatePrefix = '*';
42 
43  function __construct(DbManager $dbManager)
44  {
45  $this->dbManager = $dbManager;
46  $this->logger = new Logger(self::class);
47  }
48 
56  function getAgentFileLicenseMatches(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
57  {
58  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
59  $statementName = __METHOD__ . ".$uploadTreeTableName.$usageId";
60  $params = array($itemTreeBounds->getUploadId(), $itemTreeBounds->getLeft(), $itemTreeBounds->getRight());
61  if ($usageId==LicenseMap::TRIVIAL) {
62  $licenseJoin = "ONLY license_ref mlr ON license_file.rf_fk = mlr.rf_pk";
63  } else {
64  $params[] = $usageId;
65  $licenseMapCte = LicenseMap::getMappedLicenseRefView('$4');
66  $licenseJoin = "($licenseMapCte) AS mlr ON license_file.rf_fk = mlr.rf_origin";
67  }
68 
69  $this->dbManager->prepare($statementName,
70  "SELECT LFR.rf_shortname AS license_shortname,
71  LFR.rf_fullname AS license_fullname,
72  LFR.rf_pk AS license_id,
73  LFR.fl_pk AS license_file_id,
74  LFR.pfile_fk as file_id,
75  LFR.rf_match_pct AS percent_match,
76  AG.agent_name AS agent_name,
77  AG.agent_pk AS agent_id,
78  AG.agent_rev AS agent_revision
79  FROM ( SELECT mlr.rf_fullname, mlr.rf_shortname, mlr.rf_pk, license_file.fl_pk, license_file.agent_fk, license_file.pfile_fk, license_file.rf_match_pct
80  FROM license_file JOIN $licenseJoin) as LFR
81  INNER JOIN $uploadTreeTableName as UT ON UT.pfile_fk = LFR.pfile_fk
82  INNER JOIN agent as AG ON AG.agent_pk = LFR.agent_fk
83  WHERE AG.agent_enabled='true' and
84  UT.upload_fk=$1 AND UT.lft BETWEEN $2 and $3
85  ORDER BY license_shortname ASC, percent_match DESC");
86  $result = $this->dbManager->execute($statementName, $params);
87  $matches = array();
88  while ($row = $this->dbManager->fetchArray($result)) {
89  $licenseRef = new LicenseRef(intval($row['license_id']), $row['license_shortname'], $row['license_fullname']);
90  $agentRef = new AgentRef(intval($row['agent_id']), $row['agent_name'], $row['agent_revision']);
91  $matches[] = new LicenseMatch(intval($row['file_id']), $licenseRef, $agentRef, intval($row['license_file_id']), intval($row['percent_match']));
92  }
93 
94  $this->dbManager->freeResult($result);
95  return $matches;
96  }
97 
98 
105  function getBulkFileLicenseMatches(ItemTreeBounds $itemTreeBounds)
106  {
107  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
108  $statementName = __METHOD__ . ".$uploadTreeTableName";
109 
110  $this->dbManager->prepare($statementName,
111  "SELECT LF.rf_shortname AS license_shortname,
112  LF.rf_fullname AS license_fullname,
113  LF.rf_pk AS license_id,
114  LFB.lrb_pk AS license_file_id,
115  LFB.removing AS removing,
116  UT.pfile_fk as file_id
117  FROM license_ref_bulk as LFB
118  INNER JOIN license_ref as LF on LF.rf_pk = LFB.rf_fk
119  INNER JOIN $uploadTreeTableName as UT ON UT.uploadtree_pk = LFB.uploadtree_fk
120  WHERE UT.upload_fk=$1 AND UT.lft BETWEEN $2 and $3
121  ORDER BY license_file_id ASC");
122 
123  $result = $this->dbManager->execute($statementName,
124  array($itemTreeBounds->getUploadId(), $itemTreeBounds->getLeft(), $itemTreeBounds->getRight()));
125 
126  $matches = array();
127 
128  while ($row = $this->dbManager->fetchArray($result)) {
129  $licenseRef = new LicenseRef($row['license_id'], $row['license_shortname'], $row['license_fullname']);
130  if ($row['removing'] == 'f') {
131  $agentID = 1;
132  $agentName = "bulk addition";
133  } else {
134  $agentID = 2;
135  $agentName = "bulk removal";
136  }
137  $agentRef = new AgentRef($agentID, $agentName, "empty");
138  $matches[] = new LicenseMatch(intval($row['file_id']), $licenseRef, $agentRef, intval($row['license_file_id']));
139  }
140 
141  $this->dbManager->freeResult($result);
142  return $matches;
143  }
144 
148  public function getLicenseRefs($search = null, $orderAscending = true)
149  {
150  if (isset($_SESSION) && array_key_exists('GroupId', $_SESSION)) {
151  $rfTable = 'license_all';
152  $options = array('columns' => array('rf_pk', 'rf_shortname', 'rf_fullname'), 'candidatePrefix' => $this->candidatePrefix);
153  $licenseViewDao = new LicenseViewProxy($_SESSION['GroupId'], $options, $rfTable);
154  $withCte = $licenseViewDao->asCTE();
155  } else {
156  $withCte = '';
157  $rfTable = 'ONLY license_ref';
158  }
159 
160  $searchCondition = $search ? "WHERE rf_shortname ilike $1" : "";
161 
162  $order = $orderAscending ? "ASC" : "DESC";
163  $statementName = __METHOD__ . ($search ? ".search_" . $search : "") . ".order_$order";
164 
165  $this->dbManager->prepare($statementName,
166  $sql = $withCte . " select rf_pk,rf_shortname,rf_fullname from $rfTable $searchCondition order by LOWER(rf_shortname) $order");
167  $result = $this->dbManager->execute($statementName, $search ? array('%' . strtolower($search) . '%') : array());
168  $licenseRefs = array();
169  while ($row = $this->dbManager->fetchArray($result)) {
170  $licenseRefs[] = new LicenseRef(intval($row['rf_pk']), $row['rf_shortname'], $row['rf_fullname']);
171  }
172  $this->dbManager->freeResult($result);
173  return $licenseRefs;
174  }
175 
176 
180  public function getConclusionLicenseRefs($groupId, $search = null, $orderAscending = true, $exclude=array())
181  {
182  $rfTable = 'license_all';
183  $options = array('columns' => array('rf_pk', 'rf_shortname', 'rf_fullname'),
184  'candidatePrefix' => $this->candidatePrefix);
185  $licenseViewDao = new LicenseViewProxy($groupId, $options, $rfTable);
186  $order = $orderAscending ? "ASC" : "DESC";
187  $statementName = __METHOD__ . ".order_$order";
188  $param = array();
189  /* exclude license with parent, excluded child or selfexcluded */
190  $sql = $licenseViewDao->asCTE()." SELECT rf_pk,rf_shortname,rf_fullname FROM $rfTable
191  WHERE NOT EXISTS (select * from license_map WHERE rf_pk=rf_fk AND rf_fk!=rf_parent)";
192  if ($search) {
193  $param[] = '%' . $search . '%';
194  $statementName .= '.search';
195  $sql .= " AND rf_shortname ilike $1";
196  }
197  if (count($exclude)>0) {
198  // $param[] = $exclude;
199  $tuple = implode(',', $exclude);
200  $statementName .= '.exclude'.$tuple;
201  $sql .= " AND NOT EXISTS (select * from license_map WHERE rf_pk=rf_parent AND rf_fk IN ($tuple))
202  AND rf_pk NOT IN($tuple)";
203  }
204  $this->dbManager->prepare($statementName, "$sql ORDER BY LOWER(rf_shortname) $order");
205  $result = $this->dbManager->execute($statementName, $param);
206  $licenseRefs = array();
207  while ($row = $this->dbManager->fetchArray($result)) {
208  $licenseRefs[] = new LicenseRef(intval($row['rf_pk']), $row['rf_shortname'], $row['rf_fullname']);
209  }
210  $this->dbManager->freeResult($result);
211  return $licenseRefs;
212  }
213 
214 
218  public function getLicenseArray($groupId = null)
219  {
220  $statementName = __METHOD__;
221  $rfTable = 'license_all';
222  $options = array('columns' => array('rf_pk', 'rf_shortname', 'rf_fullname'), 'candidatePrefix' => $this->candidatePrefix);
223  if ($groupId === null) {
224  $groupId = (isset($_SESSION) && array_key_exists('GroupId', $_SESSION)) ? $_SESSION['GroupId'] : 0;
225  }
226  $licenseViewDao = new LicenseViewProxy($groupId, $options, $rfTable);
227  $withCte = $licenseViewDao->asCTE();
228 
229  $this->dbManager->prepare($statementName,
230  $withCte . " select rf_pk id,rf_shortname shortname,rf_fullname fullname from $rfTable order by LOWER(rf_shortname)");
231  $result = $this->dbManager->execute($statementName);
232  $licenseRefs = $this->dbManager->fetchAll($result);
233  $this->dbManager->freeResult($result);
234  return $licenseRefs;
235  }
236 
237 
244  public function getLicenseIdPerPfileForAgentId(ItemTreeBounds $itemTreeBounds, $selectedAgentId, $includeSubfolders=true, $nameRange=array())
245  {
246  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
247  $statementName = __METHOD__ . '.' . $uploadTreeTableName;
248  $param = array($selectedAgentId);
249 
250  if ($includeSubfolders) {
251  $param[] = $itemTreeBounds->getLeft();
252  $param[] = $itemTreeBounds->getRight();
253  $condition = "lft BETWEEN $2 AND $3";
254  $statementName .= ".subfolders";
255  if (!empty($nameRange)) {
256  $condition .= " AND ufile_name BETWEEN $4 and $5";
257  $param[] = $nameRange[0];
258  $param[] = $nameRange[1];
259  $statementName .= ".nameRange";
260  }
261  } else {
262  $param[] = $itemTreeBounds->getItemId();
263  $condition = "realparent = $2";
264  }
265 
266  if ('uploadtree_a' == $uploadTreeTableName) {
267  $param[] = $itemTreeBounds->getUploadId();
268  $condition .= " AND utree.upload_fk=$".count($param);
269  }
270 
271  $sql = "SELECT utree.pfile_fk as pfile_id,
272  license_ref.rf_pk as license_id,
273  rf_match_pct as match_percentage,
274  CAST($1 AS INT) AS agent_id,
275  uploadtree_pk
276  FROM license_file, license_ref, $uploadTreeTableName utree
277  WHERE agent_fk = $1
278  AND license_file.rf_fk = license_ref.rf_pk
279  AND license_file.pfile_fk = utree.pfile_fk
280  AND $condition
281  ORDER BY match_percentage ASC";
282 
283  $this->dbManager->prepare($statementName, $sql);
284  $result = $this->dbManager->execute($statementName, $param);
285  $licensesPerFileId = array();
286  while ($row = $this->dbManager->fetchArray($result)) {
287  $licensesPerFileId[$row['pfile_id']][$row['license_id']] = $row;
288  }
289 
290  $this->dbManager->freeResult($result);
291  return $licensesPerFileId;
292  }
293 
302  public function getLicensesPerFileNameForAgentId(ItemTreeBounds $itemTreeBounds,
303  $selectedAgentIds=null,
304  $includeSubfolders=true,
305  $excluding='',
306  $ignore=false,
307  &$clearingDecisionsForLicList = array())
308  {
309  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
310  $statementName = __METHOD__ . '.' . $uploadTreeTableName;
311  $param = array();
312 
313  $condition = " (ufile_mode & (1<<28)) = 0";
314  if ($includeSubfolders) {
315  $param[] = $itemTreeBounds->getLeft();
316  $param[] = $itemTreeBounds->getRight();
317  $condition .= " AND lft BETWEEN $1 AND $2";
318  $statementName .= ".subfolders";
319  } else {
320  $param[] = $itemTreeBounds->getItemId();
321  $condition .= " AND realparent = $1";
322  }
323 
324  if ('uploadtree_a' == $uploadTreeTableName) {
325  $param[] = $itemTreeBounds->getUploadId();
326  $condition .= " AND upload_fk=$".count($param);
327  }
328 
329  $agentSelect = "";
330  if ($selectedAgentIds !== null) {
331  $statementName .= ".".count($selectedAgentIds)."agents";
332  $agentSelect = "WHERE agent_fk IS NULL";
333  foreach ($selectedAgentIds as $selectedAgentId) {
334  $param[] = $selectedAgentId;
335  $agentSelect .= " OR agent_fk = $".count($param);
336  }
337  }
338 
339  $sql = "
340 SELECT uploadtree_pk, ufile_name, lft, rgt, ufile_mode,
341  rf_shortname, agent_fk
342 FROM (SELECT
343  uploadtree_pk, ufile_name,
344  lft, rgt, ufile_mode, pfile_fk
345  FROM $uploadTreeTableName
346  WHERE $condition) AS subselect1
347 LEFT JOIN (SELECT rf_shortname,pfile_fk,agent_fk
348  FROM license_file, license_ref
349  WHERE rf_fk = rf_pk) AS subselect2
350  ON subselect1.pfile_fk = subselect2.pfile_fk
351 $agentSelect
352 ORDER BY lft asc
353 ";
354 
355  $this->dbManager->prepare($statementName, $sql);
356  $result = $this->dbManager->execute($statementName, $param);
357  $licensesPerFileName = array();
358 
359  $row = $this->dbManager->fetchArray($result);
360  $pathStack = array($row['ufile_name']);
361  $rgtStack = array($row['rgt']);
362  $lastLft = $row['lft'];
363  $path = implode($pathStack,'/');
364  $this->addToLicensesPerFileName($licensesPerFileName, $path, $row, $ignore, $clearingDecisionsForLicList);
365  while ($row = $this->dbManager->fetchArray($result)) {
366  if (!empty($excluding) && false!==strpos("/$row[ufile_name]/", $excluding)) {
367  $lastLft = $row['rgt'] + 1;
368  continue;
369  }
370  if ($row['lft'] < $lastLft) {
371  continue;
372  }
373 
374  $this->updateStackState($pathStack, $rgtStack, $lastLft, $row);
375  $path = implode($pathStack,'/');
376  $this->addToLicensesPerFileName($licensesPerFileName, $path, $row, $ignore, $clearingDecisionsForLicList);
377  }
378  $this->dbManager->freeResult($result);
379  return array_reverse($licensesPerFileName);
380  }
381 
382  private function updateStackState(&$pathStack, &$rgtStack, &$lastLft, $row)
383  {
384  if ($row['lft'] >= $lastLft) {
385  while (count($rgtStack) > 0 && $row['lft'] > $rgtStack[count($rgtStack)-1]) {
386  array_pop($pathStack);
387  array_pop($rgtStack);
388  }
389  if ($row['lft'] > $lastLft) {
390  array_push($pathStack, $row['ufile_name']);
391  array_push($rgtStack, $row['rgt']);
392  $lastLft = $row['lft'];
393  }
394  }
395  }
396 
397  private function addToLicensesPerFileName(&$licensesPerFileName, $path, $row, $ignore, &$clearingDecisionsForLicList = array())
398  {
399  if (($row['ufile_mode'] & (1 << 29)) == 0) {
400  if ($row['rf_shortname']) {
401  $licensesPerFileName[$path]['scanResults'][] = $row['rf_shortname'];
402  if (array_key_exists($row['uploadtree_pk'], $clearingDecisionsForLicList)) {
403  $licensesPerFileName[$path]['concludedResults'][] = $clearingDecisionsForLicList[$row['uploadtree_pk']];
404  }
405  }
406  } else if (!$ignore) {
407  $licensesPerFileName[$path] = false;
408  }
409  }
410 
416  public function getLicenseHistogram(ItemTreeBounds $itemTreeBounds, $agentId=null)
417  {
418  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
419  $agentText = $agentId ? (is_array($agentId) ? implode(',', $agentId) : $agentId) : '-';
420  $statementName = __METHOD__ . '.' . $uploadTreeTableName . ".$agentText";
421  $param = array($itemTreeBounds->getUploadId(), $itemTreeBounds->getLeft(), $itemTreeBounds->getRight());
422  $sql = "SELECT rf_shortname AS license_shortname, rf_pk, count(*) AS count, count(distinct pfile_ref.pfile_fk) as unique
423  FROM ( SELECT license_ref.rf_shortname, license_ref.rf_pk, license_file.fl_pk, license_file.agent_fk, license_file.pfile_fk
424  FROM license_file
425  JOIN license_ref ON license_file.rf_fk = license_ref.rf_pk) AS pfile_ref
426  RIGHT JOIN $uploadTreeTableName UT ON pfile_ref.pfile_fk = UT.pfile_fk";
427  if (is_array($agentId)) {
428  $sql .= ' AND agent_fk=ANY($4)';
429  $param[] = '{' . implode(',', $agentId) . '}';
430  } elseif (!empty($agentId)) {
431  $sql .= ' AND agent_fk=$4';
432  $param[] = $agentId;
433  }
434  $sql .= " WHERE (rf_shortname IS NULL OR rf_shortname NOT IN ('Void')) AND upload_fk=$1
435  AND (UT.lft BETWEEN $2 AND $3) AND UT.ufile_mode&(3<<28)=0
436  GROUP BY license_shortname, rf_pk";
437  $this->dbManager->prepare($statementName, $sql);
438  $result = $this->dbManager->execute($statementName, $param);
439  $assocLicenseHist = array();
440  while ($row = $this->dbManager->fetchArray($result)) {
441  $shortname = empty($row['rf_pk']) ? self::NO_LICENSE_FOUND : $row['license_shortname'];
442  $assocLicenseHist[$shortname] = array(
443  'count' => intval($row['count']),
444  'unique' => intval($row['unique']),
445  'rf_pk' => intval($row['rf_pk']));
446  }
447  $this->dbManager->freeResult($result);
448  return $assocLicenseHist;
449  }
450 
451  public function getLicenseShortnamesContained(ItemTreeBounds $itemTreeBounds, $latestSuccessfulAgentIds=null, $filterLicenses = array('VOID')) //'No_license_found',
452  {
453  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
454 
455  $noLicenseFoundStmt = empty($filterLicenses) ? "" : " AND rf_shortname NOT IN ("
456  . implode(", ", array_map(function ($name)
457  {
458  return "'" . $name . "'";
459  }, $filterLicenses)) . ")";
460 
461  $statementName = __METHOD__ . '.' . $uploadTreeTableName;
462 
463  $agentFilter = '';
464  if (is_array($latestSuccessfulAgentIds)) {
465  $agentIdSet = "{" . implode(',', $latestSuccessfulAgentIds) . "}";
466  $statementName .= ".$agentIdSet";
467  $agentFilter = " AND agent_fk=ANY('$agentIdSet')";
468  }
469 
470  $this->dbManager->prepare($statementName,
471  "SELECT license_ref.rf_shortname
472  FROM license_file JOIN license_ref ON license_file.rf_fk = license_ref.rf_pk
473  INNER JOIN $uploadTreeTableName uploadTree ON uploadTree.pfile_fk=license_file.pfile_fk
474  WHERE upload_fk=$1
475  AND lft BETWEEN $2 AND $3
476  $noLicenseFoundStmt $agentFilter
477  GROUP BY rf_shortname
478  ORDER BY rf_shortname ASC");
479  $result = $this->dbManager->execute($statementName,
480  array($itemTreeBounds->getUploadId(), $itemTreeBounds->getLeft(), $itemTreeBounds->getRight()));
481 
482  $licenses = array();
483  while ($row = $this->dbManager->fetchArray($result)) {
484  $licenses[] = $row['rf_shortname'];
485  }
486  $this->dbManager->freeResult($result);
487 
488  return $licenses;
489  }
490 
497  private function getLicenseByCondition($condition, $param, $groupId=null)
498  {
499  $extraCondition = "";
500  $row = $this->dbManager->getSingleRow(
501  "SELECT rf_pk, rf_shortname, rf_fullname, rf_text, rf_url, rf_risk, rf_detector_type, rf_spdx_compatible FROM ONLY license_ref WHERE $condition",
502  $param, __METHOD__ . ".$condition.only");
503  if (false === $row && isset($groupId)) {
504  $userId = (isset($_SESSION) && array_key_exists('UserId', $_SESSION)) ? $_SESSION['UserId'] : 0;
505  if (!empty($userId)) {
506  $param[] = $userId;
507  $extraCondition = "AND group_fk IN (SELECT group_fk FROM group_user_member WHERE user_fk=$".count($param).")";
508  }
509  if (is_int($groupId) && empty($userId)) {
510  $param[] = $groupId;
511  $extraCondition = "AND group_fk=$".count($param);
512  }
513  $row = $this->dbManager->getSingleRow(
514  "SELECT rf_pk, rf_shortname, rf_fullname, rf_text, rf_url, rf_risk, rf_detector_type, rf_spdx_compatible FROM license_candidate WHERE $condition $extraCondition",
515  $param, __METHOD__ . ".$condition.group");
516  }
517  if (false === $row) {
518  return null;
519  }
520  $license = new License(intval($row['rf_pk']), $row['rf_shortname'],
521  $row['rf_fullname'], $row['rf_risk'], $row['rf_text'], $row['rf_url'],
522  $row['rf_detector_type'], $row['rf_spdx_compatible']);
523  return $license;
524  }
525 
531  public function getLicenseById($licenseId, $groupId=null)
532  {
533  return $this->getLicenseByCondition('rf_pk=$1', array($licenseId), $groupId);
534  }
535 
541  public function getLicenseByShortName($licenseShortname, $groupId=null)
542  {
543  return $this->getLicenseByCondition('rf_shortname=$1', array($licenseShortname), $groupId);
544  }
545 
554  public function insertBulkLicense($userId, $groupId, $uploadTreeId, $licenseRemovals, $refText)
555  {
556  $licenseRefBulkIdResult = $this->dbManager->getSingleRow(
557  "INSERT INTO license_ref_bulk (user_fk, group_fk, uploadtree_fk, rf_text)
558  VALUES ($1,$2,$3,$4) RETURNING lrb_pk",
559  array($userId, $groupId, $uploadTreeId, $refText),
560  __METHOD__ . '.getLrb'
561  );
562  if ($licenseRefBulkIdResult === false) {
563  return -1;
564  }
565  $bulkId = $licenseRefBulkIdResult['lrb_pk'];
566 
567  $stmt = __METHOD__ . '.insertAction';
568  $this->dbManager->prepare($stmt, "INSERT INTO license_set_bulk (lrb_fk, rf_fk, removing, comment, reportinfo, acknowledgement) VALUES ($1,$2,$3,$4,$5,$6)");
569  foreach ($licenseRemovals as $licenseId=>$removing) {
570  $this->dbManager->execute($stmt, array($bulkId, $licenseId, $this->dbManager->booleanToDb($removing[0]), $removing[1], $removing[2], $removing[3]));
571  }
572 
573  return $bulkId ;
574  }
575 
581  public function isNewLicense($newShortname, $groupId)
582  {
583  $licenceViewDao = new LicenseViewProxy($groupId, array('columns' => array('rf_shortname')));
584  $sql = 'SELECT count(*) cnt FROM (' . $licenceViewDao->getDbViewQuery() . ') AS license_all WHERE rf_shortname=$1';
585  $duplicatedRef = $this->dbManager->getSingleRow($sql, array($newShortname), __METHOD__.".$groupId" );
586  return $duplicatedRef['cnt'] == 0;
587  }
588 
595  public function insertLicense($shortname, $refText, $spdxCompatible = false)
596  {
597  $row = $this->dbManager->getSingleRow(
598  "INSERT INTO license_ref (rf_shortname, rf_text, rf_detector_type, rf_spdx_compatible) VALUES ($1, $2, 2, $3) RETURNING rf_pk",
599  array($shortname, $refText, $spdxCompatible ? 1 : 0),
600  __METHOD__.".addLicense" );
601  return $row["rf_pk"];
602  }
603 
609  public function insertUploadLicense($newShortname, $refText, $groupId)
610  {
611  $sql = 'INSERT INTO license_candidate (group_fk,rf_shortname,rf_fullname,rf_text,rf_md5,rf_detector_type) VALUES ($1,$2,$2,$3,md5($3),1) RETURNING rf_pk';
612  $refArray = $this->dbManager->getSingleRow($sql, array($groupId, $newShortname, $refText), __METHOD__);
613  return $refArray['rf_pk'];
614  }
615 
620  public function getLicenseCount()
621  {
622  $licenseRefTable = $this->dbManager->getSingleRow("SELECT COUNT(*) cnt FROM license_ref WHERE rf_text!=$1", array("License by Nomos."));
623  return intval($licenseRefTable['cnt']);
624  }
625 
634  public function updateCandidate($rf_pk, $shortname, $fullname, $rfText, $url, $rfNotes, $readyformerge, $riskLvl)
635  {
636  $marydone = $this->dbManager->booleanToDb($readyformerge);
637  $this->dbManager->getSingleRow('UPDATE license_candidate SET rf_shortname=$2, rf_fullname=$3, rf_text=$4, rf_url=$5, rf_notes=$6, marydone=$7, rf_risk=$8 WHERE rf_pk=$1',
638  array($rf_pk, $shortname, $fullname, $rfText, $url, $rfNotes, $marydone, $riskLvl), __METHOD__);
639  }
640 
645  public function getLicenseParentById($licenseId, $groupId=null)
646  {
647  return $this->getLicenseByCondition(" rf_pk=(SELECT rf_parent FROM license_map WHERE usage=$1 AND rf_fk=$2 AND rf_fk!=rf_parent)",
648  array(LicenseMap::CONCLUSION,$licenseId), $groupId);
649  }
650 
655  public function getLicenseObligations($licenseLists, $tableName='obligation_map')
656  {
657  if (!empty($licenseLists)) {
658  $licenseList = implode (",",$licenseLists);
659  $statementName = __METHOD__.$tableName;
660  $this->dbManager->prepare($statementName,
661  "SELECT ob_pk, ob_topic, ob_text, ob_active, rf_fk, rf_shortname FROM obligation_ref
662  JOIN $tableName ON $tableName.ob_fk=obligation_ref.ob_pk
663  JOIN license_ref ON $tableName.rf_fk=license_ref.rf_pk WHERE ob_active='t' and rf_fk in ($licenseList)");
664  $result = $this->dbManager->execute($statementName, array());
665  $ObligationRef = $this->dbManager->fetchAll($result);
666  $this->dbManager->freeResult($result);
667  return $ObligationRef;
668  }
669  }
670 }
getAgentFileLicenseMatches(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
get all the licenses for a single file or uploadtree
Definition: LicenseDao.php:56
insertUploadLicense($newShortname, $refText, $groupId)
Definition: LicenseDao.php:609
getLicenseArray($groupId=null)
Definition: LicenseDao.php:218
getLicenseParentById($licenseId, $groupId=null)
Definition: LicenseDao.php:645
isNewLicense($newShortname, $groupId)
Definition: LicenseDao.php:581
insertBulkLicense($userId, $groupId, $uploadTreeId, $licenseRemovals, $refText)
Definition: LicenseDao.php:554
getLicenseByShortName($licenseShortname, $groupId=null)
Definition: LicenseDao.php:541
getLicensesPerFileNameForAgentId(ItemTreeBounds $itemTreeBounds, $selectedAgentIds=null, $includeSubfolders=true, $excluding='', $ignore=false, &$clearingDecisionsForLicList=array())
Definition: LicenseDao.php:302
getLicenseById($licenseId, $groupId=null)
Definition: LicenseDao.php:531
getLicenseObligations($licenseLists, $tableName='obligation_map')
Definition: LicenseDao.php:655
getLicenseRefs($search=null, $orderAscending=true)
Definition: LicenseDao.php:148
insertLicense($shortname, $refText, $spdxCompatible=false)
Definition: LicenseDao.php:595
getBulkFileLicenseMatches(ItemTreeBounds $itemTreeBounds)
get all the tried bulk recognitions for a single file or uploadtree (currently unused) ...
Definition: LicenseDao.php:105
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
getConclusionLicenseRefs($groupId, $search=null, $orderAscending=true, $exclude=array())
Definition: LicenseDao.php:180
getLicenseHistogram(ItemTreeBounds $itemTreeBounds, $agentId=null)
Definition: LicenseDao.php:416
getLicenseByCondition($condition, $param, $groupId=null)
Definition: LicenseDao.php:497
static getMappedLicenseRefView($usageExpr='$1')
Query to get license map view along with license ref.
Definition: LicenseMap.php:160
updateCandidate($rf_pk, $shortname, $fullname, $rfText, $url, $rfNotes, $readyformerge, $riskLvl)
Definition: LicenseDao.php:634
getLicenseIdPerPfileForAgentId(ItemTreeBounds $itemTreeBounds, $selectedAgentId, $includeSubfolders=true, $nameRange=array())
Definition: LicenseDao.php:244