9 namespace Fossology\Lib\Dao;
30 $this->uploadDao = $uploadDao;
31 $this->logger =
new Logger(self::class);
42 public function getHighlights($uploadTreeId, $tableName=
"copyright", $agentId=array(0),
43 $typeToHighlightTypeMap=array(
44 'statement' => Highlight::COPYRIGHT,
45 'email' => Highlight::EMAIL,
46 'url' => Highlight::URL,
47 'author' => Highlight::AUTHOR)
51 $row = $this->uploadDao->getUploadEntry($uploadTreeId);
53 if (!empty($row[
'pfile_fk'])) {
54 $pFileId = $row[
'pfile_fk'];
56 $text = _(
"Could not locate the corresponding pfile.");
60 $statementName = __METHOD__.$tableName;
61 $params = array($pFileId);
63 if (!empty($agentId) && $agentId[0] != 0) {
64 $agentIds = implode(
",", $agentId);
65 $statementName .=
'.agentId';
66 $addAgentValue =
' AND agent_fk= ANY($2::int[])';
67 $params[] =
"{" . $agentIds .
"}";
69 $columnsToSelect =
"type, content, copy_startbyte, copy_endbyte";
70 $getHighlightForTableName =
"SELECT $columnsToSelect FROM $tableName WHERE copy_startbyte IS NOT NULL AND pfile_fk=$1 $addAgentValue";
71 if ($tableName !=
"copyright") {
72 $sql = $getHighlightForTableName;
74 $sql =
"$getHighlightForTableName UNION SELECT $columnsToSelect FROM author WHERE copy_startbyte IS NOT NULL AND pfile_fk=$1 $addAgentValue";
76 $this->
dbManager->prepare($statementName,$sql);
77 $result = $this->
dbManager->execute($statementName, $params);
79 $highlights = array();
80 while ($row = $this->
dbManager->fetchArray($result)) {
82 $content = $row[
'content'];
84 $highlightType = array_key_exists($type, $typeToHighlightTypeMap) ? $typeToHighlightTypeMap[$type] : Highlight::UNDEFINED;
85 $highlights[] =
new Highlight($row[
'copy_startbyte'], $row[
'copy_endbyte'], $highlightType, -1, -1, $content, $htmlElement);
103 public function saveDecision($tableName, $pfileId, $userId , $clearingType,
104 $description, $textFinding, $comment, $decision_pk=-1)
107 if (empty($textFinding)) {
110 $primaryColumn = $tableName .
'_pk';
111 $assocParams = array(
112 'user_fk' => $userId,
113 'pfile_fk' => $pfileId,
114 'clearing_decision_type_fk' => $clearingType,
115 'description' => $description,
116 'textfinding' => $textFinding,
117 'hash' => hash(
'sha256', $textFinding),
121 if ($decision_pk <= 0) {
123 foreach ($rows as $row) {
124 if ($row[
'pfile_fk'] == $pfileId) {
125 $decision_pk = $row[$primaryColumn];
129 if ($decision_pk <= 0) {
130 return $this->
dbManager->insertTableRow($tableName, $assocParams,
131 __METHOD__.
'Insert.'.$tableName, $primaryColumn);
133 $assocParams[
'is_enabled'] =
true;
134 $this->
dbManager->updateTableRow($tableName, $assocParams, $primaryColumn,
135 $decision_pk, __METHOD__.
'Update.'.$tableName);
140 public function removeDecision($tableName,$pfileId, $decisionId)
142 $primaryColumn = $tableName .
'_pk';
146 WHERE $primaryColumn = $1
148 $this->
dbManager->execute(__METHOD__, array($decisionId, $pfileId));
151 public function undoDecision($tableName,$pfileId, $decisionId)
153 $primaryColumn = $tableName .
'_pk';
157 WHERE $primaryColumn = $1
159 $this->
dbManager->execute(__METHOD__, array($decisionId, $pfileId));
170 $statementName = __METHOD__ . $uploadFk;
171 $params[] = $uploadFk;
172 $params[] = $agentId;
174 $sql =
"SELECT copyright_pk, CE.is_enabled, C.content, c.hash,
175 CE.content AS contentedited, CE.hash AS hashedited
176 FROM copyright_event CE
177 INNER JOIN copyright C ON C.copyright_pk = CE.copyright_fk
178 WHERE CE.upload_fk=$1 AND scope=$3 AND C.agent_fk = $2";
179 return $this->
dbManager->getRows($sql, $params, $statementName);
192 $type, $extrawhere, $enabled=
'true')
194 $statementName = __METHOD__.$tableName.$uploadTreeTableName;
195 $params = array($uploadId);
196 $extendWClause =
null;
197 $tableNameEvent = $tableName.
'_event';
199 if ($uploadTreeTableName ===
"uploadtree_a") {
200 $extendWClause .=
" AND UT.upload_fk = $1";
201 $statementName .=
".withUI";
204 if ($type !==
null && $type !=
"skipcontent") {
206 $extendWClause .=
" AND C.type = $".count($params);
207 $statementName .=
".withType";
210 if ($extrawhere !==
null) {
211 $extendWClause .=
" AND ". $extrawhere;
212 $statementName .=
"._".$extrawhere.
"_";
215 $activatedClause =
"ce.is_enabled = 'false'";
216 if ($enabled !=
'false') {
217 $activatedClause =
"ce.is_enabled IS NULL OR ce.is_enabled = 'true'";
218 $statementName .=
"._".
"enabled";
221 $sql =
"SELECT DISTINCT ON(copyright_pk, UT.uploadtree_pk)
222 copyright_pk, UT.uploadtree_pk as uploadtree_pk,
223 (CASE WHEN (CE.content IS NULL OR CE.content = '') THEN C.content ELSE CE.content END) AS content,
224 (CASE WHEN (CE.hash IS NULL OR CE.hash = '') THEN C.hash ELSE CE.hash END) AS hash,
225 C.agent_fk as agent_fk
227 INNER JOIN $uploadTreeTableName UT ON C.pfile_fk = UT.pfile_fk
228 LEFT JOIN $tableNameEvent AS CE ON CE.".$tableName.
"_fk = C.".$tableName.
"_pk
229 AND CE.upload_fk = $1 AND CE.uploadtree_fk = UT.uploadtree_pk
230 WHERE C.content IS NOT NULL
232 AND ($activatedClause)
234 ORDER BY copyright_pk, UT.uploadtree_pk, content DESC";
235 return $this->
dbManager->getRows($sql, $params, $statementName);
247 $decisionType, $extrawhere=
"")
249 $statementName = __METHOD__.$tableName.$uploadTreeTableName;
251 $extendWClause =
null;
253 if ($uploadTreeTableName ===
"uploadtree_a") {
254 $params[]= $uploadId;
255 $extendWClause .=
" AND UT.upload_fk = $".count($params);
256 $statementName .=
".withUI";
259 if (!empty($decisionType)) {
260 $params[]= $decisionType;
261 $extendWClause .=
" AND clearing_decision_type_fk = $".count($params);
262 $statementName .=
".withDecisionType";
265 if (!empty($extrawhere)) {
266 $extendWClause .=
" AND ". $extrawhere;
267 $statementName .=
"._".$extrawhere.
"_";
270 $columns =
"CD.description as description, CD.textfinding as textfinding, CD.comment as comments, UT.uploadtree_pk as uploadtree_pk";
272 $primaryColumn = $tableName .
'_pk';
273 $sql =
"SELECT $columns
275 INNER JOIN $uploadTreeTableName UT ON CD.pfile_fk = UT.pfile_fk
276 WHERE CD.is_enabled = 'true'
278 ORDER BY CD.pfile_fk, UT.uploadtree_pk, CD.textfinding, CD.$primaryColumn DESC";
279 $this->
dbManager->prepare($statementName, $sql);
280 $sqlResult = $this->
dbManager->execute($statementName, $params);
281 $result = $this->
dbManager->fetchAll($sqlResult);
282 $this->
dbManager->freeResult($sqlResult);
298 public function getAllEntriesReport($tableName, $uploadId, $uploadTreeTableName, $type=
null, $onlyCleared=
false, $decisionType=
null, $extrawhere=
null, $groupId=
null)
300 $tableNameDecision = $tableName.
"_decision";
301 if ($tableName ==
'copyright') {
302 $scannerEntries = $this->
getScannerEntries($tableName, $uploadTreeTableName, $uploadId, $type, $extrawhere);
303 $editedEntries = $this->
getEditedEntries($tableNameDecision, $uploadTreeTableName, $uploadId, $decisionType);
304 return array_merge($scannerEntries, $editedEntries);
306 return $this->
getEditedEntries($tableNameDecision, $uploadTreeTableName, $uploadId, $decisionType);
310 public function getAllEntries($tableName, $uploadId, $uploadTreeTableName, $type=
null, $onlyCleared=
false, $decisionType=
null, $extrawhere=
null)
312 $statementName = __METHOD__.$tableName.$uploadTreeTableName;
313 $tableNameEvent = $tableName.
'_event';
315 $params = array($uploadId);
317 $distinctContent =
"";
318 $tableNameDecision = $tableName.
"_decision";
320 if ($uploadTreeTableName ===
"uploadtree_a") {
321 $whereClause .=
" AND UT.upload_fk = $1";
322 $statementName .=
".withUI";
324 if ($type !==
null && $type !=
"skipcontent") {
326 $whereClause .=
" AND C.type = $".count($params);
327 $statementName .=
".withType";
330 $clearingTypeClause =
null;
333 if ($decisionType !==
null) {
334 $params []= $decisionType;
335 $clearingTypeClause =
"WHERE clearing_decision_type_fk = $".count($params);
336 $statementName .=
".withDecisionType";
338 throw new \Exception(
"requested only cleared but no type given");
342 if ($decisionType !==
null) {
343 $params []= $decisionType;
344 $clearingTypeClause =
"WHERE clearing_decision_type_fk IS NULL OR clearing_decision_type_fk = $".count($params);
345 $statementName .=
".withDecisionType";
348 $statementName .=
".".$joinType.
"Join";
350 if ($extrawhere !==
null) {
351 $whereClause .=
" AND ". $extrawhere;
352 $statementName .=
"._".$extrawhere.
"_";
354 $decisionTableKey = $tableNameDecision .
"_pk";
356 $latestInfo =
"SELECT DISTINCT ON(CD.pfile_fk, UT.uploadtree_pk, C.content, CD.textfinding)
357 CD.description as description, CD.textfinding as textfinding,
358 CD.comment as comments, UT.uploadtree_pk as uploadtree_pk,
359 CD.clearing_decision_type_fk AS clearing_decision_type_fk,
362 INNER JOIN $uploadTreeTableName UT
363 ON C.pfile_fk = UT.pfile_fk
364 LEFT JOIN $tableNameEvent AS CE
365 ON CE.".$tableName.
"_fk = C.".$tableName.
"_pk
366 AND CE.upload_fk = $1 AND CE.uploadtree_fk = UT.uploadtree_pk
367 $joinType JOIN (SELECT * FROM $tableNameDecision WHERE is_enabled='true') AS CD
368 ON C.pfile_fk = CD.pfile_fk
369 WHERE C.content IS NOT NULL
371 AND (ce.is_enabled IS NULL OR ce.is_enabled = 'true')
373 ORDER BY CD.pfile_fk, UT.uploadtree_pk, C.content, CD.textfinding, CD.$decisionTableKey DESC";
375 if ($clearingTypeClause !==
null) {
376 $sql =
"SELECT * FROM ($latestInfo) AS latestInfo $clearingTypeClause";
381 $this->
dbManager->prepare($statementName, $sql);
382 $sqlResult = $this->
dbManager->execute($statementName, $params);
383 $result = $this->
dbManager->fetchAll($sqlResult);
384 $this->
dbManager->freeResult($sqlResult);
396 $statementName = __METHOD__.$tableName;
397 $orderTablePk = $tableName.
'_pk';
398 $sql =
"SELECT * FROM $tableName where pfile_fk = $1 order by $orderTablePk desc";
399 $params = array($pfileId);
401 return $this->
dbManager->getRows($sql, $params, $statementName);
419 $statementName = __METHOD__ .
".$tableName";
420 $orderTablePk = $tableName.
'_pk';
425 if ($upload !=
null) {
426 if (empty($uploadtreetable)) {
429 $statementName.=
".filterUpload";
431 $join =
"INNER JOIN $uploadtreetable AS ut ON cp.pfile_fk = ut.pfile_fk";
432 $joinWhere =
"AND ut.upload_fk = $" . count($params);
435 $sql =
"SELECT * FROM $tableName AS cp $join " .
436 "WHERE cp.hash = $1 $joinWhere ORDER BY $orderTablePk;";
438 return $this->
dbManager->getRows($sql, $params, $statementName);
448 public function updateTable($item, $hash, $content, $userId, $cpTable, $action=
'', $scope=1)
450 $cpTablePk = $cpTable.
"_pk";
451 $cpTableEvent = $cpTable.
"_event";
452 $cpTableEventFk = $cpTable.
"_fk";
453 $itemTable = $item->getUploadTreeTableName();
454 $stmt = __METHOD__.
".$cpTable.$itemTable";
455 $uploadId = $item->getUploadId();
456 $params = array($item->getLeft(),$item->getRight(),$uploadId);
461 $withHash =
" (cp.hash = $4 OR ce.hash = $4) AND ";
466 $scanJobProxy =
new ScanJobProxy($GLOBALS[
'container']->
get(
'dao.agent'),
468 if ($agentName ==
"copyright") {
469 $scanJobProxy->createAgentStatus(array($agentName,
'reso'));
471 $scanJobProxy->createAgentStatus(array($agentName));
473 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
474 if (!array_key_exists($agentName, $selectedScanners)) {
477 $latestXpAgentId[] = $selectedScanners[$agentName];
478 if (array_key_exists(
'reso', $selectedScanners)) {
479 $latestXpAgentId[] = $selectedScanners[
'reso'];
482 if (!empty($latestXpAgentId)) {
483 $latestAgentIds = implode(
",", $latestXpAgentId);
484 $agentFilter =
' AND cp.agent_fk IN ('. $latestAgentIds .
')';
487 $sql =
"SELECT DISTINCT ON ($cpTablePk, ut.uploadtree_pk) $cpTablePk, ut.uploadtree_pk, ut.upload_fk, ce." . $cpTableEvent .
"_pk
489 INNER JOIN $itemTable AS ut ON cp.pfile_fk = ut.pfile_fk
490 LEFT JOIN $cpTableEvent AS ce ON ce.$cpTableEventFk = cp.$cpTablePk
491 AND ce.upload_fk = ut.upload_fk AND ce.uploadtree_fk = ut.uploadtree_pk
492 WHERE $withHash ( ut.lft BETWEEN $1 AND $2 ) $agentFilter AND ut.upload_fk = $3";
494 $rows = $this->
dbManager->getRows($sql, $params, $stmt);
496 foreach ($rows as $row) {
497 $paramEvent = array();
498 $paramEvent[] = $row[
'upload_fk'];
499 $paramEvent[] = $row[$cpTablePk];
500 $paramEvent[] = $row[
'uploadtree_pk'];
501 $sqlExists =
"SELECT exists(SELECT 1 FROM $cpTableEvent WHERE $cpTableEventFk = $1 AND upload_fk = $2 AND uploadtree_fk = $3)::int";
502 $rowExists = $this->
dbManager->getSingleRow($sqlExists, array($row[$cpTablePk], $row[
'upload_fk'], $row[
'uploadtree_pk']), $stmt.
'Exists');
503 $eventExists = $rowExists[
'exists'];
504 if ($action ==
"delete") {
505 $paramEvent[] = $scope;
507 $sqlEvent =
"UPDATE $cpTableEvent SET scope = $4, is_enabled = false
508 WHERE upload_fk = $1 AND $cpTableEventFk = $2 AND uploadtree_fk = $3";
509 $statement =
"$stmt.delete.up";
511 $sqlEvent =
"INSERT INTO $cpTableEvent (upload_fk, $cpTableEventFk, uploadtree_fk, is_enabled, scope) VALUES($1, $2, $3, 'f', $4)";
512 $statement =
"$stmt.delete";
514 }
else if ($action ==
"rollback" && $eventExists) {
515 $sqlEvent =
"UPDATE $cpTableEvent SET scope = 1, is_enabled = true
516 WHERE upload_fk = $1 AND $cpTableEventFk = $2 AND uploadtree_fk = $3";
517 $statement =
"$stmt.rollback.up";
522 $sqlEvent =
"UPDATE $cpTableEvent
523 SET upload_fk = $1, content = $4, hash = md5($4)
524 WHERE $cpTableEventFk = $2 AND uploadtree_fk = $3";
525 $statement =
"$stmt.update";
527 $sqlEvent =
"INSERT INTO $cpTableEvent(upload_fk, uploadtree_fk, $cpTableEventFk, is_enabled, content, hash)
528 VALUES($1, $3, $2, 'true', $4, md5($4))";
529 $statement =
"$stmt.insert";
532 $this->
dbManager->getSingleRow($sqlEvent, $paramEvent, $statement);
550 if (array_search($table, [
"ecc",
"keyword",
"copyright",
"ipra"]) !==
false) {
552 }
else if (array_search($table, [
"scancode_copyright",
"scancode_author"]) !==
false) {
581 $tableName =
"keyword";
585 $tableName =
"copyright";
587 case "userfindingcopyright":
588 $tableName =
"copyright_decision";
590 case "scancode_statement":
591 $tableName =
"scancode_copyright";
593 case "scancode_email":
594 case "scancode_author":
596 $tableName =
"scancode_author";
599 $tableName =
"author";
614 public function getTotalCopyrights($upload_pk, $item, $uploadTreeTableName, $agentId, $type, $activated =
true)
617 $tableNameEvent = $tableName .
'_event';
618 list($left, $right) = $this->uploadDao->getLeftAndRight($item, $uploadTreeTableName);
620 if (
'uploadtree_a' == $uploadTreeTableName) {
621 $sql_upload =
" AND UT.upload_fk=$5 ";
623 $activatedClause =
"ce.is_enabled = 'false'";
625 $activatedClause =
"ce.is_enabled IS NULL OR ce.is_enabled = 'true'";
627 $params = array($left, $right, $type,
"{" . $agentId .
"}", $upload_pk);
628 $join =
" INNER JOIN license_file AS LF on cp.pfile_fk=LF.pfile_fk ";
629 $unorderedQuery =
"FROM $tableName AS cp " .
630 "INNER JOIN $uploadTreeTableName AS UT ON cp.pfile_fk = UT.pfile_fk " .
631 "LEFT JOIN $tableNameEvent AS ce ON ce." . $tableName .
"_fk = cp." . $tableName .
"_pk " .
632 "AND ce.upload_fk = $5 AND ce.uploadtree_fk = UT.uploadtree_pk " .
634 "WHERE cp.content!='' " .
635 "AND ( UT.lft BETWEEN $1 AND $2 ) " .
636 "AND cp.type = $3 " .
637 "AND cp.agent_fk = ANY($4::int[]) " .
638 "AND ($activatedClause)" .
640 $grouping =
" GROUP BY mcontent ";
641 $countAllQuery =
"SELECT count(*) FROM (SELECT
642 (CASE WHEN (ce.content IS NULL OR ce.content = '') THEN cp.content ELSE ce.content END) AS mcontent
643 $unorderedQuery$grouping) as K";
644 $iTotalRecordsRow = $this->
dbManager->getSingleRow($countAllQuery, $params, __METHOD__, $tableName .
"count.all" . ($activated ?
'' :
'_deactivated'));
645 return $iTotalRecordsRow[
'count'];
657 public function getUserCopyrights($upload_pk, $item, $uploadTreeTableName, $type, $activated =
true, $offset =
null , $limit =
null)
660 list($left, $right) = $this->uploadDao->getLeftAndRight($item, $uploadTreeTableName);
663 if (
'uploadtree_a' == $uploadTreeTableName) {
664 $sql_upload =
" AND UT.upload_fk=$3 ";
667 $params = array($left, $right, $upload_pk);
668 $orderString =
'ORDER BY copyright_count desc, textfinding desc';
670 $activatedClause =
"cd.is_enabled = 'false'";
672 $activatedClause =
"cd.is_enabled IS NULL OR cd.is_enabled = 'true'";
675 $unorderedQuery =
"FROM $tableName AS cd " .
676 "INNER JOIN $uploadTreeTableName AS UT ON cd.pfile_fk = UT.pfile_fk " .
677 "WHERE cd.textfinding!='' " .
678 "AND ( UT.lft BETWEEN $1 AND $2 ) " .
679 "AND cd.user_fk IS NOT NULL " .
680 "AND ($activatedClause)" .
682 $grouping =
" GROUP BY cd.textfinding, cd.hash ";
684 $countAllQuery =
"SELECT count(*) FROM (SELECT
685 (CASE WHEN (cd.textfinding IS NULL OR cd.textfinding = '') THEN '' ELSE cd.textfinding END) AS mcontent
686 $unorderedQuery GROUP BY cd.textfinding) as K;";
687 $iTotalRecordsRow = $this->
dbManager->getSingleRow($countAllQuery, $params, __METHOD__ . $tableName .
"count.all" . ($activated ?
'' :
'_deactivated'));
688 $iTotalRecords = $iTotalRecordsRow[
'count'];
692 $range .=
' OFFSET $' . count($params);
694 $range .=
' LIMIT $' . count($params);
696 $sql =
"SELECT cd.textfinding AS content, cd.hash AS hash, COUNT(*) AS copyright_count " .
697 "$unorderedQuery $grouping $orderString $range";
698 $statement = __METHOD__ . $tableName . $uploadTreeTableName . ($activated ?
'' :
'_deactivated');
699 $rows = $this->
dbManager->getRows($sql, $params, $statement);
701 return array($rows, $iTotalRecords);
getAllEventEntriesForUpload($uploadFk, $agentId, $scope=1)
getAgentName($table)
Get agent name based on table name.
updateTable($item, $hash, $content, $userId, $cpTable, $action='', $scope=1)
getDecisionsFromHash($tableName, $hash, $upload=null, $uploadtreetable=null)
Get all the decisions based on hash.
getAllEntriesReport($tableName, $uploadId, $uploadTreeTableName, $type=null, $onlyCleared=false, $decisionType=null, $extrawhere=null, $groupId=null)
getScannerEntries($tableName, $uploadTreeTableName, $uploadId, $type, $extrawhere, $enabled='true')
getDecisions($tableName, $pfileId)
saveDecision($tableName, $pfileId, $userId, $clearingType, $description, $textFinding, $comment, $decision_pk=-1)
getUserCopyrights($upload_pk, $item, $uploadTreeTableName, $type, $activated=true, $offset=null, $limit=null)
get user copyright findings for a uploadtree
getHighlights($uploadTreeId, $tableName="copyright", $agentId=array(0), $typeToHighlightTypeMap=array('statement'=> Highlight::COPYRIGHT, 'email'=> Highlight::EMAIL, 'url'=> Highlight::URL, 'author'=> Highlight::AUTHOR))
getEditedEntries($tableName, $uploadTreeTableName, $uploadId, $decisionType, $extrawhere="")
getTableName($type)
Get table name based on statement type.
getTotalCopyrights($upload_pk, $item, $uploadTreeTableName, $agentId, $type, $activated=true)
Get total number of copyrights for a uploadtree.
static replaceUnicodeControlChar($input, $replace="")
fo_dbManager * dbManager
fo_dbManager object
list_t type structure used to keep various lists. (e.g. there are multiple lists).