22 require_once
'ReportImportData.php';
23 require_once
'ReportImportDataItem.php';
24 require_once
'ImportSource.php';
28 const TERMS =
'http://spdx.org/rdf/terms#';
29 const SPDX_URL =
'http://spdx.org/licenses/';
30 const SYNTAX_NS =
'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
41 private $licenseRefPrefix =
"LicenseRef-";
43 function __construct($filename, $uri = null)
45 $this->filename = $filename;
54 $this->graph = $this->loadGraph($this->filename, $this->uri);
55 $this->index = $this->loadIndex($this->graph);
59 private function loadGraph($filename, $uri = null)
63 $graph->parseFile($filename,
'rdfxml', $uri);
67 private function loadIndex($graph)
69 return $graph->toRdfPhp();
78 foreach ($this->index as $subject => $property){
94 $key = self::SYNTAX_NS .
'type';
95 $target = self::TERMS . $type;
97 return is_array ($property) &&
98 array_key_exists($key, $property) &&
99 sizeof($property[$key]) === 1 &&
100 $property[$key][0][
'type'] ===
"uri" &&
101 $property[$key][0][
'value'] === $target;
124 $hashItems = $this->
getValues($fileid,
'checksum');
127 $keyAlgo = self::TERMS .
'algorithm';
128 $algoKeyPrefix = self::TERMS .
'checksumAlgorithm_';
129 $keyAlgoVal = self::TERMS .
'checksumValue';
130 foreach ($hashItems as $hashItem)
132 $algorithm = $hashItem[$keyAlgo][0][
'value'];
133 if(substr($algorithm, 0, strlen($algoKeyPrefix)) === $algoKeyPrefix)
135 $algorithm = substr($algorithm, strlen($algoKeyPrefix));
137 $hashes[$algorithm] = $hashItem[$keyAlgoVal][0][
'value'];
149 private function getValue($propertyOrId, $key, $default=null)
151 $values = $this->
getValues($propertyOrId, $key);
152 if(
sizeof($values) === 1)
166 if (is_string($propertyOrId))
168 $property = $this->index[$propertyOrId];
172 $property = $propertyOrId;
175 $key = self::TERMS . $key;
176 if (is_array($property) && isset($property[$key]))
179 foreach($property[$key] as $entry)
181 if($entry[
'type'] ===
'literal')
183 $values[] = $entry[
'value'];
185 elseif($entry[
'type'] ===
'uri')
187 if(array_key_exists($entry[
'value'],$this->index))
189 $values[$entry[
'value']] = $this->index[$entry[
'value']];
193 $values[] = $entry[
'value'];
196 elseif($entry[
'type'] ===
'bnode')
198 $values[$entry[
'value']] = $this->index[$entry[
'value']];
202 error_log(
"ERROR: can not handle entry=[".$entry.
"] of type=[" . $entry[
'type'] .
"]");
216 return $this->
getValue($propertyOrId,
'fileName');
237 private function stripLicenseRefPrefix($licenseId)
239 if(substr($licenseId, 0, strlen($this->licenseRefPrefix)) === $this->licenseRefPrefix)
241 return urldecode(substr($licenseId, strlen($this->licenseRefPrefix)));
245 return urldecode($licenseId);
249 private function isNotNoassertion($str)
251 return ! ( strtolower($str) === self::TERMS.
"noassertion" ||
252 strtolower($str) ===
"http://spdx.org/licenses/noassertion" );
255 private function parseLicenseId($licenseId)
257 if (!is_string($licenseId))
259 error_log(
"ERROR: Id not a string: ".$licenseId);
262 if (strtolower($licenseId) === self::TERMS.
"noassertion" ||
263 strtolower($licenseId) ===
"http://spdx.org/licenses/noassertion")
268 $license = $this->index[$licenseId];
272 return $this->parseLicense($license);
274 elseif(substr($licenseId, 0, strlen(self::SPDX_URL)) === self::SPDX_URL)
276 $spdxId = urldecode(substr($licenseId, strlen(self::SPDX_URL)));
282 error_log(
"ERROR: can not handle license with ID=".$licenseId);
287 private function parseLicense($license)
289 if (is_string($license))
291 return $this->parseLicenseId($license);
295 $licenseId = $this->stripLicenseRefPrefix($this->
getValue($license,
'licenseId'));
297 if(strlen($licenseId) > 33 &&
298 substr($licenseId, -33, 1) ===
"-" &&
299 ctype_alnum(substr($licenseId, -32)))
301 $licenseId = substr($licenseId, 0, -33);
303 $item->setCustomText($this->
getValue($license,
'extractedText'));
310 $item->setLicenseCandidate($this->
getValue($license,
'name', $licenseId),
311 $this->
getValue($license,
'extractedText'),
312 strpos($this->
getValue($license,
'licenseId'), $this->licenseRefPrefix));
318 $licenseId = $this->stripLicenseRefPrefix($this->
getValue($license,
'licenseId'));
320 $item->setLicenseCandidate($this->
getValue($license,
'name', $licenseId),
321 $this->
getValue($license,
'licenseText'),
322 strpos($this->
getValue($license,
'licenseId'), $this->licenseRefPrefix));
330 $subLicenses = $this->
getValues($license,
'member');
331 if (
sizeof($subLicenses) > 1 &&
336 foreach($subLicenses as $subLicense)
338 $innerOutput = $this->parseLicense($subLicense);
339 foreach($innerOutput as $innerItem)
341 $output[] = $innerItem;
349 $subLicenses = $this->
getValues($license,
'member');
350 foreach($subLicenses as $subLicense) {
352 $innerOutput = $this->parseLicense($subLicense);
353 foreach($innerOutput as $innerItem)
359 $item->setLicenseCandidate($innerLicenseCandidate->getFullName() .
" or later",
360 $innerLicenseCandidate->getText(),
369 error_log(
"ERROR: can not handle license=[".$license.
"] of type=[".gettype($license).
"]");
381 $property = $this->index[$propertyId];
382 $licenses = $this->
getValues($property, $kind);
385 foreach ($licenses as $license)
387 $innerOutput = $this->parseLicense($license);
388 foreach($innerOutput as $innerItem)
390 $output[] = $innerItem;
396 private function getCopyrightTextsForFile($propertyId)
401 $this->
getValues($propertyId,
"copyrightText")) ,
402 array($this,
"isNotNoassertion"));
409 $this->getCopyrightTextsForFile($propertyId));
isPropertyAFile(&$property)
getLicenseInfoForFile($propertyId, $kind)
getConcludedLicenseInfoForFile($propertyId)
getValues($propertyOrId, $key)
getFileName($propertyOrId)
getValue($propertyOrId, $key, $default=null)
isPropertyOfType(&$property, $type)
getLicenseInfoInFileForFile($propertyId)
getDataForFile($propertyId)