FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseCompatibilityRulesYamlImport.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
15 
26 {
29  protected $dbManager;
32  protected $userDao;
35  protected $licenseDao;
38  protected $compatibilityDao;
39 
40  protected $alias = [
41  'firstname' => ['firstname', 'mainname', 'First Name'],
42  'secondname' => ['secondname', 'subname', 'Second Name'],
43  'firsttype' => ['firsttype', 'maintype', 'First Type'],
44  'secondtype' => ['secondtype', 'subtype', 'Second Type'],
45  'compatibility' => ['compatibility', 'Compatibility'],
46  'comment' => ['comment', 'text', 'Comment']
47  ];
48 
59  {
60  $this->dbManager = $dbManager;
61  $this->userDao = $userDao;
62  $this->licenseDao = $licenseDao;
63  $this->compatibilityDao = $compatibilityDao;
64  }
65 
72  public function handleFile($filename)
73  {
74  if (!is_file($filename) || ($handle = fopen($filename, 'r')) === false) {
75  return _('Internal error');
76  }
77  $cnt = 0;
78  $msg = '';
79  $file_content = yaml_parse_file($filename);
80  try {
81  foreach ($file_content["rules"] as $row) {
82  $log = $this->handleYaml($row);
83  if (!empty($log)) {
84  $msg .= "$log\n";
85  }
86  $cnt++;
87  }
88  $msg .= _('Read yaml').(": $cnt ")._('license rules');
89  } catch(\Exception $e) {
90  fclose($handle);
91  return $msg . _('Error while parsing file') . ': ' . $e->getMessage();
92  }
93  fclose($handle);
94  return $msg;
95  }
96 
102  private function handleYaml($row)
103  {
104  $normalizedRow = [];
105  foreach (array_keys($this->alias) as $key) {
106  $col = ArrayOperation::multiSearch($this->alias[$key], array_keys($row));
107  if ($col === false) {
108  throw new \UnexpectedValueException("Unable to find key for $key in " .
109  "YAML");
110  }
111  $col = array_keys($row)[$col];
112  $normalizedRow[$key] = $row[$col];
113  }
114  if ($normalizedRow["firstname"] != null) {
115  $firstLicense = $this->licenseDao->getLicenseByShortName(
116  $normalizedRow["firstname"], null);
117  $firstLicenseId = $firstLicense->getId();
118  $normalizedRow["firstname"] = $firstLicenseId;
119  }
120  if ($normalizedRow["secondname"] != null) {
121  $secondLicense = $this->licenseDao->getLicenseByShortName(
122  $normalizedRow["secondname"], null);
123  $secondLicenseId = $secondLicense->getId();
124  $normalizedRow["secondname"] = $secondLicenseId;
125  }
126  return $this->handleYamlLicense($normalizedRow);
127  }
128 
135  private function updateLicense($row, $lrPk)
136  {
137  $rule = [];
138  $oldRule = $this->dbManager->getSingleRow("SELECT
139  compatibility, comment FROM license_rules WHERE lr_pk = $1",
140  [$lrPk], __METHOD__ . '.getOldRule');
141 
142  $old_comp= $this->dbManager->booleanFromDb($oldRule["compatibility"]);
143  $new_comp= filter_var($row["compatibility"], FILTER_VALIDATE_BOOLEAN);
144 
145  $log = [];
146  if ($old_comp != $new_comp) {
147  $rule["compatibility"] = $this->dbManager->booleanToDb($new_comp);
148  $log[] = "updated compatibility";
149  }
150  if (!empty($row['comment']) && $row['comment'] != $oldRule['comment']) {
151  $rule["comment"] = $row["comment"];
152  $log[] = "updated comment";
153  }
154  if (count($rule) > 1) {
155  try {
156  $this->compatibilityDao->updateRuleFromArray([$lrPk => $rule]);
157  } catch (\UnexpectedValueException $e) {
158  $log[] = $e->getMessage();
159  }
160  }
161  return join(", ", $log);
162  }
163 
172  private function handleYamlLicense($row)
173  {
174  $stmt = __METHOD__ . '.LicRules';
175  $sql = "SELECT lr_pk FROM license_rules WHERE ";
176  $extraParams = [];
177  $param = [];
178  $lr_pk = "";
179  if (!empty($row['firstname'])) {
180  $param[] = $row['firstname'];
181  $stmt .= '.lic1';
182  $extraParams[] = "first_rf_fk=$" . count($param);
183  }
184  if (!empty($row['secondname'])) {
185  $param[] = $row['secondname'];
186  $stmt .= '.lic2';
187  $extraParams[] = "second_rf_fk=$" . count($param);
188  }
189  if (!empty($row['firsttype'])) {
190  $param[] = $row['firsttype'];
191  $stmt .= '.type1';
192  $extraParams[] = "first_type=$" . count($param);
193  }
194  if (!empty($row['secondtype'])) {
195  $param[] = $row['secondtype'];
196  $stmt .= '.type2';
197  $extraParams[] = "second_type=$" . count($param);
198  }
199  if (count($param) == 2) {
200  $sql .= join(" AND ", $extraParams);
201  $res = $this->dbManager->getSingleRow($sql, $param, $stmt);
202  if ($res) {
203  $lr_pk = $res["lr_pk"];
204  }
205  }
206  if (!empty($lr_pk)) {
207  return $this->updateLicense($row, $lr_pk);
208  } else {
209  $return = $this->insertNewLicense($row);
210  }
211  return $return;
212  }
213 
219  private function insertNewLicense($row)
220  {
221  return $this->compatibilityDao->insertRule($row["firstname"],
222  $row["secondname"], $row["firsttype"], $row["secondtype"],
223  $row["comment"], $row["compatibility"]);
224  }
225 }
__construct(DbManager $dbManager, UserDao $userDao, LicenseDao $licenseDao, CompatibilityDao $compatibilityDao)
Fossology exception.
Definition: Exception.php:15
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Utility functions for specific applications.