2012-05-07 19:34:25 +00:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$identifiers = new SPDXLicenseIdentifiersOnline;
|
|
|
|
$printer = new JsonPrinter;
|
|
|
|
$printer->printStringArray($identifiers->getStrings());
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SPDX Identifier List from the registry.
|
|
|
|
*/
|
|
|
|
class SPDXLicenseIdentifiersOnline
|
|
|
|
{
|
|
|
|
const REGISTRY = 'http://www.spdx.org/licenses/';
|
|
|
|
const EXPRESSION = '//*[@typeof="spdx:License"]/code[@property="spdx:licenseId"]/text()';
|
|
|
|
|
|
|
|
private $identifiers;
|
|
|
|
|
|
|
|
/**
|
2012-05-12 15:10:28 +00:00
|
|
|
* @return array
|
2012-05-07 19:34:25 +00:00
|
|
|
*/
|
|
|
|
public function getStrings()
|
|
|
|
{
|
|
|
|
if ($this->identifiers) {
|
|
|
|
return $this->identifiers;
|
|
|
|
}
|
|
|
|
$this->identifiers = $this->importNodesFromURL(
|
|
|
|
self::REGISTRY,
|
|
|
|
self::EXPRESSION
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->identifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function importNodesFromURL($url, $expressionTextNodes)
|
|
|
|
{
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
$doc->loadHTMLFile($url);
|
|
|
|
$xp = new DOMXPath($doc);
|
|
|
|
$codes = $xp->query($expressionTextNodes);
|
|
|
|
if (!$codes) {
|
|
|
|
throw new \Exception(sprintf('XPath query failed: %s', $expressionTextNodes));
|
|
|
|
}
|
|
|
|
if ($codes->length < 20) {
|
|
|
|
throw new \Exception('Obtaining the license table failed, there can not be less than 20 identifiers.');
|
|
|
|
}
|
|
|
|
$identifiers = array();
|
|
|
|
foreach ($codes as $code) {
|
|
|
|
$identifiers[] = $code->nodeValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $identifiers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print an array the way this script needs it.
|
|
|
|
*/
|
|
|
|
class JsonPrinter
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
2012-05-12 15:10:28 +00:00
|
|
|
* @param array $array
|
2012-05-07 19:34:25 +00:00
|
|
|
*/
|
|
|
|
public function printStringArray(array $array)
|
|
|
|
{
|
|
|
|
$lines = array('');
|
|
|
|
$line = &$lines[0];
|
|
|
|
$last = count($array) - 1;
|
|
|
|
foreach ($array as $item => $code) {
|
2013-02-22 18:24:20 +00:00
|
|
|
$code = sprintf('"%s"%s', trim($code), $item === $last ? '' : ', ');
|
2012-05-07 19:34:25 +00:00
|
|
|
$length = strlen($line) + strlen($code) - 1;
|
|
|
|
if ($length > 76) {
|
|
|
|
$line = rtrim($line);
|
|
|
|
unset($line);
|
|
|
|
$lines[] = $code;
|
|
|
|
$line = &$lines[count($lines) - 1];
|
|
|
|
} else {
|
|
|
|
$line .= $code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$json = sprintf("[%s]", implode("\n ", $lines));
|
|
|
|
$json = str_replace(array("[\"", "\"]"), array("[\n \"", "\"\n]"), $json);
|
|
|
|
echo $json;
|
|
|
|
}
|
|
|
|
}
|