34 const NO_LICENSE_FOUND =
'No_license_found';
41 private $candidatePrefix =
'*';
43 function __construct(
DbManager $dbManager)
46 $this->logger =
new Logger(self::class);
59 $statementName = __METHOD__ .
".$uploadTreeTableName.$usageId";
61 if ($usageId==LicenseMap::TRIVIAL) {
62 $licenseJoin =
"ONLY license_ref mlr ON license_file.rf_fk = mlr.rf_pk";
66 $licenseJoin =
"($licenseMapCte) AS mlr ON license_file.rf_fk = mlr.rf_origin";
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);
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']));
108 $statementName = __METHOD__ .
".$uploadTreeTableName";
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");
123 $result = $this->
dbManager->execute($statementName,
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') {
132 $agentName =
"bulk addition";
135 $agentName =
"bulk removal";
137 $agentRef =
new AgentRef($agentID, $agentName,
"empty");
138 $matches[] =
new LicenseMatch(intval($row[
'file_id']), $licenseRef, $agentRef, intval($row[
'license_file_id']));
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();
157 $rfTable =
'ONLY license_ref';
160 $searchCondition = $search ?
"WHERE rf_shortname ilike $1" :
"";
162 $order = $orderAscending ?
"ASC" :
"DESC";
163 $statementName = __METHOD__ . ($search ?
".search_" . $search :
"") .
".order_$order";
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']);
182 $rfTable =
'license_all';
183 $options = array(
'columns' => array(
'rf_pk',
'rf_shortname',
'rf_fullname'),
184 'candidatePrefix' => $this->candidatePrefix);
186 $order = $orderAscending ?
"ASC" :
"DESC";
187 $statementName = __METHOD__ .
".order_$order";
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)";
193 $param[] =
'%' . $search .
'%';
194 $statementName .=
'.search';
195 $sql .=
" AND rf_shortname ilike $1";
197 if (count($exclude)>0) {
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)";
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']);
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;
227 $withCte = $licenseViewDao->asCTE();
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);
247 $statementName = __METHOD__ .
'.' . $uploadTreeTableName;
248 $param = array($selectedAgentId);
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";
263 $condition =
"realparent = $2";
266 if (
'uploadtree_a' == $uploadTreeTableName) {
268 $condition .=
" AND utree.upload_fk=$".count($param);
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, 276 FROM license_file, license_ref, $uploadTreeTableName utree 278 AND license_file.rf_fk = license_ref.rf_pk 279 AND license_file.pfile_fk = utree.pfile_fk 281 ORDER BY match_percentage ASC";
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;
291 return $licensesPerFileId;
303 $selectedAgentIds=null,
304 $includeSubfolders=
true,
307 &$clearingDecisionsForLicList = array())
310 $statementName = __METHOD__ .
'.' . $uploadTreeTableName;
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";
321 $condition .=
" AND realparent = $1";
324 if (
'uploadtree_a' == $uploadTreeTableName) {
326 $condition .=
" AND upload_fk=$".count($param);
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);
340 SELECT uploadtree_pk, ufile_name, lft, rgt, ufile_mode, 341 rf_shortname, agent_fk 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 355 $this->
dbManager->prepare($statementName, $sql);
356 $result = $this->
dbManager->execute($statementName, $param);
357 $licensesPerFileName = array();
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;
370 if ($row[
'lft'] < $lastLft) {
374 $this->updateStackState($pathStack, $rgtStack, $lastLft, $row);
375 $path = implode($pathStack,
'/');
376 $this->addToLicensesPerFileName($licensesPerFileName, $path, $row, $ignore, $clearingDecisionsForLicList);
379 return array_reverse($licensesPerFileName);
382 private function updateStackState(&$pathStack, &$rgtStack, &$lastLft, $row)
384 if ($row[
'lft'] >= $lastLft) {
385 while (count($rgtStack) > 0 && $row[
'lft'] > $rgtStack[count($rgtStack)-1]) {
386 array_pop($pathStack);
387 array_pop($rgtStack);
389 if ($row[
'lft'] > $lastLft) {
390 array_push($pathStack, $row[
'ufile_name']);
391 array_push($rgtStack, $row[
'rgt']);
392 $lastLft = $row[
'lft'];
397 private function addToLicensesPerFileName(&$licensesPerFileName, $path, $row, $ignore, &$clearingDecisionsForLicList = array())
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']];
406 }
else if (!$ignore) {
407 $licensesPerFileName[$path] =
false;
419 $agentText = $agentId ? (is_array($agentId) ? implode(
',', $agentId) : $agentId) :
'-';
420 $statementName = __METHOD__ .
'.' . $uploadTreeTableName .
".$agentText";
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 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';
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']));
448 return $assocLicenseHist;
451 public function getLicenseShortnamesContained(
ItemTreeBounds $itemTreeBounds, $latestSuccessfulAgentIds=null, $filterLicenses = array(
'VOID'))
455 $noLicenseFoundStmt = empty($filterLicenses) ?
"" :
" AND rf_shortname NOT IN (" 456 . implode(
", ", array_map(
function ($name)
458 return "'" . $name .
"'";
459 }, $filterLicenses)) .
")";
461 $statementName = __METHOD__ .
'.' . $uploadTreeTableName;
464 if (is_array($latestSuccessfulAgentIds)) {
465 $agentIdSet =
"{" . implode(
',', $latestSuccessfulAgentIds) .
"}";
466 $statementName .=
".$agentIdSet";
467 $agentFilter =
" AND agent_fk=ANY('$agentIdSet')";
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 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,
483 while ($row = $this->
dbManager->fetchArray($result)) {
484 $licenses[] = $row[
'rf_shortname'];
499 $extraCondition =
"";
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)) {
507 $extraCondition =
"AND group_fk IN (SELECT group_fk FROM group_user_member WHERE user_fk=$".count($param).
")";
509 if (is_int($groupId) && empty($userId)) {
511 $extraCondition =
"AND group_fk=$".count($param);
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");
517 if (
false === $row) {
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']);
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' 562 if ($licenseRefBulkIdResult ===
false) {
565 $bulkId = $licenseRefBulkIdResult[
'lrb_pk'];
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]));
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;
595 public function insertLicense($shortname, $refText, $spdxCompatible =
false)
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"];
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'];
622 $licenseRefTable = $this->
dbManager->getSingleRow(
"SELECT COUNT(*) cnt FROM license_ref WHERE rf_text!=$1", array(
"License by Nomos."));
623 return intval($licenseRefTable[
'cnt']);
634 public function updateCandidate($rf_pk, $shortname, $fullname, $rfText, $url, $rfNotes, $readyformerge, $riskLvl)
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__);
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);
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);
667 return $ObligationRef;
getAgentFileLicenseMatches(ItemTreeBounds $itemTreeBounds, $usageId=LicenseMap::TRIVIAL)
get all the licenses for a single file or uploadtree
insertUploadLicense($newShortname, $refText, $groupId)
getLicenseArray($groupId=null)
getLicenseParentById($licenseId, $groupId=null)
isNewLicense($newShortname, $groupId)
insertBulkLicense($userId, $groupId, $uploadTreeId, $licenseRemovals, $refText)
getLicenseByShortName($licenseShortname, $groupId=null)
getLicensesPerFileNameForAgentId(ItemTreeBounds $itemTreeBounds, $selectedAgentIds=null, $includeSubfolders=true, $excluding='', $ignore=false, &$clearingDecisionsForLicList=array())
getLicenseById($licenseId, $groupId=null)
getLicenseObligations($licenseLists, $tableName='obligation_map')
getLicenseRefs($search=null, $orderAscending=true)
insertLicense($shortname, $refText, $spdxCompatible=false)
getBulkFileLicenseMatches(ItemTreeBounds $itemTreeBounds)
get all the tried bulk recognitions for a single file or uploadtree (currently unused) ...
fo_dbManager * dbManager
fo_dbManager object
getConclusionLicenseRefs($groupId, $search=null, $orderAscending=true, $exclude=array())
getLicenseHistogram(ItemTreeBounds $itemTreeBounds, $agentId=null)
getLicenseByCondition($condition, $param, $groupId=null)
static getMappedLicenseRefView($usageExpr='$1')
Query to get license map view along with license ref.
updateCandidate($rf_pk, $shortname, $fullname, $rfText, $url, $rfNotes, $readyformerge, $riskLvl)
getLicenseIdPerPfileForAgentId(ItemTreeBounds $itemTreeBounds, $selectedAgentId, $includeSubfolders=true, $nameRange=array())