50 'shortname'=>array(
'shortname',
'Short Name'),
51 'licensetype'=>array(
'licensetype',
'License Type'),
52 'fullname'=>array(
'fullname',
'Long Name'),
53 'spdx_id'=>array(
'spdx_id',
'SPDX ID'),
54 'text'=>array(
'text',
'Full Text'),
55 'parent_shortname'=>array(
'parent_shortname',
'Decider Short Name'),
56 'report_shortname'=>array(
'report_shortname',
'Regular License Text Short Name'),
57 'url'=>array(
'url',
'URL'),
58 'notes'=>array(
'notes'),
59 'source'=>array(
'source',
'Foreign ID'),
60 'risk'=>array(
'risk',
'risk_level'),
61 'group'=>array(
'group',
'License group'),
62 'obligations'=>array(
'obligations',
'License obligations')
102 if (!is_file($filename) || ($handle = fopen($filename,
'r')) ===
false) {
103 return _(
'Internal error');
108 if ($fileExtension ==
'csv') {
109 while (($row = fgetcsv($handle,0,$this->delimiter,$this->enclosure)) !==
false) {
116 $msg .= _(
'Read csv').(
": $cnt ")._(
'licenses');
118 $jsonContent = fread($handle, filesize($filename));
119 $data = json_decode($jsonContent,
true);
120 if ($data ===
null && json_last_error() !== JSON_ERROR_NONE) {
121 $msg .=
"Error decoding JSON: " . json_last_error_msg().
"\n";
123 foreach ($data as $row) {
129 $msg .= _(
'Read json').(
":". count($data) .
" ")._(
'licenses');
133 return $msg .= _(
'Error while parsing file').
': '.$e->getMessage();
147 $defaultValues = array(
148 'parent_shortname' =>
null,
149 'report_shortname' =>
null,
158 foreach ($row as $key => $value) {
160 foreach ($this->alias as $aliasKey => $aliasValues) {
161 if (in_array($key, $aliasValues)) {
166 $newArray[$newKey] = $value;
168 foreach ($defaultValues as $key => $defaultValue) {
169 if (!array_key_exists($key, $newArray)) {
170 $newArray[$key] = $defaultValue;
184 if ($this->headrow ===
null) {
190 foreach (array(
'shortname',
'fullname',
'text') as $needle) {
191 $mRow[$needle] = $row[$this->headrow[$needle]];
193 foreach (array(
'parent_shortname' =>
null,
'report_shortname' =>
null,
194 'url' =>
'',
'notes' =>
'',
'source' =>
'',
'risk' => 0,
195 'group' =>
null,
'spdx_id' =>
null,
'licensetype' =>
"Permisssive"
196 ) as $optNeedle=>$defaultValue) {
197 $mRow[$optNeedle] = $defaultValue;
198 if ($this->headrow[$optNeedle]!==
false && array_key_exists($this->headrow[$optNeedle], $row)) {
199 $mRow[$optNeedle] = $row[$this->headrow[$optNeedle]];
215 $row[0] =
trim($row[0],
"\xEF\xBB\xBF");
216 foreach (array(
'shortname',
'fullname',
'text') as $needle) {
217 $col = ArrayOperation::multiSearch($this->alias[$needle], $row);
218 if (
false === $col) {
219 throw new \Exception(
"Undetermined position of $needle");
223 foreach (array(
'parent_shortname',
'report_shortname',
'url',
'notes',
224 'source',
'risk',
'group',
'spdx_id',
'licensetype') as $optNeedle) {
225 $headrow[$optNeedle] = ArrayOperation::multiSearch($this->alias[$optNeedle], $row);
238 $stmt = __METHOD__ .
'.getOldLicense';
239 $oldLicense = $this->
dbManager->getSingleRow(
'SELECT ' .
240 'rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype ' .
241 'FROM license_ref WHERE rf_pk = $1', array($rfPk), $stmt);
243 $stmt = __METHOD__ .
'.getOldMapping';
244 $sql =
'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;';
246 $oldParentRow = $this->
dbManager->getSingleRow($sql, array($rfPk,
247 LicenseMap::CONCLUSION), $stmt);
248 if (!empty($oldParentRow)) {
249 $oldParent = $oldParentRow[
'rf_parent'];
252 $oldReportRow = $this->
dbManager->getSingleRow($sql, array($rfPk,
253 LicenseMap::REPORT), $stmt);
254 if (!empty($oldReportRow)) {
255 $oldReport = $oldReportRow[
'rf_parent'];
259 $newParent = ($row[
'parent_shortname'] ==
null) ?
null :
263 $newReport = ($row[
'report_shortname'] ==
null) ?
null :
266 $log =
"License '$row[shortname]' already exists in DB (id = $rfPk)";
267 $stmt = __METHOD__ .
'.updateLicense';
268 $sql =
"UPDATE license_ref SET ";
269 if (! empty($row[
'group'])) {
270 $sql =
"UPDATE license_candidate SET ";
272 $extraParams = array();
273 $param = array($rfPk);
274 if (!empty($row[
'fullname']) && $row[
'fullname'] != $oldLicense[
'rf_fullname']) {
275 $param[] = $row[
'fullname'];
277 $extraParams[] =
"rf_fullname=$" . count($param);
278 $log .=
", updated fullname";
280 if (!empty($row[
'spdx_id']) && $row[
'spdx_id'] != $oldLicense[
'rf_spdx_id']) {
281 $param[] = $row[
'spdx_id'];
283 $extraParams[] =
"rf_spdx_id=$" . count($param);
284 $log .=
", updated SPDX ID";
286 if (!empty($row[
'text']) && $row[
'text'] != $oldLicense[
'rf_text'] && $row[
'text'] != LicenseMap::TEXT_MAX_CHAR_LIMIT) {
287 $param[] = $row[
'text'];
289 $extraParams[] =
"rf_text=$" . count($param) .
",rf_md5=md5($" .
291 $log .=
", updated text";
293 if (!empty($row[
'url']) && $row[
'url'] != $oldLicense[
'rf_url']) {
294 $param[] = $row[
'url'];
296 $extraParams[] =
"rf_url=$" . count($param);
297 $log .=
", updated URL";
299 if (!empty($row[
'notes']) && $row[
'notes'] != $oldLicense[
'rf_notes']) {
300 $param[] = $row[
'notes'];
302 $extraParams[] =
"rf_notes=$" . count($param);
303 $log .=
", updated notes";
305 if (!empty($row[
'source']) && $row[
'source'] != $oldLicense[
'rf_source']) {
306 $param[] = $row[
'source'];
307 $stmt .=
'.updSource';
308 $extraParams[] =
"rf_source=$".count($param);
309 $log .=
', updated the source';
311 if (!empty($row[
'risk']) && $row[
'risk'] != $oldLicense[
'rf_risk']) {
312 $param[] = $row[
'risk'];
314 $extraParams[] =
"rf_risk=$".count($param);
315 $log .=
', updated the risk level';
317 if (!empty($row[
'licensetype']) && $row[
'licensetype'] != $oldLicense[
'rf_licensetype']) {
318 $param[] = $row[
'licensetype'];
320 $extraParams[] =
"rf_licensetype=$".count($param);
321 $log .=
', updated the licensetype';
323 if (count($param) > 1) {
324 $sql .= join(
",", $extraParams);
325 $sql .=
" WHERE rf_pk=$1;";
326 $this->
dbManager->getSingleRow($sql, $param, $stmt);
327 $this->mdkMap[md5($row[
'text'])] = $rfPk;
330 if (($oldParent != $newParent) && $this->
setMap($newParent, $rfPk, LicenseMap::CONCLUSION)) {
331 $log .=
" with conclusion '$row[parent_shortname]'";
333 if (($oldReport != $newReport) && $this->
setMap($newReport, $rfPk, LicenseMap::REPORT)) {
334 $log .=
" reporting '$row[report_shortname]'";
349 if (empty($row[
'risk'])) {
357 if ($rfPk !==
false) {
358 if (! empty($row[
'group']) || ($md5Match == $rfPk || $md5Match ===
false)) {
361 return "Error: MD5 checksum of '" . $row[
'shortname'] .
362 "' collides with license id=$md5Match";
365 if ($md5Match !==
false && empty($row[
'group'])) {
366 return "Error: MD5 checksum of '" . $row[
'shortname'] .
367 "' collides with license id=$md5Match";
371 if (!empty($row[
'group'])) {
392 $isNontrivial = ($fromName!==
null && $fromName!=$toName && $this->
getKeyFromShortname($fromName)!==
false);
394 $this->
dbManager->insertTableRow(
'license_map',
399 return $isNontrivial;
409 $keyName = $shortname;
410 $tableName =
"license_ref";
412 $statement = __METHOD__ .
".getId";
413 $params = array($shortname);
415 if ($groupFk !=
null) {
416 $keyName .= $groupFk;
417 $tableName =
"license_candidate";
418 $addCondition =
"AND group_fk = $2";
419 $statement .=
".candidate";
420 $params[] = $this->userDao->getGroupIdByName($groupFk);
422 $sql =
"SELECT rf_pk FROM ONLY $tableName WHERE rf_shortname = $1 $addCondition;";
423 if (array_key_exists($keyName, $this->nkMap)) {
424 return $this->nkMap[$keyName];
426 $row = $this->
dbManager->getSingleRow($sql, $params, $statement);
427 $this->nkMap[$keyName] = ($row===
false) ?
false : $row[
'rf_pk'];
428 return $this->nkMap[$keyName];
438 $md5 = md5($licenseText);
439 if (array_key_exists($md5, $this->mdkMap)) {
440 return $this->mdkMap[$md5];
442 $row = $this->
dbManager->getSingleRow(
"SELECT rf_pk " .
443 "FROM ONLY license_ref WHERE rf_md5=md5($1)",
444 array($licenseText));
445 $this->mdkMap[$md5] = (empty($row)) ?
false : $row[
'rf_pk'];
446 return $this->mdkMap[$md5];
459 private function setMap($from, $to, $usage)
463 $sql =
"SELECT license_map_pk, rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
464 $statement = __METHOD__ .
".getCurrentMapping";
465 $row = $this->
dbManager->getSingleRow($sql, array($to, $usage), $statement);
466 if (!empty($row) && $row[
'rf_parent'] != $from) {
467 $this->
dbManager->updateTableRow(
"license_map", array(
469 'rf_parent' => $from,
471 ),
'license_map_pk', $row[
'license_map_pk']);
473 } elseif (empty($row)) {
474 $this->
dbManager->insertTableRow(
'license_map', array(
476 'rf_parent' => $from,
496 $stmtInsert = __METHOD__ .
'.insert.' . $tableName;
498 "rf_shortname" => $row[
'shortname'],
499 "rf_licensetype" => $row[
'licensetype'],
500 "rf_fullname" => $row[
'fullname'],
501 "rf_spdx_id" => $row[
'spdx_id'],
502 "rf_text" => $row[
'text'],
503 "rf_md5" => md5($row[
'text']),
504 "rf_detector_type" => 1,
505 "rf_url" => $row[
'url'],
506 "rf_notes" => $row[
'notes'],
507 "rf_source" => $row[
'source'],
508 "rf_risk" => $row[
'risk']
512 if ($tableName ==
"license_candidate") {
513 $groupId = $this->userDao->getGroupIdByName($row[
'group']);
514 if (empty($groupId)) {
515 return "Error: Unable to insert candidate license " . $row[
'shortname'] .
516 " as group " . $row[
'group'] .
" does not exist";
518 $columns[
"group_fk"] = $groupId;
519 $columns[
"marydone"] = $this->
dbManager->booleanToDb(
true);
520 $as =
" as candidate license under group " . $row[
"group"];
523 $newPk = $this->
dbManager->insertTableRow($tableName, $columns, $stmtInsert,
'rf_pk');
525 if ($tableName ==
"license_candidate") {
526 $this->nkMap[$row[
'shortname'].$row[
'group']] = $newPk;
528 $this->nkMap[$row[
'shortname']] = $newPk;
530 $this->mdkMap[md5($row[
'text'])] = $newPk;
531 $return =
"Inserted '$row[shortname]' in DB" . $as;
533 if ($this->
insertMapIfNontrivial($row[
'parent_shortname'], $row[
'shortname'], LicenseMap::CONCLUSION)) {
534 $return .=
" with conclusion '$row[parent_shortname]'";
537 $return .=
" reporting '$row[report_shortname]'";
Import licenses from CSV.
insertMapIfNontrivial($fromName, $toName, $usage)
Insert in license_map table if the license conclusion is non-trivial.
handleFile($filename, $fileExtension)
Read the CSV line by line and import it.
handleCsvLicense($row)
Handle a single row from CSV.
updateLicense($row, $rfPk)
Update the license info in the DB.
__construct(DbManager $dbManager, UserDao $userDao)
setEnclosure($enclosure='"')
Update the enclosure.
getKeyFromShortname($shortname, $groupFk=null)
Get the license id using license shortname from DB or nkMap.
handleHeadCsv($row)
Handle a row as head row.
insertNewLicense($row, $tableName="license_ref")
Insert a new license in DB.
setDelimiter($delimiter=',')
Update the delimiter.
setMap($from, $to, $usage)
Update license mappings.
getKeyFromMd5($licenseText)
Wrapper class for license map.
char * trim(char *ptext)
Trimming whitespace.
fo_dbManager * dbManager
fo_dbManager object
Utility functions for specific applications.