FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
CopyrightDao.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 
25 use Monolog\Logger;
26 
28 {
30  private $dbManager;
32  private $uploadDao;
34  private $clearingDao;
36  private $logger;
37 
38  function __construct(DbManager $dbManager, UploadDao $uploadDao)
39  {
40  $this->dbManager = $dbManager;
41  $this->uploadDao = $uploadDao;
42  $this->logger = new Logger(self::class);
43  global $container;
44  $this->clearingDao = $container->get('dao.clearing');
45  }
46 
55  public function getHighlights($uploadTreeId, $tableName="copyright", $agentId=0,
56  $typeToHighlightTypeMap=array(
57  'statement' => Highlight::COPYRIGHT,
58  'email' => Highlight::EMAIL,
59  'url' => Highlight::URL,
60  'author' => Highlight::AUTHOR)
61  )
62  {
63  $pFileId = 0;
64  $row = $this->uploadDao->getUploadEntry($uploadTreeId);
65 
66  if (!empty($row['pfile_fk'])) {
67  $pFileId = $row['pfile_fk'];
68  } else {
69  $text = _("Could not locate the corresponding pfile.");
70  print $text;
71  }
72 
73  $statementName = __METHOD__.$tableName;
74  $params = array($pFileId);
75  $addAgentValue = "";
76  if (!empty($agentId)) {
77  $statementName .= '.agentId';
78  $addAgentValue = ' AND agent_fk=$2';
79  $params[] = $agentId;
80  }
81  $columnsToSelect = "type, content, copy_startbyte, copy_endbyte";
82  $getHighlightForTableName = "SELECT $columnsToSelect FROM $tableName WHERE copy_startbyte IS NOT NULL AND pfile_fk=$1 $addAgentValue";
83  if ($tableName != "copyright") {
84  $sql = $getHighlightForTableName;
85  } else {
86  $sql = "$getHighlightForTableName UNION SELECT $columnsToSelect FROM author WHERE copy_startbyte IS NOT NULL AND pfile_fk=$1 $addAgentValue";
87  }
88  $this->dbManager->prepare($statementName,$sql);
89  $result = $this->dbManager->execute($statementName, $params);
90 
91  $highlights = array();
92  while ($row = $this->dbManager->fetchArray($result)) {
93  $type = $row['type'];
94  $content = $row['content'];
95  $htmlElement =null;
96  $highlightType = array_key_exists($type, $typeToHighlightTypeMap) ? $typeToHighlightTypeMap[$type] : Highlight::UNDEFINED;
97  $highlights[] = new Highlight($row['copy_startbyte'], $row['copy_endbyte'], $highlightType, -1, -1, $content, $htmlElement);
98  }
99  $this->dbManager->freeResult($result);
100 
101  return $highlights;
102  }
103 
115  public function saveDecision($tableName, $pfileId, $userId , $clearingType,
116  $description, $textFinding, $comment, $decision_pk=-1)
117  {
118  $primaryColumn = $tableName . '_pk';
119  $assocParams = array(
120  'user_fk' => $userId,
121  'pfile_fk' => $pfileId,
122  'clearing_decision_type_fk' => $clearingType,
123  'description' => $description,
124  'textfinding' => $textFinding,
125  'hash' => hash('sha256', $textFinding),
126  'comment'=> $comment
127  );
128 
129  if ($decision_pk <= 0) {
130  $rows = $this->getDecisionsFromHash($tableName, $assocParams['hash']);
131  foreach ($rows as $row) {
132  if ($row['pfile_fk'] == $pfileId) {
133  $decision_pk = $row[$primaryColumn];
134  }
135  }
136  }
137  if ($decision_pk <= 0) {
138  return $this->dbManager->insertTableRow($tableName, $assocParams,
139  __METHOD__.'Insert.'.$tableName, $primaryColumn);
140  } else {
141  $assocParams['is_enabled'] = true;
142  $this->dbManager->updateTableRow($tableName, $assocParams, $primaryColumn,
143  $decision_pk, __METHOD__.'Update.'.$tableName);
144  return $decision_pk;
145  }
146  }
147 
148  public function removeDecision($tableName,$pfileId, $decisionId)
149  {
150  $primaryColumn = $tableName . '_pk';
151  $this->dbManager->prepare(__METHOD__,
152  "UPDATE $tableName
153  SET is_enabled = 'f'
154  WHERE $primaryColumn = $1
155  AND pfile_fk = $2");
156  $this->dbManager->execute(__METHOD__, array($decisionId, $pfileId));
157  }
158 
159  public function undoDecision($tableName,$pfileId, $decisionId)
160  {
161  $primaryColumn = $tableName . '_pk';
162  $this->dbManager->prepare(__METHOD__,
163  "UPDATE $tableName
164  SET is_enabled = 't'
165  WHERE $primaryColumn = $1
166  AND pfile_fk = $2");
167  $this->dbManager->execute(__METHOD__, array($decisionId, $pfileId));
168  }
169 
178  public function getScannerEntries($tableName, $uploadTreeTableName, $uploadId, $type, $extrawhere)
179  {
180  $statementName = __METHOD__.$tableName.$uploadTreeTableName;
181  $params = array();
182  $extendWClause = null;
183 
184  if ($uploadTreeTableName === "uploadtree_a") {
185  $params[]= $uploadId;
186  $extendWClause .= " AND UT.upload_fk = $".count($params);
187  $statementName .= ".withUI";
188  }
189 
190  if ($type !== null && $type != "skipcontent") {
191  $params[]= $type;
192  $extendWClause .= " AND C.type = $".count($params);
193  $statementName .= ".withType";
194  }
195 
196  if ($extrawhere !== null) {
197  $extendWClause .= " AND ". $extrawhere;
198  $statementName .= "._".$extrawhere."_";
199  }
200 
201  $sql = "SELECT UT.uploadtree_pk as uploadtree_pk, C.content AS content
202  FROM $tableName C
203  INNER JOIN $uploadTreeTableName UT ON C.pfile_fk = UT.pfile_fk
204  WHERE C.content IS NOT NULL
205  AND C.content!=''
206  AND C.is_enabled='true'
207  $extendWClause
208  ORDER BY UT.uploadtree_pk, C.content DESC";
209  $this->dbManager->prepare($statementName, $sql);
210  $sqlResult = $this->dbManager->execute($statementName, $params);
211  $result = $this->dbManager->fetchAll($sqlResult);
212  $this->dbManager->freeResult($sqlResult);
213 
214  return $result;
215  }
216 
225  public function getEditedEntries($tableName, $uploadTreeTableName, $uploadId,
226  $decisionType, $extrawhere="")
227  {
228  $statementName = __METHOD__.$tableName.$uploadTreeTableName;
229  $params = array();
230  $extendWClause = null;
231 
232  if ($uploadTreeTableName === "uploadtree_a") {
233  $params[]= $uploadId;
234  $extendWClause .= " AND UT.upload_fk = $".count($params);
235  $statementName .= ".withUI";
236  }
237 
238  if (!empty($decisionType)) {
239  $params[]= $decisionType;
240  $extendWClause .= " AND clearing_decision_type_fk = $".count($params);
241  $statementName .= ".withDecisionType";
242  }
243 
244  if (!empty($extrawhere)) {
245  $extendWClause .= " AND ". $extrawhere;
246  $statementName .= "._".$extrawhere."_";
247  }
248 
249  $columns = "CD.description as description, CD.textfinding as textfinding, CD.comment as comments, UT.uploadtree_pk as uploadtree_pk";
250 
251  $primaryColumn = $tableName . '_pk';
252  $sql = "SELECT $columns
253  FROM $tableName CD
254  INNER JOIN $uploadTreeTableName UT ON CD.pfile_fk = UT.pfile_fk
255  WHERE CD.is_enabled = 'true'
256  $extendWClause
257  ORDER BY CD.pfile_fk, UT.uploadtree_pk, CD.textfinding, CD.$primaryColumn DESC";
258  $this->dbManager->prepare($statementName, $sql);
259  $sqlResult = $this->dbManager->execute($statementName, $params);
260  $result = $this->dbManager->fetchAll($sqlResult);
261  $this->dbManager->freeResult($sqlResult);
262 
263  return $result;
264  }
265 
276  public function getAllEntriesReport($tableName, $uploadId, $uploadTreeTableName, $type=null, $onlyCleared=false, $decisionType=null, $extrawhere=null, $groupId=null)
277  {
278  $tableNameDecision = $tableName."_decision";
279  if ($tableName == 'copyright') {
280  $scannerEntries = $this->getScannerEntries($tableName, $uploadTreeTableName, $uploadId, $type, $extrawhere);
281  if (!empty($groupId)) {
282  $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTableName);
283  $irrelevantDecisions = $this->clearingDao->getFilesForDecisionTypeFolderLevel($itemTreeBounds, $groupId);
284  $uniqueIrrelevantDecisions = array_unique(array_column($irrelevantDecisions, 'uploadtree_pk'));
285  foreach ($scannerEntries as $key => $value) {
286  if (in_array($value['uploadtree_pk'], $uniqueIrrelevantDecisions)) {
287  unset($scannerEntries[$key]);
288  }
289  }
290  }
291  $editedEntries = $this->getEditedEntries($tableNameDecision, $uploadTreeTableName, $uploadId, $decisionType);
292  return array_merge($scannerEntries, $editedEntries);
293  } else {
294  return $this->getEditedEntries($tableNameDecision, $uploadTreeTableName, $uploadId, $decisionType);
295  }
296  }
297 
298  public function getAllEntries($tableName, $uploadId, $uploadTreeTableName, $type=null, $onlyCleared=false, $decisionType=null, $extrawhere=null)
299  {
300  $statementName = __METHOD__.$tableName.$uploadTreeTableName;
301 
302  $params = array();
303  $whereClause = "";
304  $distinctContent = "";
305  $tableNameDecision = $tableName."_decision";
306 
307  if ($uploadTreeTableName === "uploadtree_a") {
308  $params []= $uploadId;
309  $whereClause .= " AND UT.upload_fk = $".count($params);
310  $statementName .= ".withUI";
311  }
312  if ($type !== null && $type != "skipcontent") {
313  $params []= $type;
314  $whereClause .= " AND C.type = $".count($params);
315  $statementName .= ".withType";
316  }
317 
318  $clearingTypeClause = null;
319  if ($onlyCleared) {
320  $joinType = "INNER";
321  if ($decisionType !== null) {
322  $params []= $decisionType;
323  $clearingTypeClause = "WHERE clearing_decision_type_fk = $".count($params);
324  $statementName .= ".withDecisionType";
325  } else {
326  throw new \Exception("requested only cleared but no type given");
327  }
328  } else {
329  $joinType = "LEFT";
330  if ($decisionType !== null) {
331  $params []= $decisionType;
332  $clearingTypeClause = "WHERE clearing_decision_type_fk IS NULL OR clearing_decision_type_fk = $".count($params);
333  $statementName .= ".withDecisionType";
334  }
335  }
336  $statementName .= ".".$joinType."Join";
337 
338  if ($extrawhere !== null) {
339  $whereClause .= "AND ". $extrawhere;
340  $statementName .= "._".$extrawhere."_";
341  }
342  $decisionTableKey = $tableNameDecision . "_pk";
343 
344  $latestInfo = "SELECT DISTINCT ON(CD.pfile_fk, UT.uploadtree_pk, C.content, CD.textfinding)
345  CD.description as description, CD.textfinding as textfinding,
346  CD.comment as comments, UT.uploadtree_pk as uploadtree_pk,
347  CD.clearing_decision_type_fk AS clearing_decision_type_fk,
348  C.content AS content
349  FROM $tableName C
350  INNER JOIN $uploadTreeTableName UT
351  ON C.pfile_fk = UT.pfile_fk
352  $joinType JOIN (SELECT * FROM $tableNameDecision WHERE is_enabled='true') AS CD
353  ON C.pfile_fk = CD.pfile_fk
354  WHERE C.content IS NOT NULL
355  AND C.content!=''
356  AND C.is_enabled='true'
357  $whereClause
358  ORDER BY CD.pfile_fk, UT.uploadtree_pk, C.content, CD.textfinding, CD.$decisionTableKey DESC";
359 
360  if ($clearingTypeClause !== null) {
361  $sql = "SELECT * FROM ($latestInfo) AS latestInfo $clearingTypeClause";
362  } else {
363  $sql = $latestInfo;
364  }
365 
366  $this->dbManager->prepare($statementName, $sql);
367  $sqlResult = $this->dbManager->execute($statementName, $params);
368  $result = $this->dbManager->fetchAll($sqlResult);
369  $this->dbManager->freeResult($sqlResult);
370 
371  return $result;
372  }
373 
379  public function getDecisions($tableName,$pfileId)
380  {
381  $statementName = __METHOD__.$tableName;
382  $orderTablePk = $tableName.'_pk';
383  $sql = "SELECT * FROM $tableName where pfile_fk = $1 and is_enabled order by $orderTablePk desc";
384  $params = array($pfileId);
385 
386  return $this->dbManager->getRows($sql, $params, $statementName);
387  }
388 
402  public function getDecisionsFromHash($tableName, $hash, $upload = null, $uploadtreetable = null)
403  {
404  $statementName = __METHOD__ . ".$tableName";
405  $orderTablePk = $tableName.'_pk';
406  $join = "";
407  $joinWhere = "";
408  $params = [$hash];
409 
410  if ($upload != null) {
411  if (empty($uploadtreetable)) {
412  return -1;
413  }
414  $statementName.= ".filterUpload";
415  $params[] = $upload;
416  $join = "INNER JOIN $uploadtreetable AS ut ON cp.pfile_fk = ut.pfile_fk";
417  $joinWhere = "AND ut.upload_fk = $" . count($params);
418  }
419 
420  $sql = "SELECT * FROM $tableName AS cp $join " .
421  "WHERE cp.hash = $1 $joinWhere ORDER BY $orderTablePk;";
422 
423  return $this->dbManager->getRows($sql, $params, $statementName);
424  }
425 
433  public function updateTable($item, $hash, $content, $userId, $cpTable='copyright', $action='')
434  {
435  $itemTable = $item->getUploadTreeTableName();
436  $stmt = __METHOD__.".$cpTable.$itemTable";
437  $params = array($hash,$item->getLeft(),$item->getRight());
438 
439  if ($action == "delete") {
440  $setSql = "is_enabled='false'";
441  $stmt .= '.delete';
442  } else if ($action == "rollback") {
443  $setSql = "is_enabled='true'";
444  $stmt .= '.rollback';
445  } else {
446  $setSql = "content = $4, hash = md5($4), is_enabled='true'";
447  $params[] = $content;
448  }
449 
450  $cpTablePk = $cpTable."_pk";
451  $sql = "UPDATE $cpTable AS cpr SET $setSql
452  FROM $cpTable as cp
453  INNER JOIN $itemTable AS ut ON cp.pfile_fk = ut.pfile_fk
454  WHERE cpr.$cpTablePk = cp.$cpTablePk
455  AND cp.hash = $1
456  AND ( ut.lft BETWEEN $2 AND $3 )";
457  if ('uploadtree_a' == $item->getUploadTreeTableName()) {
458  $params[] = $item->getUploadId();
459  $sql .= " AND ut.upload_fk=$".count($params);
460  $stmt .= '.upload';
461  }
462 
463  $this->dbManager->prepare($stmt, "$sql");
464  $resource = $this->dbManager->execute($stmt, $params);
465  $this->dbManager->freeResult($resource);
466  }
467 }
saveDecision($tableName, $pfileId, $userId, $clearingType, $description, $textFinding, $comment, $decision_pk=-1)
updateTable($item, $hash, $content, $userId, $cpTable='copyright', $action='')
getScannerEntries($tableName, $uploadTreeTableName, $uploadId, $type, $extrawhere)
getEditedEntries($tableName, $uploadTreeTableName, $uploadId, $decisionType, $extrawhere="")
getAllEntriesReport($tableName, $uploadId, $uploadTreeTableName, $type=null, $onlyCleared=false, $decisionType=null, $extrawhere=null, $groupId=null)
getDecisionsFromHash($tableName, $hash, $upload=null, $uploadtreetable=null)
Get all the decisions based on hash.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
getHighlights($uploadTreeId, $tableName="copyright", $agentId=0, $typeToHighlightTypeMap=array( 'statement'=> Highlight::COPYRIGHT, 'email'=> Highlight::EMAIL, 'url'=> Highlight::URL, 'author'=> Highlight::AUTHOR))
getDecisions($tableName, $pfileId)