FOSSology  4.7.1
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  if ($firstLicense === null) {
118  throw new \UnexpectedValueException("Unable to find license " .
119  $normalizedRow["firstname"] . " from YAML");
120  }
121  $normalizedRow["firstname"] = $firstLicense->getId();
122  }
123  if ($normalizedRow["secondname"] != null) {
124  $secondLicense = $this->licenseDao->getLicenseByShortName(
125  $normalizedRow["secondname"], null);
126  if ($secondLicense === null) {
127  throw new \UnexpectedValueException("Unable to find license " .
128  $normalizedRow["secondname"] . " from YAML");
129  }
130  $normalizedRow["secondname"] = $secondLicense->getId();
131  }
132  return $this->handleYamlLicense($normalizedRow);
133  }
134 
141  private function updateLicense($row, $lrPk)
142  {
143  $rule = [];
144  $oldRule = $this->dbManager->getSingleRow("SELECT
145  compatibility, comment FROM license_rules WHERE lr_pk = $1",
146  [$lrPk], __METHOD__ . '.getOldRule');
147 
148  $old_comp= $this->dbManager->booleanFromDb($oldRule["compatibility"]);
149  $new_comp= filter_var($row["compatibility"], FILTER_VALIDATE_BOOLEAN);
150 
151  $log = [];
152  if ($old_comp != $new_comp) {
153  $rule["result"] = $new_comp;
154  $log[] = "updated compatibility";
155  }
156  if (!empty($row['comment']) && $row['comment'] != $oldRule['comment']) {
157  $rule["comment"] = $row["comment"];
158  $log[] = "updated comment";
159  }
160  if (!empty($rule)) {
161  try {
162  $this->compatibilityDao->updateRuleFromArray([$lrPk => $rule]);
163  } catch (\UnexpectedValueException $e) {
164  $log[] = $e->getMessage();
165  }
166  }
167  return join(", ", $log);
168  }
169 
178  private function handleYamlLicense($row)
179  {
180  $stmt = __METHOD__ . '.LicRules';
181  $sql = "SELECT lr_pk FROM license_rules WHERE ";
182  $extraParams = [];
183  $param = [];
184  $lr_pk = "";
185  if (!empty($row['firstname'])) {
186  $param[] = $row['firstname'];
187  $stmt .= '.lic1';
188  $extraParams[] = "first_rf_fk=$" . count($param);
189  }
190  if (!empty($row['secondname'])) {
191  $param[] = $row['secondname'];
192  $stmt .= '.lic2';
193  $extraParams[] = "second_rf_fk=$" . count($param);
194  }
195  if (!empty($row['firsttype'])) {
196  $param[] = $row['firsttype'];
197  $stmt .= '.type1';
198  $extraParams[] = "first_type=$" . count($param);
199  }
200  if (!empty($row['secondtype'])) {
201  $param[] = $row['secondtype'];
202  $stmt .= '.type2';
203  $extraParams[] = "second_type=$" . count($param);
204  }
205  if (count($param) == 2) {
206  $sql .= join(" AND ", $extraParams);
207  $res = $this->dbManager->getSingleRow($sql, $param, $stmt);
208  if ($res) {
209  $lr_pk = $res["lr_pk"];
210  }
211  }
212  if (!empty($lr_pk)) {
213  return $this->updateLicense($row, $lr_pk);
214  } else {
215  $return = $this->insertNewLicense($row);
216  }
217  return $return;
218  }
219 
225  private function insertNewLicense($row)
226  {
227  return $this->compatibilityDao->insertRule($row["firstname"],
228  $row["secondname"], $row["firsttype"], $row["secondtype"],
229  $row["comment"], $row["compatibility"]);
230  }
231 }
__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.