12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #!/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;
- /**
- * @return array
- */
- 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
- {
- /**
- *
- * @param array $array
- */
- public function printStringArray(array $array)
- {
- $lines = array('');
- $line = &$lines[0];
- $last = count($array) - 1;
- foreach ($array as $item => $code) {
- $code = sprintf('"%s"%s', trim($code), $item === $last ? '' : ', ');
- $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;
- }
- }
|