FOSSology  4.4.0
Open Source License Compliance by Open Source Software
HighlightDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4  Authors: Daniele Fognini, Steffen Weber, Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
14 use Monolog\Logger;
15 
17 {
21  private $dbManager;
22 
26  private $logger;
27 
28  private $typeMap;
29 
30  function __construct(DbManager $dbManager)
31  {
32  $this->dbManager = $dbManager;
33  $this->logger = new Logger(self::class);
34 
35  $this->typeMap = array(
36  'M' => Highlight::MATCH,
37  'M ' => Highlight::MATCH,
38  'M0' => Highlight::MATCH,
39  'M+' => Highlight::ADDED,
40  'M-' => Highlight::DELETED,
41  'MR' => Highlight::CHANGED,
42  'L' => Highlight::SIGNATURE,
43  'L ' => Highlight::SIGNATURE,
44  'K' => Highlight::KEYWORD,
45  'K ' => Highlight::KEYWORD,
46  );
47  }
48 
56  public function getHighlightDiffs(ItemTreeBounds $itemTreeBounds, $licenseId = null, $agentId = null, $highlightId = null)
57  {
58  $params =array($itemTreeBounds->getItemId());
59  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
60 
61  $sql = "SELECT start,len,type,rf_fk,rf_start,rf_len
62  FROM license_file
63  INNER JOIN highlight ON license_file.fl_pk = highlight.fl_fk
64  INNER JOIN $uploadTreeTableName ut ON ut.pfile_fk = license_file.pfile_fk
65  WHERE uploadtree_pk = $1 AND (type LIKE 'M_' OR type = 'L')";
66 
67  $stmt = __METHOD__.$uploadTreeTableName;
68  if (!empty($licenseId) && empty($highlightId)) {
69  $params[] = $licenseId;
70  $stmt .= '.License';
71  $sql .= " AND license_file.rf_fk=$" . count($params);
72  }
73  if (!empty($agentId) && is_array($agentId)) {
74  $params[] = '{' . implode(',', $agentId) . '}';
75  $stmt .= '.AnyAgent';
76  $sql .= " AND license_file.agent_fk=ANY($" . count($params).")";
77  } else if (!empty($agentId)) {
78  $params[] = $agentId;
79  $stmt .= '.Agent';
80  $sql .= " AND license_file.agent_fk=$" . count($params);
81  }
82 
83  if (!empty($highlightId)) {
84  $params[] = $highlightId;
85  $stmt .= '.Highlight';
86  $sql .= " AND fl_pk=$" . count($params);
87  }
88  $this->dbManager->prepare($stmt, $sql);
89  $result = $this->dbManager->execute($stmt, $params);
90  $highlightEntries = array();
91  while ($row = $this->dbManager->fetchArray($result)) {
92  $newHiglight = new Highlight(
93  intval($row['start']), intval($row['start'] + $row['len']),
94  $this->typeMap[$row['type']],
95  intval($row['rf_start']), intval($row['rf_start'] + $row['rf_len']));
96 
97  $licenseId = $row['rf_fk'];
98  if ($licenseId) {
99  $newHiglight->setLicenseId($licenseId);
100  }
101  $highlightEntries[] = $newHiglight;
102  }
103  $this->dbManager->freeResult($result);
104  return $highlightEntries;
105  }
106 
107  public function getHighlightRegion($licenseMatchId)
108  {
109  $row = $this->dbManager->getSingleRow(
110  "SELECT MIN(start) AS start, MAX(start+len) AS end FROM highlight WHERE fl_fk = $1",
111  array($licenseMatchId)
112  );
113  return false !== $row ? array($row['start'], $row['end']) : array(-1, -1);
114  }
115 
120  public function getHighlightKeywords(ItemTreeBounds $itemTreeBounds)
121  {
122  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
123  $stmt = __METHOD__.$uploadTreeTableName;
124  $sql = "SELECT start,len
125  FROM highlight_keyword AS hk
126  INNER JOIN $uploadTreeTableName AS ut
127  ON hk.pfile_fk = ut.pfile_fk
128  WHERE ut.uploadtree_pk = $1";
129  $this->dbManager->prepare($stmt, $sql);
130  $result = $this->dbManager->execute($stmt, array($itemTreeBounds->getItemId()));
131  $highlightEntries = array();
132  while ($row = $this->dbManager->fetchArray($result)) {
133  $highlightEntries[] = new Highlight(
134  intval($row['start']), intval($row['start'] + $row['len']),
135  Highlight::KEYWORD, 0, 0);
136  }
137  $this->dbManager->freeResult($result);
138  return $highlightEntries;
139  }
140 
146  public function getHighlightBulk($uploadTreeId, $clearingId = null)
147  {
148  $stmt = __METHOD__;
149  $sql = "SELECT h.clearing_event_fk, h.start, h.len, ce.rf_fk, rf_text
150  FROM clearing_event ce
151  INNER JOIN highlight_bulk h ON ce.clearing_event_pk = h.clearing_event_fk
152  INNER JOIN license_ref_bulk lrb ON lrb.lrb_pk = h.lrb_fk
153  WHERE ce.uploadtree_fk = $1";
154  $params = array($uploadTreeId);
155  if (!empty($clearingId)) {
156  $stmt .= ".clearingId";
157  $params[] = $clearingId;
158  $sql .= " AND h.clearing_event_fk = $" . count($params);
159  }
160  $this->dbManager->prepare($stmt, $sql);
161  $result = $this->dbManager->execute($stmt, $params);
162  $highlightEntries = array();
163  while ($row = $this->dbManager->fetchArray($result)) {
164  $newHighlight = new Highlight(
165  intval($row['start']), intval($row['start'] + $row['len']),
166  Highlight::BULK, 0, 0);
167  $newHighlight->setLicenseId($row['rf_fk']);
168  $highlightEntries[] = $newHighlight;
169  }
170  $this->dbManager->freeResult($result);
171  return $highlightEntries;
172  }
173 
182  public function getHighlightEntries(ItemTreeBounds $itemTreeBounds, $licenseId = null, $agentId = null, $highlightId = null, $clearingId = null)
183  {
184  $highlightDiffs = $this->getHighlightDiffs($itemTreeBounds, $licenseId, $agentId, $highlightId);
185  $highlightKeywords = $this->getHighlightKeywords($itemTreeBounds);
186  $highlightBulk = $this->getHighlightBulk($itemTreeBounds->getItemId(), $clearingId);
187  return array_merge(array_merge($highlightDiffs,$highlightKeywords),$highlightBulk);
188  }
189 
194  public function getPageNumberOfHighlightEntry($licenseMatchId)
195  {
196  $row = $this->dbManager->getSingleRow(
197  "SELECT FLOOR(
198  (
199  SELECT start FROM highlight WHERE fl_fk=$1 ORDER BY start ASC LIMIT 1
200  ) / (
201  SELECT conf_value FROM sysconfig WHERE variablename LIKE 'BlockSizeText'
202  )::numeric
203  )
204  AS page;",
205  array($licenseMatchId)
206  );
207  return $row['page'];
208  }
209 }
getPageNumberOfHighlightEntry($licenseMatchId)
getHighlightKeywords(ItemTreeBounds $itemTreeBounds)
getHighlightEntries(ItemTreeBounds $itemTreeBounds, $licenseId=null, $agentId=null, $highlightId=null, $clearingId=null)
getHighlightBulk($uploadTreeId, $clearingId=null)
getHighlightDiffs(ItemTreeBounds $itemTreeBounds, $licenseId=null, $agentId=null, $highlightId=null)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16