FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ReportImportSink.php
1 <?php
2 /*
3  * Copyright (C) 2015-2017, Siemens AG
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 namespace Fossology\ReportImport;
19 
29 
30 require_once 'ReportImportConfiguration.php';
31 
33 {
34 
36  private $userDao;
38  private $licenseDao;
40  private $clearingDao;
42  private $copyrightDao;
44  protected $dbManager;
45 
47  protected $agent_pk = -1;
49  protected $groupId = -1;
51  protected $userId = -1;
53  protected $jobId = -1;
55  protected $nserIsAdmin = false;
56 
58  protected $configuration;
59 
72  function __construct($agent_pk, $userDao, $licenseDao, $clearingDao, $copyrightDao, $dbManager, $groupId, $userId, $jobId, $configuration)
73  {
74  $this->userDao = $userDao;
75  $this->clearingDao = $clearingDao;
76  $this->licenseDao = $licenseDao;
77  $this->copyrightDao = $copyrightDao;
78  $this->dbManager = $dbManager;
79  $this->agent_pk = $agent_pk;
80  $this->groupId = $groupId;
81  $this->userId = $userId;
82  $this->jobId = $jobId;
83 
84  $this->configuration = $configuration;
85 
86  $userRow = $userDao->getUserByPk($userId);
87  $this->userIsAdmin = $userRow["user_perm"] >= PLUGIN_DB_ADMIN;
88  }
89 
93  public function handleData($data)
94  {
95  $pfiles = $data->getPfiles();
96  if(sizeof($pfiles) === 0)
97  {
98  return;
99  }
100 
101  if($this->configuration->isCreateLicensesInfosAsFindings() ||
102  $this->configuration->isCreateConcludedLicensesAsFindings() ||
103  $this->configuration->isCreateConcludedLicensesAsConclusions())
104  {
105  $licenseInfosInFile = $data->getLicenseInfosInFile();
106  $licensesConcluded = $data->getLicensesConcluded();
107 
108  $licensePKsInFile = array();
109  foreach($licenseInfosInFile as $dataItem)
110  {
111  if (strcasecmp($dataItem->getLicenseId(), "noassertion") == 0)
112  {
113  continue;
114  }
115  $licenseId = $this->getIdForDataItemOrCreateLicense($dataItem, $this->groupId);
116  $licensePKsInFile[] = $licenseId;
117  }
118 
119  $licensePKsConcluded = array();
120  foreach ($licensesConcluded as $dataItem)
121  {
122  if (strcasecmp($dataItem->getLicenseId(), "noassertion") == 0)
123  {
124  continue;
125  }
126  $licenseId = $this->getIdForDataItemOrCreateLicense($dataItem, $this->groupId);
127  $licensePKsConcluded[$licenseId] = $dataItem->getCustomText();
128  }
129 
130  $this->insertLicenseInformationToDB($licensePKsInFile, $licensePKsConcluded, $pfiles);
131  }
132 
133  if($this->configuration->isAddCopyrightInformation())
134  {
135  $this->insertFoundCopyrightTextsToDB($data->getCopyrightTexts(),
136  $data->getPfiles());
137  }
138  }
139 
146  public function getIdForDataItemOrCreateLicense($dataItem, $groupId)
147  {
148  $licenseShortName = $dataItem->getLicenseId();
149  $license = $this->licenseDao->getLicenseByShortName($licenseShortName, $groupId);
150  if ($license !== null)
151  {
152  return $license->getId();
153  }
154  elseif (! $this->licenseDao->isNewLicense($licenseShortName, $groupId))
155  {
156  throw new \Exception('shortname already in use');
157  }
158  elseif ($dataItem->isSetLicenseCandidate())
159  {
160  echo "INFO: No license with shortname=\"$licenseShortName\" found ... ";
161 
162  $licenseCandidate = $dataItem->getLicenseCandidate();
163  if($this->configuration->isCreateLicensesAsCandidate() || !$this->userIsAdmin)
164  {
165  echo "Creating it as license candidate ...\n";
166  $licenseId = $this->licenseDao->insertUploadLicense($licenseShortName, $licenseCandidate->getText(), $groupId);
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  false,
175  0);
176  return $licenseId;
177  }
178  else
179  {
180  echo "creating it as license ...\n";
181  $licenseText = trim($licenseCandidate->getText());
182  return $this->licenseDao->insertLicense($licenseCandidate->getShortName(), $licenseText, $licenseCandidate->getSpdxCompatible());
183  }
184  }
185  return -1;
186  }
187 
193  private function insertLicenseInformationToDB($licensePKsInFile, $licensePKsConcluded, $pfiles)
194  {
195  if($this->configuration->isCreateLicensesInfosAsFindings())
196  {
197  $this->saveAsLicenseFindingToDB($licensePKsInFile, $pfiles);
198  }
199 
200  if($this->configuration->isCreateConcludedLicensesAsFindings())
201  {
202  $this->saveAsLicenseFindingToDB(array_keys($licensePKsConcluded), $pfiles);
203  }
204 
205  if($this->configuration->isCreateConcludedLicensesAsConclusions())
206  {
207  $removeLicenseIds = array();
208  foreach ($licensePKsInFile as $licenseId)
209  {
210  if(! array_key_exists($licenseId,$licensePKsConcluded))
211  {
212  $removeLicenseIds[] = $licenseId;
213  }
214  }
215  $this->saveAsDecisionToDB($licensePKsConcluded, $removeLicenseIds, $pfiles);
216  }
217  }
218 
224  private function saveAsDecisionToDB($addLicenseIds, $removeLicenseIds, $pfiles)
225  {
226  if(sizeof($addLicenseIds) == 0)
227  {
228  return;
229  }
230 
231  foreach ($pfiles as $pfile)
232  {
233  $eventIds = array();
234  foreach ($addLicenseIds as $licenseId => $licenseText)
235  {
236  // echo "add decision $licenseId to " . $pfile['uploadtree_pk'] . "\n";
237  $eventIds[] = $this->clearingDao->insertClearingEvent(
238  $pfile['uploadtree_pk'],
239  $this->userId,
240  $this->groupId,
241  $licenseId,
242  false,
243  ClearingEventTypes::IMPORT,
244  trim($licenseText),
245  '', // comment
246  $this->jobId);
247  }
248  foreach ($removeLicenseIds as $licenseId)
249  {
250  // echo "remove decision $licenseId from " . $pfile['uploadtree_pk'] . "\n";
251  $eventIds[] = $this->clearingDao->insertClearingEvent(
252  $pfile['uploadtree_pk'],
253  $this->userId,
254  $this->groupId,
255  $licenseId,
256  true,
257  ClearingEventTypes::IMPORT,
258  $licenseText,
259  '', // comment
260  $this->jobId);
261  }
262  $this->clearingDao->createDecisionFromEvents(
263  $pfile['uploadtree_pk'],
264  $this->userId,
265  $this->groupId,
266  $this->configuration->getConcludeLicenseDecisionType(),
267  DecisionScopes::ITEM,
268  $eventIds);
269  }
270  }
271 
276  private function saveAsLicenseFindingToDB($licenseIds, $pfiles)
277  {
278  foreach ($pfiles as $pfile)
279  {
280  foreach($licenseIds as $licenseId)
281  {
282  $this->dbManager->getSingleRow(
283  "INSERT INTO license_file (rf_fk, agent_fk, pfile_fk) VALUES ($1,$2,$3) RETURNING fl_pk",
284  array($licenseId, $this->agent_pk, $pfile['pfile_pk']),
285  __METHOD__."forReportImport");
286  }
287  }
288  }
289 
290  public function insertFoundCopyrightTextsToDB($copyrightTexts, $entries)
291  {
292  foreach ($copyrightTexts as $copyrightText)
293  {
294  $this->insertFoundCopyrightTextToDB($copyrightText, $entries);
295  }
296  }
297 
298  public function insertFoundCopyrightTextToDB($copyrightText, $entries)
299  {
300  $copyrightLines = array_map("trim", explode("\n",$copyrightText));
301  foreach ($copyrightLines as $copyrightLine)
302  {
303  if(empty($copyrightLine))
304  {
305  continue;
306  }
307 
308  foreach ($entries as $entry)
309  {
310  $this->saveAsCopyrightFindingToDB(trim($copyrightLine), $entry['pfile_pk']);
311  }
312  }
313  }
314 
315  private function saveAsCopyrightFindingToDB($content, $pfile_fk)
316  {
317  $curDecisions = $this->copyrightDao->getDecisions("copyright_decision", $pfile_fk);
318  foreach ($curDecisions as $decision)
319  {
320  if($decision['textfinding'] == $content){
321  return;
322  }
323  }
324 
325  $this->copyrightDao->saveDecision("copyright_decision", $pfile_fk, $this->userId , DecisionTypes::IDENTIFIED,
326  "", $content, "imported via reportImport");
327  }
328 }
__construct($agent_pk, $userDao, $licenseDao, $clearingDao, $copyrightDao, $dbManager, $groupId, $userId, $jobId, $configuration)
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
saveAsDecisionToDB($addLicenseIds, $removeLicenseIds, $pfiles)
int jobId
The id of the job.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
int agent_pk
Definition: agent.h:85
insertLicenseInformationToDB($licensePKsInFile, $licensePKsConcluded, $pfiles)
getIdForDataItemOrCreateLicense($dataItem, $groupId)
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695