FOSSology  4.5.1
Open Source License Compliance by Open Source Software
ReportImportSink.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015-2017,2024 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 namespace Fossology\ReportImport;
8 
17 
18 require_once 'ReportImportConfiguration.php';
19 
21 {
22 
24  private $userDao;
26  private $licenseDao;
28  private $clearingDao;
30  private $copyrightDao;
32  protected $dbManager;
33 
35  protected $agent_pk = -1;
37  protected $groupId = -1;
39  protected $userId = -1;
41  protected $jobId = -1;
43  protected $nserIsAdmin = false;
44 
46  protected $configuration;
47 
61  function __construct($agent_pk, $userDao, $licenseDao, $clearingDao, $copyrightDao, $dbManager, $groupId, $userId, $jobId, $configuration)
62  {
63  $this->userDao = $userDao;
64  $this->clearingDao = $clearingDao;
65  $this->licenseDao = $licenseDao;
66  $this->copyrightDao = $copyrightDao;
67  $this->dbManager = $dbManager;
68  $this->agent_pk = $agent_pk;
69  $this->groupId = $groupId;
70  $this->userId = $userId;
71  $this->jobId = $jobId;
72 
73  $this->configuration = $configuration;
74 
75  $userRow = $userDao->getUserByPk($userId);
76  $this->userIsAdmin = $userRow["user_perm"] >= PLUGIN_DB_ADMIN;
77  }
78 
82  public function handleData($data)
83  {
84  $pfiles = $data->getPfiles();
85  if(sizeof($pfiles) === 0)
86  {
87  return;
88  }
89 
90  if($this->configuration->isCreateLicensesInfosAsFindings() ||
91  $this->configuration->isCreateConcludedLicensesAsFindings() ||
92  $this->configuration->isCreateConcludedLicensesAsConclusions())
93  {
94  $licenseInfosInFile = $data->getLicenseInfosInFile();
95  $licensesConcluded = $data->getLicensesConcluded();
96 
97  $licensePKsInFile = array();
98  foreach($licenseInfosInFile as $dataItem)
99  {
100  if (strcasecmp($dataItem->getLicenseId(), "noassertion") == 0)
101  {
102  continue;
103  }
104  $licenseId = $this->getIdForDataItemOrCreateLicense($dataItem, $this->groupId);
105  $licensePKsInFile[] = $licenseId;
106  }
107 
108  $licensePKsConcluded = array();
109  foreach ($licensesConcluded as $dataItem)
110  {
111  if (strcasecmp($dataItem->getLicenseId(), "noassertion") == 0)
112  {
113  continue;
114  }
115  $licenseId = $this->getIdForDataItemOrCreateLicense($dataItem, $this->groupId);
116  $licensePKsConcluded[$licenseId] = $dataItem->getCustomText();
117  }
118 
119  $this->insertLicenseInformationToDB($licensePKsInFile, $licensePKsConcluded, $pfiles);
120  }
121 
122  if($this->configuration->isAddCopyrightInformation())
123  {
124  $this->insertFoundCopyrightTextsToDB($data->getCopyrightTexts(),
125  $data->getPfiles());
126  }
127  }
128 
135  public function getIdForDataItemOrCreateLicense($dataItem, $groupId)
136  {
137  $licenseShortName = $dataItem->getLicenseId();
138  if ($this->configuration->shouldMatchLicenseNameWithSPDX()) {
139  $license = $this->licenseDao->getLicenseBySpdxId($licenseShortName, $groupId);
140  if ($license === null) {
141  echo "WARNING: Could not find license with spdx id '$licenseShortName' ... trying ShortName\n";
142  $license = $this->licenseDao->getLicenseByShortName($licenseShortName, $groupId);
143  }
144  } else {
145  $license = $this->licenseDao->getLicenseByShortName($licenseShortName, $groupId);
146  }
147  if ($license !== null)
148  {
149  return $license->getId();
150  }
151  elseif (! $this->licenseDao->isNewLicense($licenseShortName, $groupId))
152  {
153  throw new \Exception('shortname already in use');
154  }
155  elseif ($dataItem->isSetLicenseCandidate())
156  {
157  echo "INFO: No license with shortname=\"$licenseShortName\" found ... ";
158 
159  $licenseCandidate = $dataItem->getLicenseCandidate();
160  if($this->configuration->isCreateLicensesAsCandidate() || !$this->userIsAdmin)
161  {
162  echo "Creating it as license candidate ...\n";
163  $licenseId = $this->licenseDao->insertUploadLicense($licenseShortName,
164  $licenseCandidate->getText(), $groupId, $this->userId);
165  $this->licenseDao->updateCandidate(
166  $licenseId,
167  $licenseCandidate->getShortName(),
168  $licenseCandidate->getFullName(),
169  $licenseCandidate->getText(),
170  $licenseCandidate->getUrl(),
171  "Created for ReportImport with jobId=[".$this->jobId."]",
172  date(DATE_ATOM),
173  $this->userId,
174  false,
175  0,
176  $licenseCandidate->getShortName()
177  );
178  return $licenseId;
179  }
180  else
181  {
182  echo "creating it as license ...\n";
183  $licenseText = trim($licenseCandidate->getText());
184  return $this->licenseDao->insertLicense($licenseCandidate->getShortName(), $licenseText, $licenseCandidate->getShortName());
185  }
186  }
187  return -1;
188  }
189 
195  private function insertLicenseInformationToDB($licensePKsInFile, $licensePKsConcluded, $pfiles)
196  {
197  if($this->configuration->isCreateLicensesInfosAsFindings())
198  {
199  $this->saveAsLicenseFindingToDB($licensePKsInFile, $pfiles);
200  }
201 
202  if($this->configuration->isCreateConcludedLicensesAsFindings())
203  {
204  $this->saveAsLicenseFindingToDB(array_keys($licensePKsConcluded), $pfiles);
205  }
206 
207  if($this->configuration->isCreateConcludedLicensesAsConclusions())
208  {
209  $removeLicenseIds = array();
210  foreach ($licensePKsInFile as $licenseId)
211  {
212  if(! array_key_exists($licenseId,$licensePKsConcluded))
213  {
214  $removeLicenseIds[] = $licenseId;
215  }
216  }
217  $this->saveAsDecisionToDB($licensePKsConcluded, $removeLicenseIds, $pfiles);
218  }
219  }
220 
226  private function saveAsDecisionToDB($addLicenseIds, $removeLicenseIds, $pfiles)
227  {
228  if(sizeof($addLicenseIds) == 0)
229  {
230  return;
231  }
232 
233  foreach ($pfiles as $pfile)
234  {
235  $eventIds = array();
236  foreach ($addLicenseIds as $licenseId => $licenseText)
237  {
238  // echo "add decision $licenseId to " . $pfile['uploadtree_pk'] . "\n";
239  $eventIds[] = $this->clearingDao->insertClearingEvent(
240  $pfile['uploadtree_pk'],
241  $this->userId,
242  $this->groupId,
243  $licenseId,
244  false,
245  ClearingEventTypes::IMPORT,
246  trim($licenseText),
247  '', // comment
248  '', // ack
249  $this->jobId);
250  }
251  foreach ($removeLicenseIds as $licenseId)
252  {
253  // echo "remove decision $licenseId from " . $pfile['uploadtree_pk'] . "\n";
254  $eventIds[] = $this->clearingDao->insertClearingEvent(
255  $pfile['uploadtree_pk'],
256  $this->userId,
257  $this->groupId,
258  $licenseId,
259  true,
260  ClearingEventTypes::IMPORT,
261  $licenseText,
262  '', // comment
263  '', // ack
264  $this->jobId);
265  }
266  $this->clearingDao->createDecisionFromEvents(
267  $pfile['uploadtree_pk'],
268  $this->userId,
269  $this->groupId,
270  $this->configuration->getConcludeLicenseDecisionType(),
271  DecisionScopes::ITEM,
272  $eventIds);
273  }
274  }
275 
280  private function saveAsLicenseFindingToDB($licenseIds, $pfiles)
281  {
282  foreach ($pfiles as $pfile)
283  {
284  foreach($licenseIds as $licenseId)
285  {
286  $this->dbManager->getSingleRow(
287  "INSERT INTO license_file (rf_fk, agent_fk, pfile_fk) VALUES ($1,$2,$3) RETURNING fl_pk",
288  array($licenseId, $this->agent_pk, $pfile['pfile_pk']),
289  __METHOD__."forReportImport");
290  }
291  }
292  }
293 
294  public function insertFoundCopyrightTextsToDB($copyrightTexts, $entries)
295  {
296  foreach ($copyrightTexts as $copyrightText)
297  {
298  $this->insertFoundCopyrightTextToDB($copyrightText, $entries);
299  }
300  }
301 
302  public function insertFoundCopyrightTextToDB($copyrightText, $entries)
303  {
304  $copyrightLines = array_map("trim", explode("\n",$copyrightText));
305  foreach ($copyrightLines as $copyrightLine)
306  {
307  if(empty($copyrightLine))
308  {
309  continue;
310  }
311 
312  foreach ($entries as $entry)
313  {
314  $this->saveAsCopyrightFindingToDB(trim($copyrightLine), $entry['pfile_pk']);
315  }
316  }
317  }
318 
319  private function saveAsCopyrightFindingToDB($content, $pfile_fk)
320  {
321  $curDecisions = $this->copyrightDao->getDecisions("copyright_decision", $pfile_fk);
322  foreach ($curDecisions as $decision)
323  {
324  if($decision['textfinding'] == $content){
325  return;
326  }
327  }
328 
329  $this->copyrightDao->saveDecision("copyright_decision", $pfile_fk, $this->userId , DecisionTypes::IDENTIFIED,
330  "", $content, "imported via reportImport");
331  }
332 }
int agent_pk
Definition: agent.h:74
saveAsDecisionToDB($addLicenseIds, $removeLicenseIds, $pfiles)
insertLicenseInformationToDB($licensePKsInFile, $licensePKsConcluded, $pfiles)
getIdForDataItemOrCreateLicense($dataItem, $groupId)
__construct($agent_pk, $userDao, $licenseDao, $clearingDao, $copyrightDao, $dbManager, $groupId, $userId, $jobId, $configuration)
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
int jobId
The id of the job.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16