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