fetch-spdx-identifiers 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env php
  2. <?php
  3. $identifiers = new SPDXLicenseIdentifiersOnline;
  4. $printer = new JsonPrinter;
  5. $printer->printStringArray($identifiers->getStrings());
  6. /**
  7. * SPDX Identifier List from the registry.
  8. */
  9. class SPDXLicenseIdentifiersOnline
  10. {
  11. const REGISTRY = 'http://www.spdx.org/licenses/';
  12. const EXPRESSION = '//*[@typeof="spdx:License"]/code[@property="spdx:licenseId"]/text()';
  13. private $identifiers;
  14. /**
  15. * @return array
  16. */
  17. public function getStrings()
  18. {
  19. if ($this->identifiers) {
  20. return $this->identifiers;
  21. }
  22. $this->identifiers = $this->importNodesFromURL(
  23. self::REGISTRY,
  24. self::EXPRESSION
  25. );
  26. return $this->identifiers;
  27. }
  28. private function importNodesFromURL($url, $expressionTextNodes)
  29. {
  30. $doc = new DOMDocument();
  31. $doc->loadHTMLFile($url);
  32. $xp = new DOMXPath($doc);
  33. $codes = $xp->query($expressionTextNodes);
  34. if (!$codes) {
  35. throw new \Exception(sprintf('XPath query failed: %s', $expressionTextNodes));
  36. }
  37. if ($codes->length < 20) {
  38. throw new \Exception('Obtaining the license table failed, there can not be less than 20 identifiers.');
  39. }
  40. $identifiers = array();
  41. foreach ($codes as $code) {
  42. $identifiers[] = $code->nodeValue;
  43. }
  44. return $identifiers;
  45. }
  46. }
  47. /**
  48. * Print an array the way this script needs it.
  49. */
  50. class JsonPrinter
  51. {
  52. /**
  53. *
  54. * @param array $array
  55. */
  56. public function printStringArray(array $array)
  57. {
  58. $lines = array('');
  59. $line = &$lines[0];
  60. $last = count($array) - 1;
  61. foreach ($array as $item => $code) {
  62. $code = sprintf('"%s"%s', trim($code), $item === $last ? '' : ', ');
  63. $length = strlen($line) + strlen($code) - 1;
  64. if ($length > 76) {
  65. $line = rtrim($line);
  66. unset($line);
  67. $lines[] = $code;
  68. $line = &$lines[count($lines) - 1];
  69. } else {
  70. $line .= $code;
  71. }
  72. }
  73. $json = sprintf("[%s]", implode("\n ", $lines));
  74. $json = str_replace(array("[\"", "\"]"), array("[\n \"", "\"\n]"), $json);
  75. echo $json;
  76. }
  77. }