7 namespace Fossology\ReportImport;
11 require_once
'ReportImportData.php';
12 require_once
'ReportImportDataItem.php';
13 require_once
'ImportSource.php';
17 const TERMS =
'http://spdx.org/rdf/terms#';
18 const SPDX_URL =
'http://spdx.org/licenses/';
19 const SYNTAX_NS =
'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
30 private $licenseRefPrefix =
"LicenseRef-";
32 function __construct($filename, $uri =
null)
34 $this->filename = $filename;
43 $this->graph = $this->loadGraph($this->filename, $this->uri);
44 $this->index = $this->loadIndex($this->graph);
48 private function loadGraph($filename, $uri =
null)
52 $graph->parseFile($filename,
'rdfxml', $uri);
56 private function loadIndex($graph)
58 return $graph->toRdfPhp();
67 foreach ($this->index as $subject => $property){
83 $key = self::SYNTAX_NS .
'type';
84 $target = self::TERMS . $type;
86 return is_array ($property) &&
87 array_key_exists($key, $property) &&
88 sizeof($property[$key]) === 1 &&
89 $property[$key][0][
'type'] ===
"uri" &&
90 $property[$key][0][
'value'] === $target;
113 $hashItems = $this->
getValues($fileid,
'checksum');
116 $keyAlgo = self::TERMS .
'algorithm';
117 $algoKeyPrefix = self::TERMS .
'checksumAlgorithm_';
118 $keyAlgoVal = self::TERMS .
'checksumValue';
119 foreach ($hashItems as $hashItem)
121 $algorithm = $hashItem[$keyAlgo][0][
'value'];
122 if(substr($algorithm, 0, strlen($algoKeyPrefix)) === $algoKeyPrefix)
124 $algorithm = substr($algorithm, strlen($algoKeyPrefix));
126 $hashes[$algorithm] = $hashItem[$keyAlgoVal][0][
'value'];
138 private function getValue($propertyOrId, $key, $default=
null)
140 $values = $this->
getValues($propertyOrId, $key);
141 if(
sizeof($values) === 1)
155 if (is_string($propertyOrId))
157 $property = $this->index[$propertyOrId];
161 $property = $propertyOrId;
164 $key = self::TERMS . $key;
165 if (is_array($property) && isset($property[$key]))
168 foreach($property[$key] as $entry)
170 if($entry[
'type'] ===
'literal')
172 $values[] = $entry[
'value'];
174 elseif($entry[
'type'] ===
'uri')
176 if(array_key_exists($entry[
'value'],$this->index))
178 $values[$entry[
'value']] = $this->index[$entry[
'value']];
182 $values[] = $entry[
'value'];
185 elseif($entry[
'type'] ===
'bnode')
187 $values[$entry[
'value']] = $this->index[$entry[
'value']];
191 error_log(
"ERROR: can not handle entry=[".$entry.
"] of type=[" . $entry[
'type'] .
"]");
205 return $this->
getValue($propertyOrId,
'fileName');
226 private function stripLicenseRefPrefix($licenseId)
228 if(substr($licenseId, 0, strlen($this->licenseRefPrefix)) === $this->licenseRefPrefix)
230 return urldecode(substr($licenseId, strlen($this->licenseRefPrefix)));
234 return urldecode($licenseId);
238 private function isNotNoassertion($str)
240 return ! ( strtolower($str) === self::TERMS.
"noassertion" ||
241 strtolower($str) ===
"http://spdx.org/licenses/noassertion" );
244 private function parseLicenseId($licenseId)
246 if (!is_string($licenseId))
248 error_log(
"ERROR: Id not a string: ".$licenseId);
251 if (strtolower($licenseId) === self::TERMS.
"noassertion" ||
252 strtolower($licenseId) ===
"http://spdx.org/licenses/noassertion")
257 $license = $this->index[$licenseId];
261 return $this->parseLicense($license);
263 elseif(substr($licenseId, 0, strlen(self::SPDX_URL)) === self::SPDX_URL)
265 $spdxId = urldecode(substr($licenseId, strlen(self::SPDX_URL)));
266 $item =
new ReportImportDataItem($spdxId);
271 error_log(
"ERROR: can not handle license with ID=".$licenseId);
276 private function parseLicense($license)
278 if (is_string($license))
280 return $this->parseLicenseId($license);
284 $licenseId = $this->stripLicenseRefPrefix($this->
getValue($license,
'licenseId'));
286 if(strlen($licenseId) > 33 &&
287 substr($licenseId, -33, 1) ===
"-" &&
288 ctype_alnum(substr($licenseId, -32)))
290 $licenseId = substr($licenseId, 0, -33);
291 $item =
new ReportImportDataItem($licenseId);
292 $item->setCustomText($this->
getValue($license,
'extractedText'));
298 $item =
new ReportImportDataItem($licenseId);
299 $item->setLicenseCandidate($this->
getValue($license,
'name', $licenseId),
300 $this->
getValue($license,
'extractedText'),
301 strpos($this->
getValue($license,
'licenseId'), $this->licenseRefPrefix));
308 $licenseId = $this->stripLicenseRefPrefix($this->
getValue($license,
'licenseId'));
309 $item =
new ReportImportDataItem($licenseId);
310 $item->setLicenseCandidate($this->
getValue($license,
'name', $licenseId),
311 $this->
getValue($license,
'licenseText'),
312 strpos($this->
getValue($license,
'licenseId'), $this->licenseRefPrefix));
320 $subLicenses = $this->
getValues($license,
'member');
321 if (
sizeof($subLicenses) > 1 &&
324 $output[] =
new ReportImportDataItem(
"Dual-license");
326 foreach($subLicenses as $subLicense)
328 $innerOutput = $this->parseLicense($subLicense);
329 foreach($innerOutput as $innerItem)
331 $output[] = $innerItem;
339 $subLicenses = $this->
getValues($license,
'member');
340 foreach($subLicenses as $subLicense) {
342 $innerOutput = $this->parseLicense($subLicense);
343 foreach($innerOutput as $innerItem)
346 $item =
new ReportImportDataItem($innerItem->getLicenseId() .
"+");
348 $innerLicenseCandidate = $innerItem->getLicenseCandidate();
349 $item->setLicenseCandidate($innerLicenseCandidate->getFullName() .
" or later",
350 $innerLicenseCandidate->getText(),
359 error_log(
"ERROR: can not handle license=[".$license.
"] of type=[".gettype($license).
"]");
371 $property = $this->index[$propertyId];
372 $licenses = $this->
getValues($property, $kind);
375 foreach ($licenses as $license)
377 $innerOutput = $this->parseLicense($license);
378 foreach($innerOutput as $innerItem)
380 $output[] = $innerItem;
386 private function getCopyrightTextsForFile($propertyId)
391 $this->
getValues($propertyId,
"copyrightText")) ,
392 array($this,
"isNotNoassertion"));
399 $this->getCopyrightTextsForFile($propertyId));
getLicenseInfoInFileForFile($propertyId)
getLicenseInfoForFile($propertyId, $kind)
getConcludedLicenseInfoForFile($propertyId)
getValue($propertyOrId, $key, $default=null)
getFileName($propertyOrId)
getValues($propertyOrId, $key)
getDataForFile($propertyId)
isPropertyAFile(&$property)
isPropertyOfType(&$property, $type)