FOSSology  4.4.0
Open Source License Compliance by Open Source Software
dbmigrate_3.6-3.7.php
Go to the documentation of this file.
1 <?php
5 
6 /*
7  SPDX-FileCopyrightText: © 2019 Siemens AG
8  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
9 
10  SPDX-License-Identifier: GPL-2.0-only
11 */
12 
28 function moveCandidateLicenseMap($dbManager, $obPk, $verbose)
29 {
30  if($dbManager == NULL){
31  echo "No connection object passed!\n";
32  return false;
33  }
34 
35  $sql = "SELECT rf_fk FROM obligation_map WHERE rf_fk NOT IN (" .
36  "SELECT rf_pk FROM ONLY license_ref) AND ob_fk = $1;";
37  $statement = __METHOD__ . ".getLicenseList";
38  $licenses = $dbManager->getRows($sql, array($obPk), $statement);
39  foreach ($licenses as $license) {
40  $dbManager->begin();
41  if ($verbose) {
42  echo "* Moving license " . $license['rf_fk'] . " to candidate map of " .
43  "obligation $obPk *\n";
44  }
45  $sql = "SELECT om_pk FROM obligation_candidate_map WHERE " .
46  "ob_fk = $1 AND rf_fk = $2;";
47  $statement = __METHOD__ . ".checkMaping";
48  $exists = $dbManager->getSingleRow($sql, array($obPk, $license['rf_fk']),
49  $statement);
50  if (empty($exists)) {
51  $statement = __METHOD__ . ".insertCandidateMap";
52  $dbManager->insertTableRow("obligation_candidate_map", array(
53  "ob_fk" => $obPk,
54  "rf_fk" => $license['rf_fk']
55  ), $statement);
56  }
57 
58  $sql = "DELETE FROM obligation_map WHERE ob_fk = $1 AND rf_fk = $2;";
59  $statement = __METHOD__ . ".removeMap";
60  $dbManager->getSingleRow($sql, array($obPk, $license['rf_fk']), $statement);
61  $dbManager->commit();
62  }
63 }
64 
70 function checkMigrate3637Required($dbManager)
71 {
72  if($dbManager == NULL){
73  echo "No connection object passed!\n";
74  return false;
75  }
76  $requiredTables = array(
77  "obligation_map",
78  "obligation_candidate_map",
79  "license_ref"
80  );
81  $migRequired = true;
82  foreach ($requiredTables as $table) {
83  if (DB_TableExists($table) != 1) {
84  $migRequired = false;
85  break;
86  }
87  }
88  if ($migRequired) {
89  $sql = "SELECT count(*) AS cnt FROM obligation_map WHERE rf_fk NOT IN (" .
90  "SELECT rf_pk FROM ONLY license_ref);";
91  $statement = __METHOD__ . ".candCount";
92  $row = $dbManager->getSingleRow($sql, array(), $statement);
93  $migRequired = false;
94  if (array_key_exists("cnt", $row) && $row["cnt"] > 0) {
95  $migRequired = true;
96  }
97  }
98 
99  return $migRequired;
100 }
101 
107 function getLicConObligationMigrate($dbManager)
108 {
109  if($dbManager == NULL){
110  echo "No connection object passed!\n";
111  return false;
112  }
113  $requiredTables = array(
114  "obligation_map",
115  "license_ref",
116  "license_map"
117  );
118  foreach ($requiredTables as $table) {
119  if (DB_TableExists($table) != 1) {
120  return array();
121  }
122  }
123 
124  $sql = "WITH comp AS (" . LicenseMap::getMappedLicenseRefView() .
125  ") SELECT * FROM obligation_map WHERE rf_fk NOT IN " .
126  "(SELECT rf_pk FROM comp);";
127  $statement = __METHOD__ . ".conclusionMigrate";
128  return $dbManager->getRows($sql, array(LicenseMap::CONCLUSION), $statement);
129 }
130 
136 function moveObligation($dbManager, $verbose)
137 {
138  if($dbManager == NULL){
139  echo "No connection object passed!\n";
140  return false;
141  }
142 
143  $sql = "SELECT ob_pk FROM obligation_ref;";
144  $obligations = $dbManager->getRows($sql);
145  foreach ($obligations as $obligation) {
146  moveCandidateLicenseMap($dbManager, $obligation['ob_pk'], $verbose);
147  }
148 }
149 
156 {
157  if ($dbManager === NULL) {
158  echo "No connection object passed!\n";
159  return false;
160  }
161  if (DB_TableExists("upload_reuse") != 1) {
162  return false;
163  }
164  $stmt = __METHOD__;
165  $sql = "SELECT exists(SELECT 1 FROM upload_reuse WHERE reuse_mode = $1 LIMIT 1)::int";
166  $row = $dbManager->getSingleRow($sql, array(8), $stmt);
167 
168  if ($row['exists']) {
169  echo "*** Changing enhance reuse with main license value to 6 from 8 ***\n";
170  $stmt = __METHOD__ . "ReplaceValuesFrom8to6";
171  $dbManager->prepare($stmt,
172  "UPDATE upload_reuse
173  SET reuse_mode = $1 WHERE reuse_mode = $2"
174  );
175  $dbManager->freeResult($dbManager->execute($stmt, array(6,8)));
176  return true;
177  }
178  return false;
179 }
180 
191 function removeNonConcLicensesFromObligation($dbManager, $migrationData)
192 {
193  $obligationMap = new ObligationMap($dbManager);
194  $mappedLicenses = [];
195 
196  $sql = LicenseMap::getMappedLicenseRefView();
197  $params = array(LicenseMap::CONCLUSION);
198  $statement = __METHOD__ . ".getLicenseMap";
199  $rows = $dbManager->getRows($sql, $params, $statement);
200  foreach ($rows as $row) {
201  $mappedLicenses[$row["rf_origin"]] = $row;
202  }
203 
204  foreach ($migrationData as $row) {
205  $dbManager->begin();
206  $obligationId = $row["ob_fk"];
207  $licenseId = $row["rf_fk"];
208  $parentId = $mappedLicenses[$licenseId]["rf_pk"];
209 
210  $licenseName = $obligationMap->getShortnameFromId($row["rf_fk"]);
211  $obligationTopic = $obligationMap->getTopicNameFromId($row["ob_fk"]);
212  echo " Removed '$licenseName' license from '$obligationTopic' obligation";
213  if (! $obligationMap->isLicenseAssociated($obligationId, $parentId)) {
214  $obligationMap->associateLicenseWithObligation($obligationId, $parentId);
215  echo " replacing with parent license '" .
216  $obligationMap->getShortnameFromId($parentId) . "'";
217  }
218  echo ".\n";
219  $obligationMap->unassociateLicenseFromObligation($obligationId, $licenseId);
220  $dbManager->commit();
221  }
222 }
223 
229 function Migrate_36_37($dbManager, $verbose)
230 {
232  $candidateMigration = checkMigrate3637Required($dbManager);
233  $concLicenseMigration = getLicConObligationMigrate($dbManager);
234  if (! ($candidateMigration || (count($concLicenseMigration) > 0))) {
235  // Migration not required
236  return;
237  }
238  try {
239  if ($candidateMigration) {
240  echo "*** Moving candidate licenses from obligation map ***\n";
241  moveObligation($dbManager, $verbose);
242  }
243  if (count($concLicenseMigration) > 0) {
244  echo "*** Removed following obligation map (" .
245  count($concLicenseMigration) . ") ***\n";
246  removeNonConcLicensesFromObligation($dbManager, $concLicenseMigration);
247  }
248  } catch (Exception $e) {
249  echo "Something went wrong. Try running postinstall again!\n";
250  $dbManager->rollback();
251  }
252 }
Wrapper class for license map.
Definition: LicenseMap.php:19
Wrapper class for obligation map.
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:214
Migrate_36_37($dbManager, $verbose)
moveCandidateLicenseMap($dbManager, $obPk, $verbose)
Move licenses from obligation map to obligation candidate map.
removeNonConcLicensesFromObligation($dbManager, $migrationData)
Remove identified licenses from obligations.
moveObligation($dbManager, $verbose)
Get all obligations and move licenses.
checkMigrate3637Required($dbManager)
migrateReuseValueForEnhanceWithMainLicense($dbManager)
getLicConObligationMigrate($dbManager)