61 'shortname'=>array(
'shortname',
'Short Name'),
62 'fullname'=>array(
'fullname',
'Long Name'),
63 'text'=>array(
'text',
'Full Text'),
64 'parent_shortname'=>array(
'parent_shortname',
'Decider Short Name'),
65 'report_shortname'=>array(
'report_shortname',
'Regular License Text Short Name'),
66 'url'=>array(
'url',
'URL'),
67 'notes'=>array(
'notes'),
68 'source'=>array(
'source',
'Foreign ID'),
69 'risk'=>array(
'risk',
'risk_level'),
70 'group'=>array(
'group',
'License group')
110 if (!is_file($filename) || ($handle = fopen($filename,
'r')) ===
false) {
111 return _(
'Internal error');
116 while (($row = fgetcsv($handle,0,$this->delimiter,$this->enclosure)) !==
false) {
123 $msg .= _(
'Read csv').(
": $cnt ")._(
'licenses');
126 return $msg .= _(
'Error while parsing file').
': '.$e->getMessage();
140 if ($this->headrow === null) {
146 foreach (array(
'shortname',
'fullname',
'text') as $needle) {
147 $mRow[$needle] = $row[$this->headrow[$needle]];
149 foreach (array(
'parent_shortname' => null,
'report_shortname' => null,
150 'url' =>
'',
'notes' =>
'',
'source' =>
'',
'risk' => 0,
151 'group' => null) as $optNeedle=>$defaultValue) {
152 $mRow[$optNeedle] = $defaultValue;
153 if ($this->headrow[$optNeedle]!==
false && array_key_exists($this->headrow[$optNeedle], $row)) {
154 $mRow[$optNeedle] = $row[$this->headrow[$optNeedle]];
170 foreach (array(
'shortname',
'fullname',
'text') as $needle) {
171 $col = ArrayOperation::multiSearch($this->alias[$needle], $row);
172 if (
false === $col) {
173 throw new \Exception(
"Undetermined position of $needle");
177 foreach (array(
'parent_shortname',
'report_shortname',
'url',
'notes',
178 'source',
'risk',
'group') as $optNeedle) {
179 $headrow[$optNeedle] = ArrayOperation::multiSearch($this->alias[$optNeedle], $row);
192 $stmt = __METHOD__ .
'.getOldLicense';
193 $oldLicense = $this->
dbManager->getSingleRow(
'SELECT ' .
194 'rf_shortname, rf_fullname, rf_text, rf_url, rf_notes, rf_source, rf_risk ' .
195 'FROM license_ref WHERE rf_pk = $1', array($rfPk), $stmt);
197 $stmt = __METHOD__ .
'.getOldMapping';
198 $sql =
'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;';
200 $oldParentRow = $this->
dbManager->getSingleRow($sql, array($rfPk,
201 LicenseMap::CONCLUSION), $stmt);
202 if (!empty($oldParentRow)) {
203 $oldParent = $oldParentRow[
'rf_parent'];
206 $oldReportRow = $this->
dbManager->getSingleRow($sql, array($rfPk,
207 LicenseMap::REPORT), $stmt);
208 if (!empty($oldReportRow)) {
209 $oldReport = $oldReportRow[
'rf_parent'];
213 $newParent = ($row[
'parent_shortname'] == null) ? null :
217 $newReport = ($row[
'report_shortname'] == null) ? null :
220 $log =
"License '$row[shortname]' already exists in DB (id = $rfPk)";
221 $stmt = __METHOD__ .
'.updateLicense';
222 $sql =
"UPDATE license_ref SET ";
223 if (! empty($row[
'group'])) {
224 $sql =
"UPDATE license_candidate SET ";
226 $extraParams = array();
227 $param = array($rfPk);
228 if (!empty($row[
'fullname']) && $row[
'fullname'] != $oldLicense[
'rf_fullname']) {
229 $param[] = $row[
'fullname'];
231 $extraParams[] =
"rf_fullname=$" . count($param);
232 $log .=
", updated fullname";
234 if (!empty($row[
'text']) && $row[
'text'] != $oldLicense[
'rf_text']) {
235 $param[] = $row[
'text'];
237 $extraParams[] =
"rf_text=$" . count($param) .
",rf_md5=md5($" .
239 $log .=
", updated text";
241 if (!empty($row[
'url']) && $row[
'url'] != $oldLicense[
'rf_url']) {
242 $param[] = $row[
'url'];
244 $extraParams[] =
"rf_url=$" . count($param);
245 $log .=
", updated URL";
247 if (!empty($row[
'notes']) && $row[
'notes'] != $oldLicense[
'rf_notes']) {
248 $param[] = $row[
'notes'];
250 $extraParams[] =
"rf_notes=$" . count($param);
251 $log .=
", updated notes";
253 if (!empty($row[
'source']) && $row[
'source'] != $oldLicense[
'rf_source']) {
254 $param[] = $row[
'source'];
255 $stmt .=
'.updSource';
256 $extraParams[] =
"rf_source=$".count($param);
257 $log .=
', updated the source';
259 if (!empty($row[
'risk']) && $row[
'risk'] != $oldLicense[
'rf_risk']) {
260 $param[] = $row[
'risk'];
262 $extraParams[] =
"rf_risk=$".count($param);
263 $log .=
', updated the risk level';
265 if (count($param) > 1) {
266 $sql .= join(
",", $extraParams);
267 $sql .=
" WHERE rf_pk=$1;";
268 $this->
dbManager->getSingleRow($sql, $param, $stmt);
269 $this->mdkMap[md5($row[
'text'])] = $rfPk;
272 if (($oldParent != $newParent) && $this->
setMap($newParent, $rfPk, LicenseMap::CONCLUSION)) {
273 $log .=
" with conclusion '$row[parent_shortname]'";
275 if (($oldReport != $newReport) && $this->
setMap($newReport, $rfPk, LicenseMap::REPORT)) {
276 $log .=
" reporting '$row[report_shortname]'";
291 if (empty($row[
'risk'])) {
299 if ($rfPk !==
false) {
300 if (! empty($row[
'group']) || ($md5Match == $rfPk || $md5Match ===
false)) {
303 return "Error: MD5 checksum of '" . $row[
'shortname'] .
304 "' collides with license id=$md5Match";
307 if ($md5Match !==
false && empty($row[
'group'])) {
308 return "Error: MD5 checksum of '" . $row[
'shortname'] .
309 "' collides with license id=$md5Match";
313 if (!empty($row[
'group'])) {
334 $isNontrivial = ($fromName!==null && $fromName!=$toName && $this->
getKeyFromShortname($fromName)!==
false);
336 $this->
dbManager->insertTableRow(
'license_map',
341 return $isNontrivial;
351 $keyName = $shortname;
352 $tableName =
"license_ref";
354 $statement = __METHOD__ .
".getId";
355 $params = array($shortname);
357 if ($groupFk != null) {
358 $keyName .= $groupFk;
359 $tableName =
"license_candidate";
360 $addCondition =
"AND group_fk = $2";
361 $statement .=
".candidate";
362 $params[] = $this->userDao->getGroupIdByName($groupFk);
364 $sql =
"SELECT rf_pk FROM ONLY $tableName WHERE rf_shortname = $1 $addCondition;";
365 if (array_key_exists($keyName, $this->nkMap)) {
366 return $this->nkMap[$keyName];
368 $row = $this->
dbManager->getSingleRow($sql, $params, $statement);
369 $this->nkMap[$keyName] = ($row===
false) ?
false : $row[
'rf_pk'];
370 return $this->nkMap[$keyName];
380 $md5 = md5($licenseText);
381 if (array_key_exists($md5, $this->mdkMap)) {
382 return $this->mdkMap[$md5];
384 $row = $this->
dbManager->getSingleRow(
"SELECT rf_pk " .
385 "FROM ONLY license_ref WHERE rf_md5=md5($1)",
386 array($licenseText));
387 $this->mdkMap[$md5] = (empty($row)) ?
false : $row[
'rf_pk'];
388 return $this->mdkMap[$md5];
401 private function setMap($from, $to, $usage)
405 $sql =
"SELECT license_map_pk, rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
406 $statement = __METHOD__ .
".getCurrentMapping";
407 $row = $this->
dbManager->getSingleRow($sql, array($to, $usage), $statement);
408 if (!empty($row) && $row[
'rf_parent'] != $from) {
409 $this->
dbManager->updateTableRow(
"license_map", array(
411 'rf_parent' => $from,
413 ),
'license_map_pk', $row[
'license_map_pk']);
415 } elseif (empty($row)) {
416 $this->
dbManager->insertTableRow(
'license_map', array(
418 'rf_parent' => $from,
438 $stmtInsert = __METHOD__ .
'.insert.' . $tableName;
440 "rf_shortname" => $row[
'shortname'],
441 "rf_fullname" => $row[
'fullname'],
442 "rf_text" => $row[
'text'],
443 "rf_md5" => md5($row[
'text']),
444 "rf_detector_type" => 1,
445 "rf_url" => $row[
'url'],
446 "rf_notes" => $row[
'notes'],
447 "rf_source" => $row[
'source'],
448 "rf_risk" => $row[
'risk']
452 if ($tableName ==
"license_candidate") {
453 $groupId = $this->userDao->getGroupIdByName($row[
'group']);
454 if (empty($groupId)) {
455 return "Error: Unable to insert candidate license " . $row[
'shortname'] .
456 " as group " . $row[
'group'] .
" does not exist";
458 $columns[
"group_fk"] = $groupId;
459 $columns[
"marydone"] = $this->
dbManager->booleanToDb(
true);
460 $as =
" as candidate license under group " . $row[
"group"];
463 $newPk = $this->
dbManager->insertTableRow($tableName, $columns, $stmtInsert,
'rf_pk');
465 if ($tableName ==
"license_candidate") {
466 $this->nkMap[$row[
'shortname'].$row[
'group']] = $newPk;
468 $this->nkMap[$row[
'shortname']] = $newPk;
470 $this->mdkMap[md5($row[
'text'])] = $newPk;
471 $return =
"Inserted '$row[shortname]' in DB" . $as;
473 if ($this->
insertMapIfNontrivial($row[
'parent_shortname'], $row[
'shortname'], LicenseMap::CONCLUSION)) {
474 $return .=
" with conclusion '$row[parent_shortname]'";
477 $return .=
" reporting '$row[report_shortname]'";
getKeyFromMd5($licenseText)
handleFile($filename)
Read the CSV line by line and import it.
getKeyFromShortname($shortname, $groupFk=null)
Get the license id using license shortname from DB or nkMap.
updateLicense($row, $rfPk)
Update the license info in the DB.
Utility functions for specific applications.
Import licenses from CSV.
setMap($from, $to, $usage)
Update license mappings.
insertMapIfNontrivial($fromName, $toName, $usage)
Insert in license_map table if the license conclusion is non-trivial.
__construct(DbManager $dbManager, UserDao $userDao)
fo_dbManager * dbManager
fo_dbManager object
handleCsvLicense($row)
Handle a single row from CSV.
setDelimiter($delimiter=',')
Update the delimiter.
setEnclosure($enclosure='"')
Update the enclosure.
insertNewLicense($row, $tableName="license_ref")
Insert a new license in DB.
handleHeadCsv($row)
Handle a row as head row.