FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
HighlightDao.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 Authors: Daniele Fognini, Steffen Weber, 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 {
32  private $dbManager;
33 
37  private $logger;
38 
39  private $typeMap;
40 
41  function __construct(DbManager $dbManager)
42  {
43  $this->dbManager = $dbManager;
44  $this->logger = new Logger(self::class);
45 
46  $this->typeMap = array(
47  'M' => Highlight::MATCH,
48  'M ' => Highlight::MATCH,
49  'M0' => Highlight::MATCH,
50  'M+' => Highlight::ADDED,
51  'M-' => Highlight::DELETED,
52  'MR' => Highlight::CHANGED,
53  'L' => Highlight::SIGNATURE,
54  'L ' => Highlight::SIGNATURE,
55  'K' => Highlight::KEYWORD,
56  'K ' => Highlight::KEYWORD,
57  );
58  }
59 
67  public function getHighlightDiffs(ItemTreeBounds $itemTreeBounds, $licenseId = null, $agentId = null, $highlightId = null)
68  {
69  $params =array($itemTreeBounds->getItemId());
70  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
71 
72  $sql = "SELECT start,len,type,rf_fk,rf_start,rf_len
73  FROM license_file
74  INNER JOIN highlight ON license_file.fl_pk = highlight.fl_fk
75  INNER JOIN $uploadTreeTableName ut ON ut.pfile_fk = license_file.pfile_fk
76  WHERE uploadtree_pk = $1 AND (type LIKE 'M_' OR type = 'L')";
77 
78  $stmt = __METHOD__.$uploadTreeTableName;
79  if (!empty($licenseId) && empty($highlightId)) {
80  $params[] = $licenseId;
81  $stmt .= '.License';
82  $sql .= " AND license_file.rf_fk=$" . count($params);
83  }
84  if (!empty($agentId) && is_array($agentId)) {
85  $params[] = '{' . implode(',', $agentId) . '}';
86  $stmt .= '.AnyAgent';
87  $sql .= " AND license_file.agent_fk=ANY($" . count($params).")";
88  } else if (!empty($agentId)) {
89  $params[] = $agentId;
90  $stmt .= '.Agent';
91  $sql .= " AND license_file.agent_fk=$" . count($params);
92  }
93 
94  if (!empty($highlightId)) {
95  $params[] = $highlightId;
96  $stmt .= '.Highlight';
97  $sql .= " AND fl_pk=$" . count($params);
98  }
99  $this->dbManager->prepare($stmt, $sql);
100  $result = $this->dbManager->execute($stmt, $params);
101  $highlightEntries = array();
102  while ($row = $this->dbManager->fetchArray($result)) {
103  $newHiglight = new Highlight(
104  intval($row['start']), intval($row['start'] + $row['len']),
105  $this->typeMap[$row['type']],
106  intval($row['rf_start']), intval($row['rf_start'] + $row['rf_len']));
107 
108  $licenseId = $row['rf_fk'];
109  if ($licenseId) {
110  $newHiglight->setLicenseId($licenseId);
111  }
112  $highlightEntries[] = $newHiglight;
113  }
114  $this->dbManager->freeResult($result);
115  return $highlightEntries;
116  }
117 
118  public function getHighlightRegion($licenseMatchId)
119  {
120  $row = $this->dbManager->getSingleRow(
121  "SELECT MIN(start) AS start, MAX(start+len) AS end FROM highlight WHERE fl_fk = $1",
122  array($licenseMatchId)
123  );
124  return false !== $row ? array($row['start'], $row['end']) : array(-1, -1);
125  }
126 
131  public function getHighlightKeywords(ItemTreeBounds $itemTreeBounds)
132  {
133  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
134  $stmt = __METHOD__.$uploadTreeTableName;
135  $sql = "SELECT start,len
136  FROM highlight_keyword AS hk
137  INNER JOIN $uploadTreeTableName AS ut
138  ON hk.pfile_fk = ut.pfile_fk
139  WHERE ut.uploadtree_pk = $1";
140  $this->dbManager->prepare($stmt, $sql);
141  $result = $this->dbManager->execute($stmt, array($itemTreeBounds->getItemId()));
142  $highlightEntries = array();
143  while ($row = $this->dbManager->fetchArray($result)) {
144  $highlightEntries[] = new Highlight(
145  intval($row['start']), intval($row['start'] + $row['len']),
146  Highlight::KEYWORD, 0, 0);
147  }
148  $this->dbManager->freeResult($result);
149  return $highlightEntries;
150  }
151 
157  public function getHighlightBulk($uploadTreeId, $clearingId = null)
158  {
159  $stmt = __METHOD__;
160  $sql = "SELECT h.clearing_event_fk, h.start, h.len, ce.rf_fk, rf_text
161  FROM clearing_event ce
162  INNER JOIN highlight_bulk h ON ce.clearing_event_pk = h.clearing_event_fk
163  INNER JOIN license_ref_bulk lrb ON lrb.lrb_pk = h.lrb_fk
164  WHERE ce.uploadtree_fk = $1";
165  $params = array($uploadTreeId);
166  if (!empty($clearingId)) {
167  $stmt .= ".clearingId";
168  $params[] = $clearingId;
169  $sql .= " AND h.clearing_event_fk = $" . count($params);
170  }
171  $this->dbManager->prepare($stmt, $sql);
172  $result = $this->dbManager->execute($stmt, $params);
173  $highlightEntries = array();
174  while ($row = $this->dbManager->fetchArray($result)) {
175  $newHighlight = new Highlight(
176  intval($row['start']), intval($row['start'] + $row['len']),
177  Highlight::BULK, 0, 0);
178  $newHighlight->setLicenseId($row['rf_fk']);
179  $highlightEntries[] = $newHighlight;
180  }
181  $this->dbManager->freeResult($result);
182  return $highlightEntries;
183  }
184 
193  public function getHighlightEntries(ItemTreeBounds $itemTreeBounds, $licenseId = null, $agentId = null, $highlightId = null, $clearingId = null)
194  {
195  $highlightDiffs = $this->getHighlightDiffs($itemTreeBounds, $licenseId, $agentId, $highlightId);
196  $highlightKeywords = $this->getHighlightKeywords($itemTreeBounds);
197  $highlightBulk = $this->getHighlightBulk($itemTreeBounds->getItemId(), $clearingId);
198  $highlightEntries = array_merge(array_merge($highlightDiffs,$highlightKeywords),$highlightBulk);
199  return $highlightEntries;
200  }
201 
206  public function getPageNumberOfHighlightEntry($licenseMatchId)
207  {
208  $row = $this->dbManager->getSingleRow(
209  "SELECT FLOOR(
210  (
211  SELECT start FROM highlight WHERE fl_fk=$1 ORDER BY start ASC LIMIT 1
212  ) / (
213  SELECT conf_value FROM sysconfig WHERE variablename LIKE 'BlockSizeText'
214  )::numeric
215  )
216  AS page;",
217  array($licenseMatchId)
218  );
219  return $row['page'];
220  }
221 }
getHighlightEntries(ItemTreeBounds $itemTreeBounds, $licenseId=null, $agentId=null, $highlightId=null, $clearingId=null)
getHighlightKeywords(ItemTreeBounds $itemTreeBounds)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
getHighlightBulk($uploadTreeId, $clearingId=null)
getPageNumberOfHighlightEntry($licenseMatchId)
getHighlightDiffs(ItemTreeBounds $itemTreeBounds, $licenseId=null, $agentId=null, $highlightId=null)